]> code.delx.au - pulseaudio/blob - polyp/cpulimit.c
a60c03d784913750b8913ff39972ae436708bdf6
[pulseaudio] / polyp / cpulimit.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #include <errno.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <assert.h>
26 #include <sys/time.h>
27 #include <sys/resource.h>
28 #include <unistd.h>
29 #include <signal.h>
30
31 #include "cpulimit.h"
32 #include "util.h"
33 #include "log.h"
34
35 /* Utilize this much CPU time at maximum */
36 #define CPUTIME_PERCENT 70
37
38 #define CPUTIME_INTERVAL_SOFT (10)
39 #define CPUTIME_INTERVAL_HARD (2)
40
41 static time_t last_time = 0;
42 static int the_pipe[2] = {-1, -1};
43 static struct pa_mainloop_api *api = NULL;
44 static struct pa_io_event *io_event = NULL;
45 static struct sigaction sigaction_prev;
46 static int installed = 0;
47
48 static enum {
49 PHASE_IDLE,
50 PHASE_SOFT
51 } phase = PHASE_IDLE;
52
53 static void reset_cpu_time(int t) {
54 int r;
55 long n;
56 struct rlimit rl;
57 struct rusage ru;
58
59 r = getrusage(RUSAGE_SELF, &ru);
60 assert(r >= 0);
61
62 n = ru.ru_utime.tv_sec + ru.ru_stime.tv_sec + t;
63
64 r = getrlimit(RLIMIT_CPU, &rl);
65 assert(r >= 0);
66
67 rl.rlim_cur = n;
68 r = setrlimit(RLIMIT_CPU, &rl);
69 assert(r >= 0);
70 }
71
72 static void write_err(const char *p) {
73 pa_loop_write(2, p, strlen(p));
74 }
75
76 static void signal_handler(int sig) {
77 assert(sig == SIGXCPU);
78
79 if (phase == PHASE_IDLE) {
80 time_t now;
81
82 #ifdef PRINT_CPU_LOAD
83 char t[256];
84 #endif
85
86 time(&now);
87
88 #ifdef PRINT_CPU_LOAD
89 snprintf(t, sizeof(t), "Using %0.1f%% CPU\n", (double)CPUTIME_INTERVAL_SOFT/(now-last_time)*100);
90 write_err(t);
91 #endif
92
93 if (CPUTIME_INTERVAL_SOFT >= ((now-last_time)*(double)CPUTIME_PERCENT/100)) {
94 static const char c = 'X';
95
96 write_err("Soft CPU time limit exhausted, terminating.\n");
97
98 /* Try a soft cleanup */
99 write(the_pipe[1], &c, sizeof(c));
100 phase = PHASE_SOFT;
101 reset_cpu_time(CPUTIME_INTERVAL_HARD);
102
103 } else {
104
105 /* Everything's fine */
106 reset_cpu_time(CPUTIME_INTERVAL_SOFT);
107 last_time = now;
108 }
109
110 } else if (phase == PHASE_SOFT) {
111 write_err("Hard CPU time limit exhausted, terminating forcibly.\n");
112 _exit(1);
113 }
114 }
115
116 static void callback(struct pa_mainloop_api*m, struct pa_io_event*e, int fd, enum pa_io_event_flags f, void *userdata) {
117 char c;
118 assert(m && e && f == PA_IO_EVENT_INPUT && e == io_event && fd == the_pipe[0]);
119 read(the_pipe[0], &c, sizeof(c));
120 m->quit(m, 1);
121 }
122
123 int pa_cpu_limit_init(struct pa_mainloop_api *m) {
124 struct sigaction sa;
125 assert(m && !api && !io_event && the_pipe[0] == -1 && the_pipe[1] == -1);
126
127 time(&last_time);
128
129 if (pipe(the_pipe) < 0) {
130 pa_log(__FILE__": pipe() failed: %s\n", strerror(errno));
131 return -1;
132 }
133
134 pa_make_nonblock_fd(the_pipe[0]);
135 pa_make_nonblock_fd(the_pipe[1]);
136 pa_fd_set_cloexec(the_pipe[0], 1);
137 pa_fd_set_cloexec(the_pipe[1], 1);
138
139 api = m;
140 io_event = api->io_new(m, the_pipe[0], PA_IO_EVENT_INPUT, callback, NULL);
141
142 phase = PHASE_IDLE;
143
144 memset(&sa, 0, sizeof(sa));
145 sa.sa_handler = signal_handler;
146 sigemptyset(&sa.sa_mask);
147 sa.sa_flags = SA_RESTART;
148
149 if (sigaction(SIGXCPU, &sa, &sigaction_prev) < 0) {
150 pa_cpu_limit_done();
151 return -1;
152 }
153
154 installed = 1;
155
156 reset_cpu_time(CPUTIME_INTERVAL_SOFT);
157
158 return 0;
159 }
160
161 void pa_cpu_limit_done(void) {
162 int r;
163
164 if (io_event) {
165 assert(api);
166 api->io_free(io_event);
167 io_event = NULL;
168 api = NULL;
169 }
170
171 if (the_pipe[0] >= 0)
172 close(the_pipe[0]);
173 if (the_pipe[1] >= 0)
174 close(the_pipe[1]);
175 the_pipe[0] = the_pipe[1] = -1;
176
177 if (installed) {
178 r = sigaction(SIGXCPU, &sigaction_prev, NULL);
179 assert(r >= 0);
180 installed = 0;
181 }
182 }