From: Timo Sirainen Date: Thu, 5 May 2016 18:23:17 +0000 (+0300) Subject: stats: Work around shrinking system CPU usage X-Git-Tag: 2.3.0.rc1~3829 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=553dc92ad045d748139cab65ad5e2da4c62f977a;p=thirdparty%2Fdovecot%2Fcore.git stats: Work around shrinking system CPU usage 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 --- diff --git a/src/plugins/stats/mail-stats-fill.c b/src/plugins/stats/mail-stats-fill.c index 7c48f6feab..95e4445f6c 100644 --- a/src/plugins/stats/mail-stats-fill.c +++ b/src/plugins/stats/mail-stats-fill.c @@ -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;