taskstats.txt 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. Per-task statistics interface
  2. -----------------------------
  3. Taskstats is a netlink-based interface for sending per-task and
  4. per-process statistics from the kernel to userspace.
  5. Taskstats was designed for the following benefits:
  6. - efficiently provide statistics during lifetime of a task and on its exit
  7. - unified interface for multiple accounting subsystems
  8. - extensibility for use by future accounting patches
  9. Terminology
  10. -----------
  11. "pid", "tid" and "task" are used interchangeably and refer to the standard
  12. Linux task defined by struct task_struct. per-pid stats are the same as
  13. per-task stats.
  14. "tgid", "process" and "thread group" are used interchangeably and refer to the
  15. tasks that share an mm_struct i.e. the traditional Unix process. Despite the
  16. use of tgid, there is no special treatment for the task that is thread group
  17. leader - a process is deemed alive as long as it has any task belonging to it.
  18. Usage
  19. -----
  20. To get statistics during a task's lifetime, userspace opens a unicast netlink
  21. socket (NETLINK_GENERIC family) and sends commands specifying a pid or a tgid.
  22. The response contains statistics for a task (if pid is specified) or the sum of
  23. statistics for all tasks of the process (if tgid is specified).
  24. To obtain statistics for tasks which are exiting, the userspace listener
  25. sends a register command and specifies a cpumask. Whenever a task exits on
  26. one of the cpus in the cpumask, its per-pid statistics are sent to the
  27. registered listener. Using cpumasks allows the data received by one listener
  28. to be limited and assists in flow control over the netlink interface and is
  29. explained in more detail below.
  30. If the exiting task is the last thread exiting its thread group,
  31. an additional record containing the per-tgid stats is also sent to userspace.
  32. The latter contains the sum of per-pid stats for all threads in the thread
  33. group, both past and present.
  34. getdelays.c is a simple utility demonstrating usage of the taskstats interface
  35. for reporting delay accounting statistics. Users can register cpumasks,
  36. send commands and process responses, listen for per-tid/tgid exit data,
  37. write the data received to a file and do basic flow control by increasing
  38. receive buffer sizes.
  39. Interface
  40. ---------
  41. The user-kernel interface is encapsulated in include/linux/taskstats.h
  42. To avoid this documentation becoming obsolete as the interface evolves, only
  43. an outline of the current version is given. taskstats.h always overrides the
  44. description here.
  45. struct taskstats is the common accounting structure for both per-pid and
  46. per-tgid data. It is versioned and can be extended by each accounting subsystem
  47. that is added to the kernel. The fields and their semantics are defined in the
  48. taskstats.h file.
  49. The data exchanged between user and kernel space is a netlink message belonging
  50. to the NETLINK_GENERIC family and using the netlink attributes interface.
  51. The messages are in the format
  52. +----------+- - -+-------------+-------------------+
  53. | nlmsghdr | Pad | genlmsghdr | taskstats payload |
  54. +----------+- - -+-------------+-------------------+
  55. The taskstats payload is one of the following three kinds:
  56. 1. Commands: Sent from user to kernel. Commands to get data on
  57. a pid/tgid consist of one attribute, of type TASKSTATS_CMD_ATTR_PID/TGID,
  58. containing a u32 pid or tgid in the attribute payload. The pid/tgid denotes
  59. the task/process for which userspace wants statistics.
  60. Commands to register/deregister interest in exit data from a set of cpus
  61. consist of one attribute, of type
  62. TASKSTATS_CMD_ATTR_REGISTER/DEREGISTER_CPUMASK and contain a cpumask in the
  63. attribute payload. The cpumask is specified as an ascii string of
  64. comma-separated cpu ranges e.g. to listen to exit data from cpus 1,2,3,5,7,8
  65. the cpumask would be "1-3,5,7-8". If userspace forgets to deregister interest
  66. in cpus before closing the listening socket, the kernel cleans up its interest
  67. set over time. However, for the sake of efficiency, an explicit deregistration
  68. is advisable.
  69. 2. Response for a command: sent from the kernel in response to a userspace
  70. command. The payload is a series of three attributes of type:
  71. a) TASKSTATS_TYPE_AGGR_PID/TGID : attribute containing no payload but indicates
  72. a pid/tgid will be followed by some stats.
  73. b) TASKSTATS_TYPE_PID/TGID: attribute whose payload is the pid/tgid whose stats
  74. are being returned.
  75. c) TASKSTATS_TYPE_STATS: attribute with a struct taskstats as payload. The
  76. same structure is used for both per-pid and per-tgid stats.
  77. 3. New message sent by kernel whenever a task exits. The payload consists of a
  78. series of attributes of the following type:
  79. a) TASKSTATS_TYPE_AGGR_PID: indicates next two attributes will be pid+stats
  80. b) TASKSTATS_TYPE_PID: contains exiting task's pid
  81. c) TASKSTATS_TYPE_STATS: contains the exiting task's per-pid stats
  82. d) TASKSTATS_TYPE_AGGR_TGID: indicates next two attributes will be tgid+stats
  83. e) TASKSTATS_TYPE_TGID: contains tgid of process to which task belongs
  84. f) TASKSTATS_TYPE_STATS: contains the per-tgid stats for exiting task's process
  85. per-tgid stats
  86. --------------
  87. Taskstats provides per-process stats, in addition to per-task stats, since
  88. resource management is often done at a process granularity and aggregating task
  89. stats in userspace alone is inefficient and potentially inaccurate (due to lack
  90. of atomicity).
  91. However, maintaining per-process, in addition to per-task stats, within the
  92. kernel has space and time overheads. To address this, the taskstats code
  93. accumulates each exiting task's statistics into a process-wide data structure.
  94. When the last task of a process exits, the process level data accumulated also
  95. gets sent to userspace (along with the per-task data).
  96. When a user queries to get per-tgid data, the sum of all other live threads in
  97. the group is added up and added to the accumulated total for previously exited
  98. threads of the same thread group.
  99. Extending taskstats
  100. -------------------
  101. There are two ways to extend the taskstats interface to export more
  102. per-task/process stats as patches to collect them get added to the kernel
  103. in future:
  104. 1. Adding more fields to the end of the existing struct taskstats. Backward
  105. compatibility is ensured by the version number within the
  106. structure. Userspace will use only the fields of the struct that correspond
  107. to the version its using.
  108. 2. Defining separate statistic structs and using the netlink attributes
  109. interface to return them. Since userspace processes each netlink attribute
  110. independently, it can always ignore attributes whose type it does not
  111. understand (because it is using an older version of the interface).
  112. Choosing between 1. and 2. is a matter of trading off flexibility and
  113. overhead. If only a few fields need to be added, then 1. is the preferable
  114. path since the kernel and userspace don't need to incur the overhead of
  115. processing new netlink attributes. But if the new fields expand the existing
  116. struct too much, requiring disparate userspace accounting utilities to
  117. unnecessarily receive large structures whose fields are of no interest, then
  118. extending the attributes structure would be worthwhile.
  119. Flow control for taskstats
  120. --------------------------
  121. When the rate of task exits becomes large, a listener may not be able to keep
  122. up with the kernel's rate of sending per-tid/tgid exit data leading to data
  123. loss. This possibility gets compounded when the taskstats structure gets
  124. extended and the number of cpus grows large.
  125. To avoid losing statistics, userspace should do one or more of the following:
  126. - increase the receive buffer sizes for the netlink sockets opened by
  127. listeners to receive exit data.
  128. - create more listeners and reduce the number of cpus being listened to by
  129. each listener. In the extreme case, there could be one listener for each cpu.
  130. Users may also consider setting the cpu affinity of the listener to the subset
  131. of cpus to which it listens, especially if they are listening to just one cpu.
  132. Despite these measures, if the userspace receives ENOBUFS error messages
  133. indicated overflow of receive buffers, it should take measures to handle the
  134. loss of data.
  135. ----