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