]> code.delx.au - pulseaudio/blob - polyp/cpulimit.c
a834b09489c93b7952ec72ca827f86a26eb8be95
[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 Lesser General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, 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 Lesser General Public
17 License 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 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include "cpulimit.h"
27 #include "util.h"
28 #include "log.h"
29
30 #ifdef HAVE_SIGXCPU
31
32 #include <errno.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <assert.h>
36 #include <sys/time.h>
37 #include <unistd.h>
38 #include <signal.h>
39
40 #ifdef HAVE_SYS_RESOURCE_H
41 #include <sys/resource.h>
42 #endif
43
44 /* This module implements a watchdog that makes sure that the current
45 * process doesn't consume more than 70% CPU time for 10 seconds. This
46 * is very useful when using SCHED_FIFO scheduling which effectively
47 * disables multitasking. */
48
49 /* Method of operation: Using SIGXCPU a signal handler is called every
50 * 10s process CPU time. That function checks if less than 14s system
51 * time have passed. In that case, it tries to contact the main event
52 * loop through a pipe. After two additional seconds it is checked
53 * whether the main event loop contact was successful. If not, the
54 * program is terminated forcibly. */
55
56 /* Utilize this much CPU time at maximum */
57 #define CPUTIME_PERCENT 70
58
59 /* Check every 10s */
60 #define CPUTIME_INTERVAL_SOFT (10)
61
62 /* Recheck after 2s */
63 #define CPUTIME_INTERVAL_HARD (2)
64
65 /* Time of the last CPU load check */
66 static time_t last_time = 0;
67
68 /* Pipe for communicating with the main loop */
69 static int the_pipe[2] = {-1, -1};
70
71 /* Main event loop and IO event for the FIFO */
72 static pa_mainloop_api *api = NULL;
73 static pa_io_event *io_event = NULL;
74
75 /* Saved sigaction struct for SIGXCPU */
76 static struct sigaction sigaction_prev;
77
78 /* Nonzero after pa_cpu_limit_init() */
79 static int installed = 0;
80
81 /* The current state of operation */
82 static enum {
83 PHASE_IDLE, /* Normal state */
84 PHASE_SOFT /* After CPU overload has been detected */
85 } phase = PHASE_IDLE;
86
87 /* Reset the SIGXCPU timer to the next t seconds */
88 static void reset_cpu_time(int t) {
89 int r;
90 long n;
91 struct rlimit rl;
92 struct rusage ru;
93
94 /* Get the current CPU time of the current process */
95 r = getrusage(RUSAGE_SELF, &ru);
96 assert(r >= 0);
97
98 n = ru.ru_utime.tv_sec + ru.ru_stime.tv_sec + t;
99
100 r = getrlimit(RLIMIT_CPU, &rl);
101 assert(r >= 0);
102
103 rl.rlim_cur = n;
104 r = setrlimit(RLIMIT_CPU, &rl);
105 assert(r >= 0);
106 }
107
108 /* A simple, thread-safe puts() work-alike */
109 static void write_err(const char *p) {
110 pa_loop_write(2, p, strlen(p));
111 }
112
113 /* The signal handler, called on every SIGXCPU */
114 static void signal_handler(int sig) {
115 assert(sig == SIGXCPU);
116
117 if (phase == PHASE_IDLE) {
118 time_t now;
119
120 #ifdef PRINT_CPU_LOAD
121 char t[256];
122 #endif
123
124 time(&now);
125
126 #ifdef PRINT_CPU_LOAD
127 snprintf(t, sizeof(t), "Using %0.1f%% CPU\n", (double)CPUTIME_INTERVAL_SOFT/(now-last_time)*100);
128 write_err(t);
129 #endif
130
131 if (CPUTIME_INTERVAL_SOFT >= ((now-last_time)*(double)CPUTIME_PERCENT/100)) {
132 static const char c = 'X';
133
134 write_err("Soft CPU time limit exhausted, terminating.\n");
135
136 /* Try a soft cleanup */
137 write(the_pipe[1], &c, sizeof(c));
138 phase = PHASE_SOFT;
139 reset_cpu_time(CPUTIME_INTERVAL_HARD);
140
141 } else {
142
143 /* Everything's fine */
144 reset_cpu_time(CPUTIME_INTERVAL_SOFT);
145 last_time = now;
146 }
147
148 } else if (phase == PHASE_SOFT) {
149 write_err("Hard CPU time limit exhausted, terminating forcibly.\n");
150 _exit(1); /* Forced exit */
151 }
152 }
153
154 /* Callback for IO events on the FIFO */
155 static void callback(pa_mainloop_api*m, pa_io_event*e, int fd, pa_io_event_flags f, void *userdata) {
156 char c;
157 assert(m && e && f == PA_IO_EVENT_INPUT && e == io_event && fd == the_pipe[0]);
158 read(the_pipe[0], &c, sizeof(c));
159 m->quit(m, 1); /* Quit the main loop */
160 }
161
162 /* Initializes CPU load limiter */
163 int pa_cpu_limit_init(pa_mainloop_api *m) {
164 struct sigaction sa;
165 assert(m && !api && !io_event && the_pipe[0] == -1 && the_pipe[1] == -1 && !installed);
166
167 time(&last_time);
168
169 /* Prepare the main loop pipe */
170 if (pipe(the_pipe) < 0) {
171 pa_log(__FILE__": pipe() failed: %s\n", strerror(errno));
172 return -1;
173 }
174
175 pa_make_nonblock_fd(the_pipe[0]);
176 pa_make_nonblock_fd(the_pipe[1]);
177 pa_fd_set_cloexec(the_pipe[0], 1);
178 pa_fd_set_cloexec(the_pipe[1], 1);
179
180 api = m;
181 io_event = api->io_new(m, the_pipe[0], PA_IO_EVENT_INPUT, callback, NULL);
182
183 phase = PHASE_IDLE;
184
185 /* Install signal handler for SIGXCPU */
186 memset(&sa, 0, sizeof(sa));
187 sa.sa_handler = signal_handler;
188 sigemptyset(&sa.sa_mask);
189 sa.sa_flags = SA_RESTART;
190
191 if (sigaction(SIGXCPU, &sa, &sigaction_prev) < 0) {
192 pa_cpu_limit_done();
193 return -1;
194 }
195
196 installed = 1;
197
198 reset_cpu_time(CPUTIME_INTERVAL_SOFT);
199
200 return 0;
201 }
202
203 /* Shutdown CPU load limiter */
204 void pa_cpu_limit_done(void) {
205 int r;
206
207 if (io_event) {
208 assert(api);
209 api->io_free(io_event);
210 io_event = NULL;
211 api = NULL;
212 }
213
214 if (the_pipe[0] >= 0)
215 close(the_pipe[0]);
216 if (the_pipe[1] >= 0)
217 close(the_pipe[1]);
218 the_pipe[0] = the_pipe[1] = -1;
219
220 if (installed) {
221 r = sigaction(SIGXCPU, &sigaction_prev, NULL);
222 assert(r >= 0);
223 installed = 0;
224 }
225 }
226
227 #else /* HAVE_SIGXCPU */
228
229 int pa_cpu_limit_init(PA_GCC_UNUSED pa_mainloop_api *m) {
230 return 0;
231 }
232
233 void pa_cpu_limit_done(void) {
234 }
235
236 #endif