xref: /OK3568_Linux_fs/kernel/Documentation/admin-guide/cgroup-v1/cpuacct.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun=========================
2*4882a593SmuzhiyunCPU Accounting Controller
3*4882a593Smuzhiyun=========================
4*4882a593Smuzhiyun
5*4882a593SmuzhiyunThe CPU accounting controller is used to group tasks using cgroups and
6*4882a593Smuzhiyunaccount the CPU usage of these groups of tasks.
7*4882a593Smuzhiyun
8*4882a593SmuzhiyunThe CPU accounting controller supports multi-hierarchy groups. An accounting
9*4882a593Smuzhiyungroup accumulates the CPU usage of all of its child groups and the tasks
10*4882a593Smuzhiyundirectly present in its group.
11*4882a593Smuzhiyun
12*4882a593SmuzhiyunAccounting groups can be created by first mounting the cgroup filesystem::
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun  # mount -t cgroup -ocpuacct none /sys/fs/cgroup
15*4882a593Smuzhiyun
16*4882a593SmuzhiyunWith the above step, the initial or the parent accounting group becomes
17*4882a593Smuzhiyunvisible at /sys/fs/cgroup. At bootup, this group includes all the tasks in
18*4882a593Smuzhiyunthe system. /sys/fs/cgroup/tasks lists the tasks in this cgroup.
19*4882a593Smuzhiyun/sys/fs/cgroup/cpuacct.usage gives the CPU time (in nanoseconds) obtained
20*4882a593Smuzhiyunby this group which is essentially the CPU time obtained by all the tasks
21*4882a593Smuzhiyunin the system.
22*4882a593Smuzhiyun
23*4882a593SmuzhiyunNew accounting groups can be created under the parent group /sys/fs/cgroup::
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun  # cd /sys/fs/cgroup
26*4882a593Smuzhiyun  # mkdir g1
27*4882a593Smuzhiyun  # echo $$ > g1/tasks
28*4882a593Smuzhiyun
29*4882a593SmuzhiyunThe above steps create a new group g1 and move the current shell
30*4882a593Smuzhiyunprocess (bash) into it. CPU time consumed by this bash and its children
31*4882a593Smuzhiyuncan be obtained from g1/cpuacct.usage and the same is accumulated in
32*4882a593Smuzhiyun/sys/fs/cgroup/cpuacct.usage also.
33*4882a593Smuzhiyun
34*4882a593Smuzhiyuncpuacct.stat file lists a few statistics which further divide the
35*4882a593SmuzhiyunCPU time obtained by the cgroup into user and system times. Currently
36*4882a593Smuzhiyunthe following statistics are supported:
37*4882a593Smuzhiyun
38*4882a593Smuzhiyunuser: Time spent by tasks of the cgroup in user mode.
39*4882a593Smuzhiyunsystem: Time spent by tasks of the cgroup in kernel mode.
40*4882a593Smuzhiyun
41*4882a593Smuzhiyunuser and system are in USER_HZ unit.
42*4882a593Smuzhiyun
43*4882a593Smuzhiyuncpuacct controller uses percpu_counter interface to collect user and
44*4882a593Smuzhiyunsystem times. This has two side effects:
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun- It is theoretically possible to see wrong values for user and system times.
47*4882a593Smuzhiyun  This is because percpu_counter_read() on 32bit systems isn't safe
48*4882a593Smuzhiyun  against concurrent writes.
49*4882a593Smuzhiyun- It is possible to see slightly outdated values for user and system times
50*4882a593Smuzhiyun  due to the batch processing nature of percpu_counter.
51