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