]> code.delx.au - pulseaudio/blob - src/daemon/cpulimit.c
Merge branch 'master' into master-tx
[pulseaudio] / src / daemon / cpulimit.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
6 PulseAudio 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 PulseAudio 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 PulseAudio; 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 <pulse/error.h>
27 #include <pulse/timeval.h>
28
29 #include <pulsecore/core-util.h>
30 #include <pulsecore/core-error.h>
31 #include <pulsecore/log.h>
32 #include <pulsecore/macro.h>
33 #include <pulsecore/rtclock.h>
34
35 #include "cpulimit.h"
36
37 #ifdef HAVE_SIGXCPU
38
39 #include <errno.h>
40 #include <stdio.h>
41 #include <string.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 pa_usec_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 pa_bool_t installed = FALSE;
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 long n;
96 struct rlimit rl;
97 struct rusage ru;
98
99 /* Get the current CPU time of the current process */
100 pa_assert_se(getrusage(RUSAGE_SELF, &ru) >= 0);
101
102 n = ru.ru_utime.tv_sec + ru.ru_stime.tv_sec + t;
103 pa_assert_se(getrlimit(RLIMIT_CPU, &rl) >= 0);
104
105 rl.rlim_cur = (rlim_t) n;
106 pa_assert_se(setrlimit(RLIMIT_CPU, &rl) >= 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), NULL);
112 }
113
114 /* The signal handler, called on every SIGXCPU */
115 static void signal_handler(int sig) {
116 int saved_errno;
117
118 saved_errno = errno;
119 pa_assert(sig == SIGXCPU);
120
121 if (phase == PHASE_IDLE) {
122 pa_usec_t now, elapsed;
123
124 #ifdef PRINT_CPU_LOAD
125 char t[256];
126 #endif
127
128 now = pa_rtclock_usec();
129 elapsed = now - last_time;
130
131 #ifdef PRINT_CPU_LOAD
132 pa_snprintf(t, sizeof(t), "Using %0.1f%% CPU\n", ((double) CPUTIME_INTERVAL_SOFT * (double) PA_USEC_PER_SEC) / (double) elapsed * 100.0);
133 write_err(t);
134 #endif
135
136 if (((double) CPUTIME_INTERVAL_SOFT * (double) PA_USEC_PER_SEC) >= ((double) elapsed * (double) CPUTIME_PERCENT / 100.0)) {
137 static const char c = 'X';
138
139 write_err("Soft CPU time limit exhausted, terminating.\n");
140
141 /* Try a soft cleanup */
142 write(the_pipe[1], &c, sizeof(c));
143 phase = PHASE_SOFT;
144 reset_cpu_time(CPUTIME_INTERVAL_HARD);
145
146 } else {
147
148 /* Everything's fine */
149 reset_cpu_time(CPUTIME_INTERVAL_SOFT);
150 last_time = now;
151 }
152
153 } else if (phase == PHASE_SOFT) {
154 write_err("Hard CPU time limit exhausted, terminating forcibly.\n");
155 abort(); /* Forced exit */
156 }
157
158 errno = saved_errno;
159 }
160
161 /* Callback for IO events on the FIFO */
162 static void callback(pa_mainloop_api*m, pa_io_event*e, int fd, pa_io_event_flags_t f, void *userdata) {
163 char c;
164 pa_assert(m);
165 pa_assert(e);
166 pa_assert(f == PA_IO_EVENT_INPUT);
167 pa_assert(e == io_event);
168 pa_assert(fd == the_pipe[0]);
169
170 pa_log("Recevied request to terminate due to CPU overload.");
171
172 pa_read(the_pipe[0], &c, sizeof(c), NULL);
173 m->quit(m, 1); /* Quit the main loop */
174 }
175
176 /* Initializes CPU load limiter */
177 int pa_cpu_limit_init(pa_mainloop_api *m) {
178 struct sigaction sa;
179
180 pa_assert(m);
181 pa_assert(!api);
182 pa_assert(!io_event);
183 pa_assert(the_pipe[0] == -1);
184 pa_assert(the_pipe[1] == -1);
185 pa_assert(!installed);
186
187 last_time = pa_rtclock_usec();
188
189 /* Prepare the main loop pipe */
190 if (pipe(the_pipe) < 0) {
191 pa_log("pipe() failed: %s", pa_cstrerror(errno));
192 return -1;
193 }
194
195 pa_make_fd_nonblock(the_pipe[0]);
196 pa_make_fd_nonblock(the_pipe[1]);
197 pa_make_fd_cloexec(the_pipe[0]);
198 pa_make_fd_cloexec(the_pipe[1]);
199
200 api = m;
201 io_event = api->io_new(m, the_pipe[0], PA_IO_EVENT_INPUT, callback, NULL);
202
203 phase = PHASE_IDLE;
204
205 /* Install signal handler for SIGXCPU */
206 memset(&sa, 0, sizeof(sa));
207 sa.sa_handler = signal_handler;
208 sigemptyset(&sa.sa_mask);
209 sa.sa_flags = SA_RESTART;
210
211 if (sigaction(SIGXCPU, &sa, &sigaction_prev) < 0) {
212 pa_cpu_limit_done();
213 return -1;
214 }
215
216 installed = TRUE;
217
218 reset_cpu_time(CPUTIME_INTERVAL_SOFT);
219
220 return 0;
221 }
222
223 /* Shutdown CPU load limiter */
224 void pa_cpu_limit_done(void) {
225
226 if (io_event) {
227 pa_assert(api);
228 api->io_free(io_event);
229 io_event = NULL;
230 api = NULL;
231 }
232
233 pa_close_pipe(the_pipe);
234
235 if (installed) {
236 pa_assert_se(sigaction(SIGXCPU, &sigaction_prev, NULL) >= 0);
237 installed = FALSE;
238 }
239 }
240
241 #else /* HAVE_SIGXCPU */
242
243 int pa_cpu_limit_init(pa_mainloop_api *m) {
244 return 0;
245 }
246
247 void pa_cpu_limit_done(void) {
248 }
249
250 #endif