]> code.delx.au - pulseaudio/blob - src/daemon/cpulimit.c
client-conf: Remove redundant function parameters
[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.1 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/rtclock.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
34 #include "cpulimit.h"
35
36 #ifdef HAVE_SIGXCPU
37
38 #include <errno.h>
39 #include <stdio.h>
40 #include <string.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 pa_usec_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 bool installed = false;
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 long n;
94 struct rlimit rl;
95 struct rusage ru;
96
97 /* Get the current CPU time of the current process */
98 pa_assert_se(getrusage(RUSAGE_SELF, &ru) >= 0);
99
100 n = ru.ru_utime.tv_sec + ru.ru_stime.tv_sec + t;
101 pa_assert_se(getrlimit(RLIMIT_CPU, &rl) >= 0);
102
103 rl.rlim_cur = (rlim_t) n;
104 pa_assert_se(setrlimit(RLIMIT_CPU, &rl) >= 0);
105 }
106
107 /* A simple, thread-safe puts() work-alike */
108 static void write_err(const char *p) {
109 pa_loop_write(2, p, strlen(p), NULL);
110 }
111
112 /* The signal handler, called on every SIGXCPU */
113 static void signal_handler(int sig) {
114 int saved_errno;
115
116 saved_errno = errno;
117 pa_assert(sig == SIGXCPU);
118
119 if (phase == PHASE_IDLE) {
120 pa_usec_t now, elapsed;
121
122 #ifdef PRINT_CPU_LOAD
123 char t[256];
124 #endif
125
126 now = pa_rtclock_now();
127 elapsed = now - last_time;
128
129 #ifdef PRINT_CPU_LOAD
130 pa_snprintf(t, sizeof(t), "Using %0.1f%% CPU\n", ((double) CPUTIME_INTERVAL_SOFT * (double) PA_USEC_PER_SEC) / (double) elapsed * 100.0);
131 write_err(t);
132 #endif
133
134 if (((double) CPUTIME_INTERVAL_SOFT * (double) PA_USEC_PER_SEC) >= ((double) elapsed * (double) CPUTIME_PERCENT / 100.0)) {
135 static const char c = 'X';
136
137 write_err("Soft CPU time limit exhausted, terminating.\n");
138
139 /* Try a soft cleanup */
140 (void) pa_write(the_pipe[1], &c, sizeof(c), NULL);
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 abort(); /* Forced exit */
154 }
155
156 errno = saved_errno;
157 }
158
159 /* Callback for IO events on the FIFO */
160 static void callback(pa_mainloop_api*m, pa_io_event*e, int fd, pa_io_event_flags_t f, void *userdata) {
161 char c;
162 pa_assert(m);
163 pa_assert(e);
164 pa_assert(f == PA_IO_EVENT_INPUT);
165 pa_assert(e == io_event);
166 pa_assert(fd == the_pipe[0]);
167
168 pa_log("Received request to terminate due to CPU overload.");
169
170 (void) pa_read(the_pipe[0], &c, sizeof(c), NULL);
171 m->quit(m, 1); /* Quit the main loop */
172 }
173
174 /* Initializes CPU load limiter */
175 int pa_cpu_limit_init(pa_mainloop_api *m) {
176 struct sigaction sa;
177
178 pa_assert(m);
179 pa_assert(!api);
180 pa_assert(!io_event);
181 pa_assert(the_pipe[0] == -1);
182 pa_assert(the_pipe[1] == -1);
183 pa_assert(!installed);
184
185 last_time = pa_rtclock_now();
186
187 /* Prepare the main loop pipe */
188 if (pa_pipe_cloexec(the_pipe) < 0) {
189 pa_log("pipe() failed: %s", pa_cstrerror(errno));
190 return -1;
191 }
192
193 pa_make_fd_nonblock(the_pipe[0]);
194 pa_make_fd_nonblock(the_pipe[1]);
195
196 api = m;
197 io_event = api->io_new(m, the_pipe[0], PA_IO_EVENT_INPUT, callback, NULL);
198
199 phase = PHASE_IDLE;
200
201 /* Install signal handler for SIGXCPU */
202 memset(&sa, 0, sizeof(sa));
203 sa.sa_handler = signal_handler;
204 sigemptyset(&sa.sa_mask);
205 sa.sa_flags = SA_RESTART;
206
207 if (sigaction(SIGXCPU, &sa, &sigaction_prev) < 0) {
208 pa_cpu_limit_done();
209 return -1;
210 }
211
212 installed = true;
213
214 reset_cpu_time(CPUTIME_INTERVAL_SOFT);
215
216 return 0;
217 }
218
219 /* Shutdown CPU load limiter */
220 void pa_cpu_limit_done(void) {
221
222 if (io_event) {
223 pa_assert(api);
224 api->io_free(io_event);
225 io_event = NULL;
226 api = NULL;
227 }
228
229 pa_close_pipe(the_pipe);
230
231 if (installed) {
232 pa_assert_se(sigaction(SIGXCPU, &sigaction_prev, NULL) >= 0);
233 installed = false;
234 }
235 }
236
237 #else /* HAVE_SIGXCPU */
238
239 int pa_cpu_limit_init(pa_mainloop_api *m) {
240 return 0;
241 }
242
243 void pa_cpu_limit_done(void) {
244 }
245
246 #endif