162306a36Sopenharmony_ci=========================
262306a36Sopenharmony_ciCPU Accounting Controller
362306a36Sopenharmony_ci=========================
462306a36Sopenharmony_ci
562306a36Sopenharmony_ciThe CPU accounting controller is used to group tasks using cgroups and
662306a36Sopenharmony_ciaccount the CPU usage of these groups of tasks.
762306a36Sopenharmony_ci
862306a36Sopenharmony_ciThe CPU accounting controller supports multi-hierarchy groups. An accounting
962306a36Sopenharmony_cigroup accumulates the CPU usage of all of its child groups and the tasks
1062306a36Sopenharmony_cidirectly present in its group.
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ciAccounting groups can be created by first mounting the cgroup filesystem::
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci  # mount -t cgroup -ocpuacct none /sys/fs/cgroup
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ciWith the above step, the initial or the parent accounting group becomes
1762306a36Sopenharmony_civisible at /sys/fs/cgroup. At bootup, this group includes all the tasks in
1862306a36Sopenharmony_cithe system. /sys/fs/cgroup/tasks lists the tasks in this cgroup.
1962306a36Sopenharmony_ci/sys/fs/cgroup/cpuacct.usage gives the CPU time (in nanoseconds) obtained
2062306a36Sopenharmony_ciby this group which is essentially the CPU time obtained by all the tasks
2162306a36Sopenharmony_ciin the system.
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ciNew accounting groups can be created under the parent group /sys/fs/cgroup::
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci  # cd /sys/fs/cgroup
2662306a36Sopenharmony_ci  # mkdir g1
2762306a36Sopenharmony_ci  # echo $$ > g1/tasks
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ciThe above steps create a new group g1 and move the current shell
3062306a36Sopenharmony_ciprocess (bash) into it. CPU time consumed by this bash and its children
3162306a36Sopenharmony_cican be obtained from g1/cpuacct.usage and the same is accumulated in
3262306a36Sopenharmony_ci/sys/fs/cgroup/cpuacct.usage also.
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_cicpuacct.stat file lists a few statistics which further divide the
3562306a36Sopenharmony_ciCPU time obtained by the cgroup into user and system times. Currently
3662306a36Sopenharmony_cithe following statistics are supported:
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ciuser: Time spent by tasks of the cgroup in user mode.
3962306a36Sopenharmony_cisystem: Time spent by tasks of the cgroup in kernel mode.
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ciuser and system are in USER_HZ unit.
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cicpuacct controller uses percpu_counter interface to collect user and
4462306a36Sopenharmony_cisystem times. This has two side effects:
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci- It is theoretically possible to see wrong values for user and system times.
4762306a36Sopenharmony_ci  This is because percpu_counter_read() on 32bit systems isn't safe
4862306a36Sopenharmony_ci  against concurrent writes.
4962306a36Sopenharmony_ci- It is possible to see slightly outdated values for user and system times
5062306a36Sopenharmony_ci  due to the batch processing nature of percpu_counter.
51