^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) ================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) Delay accounting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) ================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) Tasks encounter delays in execution when they wait
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) for some kernel resource to become available e.g. a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) runnable task may wait for a free CPU to run on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) The per-task delay accounting functionality measures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) the delays experienced by a task while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) a) waiting for a CPU (while being runnable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) b) completion of synchronous block I/O initiated by the task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) c) swapping in pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) d) memory reclaim
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) and makes these statistics available to userspace through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) the taskstats interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) Such delays provide feedback for setting a task's cpu priority,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) io priority and rss limit values appropriately. Long delays for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) important tasks could be a trigger for raising its corresponding priority.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) The functionality, through its use of the taskstats interface, also provides
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) delay statistics aggregated for all tasks (or threads) belonging to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) thread group (corresponding to a traditional Unix process). This is a commonly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) needed aggregation that is more efficiently done by the kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) Userspace utilities, particularly resource management applications, can also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) aggregate delay statistics into arbitrary groups. To enable this, delay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) statistics of a task are available both during its lifetime as well as on its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) exit, ensuring continuous and complete monitoring can be done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) Interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) ---------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) Delay accounting uses the taskstats interface which is described
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) in detail in a separate document in this directory. Taskstats returns a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) generic data structure to userspace corresponding to per-pid and per-tgid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) statistics. The delay accounting functionality populates specific fields of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) this structure. See
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) include/linux/taskstats.h
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) for a description of the fields pertaining to delay accounting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) It will generally be in the form of counters returning the cumulative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) delay seen for cpu, sync block I/O, swapin, memory reclaim etc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) Taking the difference of two successive readings of a given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) counter (say cpu_delay_total) for a task will give the delay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) experienced by the task waiting for the corresponding resource
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) in that interval.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) When a task exits, records containing the per-task statistics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) are sent to userspace without requiring a command. If it is the last exiting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) task of a thread group, the per-tgid statistics are also sent. More details
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) are given in the taskstats interface description.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) The getdelays.c userspace utility in tools/accounting directory allows simple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) commands to be run and the corresponding delay statistics to be displayed. It
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) also serves as an example of using the taskstats interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) Usage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) -----
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) Compile the kernel with::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) CONFIG_TASK_DELAY_ACCT=y
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) CONFIG_TASKSTATS=y
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) Delay accounting is enabled by default at boot up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) To disable, add::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) nodelayacct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) to the kernel boot options. The rest of the instructions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) below assume this has not been done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) After the system has booted up, use a utility
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) similar to getdelays.c to access the delays
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) seen by a given task or a task group (tgid).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) The utility also allows a given command to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) executed and the corresponding delays to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) seen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) General format of the getdelays command::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) getdelays [-t tgid] [-p pid] [-c cmd...]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) Get delays, since system boot, for pid 10::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) # ./getdelays -p 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) (output similar to next case)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) Get sum of delays, since system boot, for all pids with tgid 5::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) # ./getdelays -t 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) CPU count real total virtual total delay total
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 7876 92005750 100000000 24001500
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) IO count delay total
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 0 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) SWAP count delay total
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 0 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) RECLAIM count delay total
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 0 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) Get delays seen in executing a given simple command::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) # ./getdelays -c ls /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) bin data1 data3 data5 dev home media opt root srv sys usr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) boot data2 data4 data6 etc lib mnt proc sbin subdomain tmp var
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) CPU count real total virtual total delay total
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 6 4000250 4000000 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) IO count delay total
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 0 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) SWAP count delay total
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 0 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) RECLAIM count delay total
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 0 0