Skip to content

Commit

Permalink
stats: Work around shrinking system CPU usage
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sirainen committed May 11, 2016
1 parent 45171ad commit 22fb5b6
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions 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"

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 22fb5b6

Please sign in to comment.