2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
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.
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.
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
26 #include <pulse/error.h>
28 #include <pulsecore/core-util.h>
29 #include <pulsecore/core-error.h>
30 #include <pulsecore/log.h>
31 #include <pulsecore/macro.h>
44 #ifdef HAVE_SYS_RESOURCE_H
45 #include <sys/resource.h>
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. */
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. */
60 /* Utilize this much CPU time at maximum */
61 #define CPUTIME_PERCENT 70
64 #define CPUTIME_INTERVAL_SOFT (10)
66 /* Recheck after 5s */
67 #define CPUTIME_INTERVAL_HARD (5)
69 /* Time of the last CPU load check */
70 static time_t last_time
= 0;
72 /* Pipe for communicating with the main loop */
73 static int the_pipe
[2] = {-1, -1};
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
;
79 /* Saved sigaction struct for SIGXCPU */
80 static struct sigaction sigaction_prev
;
82 /* Nonzero after pa_cpu_limit_init() */
83 static pa_bool_t installed
= FALSE
;
85 /* The current state of operation */
87 PHASE_IDLE
, /* Normal state */
88 PHASE_SOFT
/* After CPU overload has been detected */
91 /* Reset the SIGXCPU timer to the next t seconds */
92 static void reset_cpu_time(int t
) {
97 /* Get the current CPU time of the current process */
98 pa_assert_se(getrusage(RUSAGE_SELF
, &ru
) >= 0);
100 n
= ru
.ru_utime
.tv_sec
+ ru
.ru_stime
.tv_sec
+ t
;
101 pa_assert_se(getrlimit(RLIMIT_CPU
, &rl
) >= 0);
104 pa_assert_se(setrlimit(RLIMIT_CPU
, &rl
) >= 0);
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
);
112 /* The signal handler, called on every SIGXCPU */
113 static void signal_handler(int sig
) {
117 pa_assert(sig
== SIGXCPU
);
119 if (phase
== PHASE_IDLE
) {
122 #ifdef PRINT_CPU_LOAD
128 #ifdef PRINT_CPU_LOAD
129 pa_snprintf(t
, sizeof(t
), "Using %0.1f%% CPU\n", (double)CPUTIME_INTERVAL_SOFT
/(now
-last_time
)*100);
133 if (CPUTIME_INTERVAL_SOFT
>= ((now
-last_time
)*(double)CPUTIME_PERCENT
/100)) {
134 static const char c
= 'X';
136 write_err("Soft CPU time limit exhausted, terminating.\n");
138 /* Try a soft cleanup */
139 write(the_pipe
[1], &c
, sizeof(c
));
141 reset_cpu_time(CPUTIME_INTERVAL_HARD
);
145 /* Everything's fine */
146 reset_cpu_time(CPUTIME_INTERVAL_SOFT
);
150 } else if (phase
== PHASE_SOFT
) {
151 write_err("Hard CPU time limit exhausted, terminating forcibly.\n");
152 abort(); /* Forced exit */
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
) {
163 pa_assert(f
== PA_IO_EVENT_INPUT
);
164 pa_assert(e
== io_event
);
165 pa_assert(fd
== the_pipe
[0]);
167 pa_read(the_pipe
[0], &c
, sizeof(c
), NULL
);
168 m
->quit(m
, 1); /* Quit the main loop */
171 /* Initializes CPU load limiter */
172 int pa_cpu_limit_init(pa_mainloop_api
*m
) {
177 pa_assert(!io_event
);
178 pa_assert(the_pipe
[0] == -1);
179 pa_assert(the_pipe
[1] == -1);
180 pa_assert(!installed
);
184 /* Prepare the main loop pipe */
185 if (pipe(the_pipe
) < 0) {
186 pa_log("pipe() failed: %s", pa_cstrerror(errno
));
190 pa_make_fd_nonblock(the_pipe
[0]);
191 pa_make_fd_nonblock(the_pipe
[1]);
192 pa_make_fd_cloexec(the_pipe
[0]);
193 pa_make_fd_cloexec(the_pipe
[1]);
196 io_event
= api
->io_new(m
, the_pipe
[0], PA_IO_EVENT_INPUT
, callback
, NULL
);
200 /* Install signal handler for SIGXCPU */
201 memset(&sa
, 0, sizeof(sa
));
202 sa
.sa_handler
= signal_handler
;
203 sigemptyset(&sa
.sa_mask
);
204 sa
.sa_flags
= SA_RESTART
;
206 if (sigaction(SIGXCPU
, &sa
, &sigaction_prev
) < 0) {
213 reset_cpu_time(CPUTIME_INTERVAL_SOFT
);
218 /* Shutdown CPU load limiter */
219 void pa_cpu_limit_done(void) {
223 api
->io_free(io_event
);
228 pa_close_pipe(the_pipe
);
231 pa_assert_se(sigaction(SIGXCPU
, &sigaction_prev
, NULL
) >= 0);
236 #else /* HAVE_SIGXCPU */
238 int pa_cpu_limit_init(PA_GCC_UNUSED pa_mainloop_api
*m
) {
242 void pa_cpu_limit_done(void) {