]> code.delx.au - pulseaudio/commitdiff
rework cpu limit logic to use monotonic instead of wall clock time
authorLennart Poettering <lennart@poettering.net>
Tue, 19 Aug 2008 21:03:37 +0000 (23:03 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 19 Aug 2008 21:03:37 +0000 (23:03 +0200)
src/daemon/cpulimit.c
src/pulsecore/rtclock.h

index c6e13d83c51f3c9d119b507e25543addbdff2519..59552828502cd8781fe8e7887aa71ccca874794d 100644 (file)
 #endif
 
 #include <pulse/error.h>
+#include <pulse/timeval.h>
 
 #include <pulsecore/core-util.h>
 #include <pulsecore/core-error.h>
 #include <pulsecore/log.h>
 #include <pulsecore/macro.h>
+#include <pulsecore/rtclock.h>
 
 #include "cpulimit.h"
 
@@ -67,7 +69,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};
@@ -117,20 +119,21 @@ 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_usec();
+        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 ((double) CPUTIME_INTERVAL_SOFT >= ((double) (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");
@@ -179,7 +182,7 @@ 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_usec();
 
     /* Prepare the main loop pipe */
     if (pipe(the_pipe) < 0) {
index 705de48f98572148fdbb444fbf16c70b8face25f..aa2cdace3ae4e783d11faeb9c8b986f43ff6ba64 100644 (file)
@@ -23,6 +23,7 @@
 ***/
 
 #include <pulsecore/macro.h>
+#include <pulse/sample.h>
 
 struct timeval;