4 This file is part of polypaudio.
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.
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.
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
40 #ifdef HAVE_SYS_RESOURCE_H
41 #include <sys/resource.h>
44 /* This module implements a watchdog that makes sure that the current
45 * process doesn't consume more than 70% CPU time for 10 seconds. This
46 * is very useful when using SCHED_FIFO scheduling which effectively
47 * disables multitasking. */
49 /* Method of operation: Using SIGXCPU a signal handler is called every
50 * 10s process CPU time. That function checks if less than 14s system
51 * time have passed. In that case, it tries to contact the main event
52 * loop through a pipe. After two additional seconds it is checked
53 * whether the main event loop contact was successful. If not, the
54 * program is terminated forcibly. */
56 /* Utilize this much CPU time at maximum */
57 #define CPUTIME_PERCENT 70
60 #define CPUTIME_INTERVAL_SOFT (10)
62 /* Recheck after 2s */
63 #define CPUTIME_INTERVAL_HARD (2)
65 /* Time of the last CPU load check */
66 static time_t last_time
= 0;
68 /* Pipe for communicating with the main loop */
69 static int the_pipe
[2] = {-1, -1};
71 /* Main event loop and IO event for the FIFO */
72 static pa_mainloop_api
*api
= NULL
;
73 static pa_io_event
*io_event
= NULL
;
75 /* Saved sigaction struct for SIGXCPU */
76 static struct sigaction sigaction_prev
;
78 /* Nonzero after pa_cpu_limit_init() */
79 static int installed
= 0;
81 /* The current state of operation */
83 PHASE_IDLE
, /* Normal state */
84 PHASE_SOFT
/* After CPU overload has been detected */
87 /* Reset the SIGXCPU timer to the next t seconds */
88 static void reset_cpu_time(int t
) {
94 /* Get the current CPU time of the current process */
95 r
= getrusage(RUSAGE_SELF
, &ru
);
98 n
= ru
.ru_utime
.tv_sec
+ ru
.ru_stime
.tv_sec
+ t
;
100 r
= getrlimit(RLIMIT_CPU
, &rl
);
104 r
= setrlimit(RLIMIT_CPU
, &rl
);
108 /* A simple, thread-safe puts() work-alike */
109 static void write_err(const char *p
) {
110 pa_loop_write(2, p
, strlen(p
));
113 /* The signal handler, called on every SIGXCPU */
114 static void signal_handler(int sig
) {
115 assert(sig
== SIGXCPU
);
117 if (phase
== PHASE_IDLE
) {
120 #ifdef PRINT_CPU_LOAD
126 #ifdef PRINT_CPU_LOAD
127 snprintf(t
, sizeof(t
), "Using %0.1f%% CPU\n", (double)CPUTIME_INTERVAL_SOFT
/(now
-last_time
)*100);
131 if (CPUTIME_INTERVAL_SOFT
>= ((now
-last_time
)*(double)CPUTIME_PERCENT
/100)) {
132 static const char c
= 'X';
134 write_err("Soft CPU time limit exhausted, terminating.\n");
136 /* Try a soft cleanup */
137 write(the_pipe
[1], &c
, sizeof(c
));
139 reset_cpu_time(CPUTIME_INTERVAL_HARD
);
143 /* Everything's fine */
144 reset_cpu_time(CPUTIME_INTERVAL_SOFT
);
148 } else if (phase
== PHASE_SOFT
) {
149 write_err("Hard CPU time limit exhausted, terminating forcibly.\n");
150 _exit(1); /* Forced exit */
154 /* Callback for IO events on the FIFO */
155 static void callback(pa_mainloop_api
*m
, pa_io_event
*e
, int fd
, pa_io_event_flags f
, void *userdata
) {
157 assert(m
&& e
&& f
== PA_IO_EVENT_INPUT
&& e
== io_event
&& fd
== the_pipe
[0]);
158 read(the_pipe
[0], &c
, sizeof(c
));
159 m
->quit(m
, 1); /* Quit the main loop */
162 /* Initializes CPU load limiter */
163 int pa_cpu_limit_init(pa_mainloop_api
*m
) {
165 assert(m
&& !api
&& !io_event
&& the_pipe
[0] == -1 && the_pipe
[1] == -1 && !installed
);
169 /* Prepare the main loop pipe */
170 if (pipe(the_pipe
) < 0) {
171 pa_log(__FILE__
": pipe() failed: %s\n", strerror(errno
));
175 pa_make_nonblock_fd(the_pipe
[0]);
176 pa_make_nonblock_fd(the_pipe
[1]);
177 pa_fd_set_cloexec(the_pipe
[0], 1);
178 pa_fd_set_cloexec(the_pipe
[1], 1);
181 io_event
= api
->io_new(m
, the_pipe
[0], PA_IO_EVENT_INPUT
, callback
, NULL
);
185 /* Install signal handler for SIGXCPU */
186 memset(&sa
, 0, sizeof(sa
));
187 sa
.sa_handler
= signal_handler
;
188 sigemptyset(&sa
.sa_mask
);
189 sa
.sa_flags
= SA_RESTART
;
191 if (sigaction(SIGXCPU
, &sa
, &sigaction_prev
) < 0) {
198 reset_cpu_time(CPUTIME_INTERVAL_SOFT
);
203 /* Shutdown CPU load limiter */
204 void pa_cpu_limit_done(void) {
209 api
->io_free(io_event
);
214 if (the_pipe
[0] >= 0)
216 if (the_pipe
[1] >= 0)
218 the_pipe
[0] = the_pipe
[1] = -1;
221 r
= sigaction(SIGXCPU
, &sigaction_prev
, NULL
);
227 #else /* HAVE_SIGXCPU */
229 int pa_cpu_limit_init(PA_GCC_UNUSED pa_mainloop_api
*m
) {
233 void pa_cpu_limit_done(void) {