2017-09-25 15:12:05 +00:00
|
|
|
#include "cgroup-internal.h"
|
|
|
|
|
|
|
|
#include <linux/sched/cputime.h>
|
|
|
|
|
2018-04-26 21:29:04 +00:00
|
|
|
static DEFINE_MUTEX(cgroup_rstat_mutex);
|
|
|
|
static DEFINE_PER_CPU(raw_spinlock_t, cgroup_rstat_cpu_lock);
|
2017-09-25 15:12:05 +00:00
|
|
|
|
2018-04-26 21:29:04 +00:00
|
|
|
static struct cgroup_rstat_cpu *cgroup_rstat_cpu(struct cgroup *cgrp, int cpu)
|
2017-09-25 15:12:05 +00:00
|
|
|
{
|
2018-04-26 21:29:04 +00:00
|
|
|
return per_cpu_ptr(cgrp->rstat_cpu, cpu);
|
2017-09-25 15:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-26 21:29:04 +00:00
|
|
|
* cgroup_rstat_cpu_updated - keep track of updated rstat_cpu
|
2017-09-25 15:12:05 +00:00
|
|
|
* @cgrp: target cgroup
|
2018-04-26 21:29:04 +00:00
|
|
|
* @cpu: cpu on which rstat_cpu was updated
|
2017-09-25 15:12:05 +00:00
|
|
|
*
|
2018-04-26 21:29:04 +00:00
|
|
|
* @cgrp's rstat_cpu on @cpu was updated. Put it on the parent's matching
|
|
|
|
* rstat_cpu->updated_children list. See the comment on top of
|
|
|
|
* cgroup_rstat_cpu definition for details.
|
2017-09-25 15:12:05 +00:00
|
|
|
*/
|
2018-04-26 21:29:04 +00:00
|
|
|
static void cgroup_rstat_cpu_updated(struct cgroup *cgrp, int cpu)
|
2017-09-25 15:12:05 +00:00
|
|
|
{
|
2018-04-26 21:29:04 +00:00
|
|
|
raw_spinlock_t *cpu_lock = per_cpu_ptr(&cgroup_rstat_cpu_lock, cpu);
|
2017-09-25 15:12:05 +00:00
|
|
|
struct cgroup *parent;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Speculative already-on-list test. This may race leading to
|
|
|
|
* temporary inaccuracies, which is fine.
|
|
|
|
*
|
|
|
|
* Because @parent's updated_children is terminated with @parent
|
|
|
|
* instead of NULL, we can tell whether @cgrp is on the list by
|
|
|
|
* testing the next pointer for NULL.
|
|
|
|
*/
|
2018-04-26 21:29:04 +00:00
|
|
|
if (cgroup_rstat_cpu(cgrp, cpu)->updated_next)
|
2017-09-25 15:12:05 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
raw_spin_lock_irqsave(cpu_lock, flags);
|
|
|
|
|
|
|
|
/* put @cgrp and all ancestors on the corresponding updated lists */
|
|
|
|
for (parent = cgroup_parent(cgrp); parent;
|
|
|
|
cgrp = parent, parent = cgroup_parent(cgrp)) {
|
2018-04-26 21:29:04 +00:00
|
|
|
struct cgroup_rstat_cpu *rstatc = cgroup_rstat_cpu(cgrp, cpu);
|
|
|
|
struct cgroup_rstat_cpu *prstatc = cgroup_rstat_cpu(parent, cpu);
|
2017-09-25 15:12:05 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Both additions and removals are bottom-up. If a cgroup
|
|
|
|
* is already in the tree, all ancestors are.
|
|
|
|
*/
|
2018-04-26 21:29:04 +00:00
|
|
|
if (rstatc->updated_next)
|
2017-09-25 15:12:05 +00:00
|
|
|
break;
|
|
|
|
|
2018-04-26 21:29:04 +00:00
|
|
|
rstatc->updated_next = prstatc->updated_children;
|
|
|
|
prstatc->updated_children = cgrp;
|
2017-09-25 15:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
raw_spin_unlock_irqrestore(cpu_lock, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-26 21:29:04 +00:00
|
|
|
* cgroup_rstat_cpu_pop_updated - iterate and dismantle rstat_cpu updated tree
|
2017-09-25 15:12:05 +00:00
|
|
|
* @pos: current position
|
|
|
|
* @root: root of the tree to traversal
|
|
|
|
* @cpu: target cpu
|
|
|
|
*
|
2018-04-26 21:29:04 +00:00
|
|
|
* Walks the udpated rstat_cpu tree on @cpu from @root. %NULL @pos starts
|
2017-09-25 15:12:05 +00:00
|
|
|
* the traversal and %NULL return indicates the end. During traversal,
|
|
|
|
* each returned cgroup is unlinked from the tree. Must be called with the
|
2018-04-26 21:29:04 +00:00
|
|
|
* matching cgroup_rstat_cpu_lock held.
|
2017-09-25 15:12:05 +00:00
|
|
|
*
|
|
|
|
* The only ordering guarantee is that, for a parent and a child pair
|
|
|
|
* covered by a given traversal, if a child is visited, its parent is
|
|
|
|
* guaranteed to be visited afterwards.
|
|
|
|
*/
|
2018-04-26 21:29:04 +00:00
|
|
|
static struct cgroup *cgroup_rstat_cpu_pop_updated(struct cgroup *pos,
|
|
|
|
struct cgroup *root, int cpu)
|
2017-09-25 15:12:05 +00:00
|
|
|
{
|
2018-04-26 21:29:04 +00:00
|
|
|
struct cgroup_rstat_cpu *rstatc;
|
2017-09-25 15:12:05 +00:00
|
|
|
struct cgroup *parent;
|
|
|
|
|
|
|
|
if (pos == root)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We're gonna walk down to the first leaf and visit/remove it. We
|
|
|
|
* can pick whatever unvisited node as the starting point.
|
|
|
|
*/
|
|
|
|
if (!pos)
|
|
|
|
pos = root;
|
|
|
|
else
|
|
|
|
pos = cgroup_parent(pos);
|
|
|
|
|
|
|
|
/* walk down to the first leaf */
|
|
|
|
while (true) {
|
2018-04-26 21:29:04 +00:00
|
|
|
rstatc = cgroup_rstat_cpu(pos, cpu);
|
|
|
|
if (rstatc->updated_children == pos)
|
2017-09-25 15:12:05 +00:00
|
|
|
break;
|
2018-04-26 21:29:04 +00:00
|
|
|
pos = rstatc->updated_children;
|
2017-09-25 15:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unlink @pos from the tree. As the updated_children list is
|
|
|
|
* singly linked, we have to walk it to find the removal point.
|
|
|
|
* However, due to the way we traverse, @pos will be the first
|
|
|
|
* child in most cases. The only exception is @root.
|
|
|
|
*/
|
|
|
|
parent = cgroup_parent(pos);
|
2018-04-26 21:29:04 +00:00
|
|
|
if (parent && rstatc->updated_next) {
|
|
|
|
struct cgroup_rstat_cpu *prstatc = cgroup_rstat_cpu(parent, cpu);
|
|
|
|
struct cgroup_rstat_cpu *nrstatc;
|
2017-09-25 15:12:05 +00:00
|
|
|
struct cgroup **nextp;
|
|
|
|
|
2018-04-26 21:29:04 +00:00
|
|
|
nextp = &prstatc->updated_children;
|
2017-09-25 15:12:05 +00:00
|
|
|
while (true) {
|
2018-04-26 21:29:04 +00:00
|
|
|
nrstatc = cgroup_rstat_cpu(*nextp, cpu);
|
2017-09-25 15:12:05 +00:00
|
|
|
if (*nextp == pos)
|
|
|
|
break;
|
|
|
|
|
|
|
|
WARN_ON_ONCE(*nextp == parent);
|
2018-04-26 21:29:04 +00:00
|
|
|
nextp = &nrstatc->updated_next;
|
2017-09-25 15:12:05 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 21:29:04 +00:00
|
|
|
*nextp = rstatc->updated_next;
|
|
|
|
rstatc->updated_next = NULL;
|
2017-09-25 15:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cgroup_stat_accumulate(struct cgroup_stat *dst_stat,
|
|
|
|
struct cgroup_stat *src_stat)
|
|
|
|
{
|
|
|
|
dst_stat->cputime.utime += src_stat->cputime.utime;
|
|
|
|
dst_stat->cputime.stime += src_stat->cputime.stime;
|
|
|
|
dst_stat->cputime.sum_exec_runtime += src_stat->cputime.sum_exec_runtime;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cgroup_cpu_stat_flush_one(struct cgroup *cgrp, int cpu)
|
|
|
|
{
|
|
|
|
struct cgroup *parent = cgroup_parent(cgrp);
|
2018-04-26 21:29:04 +00:00
|
|
|
struct cgroup_rstat_cpu *rstatc = cgroup_rstat_cpu(cgrp, cpu);
|
|
|
|
struct task_cputime *last_cputime = &rstatc->last_cputime;
|
2017-09-25 15:12:05 +00:00
|
|
|
struct task_cputime cputime;
|
|
|
|
struct cgroup_stat delta;
|
|
|
|
unsigned seq;
|
|
|
|
|
2018-04-26 21:29:04 +00:00
|
|
|
lockdep_assert_held(&cgroup_rstat_mutex);
|
2017-09-25 15:12:05 +00:00
|
|
|
|
|
|
|
/* fetch the current per-cpu values */
|
|
|
|
do {
|
2018-04-26 21:29:04 +00:00
|
|
|
seq = __u64_stats_fetch_begin(&rstatc->sync);
|
|
|
|
cputime = rstatc->cputime;
|
|
|
|
} while (__u64_stats_fetch_retry(&rstatc->sync, seq));
|
2017-09-25 15:12:05 +00:00
|
|
|
|
|
|
|
/* accumulate the deltas 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_stat_accumulate(&delta, &cgrp->pending_stat);
|
|
|
|
memset(&cgrp->pending_stat, 0, sizeof(cgrp->pending_stat));
|
|
|
|
|
|
|
|
/* propagate delta into the global stat and the parent's pending */
|
|
|
|
cgroup_stat_accumulate(&cgrp->stat, &delta);
|
|
|
|
if (parent)
|
|
|
|
cgroup_stat_accumulate(&parent->pending_stat, &delta);
|
|
|
|
}
|
|
|
|
|
2018-04-26 21:29:04 +00:00
|
|
|
/* see cgroup_rstat_flush() */
|
|
|
|
static void cgroup_rstat_flush_locked(struct cgroup *cgrp)
|
2017-09-25 15:12:05 +00:00
|
|
|
{
|
|
|
|
int cpu;
|
|
|
|
|
2018-04-26 21:29:04 +00:00
|
|
|
lockdep_assert_held(&cgroup_rstat_mutex);
|
2017-09-25 15:12:05 +00:00
|
|
|
|
|
|
|
for_each_possible_cpu(cpu) {
|
2018-04-26 21:29:04 +00:00
|
|
|
raw_spinlock_t *cpu_lock = per_cpu_ptr(&cgroup_rstat_cpu_lock,
|
|
|
|
cpu);
|
2017-09-25 15:12:05 +00:00
|
|
|
struct cgroup *pos = NULL;
|
|
|
|
|
|
|
|
raw_spin_lock_irq(cpu_lock);
|
2018-04-26 21:29:04 +00:00
|
|
|
while ((pos = cgroup_rstat_cpu_pop_updated(pos, cgrp, cpu)))
|
2017-09-25 15:12:05 +00:00
|
|
|
cgroup_cpu_stat_flush_one(pos, cpu);
|
|
|
|
raw_spin_unlock_irq(cpu_lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-26 21:29:04 +00:00
|
|
|
* cgroup_rstat_flush - flush stats in @cgrp's subtree
|
2017-09-25 15:12:05 +00:00
|
|
|
* @cgrp: target cgroup
|
|
|
|
*
|
|
|
|
* Collect all per-cpu stats in @cgrp's subtree into the global counters
|
|
|
|
* and propagate them upwards. After this function returns, all cgroups in
|
|
|
|
* the subtree have up-to-date ->stat.
|
|
|
|
*
|
|
|
|
* This also gets all cgroups in the subtree including @cgrp off the
|
|
|
|
* ->updated_children lists.
|
|
|
|
*/
|
2018-04-26 21:29:04 +00:00
|
|
|
void cgroup_rstat_flush(struct cgroup *cgrp)
|
2017-09-25 15:12:05 +00:00
|
|
|
{
|
2018-04-26 21:29:04 +00:00
|
|
|
mutex_lock(&cgroup_rstat_mutex);
|
|
|
|
cgroup_rstat_flush_locked(cgrp);
|
|
|
|
mutex_unlock(&cgroup_rstat_mutex);
|
2017-09-25 15:12:05 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 21:29:04 +00:00
|
|
|
static struct cgroup_rstat_cpu *
|
|
|
|
cgroup_cpu_stat_account_begin(struct cgroup *cgrp)
|
2017-09-25 15:12:05 +00:00
|
|
|
{
|
2018-04-26 21:29:04 +00:00
|
|
|
struct cgroup_rstat_cpu *rstatc;
|
2017-09-25 15:12:05 +00:00
|
|
|
|
2018-04-26 21:29:04 +00:00
|
|
|
rstatc = get_cpu_ptr(cgrp->rstat_cpu);
|
|
|
|
u64_stats_update_begin(&rstatc->sync);
|
|
|
|
return rstatc;
|
2017-09-25 15:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cgroup_cpu_stat_account_end(struct cgroup *cgrp,
|
2018-04-26 21:29:04 +00:00
|
|
|
struct cgroup_rstat_cpu *rstatc)
|
2017-09-25 15:12:05 +00:00
|
|
|
{
|
2018-04-26 21:29:04 +00:00
|
|
|
u64_stats_update_end(&rstatc->sync);
|
|
|
|
cgroup_rstat_cpu_updated(cgrp, smp_processor_id());
|
|
|
|
put_cpu_ptr(rstatc);
|
2017-09-25 15:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void __cgroup_account_cputime(struct cgroup *cgrp, u64 delta_exec)
|
|
|
|
{
|
2018-04-26 21:29:04 +00:00
|
|
|
struct cgroup_rstat_cpu *rstatc;
|
2017-09-25 15:12:05 +00:00
|
|
|
|
2018-04-26 21:29:04 +00:00
|
|
|
rstatc = cgroup_cpu_stat_account_begin(cgrp);
|
|
|
|
rstatc->cputime.sum_exec_runtime += delta_exec;
|
|
|
|
cgroup_cpu_stat_account_end(cgrp, rstatc);
|
2017-09-25 15:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void __cgroup_account_cputime_field(struct cgroup *cgrp,
|
|
|
|
enum cpu_usage_stat index, u64 delta_exec)
|
|
|
|
{
|
2018-04-26 21:29:04 +00:00
|
|
|
struct cgroup_rstat_cpu *rstatc;
|
2017-09-25 15:12:05 +00:00
|
|
|
|
2018-04-26 21:29:04 +00:00
|
|
|
rstatc = cgroup_cpu_stat_account_begin(cgrp);
|
2017-09-25 15:12:05 +00:00
|
|
|
|
|
|
|
switch (index) {
|
|
|
|
case CPUTIME_USER:
|
|
|
|
case CPUTIME_NICE:
|
2018-04-26 21:29:04 +00:00
|
|
|
rstatc->cputime.utime += delta_exec;
|
2017-09-25 15:12:05 +00:00
|
|
|
break;
|
|
|
|
case CPUTIME_SYSTEM:
|
|
|
|
case CPUTIME_IRQ:
|
|
|
|
case CPUTIME_SOFTIRQ:
|
2018-04-26 21:29:04 +00:00
|
|
|
rstatc->cputime.stime += delta_exec;
|
2017-09-25 15:12:05 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-04-26 21:29:04 +00:00
|
|
|
cgroup_cpu_stat_account_end(cgrp, rstatc);
|
2017-09-25 15:12:05 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 23:18:27 +00:00
|
|
|
void cgroup_stat_show_cputime(struct seq_file *seq)
|
2017-09-25 15:12:05 +00:00
|
|
|
{
|
|
|
|
struct cgroup *cgrp = seq_css(seq)->cgroup;
|
|
|
|
u64 usage, utime, stime;
|
|
|
|
|
|
|
|
if (!cgroup_parent(cgrp))
|
|
|
|
return;
|
|
|
|
|
2018-04-26 21:29:04 +00:00
|
|
|
mutex_lock(&cgroup_rstat_mutex);
|
2017-09-25 15:12:05 +00:00
|
|
|
|
2018-04-26 21:29:04 +00:00
|
|
|
cgroup_rstat_flush_locked(cgrp);
|
2017-09-25 15:12:05 +00:00
|
|
|
|
|
|
|
usage = cgrp->stat.cputime.sum_exec_runtime;
|
|
|
|
cputime_adjust(&cgrp->stat.cputime, &cgrp->stat.prev_cputime,
|
|
|
|
&utime, &stime);
|
|
|
|
|
2018-04-26 21:29:04 +00:00
|
|
|
mutex_unlock(&cgroup_rstat_mutex);
|
2017-09-25 15:12:05 +00:00
|
|
|
|
|
|
|
do_div(usage, NSEC_PER_USEC);
|
|
|
|
do_div(utime, NSEC_PER_USEC);
|
|
|
|
do_div(stime, NSEC_PER_USEC);
|
|
|
|
|
2017-10-23 23:18:27 +00:00
|
|
|
seq_printf(seq, "usage_usec %llu\n"
|
|
|
|
"user_usec %llu\n"
|
|
|
|
"system_usec %llu\n",
|
|
|
|
usage, utime, stime);
|
2017-09-25 15:12:05 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 21:29:04 +00:00
|
|
|
int cgroup_rstat_init(struct cgroup *cgrp)
|
2017-09-25 15:12:05 +00:00
|
|
|
{
|
|
|
|
int cpu;
|
|
|
|
|
2018-04-26 21:29:04 +00:00
|
|
|
/* the root cgrp has rstat_cpu preallocated */
|
|
|
|
if (!cgrp->rstat_cpu) {
|
|
|
|
cgrp->rstat_cpu = alloc_percpu(struct cgroup_rstat_cpu);
|
|
|
|
if (!cgrp->rstat_cpu)
|
2017-09-25 15:12:05 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ->updated_children list is self terminated */
|
2017-11-28 12:59:25 +00:00
|
|
|
for_each_possible_cpu(cpu) {
|
2018-04-26 21:29:04 +00:00
|
|
|
struct cgroup_rstat_cpu *rstatc = cgroup_rstat_cpu(cgrp, cpu);
|
2017-11-28 12:59:25 +00:00
|
|
|
|
2018-04-26 21:29:04 +00:00
|
|
|
rstatc->updated_children = cgrp;
|
|
|
|
u64_stats_init(&rstatc->sync);
|
2017-11-28 12:59:25 +00:00
|
|
|
}
|
2017-09-25 15:12:05 +00:00
|
|
|
|
|
|
|
prev_cputime_init(&cgrp->stat.prev_cputime);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-26 21:29:04 +00:00
|
|
|
void cgroup_rstat_exit(struct cgroup *cgrp)
|
2017-09-25 15:12:05 +00:00
|
|
|
{
|
|
|
|
int cpu;
|
|
|
|
|
2018-04-26 21:29:04 +00:00
|
|
|
cgroup_rstat_flush(cgrp);
|
2017-09-25 15:12:05 +00:00
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
for_each_possible_cpu(cpu) {
|
2018-04-26 21:29:04 +00:00
|
|
|
struct cgroup_rstat_cpu *rstatc = cgroup_rstat_cpu(cgrp, cpu);
|
2017-09-25 15:12:05 +00:00
|
|
|
|
2018-04-26 21:29:04 +00:00
|
|
|
if (WARN_ON_ONCE(rstatc->updated_children != cgrp) ||
|
|
|
|
WARN_ON_ONCE(rstatc->updated_next))
|
2017-09-25 15:12:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-26 21:29:04 +00:00
|
|
|
free_percpu(cgrp->rstat_cpu);
|
|
|
|
cgrp->rstat_cpu = NULL;
|
2017-09-25 15:12:05 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 21:29:04 +00:00
|
|
|
void __init cgroup_rstat_boot(void)
|
2017-09-25 15:12:05 +00:00
|
|
|
{
|
|
|
|
int cpu;
|
|
|
|
|
|
|
|
for_each_possible_cpu(cpu)
|
2018-04-26 21:29:04 +00:00
|
|
|
raw_spin_lock_init(per_cpu_ptr(&cgroup_rstat_cpu_lock, cpu));
|
2017-09-25 15:12:05 +00:00
|
|
|
|
2018-04-26 21:29:04 +00:00
|
|
|
BUG_ON(cgroup_rstat_init(&cgrp_dfl_root.cgrp));
|
2017-09-25 15:12:05 +00:00
|
|
|
}
|