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