Skip to content

Commit 0437719

Browse files
Hao Jiahtejun
authored andcommitted
cgroup/rstat: Record the cumulative per-cpu time of cgroup and its descendants
The member variable bstat of the structure cgroup_rstat_cpu records the per-cpu time of the cgroup itself, but does not include the per-cpu time of its descendants. The per-cpu time including descendants is very useful for calculating the per-cpu usage of cgroups. Although we can indirectly obtain the total per-cpu time of the cgroup and its descendants by accumulating the per-cpu bstat of each descendant of the cgroup. But after a child cgroup is removed, we will lose its bstat information. This will cause the cumulative value to be non-monotonic, thus affecting the accuracy of cgroup per-cpu usage. So we add the subtree_bstat variable to record the total per-cpu time of this cgroup and its descendants, which is similar to "cpuacct.usage*" in cgroup v1. And this is also helpful for the migration from cgroup v1 to cgroup v2. After adding this variable, we can obtain the per-cpu time of cgroup and its descendants in user mode through eBPF/drgn, etc. And we are still trying to determine how to expose it in the cgroupfs interface. Suggested-by: Tejun Heo <[email protected]> Signed-off-by: Hao Jia <[email protected]> Signed-off-by: Tejun Heo <[email protected]>
1 parent e7e64a1 commit 0437719

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

include/linux/cgroup-defs.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,20 @@ struct cgroup_rstat_cpu {
341341
*/
342342
struct cgroup_base_stat last_bstat;
343343

344+
/*
345+
* This field is used to record the cumulative per-cpu time of
346+
* the cgroup and its descendants. Currently it can be read via
347+
* eBPF/drgn etc, and we are still trying to determine how to
348+
* expose it in the cgroupfs interface.
349+
*/
350+
struct cgroup_base_stat subtree_bstat;
351+
352+
/*
353+
* Snapshots at the last reading. These are used to calculate the
354+
* deltas to propagate to the per-cpu subtree_bstat.
355+
*/
356+
struct cgroup_base_stat last_subtree_bstat;
357+
344358
/*
345359
* Child cgroups with stat updates on this cpu since the last read
346360
* are linked on the parent's ->updated_children through

kernel/cgroup/rstat.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ static void cgroup_base_stat_flush(struct cgroup *cgrp, int cpu)
344344
{
345345
struct cgroup_rstat_cpu *rstatc = cgroup_rstat_cpu(cgrp, cpu);
346346
struct cgroup *parent = cgroup_parent(cgrp);
347+
struct cgroup_rstat_cpu *prstatc;
347348
struct cgroup_base_stat delta;
348349
unsigned seq;
349350

@@ -357,17 +358,24 @@ static void cgroup_base_stat_flush(struct cgroup *cgrp, int cpu)
357358
delta = rstatc->bstat;
358359
} while (__u64_stats_fetch_retry(&rstatc->bsync, seq));
359360

360-
/* propagate percpu delta to global */
361+
/* propagate per-cpu delta to cgroup and per-cpu global statistics */
361362
cgroup_base_stat_sub(&delta, &rstatc->last_bstat);
362363
cgroup_base_stat_add(&cgrp->bstat, &delta);
363364
cgroup_base_stat_add(&rstatc->last_bstat, &delta);
365+
cgroup_base_stat_add(&rstatc->subtree_bstat, &delta);
364366

365-
/* propagate global delta to parent (unless that's root) */
367+
/* propagate cgroup and per-cpu global delta to parent (unless that's root) */
366368
if (cgroup_parent(parent)) {
367369
delta = cgrp->bstat;
368370
cgroup_base_stat_sub(&delta, &cgrp->last_bstat);
369371
cgroup_base_stat_add(&parent->bstat, &delta);
370372
cgroup_base_stat_add(&cgrp->last_bstat, &delta);
373+
374+
delta = rstatc->subtree_bstat;
375+
prstatc = cgroup_rstat_cpu(parent, cpu);
376+
cgroup_base_stat_sub(&delta, &rstatc->last_subtree_bstat);
377+
cgroup_base_stat_add(&prstatc->subtree_bstat, &delta);
378+
cgroup_base_stat_add(&rstatc->last_subtree_bstat, &delta);
371379
}
372380
}
373381

0 commit comments

Comments
 (0)