← Documents Documentation/admin-guide/cgroup-v1/cpuacct.rst GitHub 원문 ↗

Linux 6.18.37 · Administration / Cgroup v1

CPU Accounting Controller

Cgroup v1 cpuacct의 계층 누적, usage·stat 단위와 percpu_counter 관측 제약을 설명합니다.

Source pathDocumentation/admin-guide/cgroup-v1/cpuacct.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

Accounting model

cpuacct.rst:1-21

Parent가 직접 task와 모든 child group의 CPU time을 누적하는 구조를 설명합니다.

Filesystem interface

cpuacct.rst:22-41

Child group 생성, task attach, cpuacct.usage와 cpuacct.stat을 정리합니다.

Counter caveats

cpuacct.rst:42-50

32-bit concurrent read와 batch update 때문에 생기는 순간 오차를 다룹니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

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

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

CPU accounting controller와 root group

1-21

CPU accounting controller는 cgroup으로 task를 묶고 각 task group의 CPU 사용량을 집계합니다. Multi-hierarchy group을 지원하며, accounting group의 값에는 그 group에 직접 속한 task뿐 아니라 모든 child group의 CPU 사용량도 누적됩니다.

Accounting groups can be created by first mounting the cgroup filesystem::

  # mount -t cgroup -ocpuacct none /sys/fs/cgroup

위 명령으로 cgroup filesystem을 mount하면 initial 또는 parent accounting group이 `/sys/fs/cgroup`에 나타납니다. Boot 시 이 group은 system의 모든 task를 포함하고 `/sys/fs/cgroup/tasks`가 그 task 목록을 보여 줍니다.

`/sys/fs/cgroup/cpuacct.usage`는 이 group이 얻은 CPU time을 nanoseconds 단위로 제공합니다. Root group에서는 본질적으로 system 전체 task가 사용한 CPU time입니다.

Root cpuacct interface
PathMeaningUnit
/sys/fs/cgroup/tasksRoot accounting group의 task PID 목록PID
/sys/fs/cgroup/cpuacct.usageRoot와 모든 descendant task의 누적 CPU timenanoseconds

초기 accounting group에서 확인하는 핵심 파일입니다.

Hierarchical CPU accounting
Tasks directly in parentParent cpuacct.usage
Child group tasksChild cpuacct.usageParent cpuacct.usage

Parent 값은 직접 task와 모든 child group의 사용량을 합산합니다.

Child group 생성과 cpuacct.stat

22-41

새 accounting group은 parent `/sys/fs/cgroup` 아래에 directory를 만들어 생성합니다. 다음 명령은 `g1`을 만들고 현재 shell process인 Bash를 그 group으로 이동합니다.

New accounting groups can be created under the parent group /sys/fs/cgroup::

  # cd /sys/fs/cgroup
  # mkdir g1
  # echo $$ > g1/tasks

이후 Bash와 그 child가 소비한 CPU time은 `g1/cpuacct.usage`에서 읽을 수 있고, 같은 값은 ancestor인 `/sys/fs/cgroup/cpuacct.usage`에도 누적됩니다.

Create and attach g1
mkdir g1echo $$ > g1/tasksBash and future children run in g1g1/cpuacct.usage increasesParent cpuacct.usage also increases

Directory 생성, task 이동, descendant accounting 순서입니다.

`cpuacct.stat`은 cgroup의 CPU time을 user mode와 kernel mode로 더 나눕니다. 두 값 모두 `USER_HZ` 단위이므로 `cpuacct.usage`의 nanoseconds와 단위가 다릅니다.

cpuacct.stat fields
FieldMeaningUnit
userCgroup task가 user mode에서 보낸 시간USER_HZ
systemCgroup task가 kernel mode에서 보낸 시간USER_HZ

Mode별 CPU 사용 시간을 제공하는 두 statistic입니다.

percpu_counter의 두 가지 관측 오차

42-50

cpuacct controller는 user·system time을 모으는 데 `percpu_counter` interface를 사용합니다. 이 구현 방식 때문에 읽은 값에는 다음 두 가지 주의점이 있습니다.

percpu_counter side effects
EffectCauseObserved result
Concurrent-read safety`percpu_counter_read()` on 32bit systems is not safe against concurrent writes이론적으로 잘못된 user/system 값이 보일 수 있음
Batch latency`percpu_counter`가 update를 batch 처리user/system 값이 약간 오래된 상태일 수 있음

Statistic을 해석할 때 고려해야 하는 정확도와 최신성 제약입니다.

따라서 `cpuacct.stat`은 매우 빠른 변화 구간에서 순간적으로 부정확하거나 지연될 수 있습니다. 이는 controller의 계층 누적 규칙이 아니라 per-CPU counter를 읽고 합치는 방식에서 오는 관측 특성입니다.