]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
stats: Work around shrinking system CPU usage
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Thu, 5 May 2016 18:23:17 +0000 (21:23 +0300)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Tue, 10 May 2016 21:22:36 +0000 (17:22 -0400)
Happening at least in Debian's Linux kernel 4.3.0-1-amd64.

getrusage() may returns ru_stime = 4000 or 8000, but later it drops to 0.
We'll just work around this by switching to the previous working ru_stime.

This fixes errors like:
Error: stats: session stats shrank: sys_cpu 0.0 < 0.4000

src/plugins/stats/mail-stats-fill.c

index 7c48f6feab0087e1ae54e10cdba22db9494d12da..95e4445f6cb7b5358932088660179e81c80b9715 100644 (file)
@@ -1,6 +1,7 @@
 /* Copyright (c) 2011-2016 Dovecot authors, see the included COPYING file */
 
 #include "lib.h"
+#include "time-util.h"
 #include "stats-plugin.h"
 #include "mail-stats.h"
 
@@ -110,12 +111,19 @@ user_trans_stats_get(struct stats_user *suser, struct mail_stats *dest_r)
 
 void mail_stats_fill(struct stats_user *suser, struct mail_stats *stats_r)
 {
+       static struct rusage prev_usage;
        struct rusage usage;
 
        memset(stats_r, 0, sizeof(*stats_r));
        /* cputime */
        if (getrusage(RUSAGE_SELF, &usage) < 0)
                memset(&usage, 0, sizeof(usage));
+       if (timeval_diff_usecs(&usage.ru_stime, &prev_usage.ru_stime) < 0) {
+               /* This seems to be a Linux bug. */
+               usage.ru_stime = prev_usage.ru_stime;
+       }
+       prev_usage = usage;
+
        stats_r->user_cpu = usage.ru_utime;
        stats_r->sys_cpu = usage.ru_stime;
        stats_r->min_faults = usage.ru_minflt;