cgroup: use cgroup->last_bstat instead of cgroup->bstat_pending for consistency
authorTejun Heo <tj@kernel.org>
Wed, 6 Nov 2019 20:49:57 +0000 (12:49 -0800)
committerTejun Heo <tj@kernel.org>
Wed, 6 Nov 2019 20:50:15 +0000 (12:50 -0800)
cgroup->bstat_pending is used to determine the base stat delta to
propagate to the parent.  While correct, this is different from how
percpu delta is determined for no good reason and the inconsistency
makes the code more difficult to understand.

This patch makes parent propagation delta calculation use the same
method as percpu to global propagation.

* cgroup_base_stat_accumulate() is renamed to cgroup_base_stat_add()
  and cgroup_base_stat_sub() is added.

* percpu propagation calculation is updated to use the above helpers.

* cgroup->bstat_pending is replaced with cgroup->last_bstat and
  updated to use the same calculation as percpu propagation.

Signed-off-by: Tejun Heo <tj@kernel.org>
include/linux/cgroup-defs.h
kernel/cgroup/rstat.c

index 430e219e3abafdf9ac308888b4e86b67108ec621..4904b1ebd1ffe3b243b20e9dcf06bf5aa0860e01 100644 (file)
@@ -458,7 +458,7 @@ struct cgroup {
        struct list_head rstat_css_list;
 
        /* cgroup basic resource statistics */
-       struct cgroup_base_stat pending_bstat;  /* pending from children */
+       struct cgroup_base_stat last_bstat;
        struct cgroup_base_stat bstat;
        struct prev_cputime prev_cputime;       /* for printing out cputime */
 
index ca19b4c8acf53f4cd25f7a2c5f94df8f8e508503..b48b22d4deb695a8432955e942149ad3e91abef0 100644 (file)
@@ -304,44 +304,48 @@ void __init cgroup_rstat_boot(void)
  * Functions for cgroup basic resource statistics implemented on top of
  * rstat.
  */
-static void cgroup_base_stat_accumulate(struct cgroup_base_stat *dst_bstat,
-                                       struct cgroup_base_stat *src_bstat)
+static void cgroup_base_stat_add(struct cgroup_base_stat *dst_bstat,
+                                struct cgroup_base_stat *src_bstat)
 {
        dst_bstat->cputime.utime += src_bstat->cputime.utime;
        dst_bstat->cputime.stime += src_bstat->cputime.stime;
        dst_bstat->cputime.sum_exec_runtime += src_bstat->cputime.sum_exec_runtime;
 }
 
+static void cgroup_base_stat_sub(struct cgroup_base_stat *dst_bstat,
+                                struct cgroup_base_stat *src_bstat)
+{
+       dst_bstat->cputime.utime -= src_bstat->cputime.utime;
+       dst_bstat->cputime.stime -= src_bstat->cputime.stime;
+       dst_bstat->cputime.sum_exec_runtime -= src_bstat->cputime.sum_exec_runtime;
+}
+
 static void cgroup_base_stat_flush(struct cgroup *cgrp, int cpu)
 {
        struct cgroup *parent = cgroup_parent(cgrp);
        struct cgroup_rstat_cpu *rstatc = cgroup_rstat_cpu(cgrp, cpu);
-       struct task_cputime *last_cputime = &rstatc->last_bstat.cputime;
-       struct task_cputime cputime;
-       struct cgroup_base_stat delta;
+       struct cgroup_base_stat cur, delta;
        unsigned seq;
 
        /* fetch the current per-cpu values */
        do {
                seq = __u64_stats_fetch_begin(&rstatc->bsync);
-               cputime = rstatc->bstat.cputime;
+               cur.cputime = rstatc->bstat.cputime;
        } while (__u64_stats_fetch_retry(&rstatc->bsync, seq));
 
-       /* calculate the delta to propgate */
-       delta.cputime.utime = cputime.utime - last_cputime->utime;
-       delta.cputime.stime = cputime.stime - last_cputime->stime;
-       delta.cputime.sum_exec_runtime = cputime.sum_exec_runtime -
-                                        last_cputime->sum_exec_runtime;
-       *last_cputime = cputime;
-
-       /* transfer the pending stat into delta */
-       cgroup_base_stat_accumulate(&delta, &cgrp->pending_bstat);
-       memset(&cgrp->pending_bstat, 0, sizeof(cgrp->pending_bstat));
-
-       /* propagate delta into the global stat and the parent's pending */
-       cgroup_base_stat_accumulate(&cgrp->bstat, &delta);
-       if (parent)
-               cgroup_base_stat_accumulate(&parent->pending_bstat, &delta);
+       /* propagate percpu delta to global */
+       delta = cur;
+       cgroup_base_stat_sub(&delta, &rstatc->last_bstat);
+       cgroup_base_stat_add(&cgrp->bstat, &delta);
+       cgroup_base_stat_add(&rstatc->last_bstat, &delta);
+
+       /* propagate global delta to parent */
+       if (parent) {
+               delta = cgrp->bstat;
+               cgroup_base_stat_sub(&delta, &cgrp->last_bstat);
+               cgroup_base_stat_add(&parent->bstat, &delta);
+               cgroup_base_stat_add(&cgrp->last_bstat, &delta);
+       }
 }
 
 static struct cgroup_rstat_cpu *