taskstats.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright (C) 2013 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * Linux task stats reporting tool. Queries and prints out the kernel's
  18. * taskstats structure for a given process or thread group id. See
  19. * https://www.kernel.org/doc/Documentation/accounting/ for more information
  20. * about the reported fields.
  21. */
  22. #include <errno.h>
  23. #include <getopt.h>
  24. #include <netlink/attr.h>
  25. #include <netlink/genl/genl.h>
  26. #include <netlink/genl/ctrl.h>
  27. #include <netlink/handlers.h>
  28. #include <netlink/msg.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <sys/cdefs.h>
  32. #include <time.h>
  33. #include <unistd.h>
  34. #include <linux/taskstats.h>
  35. struct TaskStatistics {
  36. int pid;
  37. int tgid;
  38. struct taskstats stats;
  39. };
  40. int print_receive_error(struct sockaddr_nl* address __unused,
  41. struct nlmsgerr* error, void* arg __unused) {
  42. fprintf(stderr, "Netlink receive error: %s\n", strerror(-error->error));
  43. return NL_STOP;
  44. }
  45. void parse_aggregate_task_stats(struct nlattr* attr, int attr_size,
  46. struct TaskStatistics* stats) {
  47. nla_for_each_attr(attr, attr, attr_size, attr_size) {
  48. switch (attr->nla_type) {
  49. case TASKSTATS_TYPE_PID:
  50. stats->pid = nla_get_u32(attr);
  51. break;
  52. case TASKSTATS_TYPE_TGID:
  53. stats->tgid = nla_get_u32(attr);
  54. break;
  55. case TASKSTATS_TYPE_STATS:
  56. nla_memcpy(&stats->stats, attr, sizeof(stats->stats));
  57. break;
  58. default:
  59. break;
  60. }
  61. }
  62. }
  63. int parse_task_stats(struct nl_msg* msg, void* arg) {
  64. struct TaskStatistics* stats = (struct TaskStatistics*)arg;
  65. struct genlmsghdr* gnlh = (struct genlmsghdr*)nlmsg_data(nlmsg_hdr(msg));
  66. struct nlattr* attr = genlmsg_attrdata(gnlh, 0);
  67. int remaining = genlmsg_attrlen(gnlh, 0);
  68. nla_for_each_attr(attr, attr, remaining, remaining) {
  69. switch (attr->nla_type) {
  70. case TASKSTATS_TYPE_AGGR_PID:
  71. case TASKSTATS_TYPE_AGGR_TGID:
  72. parse_aggregate_task_stats(nla_data(attr), nla_len(attr),
  73. stats);
  74. break;
  75. default:
  76. break;
  77. }
  78. }
  79. return NL_STOP;
  80. }
  81. int query_task_stats(struct nl_sock* netlink_socket, int family_id,
  82. int command_type, int parameter,
  83. struct TaskStatistics* stats) {
  84. memset(stats, 0, sizeof(*stats));
  85. struct nl_msg* message = nlmsg_alloc();
  86. genlmsg_put(message, NL_AUTO_PID, NL_AUTO_SEQ, family_id, 0, 0,
  87. TASKSTATS_CMD_GET, TASKSTATS_VERSION);
  88. nla_put_u32(message, command_type, parameter);
  89. int result = nl_send_auto_complete(netlink_socket, message);
  90. nlmsg_free(message);
  91. if (result < 0) {
  92. return result;
  93. }
  94. struct nl_cb* callbacks = nl_cb_get(nl_cb_alloc(NL_CB_CUSTOM));
  95. nl_cb_set(callbacks, NL_CB_VALID, NL_CB_CUSTOM, &parse_task_stats, stats);
  96. nl_cb_err(callbacks, NL_CB_CUSTOM, &print_receive_error, &family_id);
  97. result = nl_recvmsgs(netlink_socket, callbacks);
  98. nl_cb_put(callbacks);
  99. if (result < 0) {
  100. return result;
  101. }
  102. return stats->pid || stats->tgid;
  103. }
  104. double average_ms(unsigned long long total, unsigned long long count) {
  105. if (!count) {
  106. return 0;
  107. }
  108. return ((double)total) / count / 1e6;
  109. }
  110. unsigned long long average_ns(unsigned long long total,
  111. unsigned long long count) {
  112. if (!count) {
  113. return 0;
  114. }
  115. return total / count;
  116. }
  117. void print_task_stats(const struct TaskStatistics* stats,
  118. int human_readable) {
  119. const struct taskstats* s = &stats->stats;
  120. printf("Basic task statistics\n");
  121. printf("---------------------\n");
  122. printf("%-25s%d\n", "Stats version:", s->version);
  123. printf("%-25s%d\n", "Exit code:", s->ac_exitcode);
  124. printf("%-25s0x%x\n", "Flags:", s->ac_flag);
  125. printf("%-25s%d\n", "Nice value:", s->ac_nice);
  126. printf("%-25s%s\n", "Command name:", s->ac_comm);
  127. printf("%-25s%d\n", "Scheduling discipline:", s->ac_sched);
  128. printf("%-25s%d\n", "UID:", s->ac_uid);
  129. printf("%-25s%d\n", "GID:", s->ac_gid);
  130. printf("%-25s%d\n", "PID:", s->ac_pid);
  131. printf("%-25s%d\n", "PPID:", s->ac_ppid);
  132. if (human_readable) {
  133. time_t begin_time = s->ac_btime;
  134. printf("%-25s%s", "Begin time:", ctime(&begin_time));
  135. } else {
  136. printf("%-25s%d sec\n", "Begin time:", s->ac_btime);
  137. }
  138. printf("%-25s%llu usec\n", "Elapsed time:", s->ac_etime);
  139. printf("%-25s%llu usec\n", "User CPU time:", s->ac_utime);
  140. printf("%-25s%llu\n", "Minor page faults:", s->ac_minflt);
  141. printf("%-25s%llu\n", "Major page faults:", s->ac_majflt);
  142. printf("%-25s%llu usec\n", "Scaled user time:", s->ac_utimescaled);
  143. printf("%-25s%llu usec\n", "Scaled system time:", s->ac_stimescaled);
  144. printf("\nDelay accounting\n");
  145. printf("----------------\n");
  146. printf(" %15s%15s%15s%15s%15s%15s\n",
  147. "Count",
  148. human_readable ? "Delay (ms)" : "Delay (ns)",
  149. "Average delay",
  150. "Real delay",
  151. "Scaled real",
  152. "Virtual delay");
  153. if (!human_readable) {
  154. printf("CPU %15llu%15llu%15llu%15llu%15llu%15llu\n",
  155. s->cpu_count,
  156. s->cpu_delay_total,
  157. average_ns(s->cpu_delay_total, s->cpu_count),
  158. s->cpu_run_real_total,
  159. s->cpu_scaled_run_real_total,
  160. s->cpu_run_virtual_total);
  161. printf("IO %15llu%15llu%15llu\n",
  162. s->blkio_count,
  163. s->blkio_delay_total,
  164. average_ns(s->blkio_delay_total, s->blkio_count));
  165. printf("Swap %15llu%15llu%15llu\n",
  166. s->swapin_count,
  167. s->swapin_delay_total,
  168. average_ns(s->swapin_delay_total, s->swapin_count));
  169. printf("Reclaim%15llu%15llu%15llu\n",
  170. s->freepages_count,
  171. s->freepages_delay_total,
  172. average_ns(s->freepages_delay_total, s->freepages_count));
  173. } else {
  174. const double ms_per_ns = 1e6;
  175. printf("CPU %15llu%15.3f%15.3f%15.3f%15.3f%15.3f\n",
  176. s->cpu_count,
  177. s->cpu_delay_total / ms_per_ns,
  178. average_ms(s->cpu_delay_total, s->cpu_count),
  179. s->cpu_run_real_total / ms_per_ns,
  180. s->cpu_scaled_run_real_total / ms_per_ns,
  181. s->cpu_run_virtual_total / ms_per_ns);
  182. printf("IO %15llu%15.3f%15.3f\n",
  183. s->blkio_count,
  184. s->blkio_delay_total / ms_per_ns,
  185. average_ms(s->blkio_delay_total, s->blkio_count));
  186. printf("Swap %15llu%15.3f%15.3f\n",
  187. s->swapin_count,
  188. s->swapin_delay_total / ms_per_ns,
  189. average_ms(s->swapin_delay_total, s->swapin_count));
  190. printf("Reclaim%15llu%15.3f%15.3f\n",
  191. s->freepages_count,
  192. s->freepages_delay_total / ms_per_ns,
  193. average_ms(s->freepages_delay_total, s->freepages_count));
  194. }
  195. printf("\nExtended accounting fields\n");
  196. printf("--------------------------\n");
  197. if (human_readable && s->ac_stime) {
  198. printf("%-25s%.3f MB\n", "Average RSS usage:",
  199. (double)s->coremem / s->ac_stime);
  200. printf("%-25s%.3f MB\n", "Average VM usage:",
  201. (double)s->virtmem / s->ac_stime);
  202. } else {
  203. printf("%-25s%llu MB\n", "Accumulated RSS usage:", s->coremem);
  204. printf("%-25s%llu MB\n", "Accumulated VM usage:", s->virtmem);
  205. }
  206. printf("%-25s%llu KB\n", "RSS high water mark:", s->hiwater_rss);
  207. printf("%-25s%llu KB\n", "VM high water mark:", s->hiwater_vm);
  208. printf("%-25s%llu\n", "IO bytes read:", s->read_char);
  209. printf("%-25s%llu\n", "IO bytes written:", s->write_char);
  210. printf("%-25s%llu\n", "IO read syscalls:", s->read_syscalls);
  211. printf("%-25s%llu\n", "IO write syscalls:", s->write_syscalls);
  212. printf("\nPer-task/thread statistics\n");
  213. printf("--------------------------\n");
  214. printf("%-25s%llu\n", "Voluntary switches:", s->nvcsw);
  215. printf("%-25s%llu\n", "Involuntary switches:", s->nivcsw);
  216. }
  217. void print_usage() {
  218. printf("Linux task stats reporting tool\n"
  219. "\n"
  220. "Usage: taskstats [options]\n"
  221. "\n"
  222. "Options:\n"
  223. " --help This text\n"
  224. " --pid PID Print stats for the process id PID\n"
  225. " --tgid TGID Print stats for the thread group id TGID\n"
  226. " --raw Print raw numbers instead of human readable units\n"
  227. "\n"
  228. "Either PID or TGID must be specified. For more documentation about "
  229. "the reported fields, see\n"
  230. "https://www.kernel.org/doc/Documentation/accounting/"
  231. "taskstats-struct.txt\n");
  232. }
  233. int main(int argc, char** argv) {
  234. int command_type = 0;
  235. int pid = 0;
  236. int human_readable = 1;
  237. const struct option long_options[] = {
  238. {"help", no_argument, 0, 0},
  239. {"pid", required_argument, 0, 0},
  240. {"tgid", required_argument, 0, 0},
  241. {"raw", no_argument, 0, 0},
  242. {0, 0, 0, 0}
  243. };
  244. while (1) {
  245. int option_index;
  246. int option_char = getopt_long_only(argc, argv, "", long_options,
  247. &option_index);
  248. if (option_char == -1) {
  249. break;
  250. }
  251. switch (option_index) {
  252. case 0:
  253. print_usage();
  254. return EXIT_SUCCESS;
  255. case 1:
  256. command_type = TASKSTATS_CMD_ATTR_PID;
  257. pid = atoi(optarg);
  258. break;
  259. case 2:
  260. command_type = TASKSTATS_CMD_ATTR_TGID;
  261. pid = atoi(optarg);
  262. break;
  263. case 3:
  264. human_readable = 0;
  265. break;
  266. default:
  267. break;
  268. };
  269. }
  270. if (!pid) {
  271. printf("Either PID or TGID must be specified\n");
  272. return EXIT_FAILURE;
  273. }
  274. struct nl_sock* netlink_socket = nl_socket_alloc();
  275. if (!netlink_socket) {
  276. fprintf(stderr, "Unable to allocate netlink socket\n");
  277. goto error;
  278. }
  279. int ret = genl_connect(netlink_socket);
  280. if (ret < 0) {
  281. nl_perror(ret, "Unable to open netlink socket (are you root?)");
  282. goto error;
  283. }
  284. int family_id = genl_ctrl_resolve(netlink_socket, TASKSTATS_GENL_NAME);
  285. if (family_id < 0) {
  286. nl_perror(family_id, "Unable to determine taskstats family id "
  287. "(does your kernel support taskstats?)");
  288. goto error;
  289. }
  290. struct TaskStatistics stats;
  291. ret = query_task_stats(netlink_socket, family_id, command_type, pid, &stats);
  292. if (ret < 0) {
  293. nl_perror(ret, "Failed to query taskstats");
  294. goto error;
  295. }
  296. print_task_stats(&stats, human_readable);
  297. nl_socket_free(netlink_socket);
  298. return EXIT_SUCCESS;
  299. error:
  300. if (netlink_socket) {
  301. nl_socket_free(netlink_socket);
  302. }
  303. return EXIT_FAILURE;
  304. }