1202031Simp========================= 2202031SimpCPU Accounting Controller 3178172Simp========================= 4202031Simp 5178172SimpThe CPU accounting controller is used to group tasks using cgroups and 6178172Simpaccount the CPU usage of these groups of tasks. 7178172Simp 8178172SimpThe CPU accounting controller supports multi-hierarchy groups. An accounting 9178172Simpgroup accumulates the CPU usage of all of its child groups and the tasks 10178172Simpdirectly present in its group. 11178172Simp 12178172SimpAccounting groups can be created by first mounting the cgroup filesystem:: 13178172Simp 14178172Simp # mount -t cgroup -ocpuacct none /sys/fs/cgroup 15178172Simp 16178172SimpWith the above step, the initial or the parent accounting group becomes 17178172Simpvisible at /sys/fs/cgroup. At bootup, this group includes all the tasks in 18178172Simpthe system. /sys/fs/cgroup/tasks lists the tasks in this cgroup. 19178172Simp/sys/fs/cgroup/cpuacct.usage gives the CPU time (in nanoseconds) obtained 20178172Simpby this group which is essentially the CPU time obtained by all the tasks 21178172Simpin the system. 22178172Simp 23178172SimpNew accounting groups can be created under the parent group /sys/fs/cgroup:: 24178172Simp 25178172Simp # cd /sys/fs/cgroup 26178172Simp # mkdir g1 27178172Simp # echo $$ > g1/tasks 28178172Simp 29178172SimpThe above steps create a new group g1 and move the current shell 30178172Simpprocess (bash) into it. CPU time consumed by this bash and its children 31178172Simpcan be obtained from g1/cpuacct.usage and the same is accumulated in 32178172Simp/sys/fs/cgroup/cpuacct.usage also. 33178172Simp 34178172Simpcpuacct.stat file lists a few statistics which further divide the 35178172SimpCPU time obtained by the cgroup into user and system times. Currently 36178172Simpthe following statistics are supported: 37178172Simp 38178172Simpuser: Time spent by tasks of the cgroup in user mode. 39178172Simpsystem: Time spent by tasks of the cgroup in kernel mode. 40202031Simp 41178172Simpuser and system are in USER_HZ unit. 42178172Simp 43178172Simpcpuacct controller uses percpu_counter interface to collect user and 44178172Simpsystem times. This has two side effects: 45178172Simp 46178172Simp- It is theoretically possible to see wrong values for user and system times. 47178172Simp This is because percpu_counter_read() on 32bit systems isn't safe 48178172Simp against concurrent writes. 49178172Simp- It is possible to see slightly outdated values for user and system times 50178172Simp due to the batch processing nature of percpu_counter. 51178172Simp