]>
code.delx.au - pulseaudio/blob - polyp/cpulimit.c
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
31 #include <sys/resource.h>
40 /* This module implements a watchdog that makes sure that the current
41 * process doesn't consume more than 70% CPU time for 10 seconds. This
42 * is very useful when using SCHED_FIFO scheduling which effectively
43 * disables multitasking. */
45 /* Method of operation: Using SIGXCPU a signal handler is called every
46 * 10s process CPU time. That function checks if less than 14s system
47 * time have passed. In that case, it tries to contact the main event
48 * loop through a pipe. After two additional seconds it is checked
49 * whether the main event loop contact was successful. If not, the
50 * program is terminated forcibly. */
52 /* Utilize this much CPU time at maximum */
53 #define CPUTIME_PERCENT 70
56 #define CPUTIME_INTERVAL_SOFT (10)
58 /* Recheck after 2s */
59 #define CPUTIME_INTERVAL_HARD (2)
61 /* Time of the last CPU load check */
62 static time_t last_time
= 0;
64 /* Pipe for communicating with the main loop */
65 static int the_pipe
[2] = {-1, -1};
67 /* Main event loop and IO event for the FIFO */
68 static struct pa_mainloop_api
*api
= NULL
;
69 static struct pa_io_event
*io_event
= NULL
;
71 /* Saved sigaction struct for SIGXCPU */
72 static struct sigaction sigaction_prev
;
74 /* Nonzero after pa_cpu_limit_init() */
75 static int installed
= 0;
77 /* The current state of operation */
79 PHASE_IDLE
, /* Normal state */
80 PHASE_SOFT
/* After CPU overload has been detected */
83 /* Reset the SIGXCPU timer to the next t seconds */
84 static void reset_cpu_time(int t
) {
90 /* Get the current CPU time of the current process */
91 r
= getrusage(RUSAGE_SELF
, &ru
);
94 n
= ru
.ru_utime
.tv_sec
+ ru
.ru_stime
.tv_sec
+ t
;
96 r
= getrlimit(RLIMIT_CPU
, &rl
);
100 r
= setrlimit(RLIMIT_CPU
, &rl
);
104 /* A simple, thread-safe puts() work-alike */
105 static void write_err(const char *p
) {
106 pa_loop_write(2, p
, strlen(p
));
109 /* The signal handler, called on every SIGXCPU */
110 static void signal_handler(int sig
) {
111 assert(sig
== SIGXCPU
);
113 if (phase
== PHASE_IDLE
) {
116 #ifdef PRINT_CPU_LOAD
122 #ifdef PRINT_CPU_LOAD
123 snprintf(t
, sizeof(t
), "Using %0.1f%% CPU\n", (double)CPUTIME_INTERVAL_SOFT
/(now
-last_time
)*100);
127 if (CPUTIME_INTERVAL_SOFT
>= ((now
-last_time
)*(double)CPUTIME_PERCENT
/100)) {
128 static const char c
= 'X';
130 write_err("Soft CPU time limit exhausted, terminating.\n");
132 /* Try a soft cleanup */
133 write(the_pipe
[1], &c
, sizeof(c
));
135 reset_cpu_time(CPUTIME_INTERVAL_HARD
);
139 /* Everything's fine */
140 reset_cpu_time(CPUTIME_INTERVAL_SOFT
);
144 } else if (phase
== PHASE_SOFT
) {
145 write_err("Hard CPU time limit exhausted, terminating forcibly.\n");
146 _exit(1); /* Forced exit */
150 /* Callback for IO events on the FIFO */
151 static void callback(struct pa_mainloop_api
*m
, struct pa_io_event
*e
, int fd
, enum pa_io_event_flags f
, void *userdata
) {
153 assert(m
&& e
&& f
== PA_IO_EVENT_INPUT
&& e
== io_event
&& fd
== the_pipe
[0]);
154 read(the_pipe
[0], &c
, sizeof(c
));
155 m
->quit(m
, 1); /* Quit the main loop */
158 /* Initializes CPU load limiter */
159 int pa_cpu_limit_init(struct pa_mainloop_api
*m
) {
161 assert(m
&& !api
&& !io_event
&& the_pipe
[0] == -1 && the_pipe
[1] == -1 && !installed
);
165 /* Prepare the main loop pipe */
166 if (pipe(the_pipe
) < 0) {
167 pa_log(__FILE__
": pipe() failed: %s\n", strerror(errno
));
171 pa_make_nonblock_fd(the_pipe
[0]);
172 pa_make_nonblock_fd(the_pipe
[1]);
173 pa_fd_set_cloexec(the_pipe
[0], 1);
174 pa_fd_set_cloexec(the_pipe
[1], 1);
177 io_event
= api
->io_new(m
, the_pipe
[0], PA_IO_EVENT_INPUT
, callback
, NULL
);
181 /* Install signal handler for SIGXCPU */
182 memset(&sa
, 0, sizeof(sa
));
183 sa
.sa_handler
= signal_handler
;
184 sigemptyset(&sa
.sa_mask
);
185 sa
.sa_flags
= SA_RESTART
;
187 if (sigaction(SIGXCPU
, &sa
, &sigaction_prev
) < 0) {
194 reset_cpu_time(CPUTIME_INTERVAL_SOFT
);
199 /* Shutdown CPU load limiter */
200 void pa_cpu_limit_done(void) {
205 api
->io_free(io_event
);
210 if (the_pipe
[0] >= 0)
212 if (the_pipe
[1] >= 0)
214 the_pipe
[0] = the_pipe
[1] = -1;
217 r
= sigaction(SIGXCPU
, &sigaction_prev
, NULL
);