]> code.delx.au - pulseaudio/blobdiff - src/daemon/cpulimit.c
Remove pa_bool_t and replace it with bool.
[pulseaudio] / src / daemon / cpulimit.c
index b77dd4437574dd304f5bde6754608a0cde810a16..7c753978d494c975aa3e1ba2c843b20bd59e765f 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id$ */
-
 /***
   This file is part of PulseAudio.
 
@@ -7,7 +5,7 @@
 
   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
@@ -25,7 +23,8 @@
 #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>
@@ -39,7 +38,6 @@
 #include <errno.h>
 #include <stdio.h>
 #include <string.h>
-#include <sys/time.h>
 #include <unistd.h>
 #include <signal.h>
 
@@ -69,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};
@@ -82,10 +80,10 @@ static pa_io_event *io_event = NULL;
 static struct sigaction sigaction_prev;
 
 /* Nonzero after pa_cpu_limit_init() */
-static int installed = 0;
+static bool 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;
@@ -102,7 +100,7 @@ static void reset_cpu_time(int t) {
     n = ru.ru_utime.tv_sec + ru.ru_stime.tv_sec + t;
     pa_assert_se(getrlimit(RLIMIT_CPU, &rl) >= 0);
 
-    rl.rlim_cur = n;
+    rl.rlim_cur = (rlim_t) n;
     pa_assert_se(setrlimit(RLIMIT_CPU, &rl) >= 0);
 }
 
@@ -119,26 +117,27 @@ static void signal_handler(int sig) {
     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
-        pa_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);
 
@@ -151,7 +150,7 @@ static void signal_handler(int sig) {
 
     } 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;
@@ -166,7 +165,9 @@ static void callback(pa_mainloop_api*m, pa_io_event*e, int fd, pa_io_event_flags
     pa_assert(e == io_event);
     pa_assert(fd == the_pipe[0]);
 
-    pa_read(the_pipe[0], &c, sizeof(c), NULL);
+    pa_log("Received request to terminate due to CPU overload.");
+
+    (void) pa_read(the_pipe[0], &c, sizeof(c), NULL);
     m->quit(m, 1); /* Quit the main loop */
 }
 
@@ -181,18 +182,16 @@ int pa_cpu_limit_init(pa_mainloop_api *m) {
     pa_assert(the_pipe[1] == -1);
     pa_assert(!installed);
 
-    time(&last_time);
+    last_time = pa_rtclock_now();
 
     /* Prepare the main loop pipe */
-    if (pipe(the_pipe) < 0) {
+    if (pa_pipe_cloexec(the_pipe) < 0) {
         pa_log("pipe() failed: %s", pa_cstrerror(errno));
         return -1;
     }
 
     pa_make_fd_nonblock(the_pipe[0]);
     pa_make_fd_nonblock(the_pipe[1]);
-    pa_make_fd_cloexec(the_pipe[0]);
-    pa_make_fd_cloexec(the_pipe[1]);
 
     api = m;
     io_event = api->io_new(m, the_pipe[0], PA_IO_EVENT_INPUT, callback, NULL);
@@ -210,7 +209,7 @@ int pa_cpu_limit_init(pa_mainloop_api *m) {
         return -1;
     }
 
-    installed = 1;
+    installed = true;
 
     reset_cpu_time(CPUTIME_INTERVAL_SOFT);
 
@@ -231,13 +230,13 @@ void pa_cpu_limit_done(void) {
 
     if (installed) {
         pa_assert_se(sigaction(SIGXCPU, &sigaction_prev, NULL) >= 0);
-        installed = 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;
 }