diff options
Diffstat (limited to 'src/daemon/cpulimit.c')
| -rw-r--r-- | src/daemon/cpulimit.c | 118 |
1 files changed, 62 insertions, 56 deletions
diff --git a/src/daemon/cpulimit.c b/src/daemon/cpulimit.c index b8740ea0..3a972975 100644 --- a/src/daemon/cpulimit.c +++ b/src/daemon/cpulimit.c @@ -1,18 +1,18 @@ -/* $Id$ */ - /*** This file is part of PulseAudio. - + + Copyright 2004-2006 Lennart Poettering + PulseAudio is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2 of the + published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. - + PulseAudio is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU Lesser General Public License along with PulseAudio; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 @@ -23,11 +23,13 @@ #include <config.h> #endif -#include <pulse/error.h> +#include <pulse/rtclock.h> +#include <pulse/timeval.h> #include <pulsecore/core-util.h> #include <pulsecore/core-error.h> #include <pulsecore/log.h> +#include <pulsecore/macro.h> #include "cpulimit.h" @@ -36,8 +38,6 @@ #include <errno.h> #include <stdio.h> #include <string.h> -#include <assert.h> -#include <sys/time.h> #include <unistd.h> #include <signal.h> @@ -67,7 +67,7 @@ #define CPUTIME_INTERVAL_HARD (5) /* Time of the last CPU load check */ -static time_t last_time = 0; +static pa_usec_t last_time = 0; /* Pipe for communicating with the main loop */ static int the_pipe[2] = {-1, -1}; @@ -80,33 +80,28 @@ static pa_io_event *io_event = NULL; static struct sigaction sigaction_prev; /* Nonzero after pa_cpu_limit_init() */ -static int installed = 0; +static pa_bool_t installed = FALSE; /* The current state of operation */ -static enum { +static enum { PHASE_IDLE, /* Normal state */ PHASE_SOFT /* After CPU overload has been detected */ } phase = PHASE_IDLE; /* Reset the SIGXCPU timer to the next t seconds */ static void reset_cpu_time(int t) { - int r; long n; struct rlimit rl; struct rusage ru; /* Get the current CPU time of the current process */ - r = getrusage(RUSAGE_SELF, &ru); - assert(r >= 0); + pa_assert_se(getrusage(RUSAGE_SELF, &ru) >= 0); n = ru.ru_utime.tv_sec + ru.ru_stime.tv_sec + t; + pa_assert_se(getrlimit(RLIMIT_CPU, &rl) >= 0); - r = getrlimit(RLIMIT_CPU, &rl); - assert(r >= 0); - - rl.rlim_cur = n; - r = setrlimit(RLIMIT_CPU, &rl); - assert(r >= 0); + rl.rlim_cur = (rlim_t) n; + pa_assert_se(setrlimit(RLIMIT_CPU, &rl) >= 0); } /* A simple, thread-safe puts() work-alike */ @@ -116,49 +111,62 @@ static void write_err(const char *p) { /* The signal handler, called on every SIGXCPU */ static void signal_handler(int sig) { - assert(sig == SIGXCPU); + int saved_errno; + + saved_errno = errno; + pa_assert(sig == SIGXCPU); if (phase == PHASE_IDLE) { - time_t now; + pa_usec_t now, elapsed; #ifdef PRINT_CPU_LOAD char t[256]; #endif - time(&now); + now = pa_rtclock_now(); + elapsed = now - last_time; #ifdef PRINT_CPU_LOAD - snprintf(t, sizeof(t), "Using %0.1f%% CPU\n", (double)CPUTIME_INTERVAL_SOFT/(now-last_time)*100); + pa_snprintf(t, sizeof(t), "Using %0.1f%% CPU\n", ((double) CPUTIME_INTERVAL_SOFT * (double) PA_USEC_PER_SEC) / (double) elapsed * 100.0); write_err(t); #endif - - if (CPUTIME_INTERVAL_SOFT >= ((now-last_time)*(double)CPUTIME_PERCENT/100)) { + + if (((double) CPUTIME_INTERVAL_SOFT * (double) PA_USEC_PER_SEC) >= ((double) elapsed * (double) CPUTIME_PERCENT / 100.0)) { static const char c = 'X'; write_err("Soft CPU time limit exhausted, terminating.\n"); - + /* Try a soft cleanup */ - write(the_pipe[1], &c, sizeof(c)); + (void) pa_write(the_pipe[1], &c, sizeof(c), NULL); phase = PHASE_SOFT; reset_cpu_time(CPUTIME_INTERVAL_HARD); - + } else { /* Everything's fine */ reset_cpu_time(CPUTIME_INTERVAL_SOFT); last_time = now; } - + } else if (phase == PHASE_SOFT) { write_err("Hard CPU time limit exhausted, terminating forcibly.\n"); - _exit(1); /* Forced exit */ + abort(); /* Forced exit */ } + + errno = saved_errno; } /* Callback for IO events on the FIFO */ static void callback(pa_mainloop_api*m, pa_io_event*e, int fd, pa_io_event_flags_t f, void *userdata) { char c; - assert(m && e && f == PA_IO_EVENT_INPUT && e == io_event && fd == the_pipe[0]); + pa_assert(m); + pa_assert(e); + pa_assert(f == PA_IO_EVENT_INPUT); + pa_assert(e == io_event); + pa_assert(fd == the_pipe[0]); + + pa_log("Received request to terminate due to CPU overload."); + pa_read(the_pipe[0], &c, sizeof(c), NULL); m->quit(m, 1); /* Quit the main loop */ } @@ -166,20 +174,24 @@ static void callback(pa_mainloop_api*m, pa_io_event*e, int fd, pa_io_event_flags /* Initializes CPU load limiter */ int pa_cpu_limit_init(pa_mainloop_api *m) { struct sigaction sa; - assert(m && !api && !io_event && the_pipe[0] == -1 && the_pipe[1] == -1 && !installed); - - time(&last_time); + + pa_assert(m); + pa_assert(!api); + pa_assert(!io_event); + pa_assert(the_pipe[0] == -1); + pa_assert(the_pipe[1] == -1); + pa_assert(!installed); + + last_time = pa_rtclock_now(); /* Prepare the main loop pipe */ - if (pipe(the_pipe) < 0) { - pa_log(__FILE__": pipe() failed: %s", pa_cstrerror(errno)); + if (pa_pipe_cloexec(the_pipe) < 0) { + pa_log("pipe() failed: %s", pa_cstrerror(errno)); return -1; } - pa_make_nonblock_fd(the_pipe[0]); - pa_make_nonblock_fd(the_pipe[1]); - pa_fd_set_cloexec(the_pipe[0], 1); - pa_fd_set_cloexec(the_pipe[1], 1); + pa_make_fd_nonblock(the_pipe[0]); + pa_make_fd_nonblock(the_pipe[1]); api = m; io_event = api->io_new(m, the_pipe[0], PA_IO_EVENT_INPUT, callback, NULL); @@ -191,46 +203,40 @@ int pa_cpu_limit_init(pa_mainloop_api *m) { sa.sa_handler = signal_handler; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART; - + if (sigaction(SIGXCPU, &sa, &sigaction_prev) < 0) { pa_cpu_limit_done(); return -1; } - installed = 1; + installed = TRUE; reset_cpu_time(CPUTIME_INTERVAL_SOFT); - + return 0; } /* Shutdown CPU load limiter */ void pa_cpu_limit_done(void) { - int r; if (io_event) { - assert(api); + pa_assert(api); api->io_free(io_event); io_event = NULL; api = NULL; } - if (the_pipe[0] >= 0) - close(the_pipe[0]); - if (the_pipe[1] >= 0) - close(the_pipe[1]); - the_pipe[0] = the_pipe[1] = -1; + pa_close_pipe(the_pipe); if (installed) { - r = sigaction(SIGXCPU, &sigaction_prev, NULL); - assert(r >= 0); - installed = 0; + pa_assert_se(sigaction(SIGXCPU, &sigaction_prev, NULL) >= 0); + installed = FALSE; } } #else /* HAVE_SIGXCPU */ -int pa_cpu_limit_init(PA_GCC_UNUSED pa_mainloop_api *m) { +int pa_cpu_limit_init(pa_mainloop_api *m) { return 0; } |
