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