getdelays.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* getdelays.c
  3. *
  4. * Utility to get per-pid and per-tgid delay accounting statistics
  5. * Also illustrates usage of the taskstats interface
  6. *
  7. * Copyright (C) Shailabh Nagar, IBM Corp. 2005
  8. * Copyright (C) Balbir Singh, IBM Corp. 2006
  9. * Copyright (c) Jay Lan, SGI. 2006
  10. *
  11. * Compile with
  12. * gcc -I/usr/src/linux/include getdelays.c -o getdelays
  13. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <errno.h>
  17. #include <unistd.h>
  18. #include <poll.h>
  19. #include <string.h>
  20. #include <fcntl.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <sys/socket.h>
  24. #include <sys/wait.h>
  25. #include <signal.h>
  26. #include <linux/genetlink.h>
  27. #include <linux/taskstats.h>
  28. #include <linux/cgroupstats.h>
  29. /*
  30. * Generic macros for dealing with netlink sockets. Might be duplicated
  31. * elsewhere. It is recommended that commercial grade applications use
  32. * libnl or libnetlink and use the interfaces provided by the library
  33. */
  34. #define GENLMSG_DATA(glh) ((void *)(NLMSG_DATA(glh) + GENL_HDRLEN))
  35. #define GENLMSG_PAYLOAD(glh) (NLMSG_PAYLOAD(glh, 0) - GENL_HDRLEN)
  36. #define NLA_DATA(na) ((void *)((char*)(na) + NLA_HDRLEN))
  37. #define NLA_PAYLOAD(len) (len - NLA_HDRLEN)
  38. #define err(code, fmt, arg...) \
  39. do { \
  40. fprintf(stderr, fmt, ##arg); \
  41. exit(code); \
  42. } while (0)
  43. int done;
  44. int rcvbufsz;
  45. char name[100];
  46. int dbg;
  47. int print_delays;
  48. int print_io_accounting;
  49. int print_task_context_switch_counts;
  50. #define PRINTF(fmt, arg...) { \
  51. if (dbg) { \
  52. printf(fmt, ##arg); \
  53. } \
  54. }
  55. /* Maximum size of response requested or message sent */
  56. #define MAX_MSG_SIZE 1024
  57. /* Maximum number of cpus expected to be specified in a cpumask */
  58. #define MAX_CPUS 32
  59. struct msgtemplate {
  60. struct nlmsghdr n;
  61. struct genlmsghdr g;
  62. char buf[MAX_MSG_SIZE];
  63. };
  64. char cpumask[100+6*MAX_CPUS];
  65. static void usage(void)
  66. {
  67. fprintf(stderr, "getdelays [-dilv] [-w logfile] [-r bufsize] "
  68. "[-m cpumask] [-t tgid] [-p pid]\n");
  69. fprintf(stderr, " -d: print delayacct stats\n");
  70. fprintf(stderr, " -i: print IO accounting (works only with -p)\n");
  71. fprintf(stderr, " -l: listen forever\n");
  72. fprintf(stderr, " -v: debug on\n");
  73. fprintf(stderr, " -C: container path\n");
  74. }
  75. /*
  76. * Create a raw netlink socket and bind
  77. */
  78. static int create_nl_socket(int protocol)
  79. {
  80. int fd;
  81. struct sockaddr_nl local;
  82. fd = socket(AF_NETLINK, SOCK_RAW, protocol);
  83. if (fd < 0)
  84. return -1;
  85. if (rcvbufsz)
  86. if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
  87. &rcvbufsz, sizeof(rcvbufsz)) < 0) {
  88. fprintf(stderr, "Unable to set socket rcv buf size to %d\n",
  89. rcvbufsz);
  90. goto error;
  91. }
  92. memset(&local, 0, sizeof(local));
  93. local.nl_family = AF_NETLINK;
  94. if (bind(fd, (struct sockaddr *) &local, sizeof(local)) < 0)
  95. goto error;
  96. return fd;
  97. error:
  98. close(fd);
  99. return -1;
  100. }
  101. static int send_cmd(int sd, __u16 nlmsg_type, __u32 nlmsg_pid,
  102. __u8 genl_cmd, __u16 nla_type,
  103. void *nla_data, int nla_len)
  104. {
  105. struct nlattr *na;
  106. struct sockaddr_nl nladdr;
  107. int r, buflen;
  108. char *buf;
  109. struct msgtemplate msg;
  110. msg.n.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN);
  111. msg.n.nlmsg_type = nlmsg_type;
  112. msg.n.nlmsg_flags = NLM_F_REQUEST;
  113. msg.n.nlmsg_seq = 0;
  114. msg.n.nlmsg_pid = nlmsg_pid;
  115. msg.g.cmd = genl_cmd;
  116. msg.g.version = 0x1;
  117. na = (struct nlattr *) GENLMSG_DATA(&msg);
  118. na->nla_type = nla_type;
  119. na->nla_len = nla_len + NLA_HDRLEN;
  120. memcpy(NLA_DATA(na), nla_data, nla_len);
  121. msg.n.nlmsg_len += NLMSG_ALIGN(na->nla_len);
  122. buf = (char *) &msg;
  123. buflen = msg.n.nlmsg_len ;
  124. memset(&nladdr, 0, sizeof(nladdr));
  125. nladdr.nl_family = AF_NETLINK;
  126. while ((r = sendto(sd, buf, buflen, 0, (struct sockaddr *) &nladdr,
  127. sizeof(nladdr))) < buflen) {
  128. if (r > 0) {
  129. buf += r;
  130. buflen -= r;
  131. } else if (errno != EAGAIN)
  132. return -1;
  133. }
  134. return 0;
  135. }
  136. /*
  137. * Probe the controller in genetlink to find the family id
  138. * for the TASKSTATS family
  139. */
  140. static int get_family_id(int sd)
  141. {
  142. struct {
  143. struct nlmsghdr n;
  144. struct genlmsghdr g;
  145. char buf[256];
  146. } ans;
  147. int id = 0, rc;
  148. struct nlattr *na;
  149. int rep_len;
  150. strcpy(name, TASKSTATS_GENL_NAME);
  151. rc = send_cmd(sd, GENL_ID_CTRL, getpid(), CTRL_CMD_GETFAMILY,
  152. CTRL_ATTR_FAMILY_NAME, (void *)name,
  153. strlen(TASKSTATS_GENL_NAME)+1);
  154. if (rc < 0)
  155. return 0; /* sendto() failure? */
  156. rep_len = recv(sd, &ans, sizeof(ans), 0);
  157. if (ans.n.nlmsg_type == NLMSG_ERROR ||
  158. (rep_len < 0) || !NLMSG_OK((&ans.n), rep_len))
  159. return 0;
  160. na = (struct nlattr *) GENLMSG_DATA(&ans);
  161. na = (struct nlattr *) ((char *) na + NLA_ALIGN(na->nla_len));
  162. if (na->nla_type == CTRL_ATTR_FAMILY_ID) {
  163. id = *(__u16 *) NLA_DATA(na);
  164. }
  165. return id;
  166. }
  167. #define average_ms(t, c) (t / 1000000ULL / (c ? c : 1))
  168. static void print_delayacct(struct taskstats *t)
  169. {
  170. printf("\n\nCPU %15s%15s%15s%15s%15s\n"
  171. " %15llu%15llu%15llu%15llu%15.3fms\n"
  172. "IO %15s%15s%15s\n"
  173. " %15llu%15llu%15llums\n"
  174. "SWAP %15s%15s%15s\n"
  175. " %15llu%15llu%15llums\n"
  176. "RECLAIM %12s%15s%15s\n"
  177. " %15llu%15llu%15llums\n",
  178. "count", "real total", "virtual total",
  179. "delay total", "delay average",
  180. (unsigned long long)t->cpu_count,
  181. (unsigned long long)t->cpu_run_real_total,
  182. (unsigned long long)t->cpu_run_virtual_total,
  183. (unsigned long long)t->cpu_delay_total,
  184. average_ms((double)t->cpu_delay_total, t->cpu_count),
  185. "count", "delay total", "delay average",
  186. (unsigned long long)t->blkio_count,
  187. (unsigned long long)t->blkio_delay_total,
  188. average_ms(t->blkio_delay_total, t->blkio_count),
  189. "count", "delay total", "delay average",
  190. (unsigned long long)t->swapin_count,
  191. (unsigned long long)t->swapin_delay_total,
  192. average_ms(t->swapin_delay_total, t->swapin_count),
  193. "count", "delay total", "delay average",
  194. (unsigned long long)t->freepages_count,
  195. (unsigned long long)t->freepages_delay_total,
  196. average_ms(t->freepages_delay_total, t->freepages_count));
  197. }
  198. static void task_context_switch_counts(struct taskstats *t)
  199. {
  200. printf("\n\nTask %15s%15s\n"
  201. " %15llu%15llu\n",
  202. "voluntary", "nonvoluntary",
  203. (unsigned long long)t->nvcsw, (unsigned long long)t->nivcsw);
  204. }
  205. static void print_cgroupstats(struct cgroupstats *c)
  206. {
  207. printf("sleeping %llu, blocked %llu, running %llu, stopped %llu, "
  208. "uninterruptible %llu\n", (unsigned long long)c->nr_sleeping,
  209. (unsigned long long)c->nr_io_wait,
  210. (unsigned long long)c->nr_running,
  211. (unsigned long long)c->nr_stopped,
  212. (unsigned long long)c->nr_uninterruptible);
  213. }
  214. static void print_ioacct(struct taskstats *t)
  215. {
  216. printf("%s: read=%llu, write=%llu, cancelled_write=%llu\n",
  217. t->ac_comm,
  218. (unsigned long long)t->read_bytes,
  219. (unsigned long long)t->write_bytes,
  220. (unsigned long long)t->cancelled_write_bytes);
  221. }
  222. int main(int argc, char *argv[])
  223. {
  224. int c, rc, rep_len, aggr_len, len2;
  225. int cmd_type = TASKSTATS_CMD_ATTR_UNSPEC;
  226. __u16 id;
  227. __u32 mypid;
  228. struct nlattr *na;
  229. int nl_sd = -1;
  230. int len = 0;
  231. pid_t tid = 0;
  232. pid_t rtid = 0;
  233. int fd = 0;
  234. int count = 0;
  235. int write_file = 0;
  236. int maskset = 0;
  237. char *logfile = NULL;
  238. int loop = 0;
  239. int containerset = 0;
  240. char *containerpath = NULL;
  241. int cfd = 0;
  242. int forking = 0;
  243. sigset_t sigset;
  244. struct msgtemplate msg;
  245. while (!forking) {
  246. c = getopt(argc, argv, "qdiw:r:m:t:p:vlC:c:");
  247. if (c < 0)
  248. break;
  249. switch (c) {
  250. case 'd':
  251. printf("print delayacct stats ON\n");
  252. print_delays = 1;
  253. break;
  254. case 'i':
  255. printf("printing IO accounting\n");
  256. print_io_accounting = 1;
  257. break;
  258. case 'q':
  259. printf("printing task/process context switch rates\n");
  260. print_task_context_switch_counts = 1;
  261. break;
  262. case 'C':
  263. containerset = 1;
  264. containerpath = optarg;
  265. break;
  266. case 'w':
  267. logfile = strdup(optarg);
  268. printf("write to file %s\n", logfile);
  269. write_file = 1;
  270. break;
  271. case 'r':
  272. rcvbufsz = atoi(optarg);
  273. printf("receive buf size %d\n", rcvbufsz);
  274. if (rcvbufsz < 0)
  275. err(1, "Invalid rcv buf size\n");
  276. break;
  277. case 'm':
  278. strncpy(cpumask, optarg, sizeof(cpumask));
  279. cpumask[sizeof(cpumask) - 1] = '\0';
  280. maskset = 1;
  281. printf("cpumask %s maskset %d\n", cpumask, maskset);
  282. break;
  283. case 't':
  284. tid = atoi(optarg);
  285. if (!tid)
  286. err(1, "Invalid tgid\n");
  287. cmd_type = TASKSTATS_CMD_ATTR_TGID;
  288. break;
  289. case 'p':
  290. tid = atoi(optarg);
  291. if (!tid)
  292. err(1, "Invalid pid\n");
  293. cmd_type = TASKSTATS_CMD_ATTR_PID;
  294. break;
  295. case 'c':
  296. /* Block SIGCHLD for sigwait() later */
  297. if (sigemptyset(&sigset) == -1)
  298. err(1, "Failed to empty sigset");
  299. if (sigaddset(&sigset, SIGCHLD))
  300. err(1, "Failed to set sigchld in sigset");
  301. sigprocmask(SIG_BLOCK, &sigset, NULL);
  302. /* fork/exec a child */
  303. tid = fork();
  304. if (tid < 0)
  305. err(1, "Fork failed\n");
  306. if (tid == 0)
  307. if (execvp(argv[optind - 1],
  308. &argv[optind - 1]) < 0)
  309. exit(-1);
  310. /* Set the command type and avoid further processing */
  311. cmd_type = TASKSTATS_CMD_ATTR_PID;
  312. forking = 1;
  313. break;
  314. case 'v':
  315. printf("debug on\n");
  316. dbg = 1;
  317. break;
  318. case 'l':
  319. printf("listen forever\n");
  320. loop = 1;
  321. break;
  322. default:
  323. usage();
  324. exit(-1);
  325. }
  326. }
  327. if (write_file) {
  328. fd = open(logfile, O_WRONLY | O_CREAT | O_TRUNC,
  329. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  330. if (fd == -1) {
  331. perror("Cannot open output file\n");
  332. exit(1);
  333. }
  334. }
  335. nl_sd = create_nl_socket(NETLINK_GENERIC);
  336. if (nl_sd < 0)
  337. err(1, "error creating Netlink socket\n");
  338. mypid = getpid();
  339. id = get_family_id(nl_sd);
  340. if (!id) {
  341. fprintf(stderr, "Error getting family id, errno %d\n", errno);
  342. goto err;
  343. }
  344. PRINTF("family id %d\n", id);
  345. if (maskset) {
  346. rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
  347. TASKSTATS_CMD_ATTR_REGISTER_CPUMASK,
  348. &cpumask, strlen(cpumask) + 1);
  349. PRINTF("Sent register cpumask, retval %d\n", rc);
  350. if (rc < 0) {
  351. fprintf(stderr, "error sending register cpumask\n");
  352. goto err;
  353. }
  354. }
  355. if (tid && containerset) {
  356. fprintf(stderr, "Select either -t or -C, not both\n");
  357. goto err;
  358. }
  359. /*
  360. * If we forked a child, wait for it to exit. Cannot use waitpid()
  361. * as all the delicious data would be reaped as part of the wait
  362. */
  363. if (tid && forking) {
  364. int sig_received;
  365. sigwait(&sigset, &sig_received);
  366. }
  367. if (tid) {
  368. rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
  369. cmd_type, &tid, sizeof(__u32));
  370. PRINTF("Sent pid/tgid, retval %d\n", rc);
  371. if (rc < 0) {
  372. fprintf(stderr, "error sending tid/tgid cmd\n");
  373. goto done;
  374. }
  375. }
  376. if (containerset) {
  377. cfd = open(containerpath, O_RDONLY);
  378. if (cfd < 0) {
  379. perror("error opening container file");
  380. goto err;
  381. }
  382. rc = send_cmd(nl_sd, id, mypid, CGROUPSTATS_CMD_GET,
  383. CGROUPSTATS_CMD_ATTR_FD, &cfd, sizeof(__u32));
  384. if (rc < 0) {
  385. perror("error sending cgroupstats command");
  386. goto err;
  387. }
  388. }
  389. if (!maskset && !tid && !containerset) {
  390. usage();
  391. goto err;
  392. }
  393. do {
  394. rep_len = recv(nl_sd, &msg, sizeof(msg), 0);
  395. PRINTF("received %d bytes\n", rep_len);
  396. if (rep_len < 0) {
  397. fprintf(stderr, "nonfatal reply error: errno %d\n",
  398. errno);
  399. continue;
  400. }
  401. if (msg.n.nlmsg_type == NLMSG_ERROR ||
  402. !NLMSG_OK((&msg.n), rep_len)) {
  403. struct nlmsgerr *err = NLMSG_DATA(&msg);
  404. fprintf(stderr, "fatal reply error, errno %d\n",
  405. err->error);
  406. goto done;
  407. }
  408. PRINTF("nlmsghdr size=%zu, nlmsg_len=%d, rep_len=%d\n",
  409. sizeof(struct nlmsghdr), msg.n.nlmsg_len, rep_len);
  410. rep_len = GENLMSG_PAYLOAD(&msg.n);
  411. na = (struct nlattr *) GENLMSG_DATA(&msg);
  412. len = 0;
  413. while (len < rep_len) {
  414. len += NLA_ALIGN(na->nla_len);
  415. switch (na->nla_type) {
  416. case TASKSTATS_TYPE_AGGR_TGID:
  417. /* Fall through */
  418. case TASKSTATS_TYPE_AGGR_PID:
  419. aggr_len = NLA_PAYLOAD(na->nla_len);
  420. len2 = 0;
  421. /* For nested attributes, na follows */
  422. na = (struct nlattr *) NLA_DATA(na);
  423. done = 0;
  424. while (len2 < aggr_len) {
  425. switch (na->nla_type) {
  426. case TASKSTATS_TYPE_PID:
  427. rtid = *(int *) NLA_DATA(na);
  428. if (print_delays)
  429. printf("PID\t%d\n", rtid);
  430. break;
  431. case TASKSTATS_TYPE_TGID:
  432. rtid = *(int *) NLA_DATA(na);
  433. if (print_delays)
  434. printf("TGID\t%d\n", rtid);
  435. break;
  436. case TASKSTATS_TYPE_STATS:
  437. count++;
  438. if (print_delays)
  439. print_delayacct((struct taskstats *) NLA_DATA(na));
  440. if (print_io_accounting)
  441. print_ioacct((struct taskstats *) NLA_DATA(na));
  442. if (print_task_context_switch_counts)
  443. task_context_switch_counts((struct taskstats *) NLA_DATA(na));
  444. if (fd) {
  445. if (write(fd, NLA_DATA(na), na->nla_len) < 0) {
  446. err(1,"write error\n");
  447. }
  448. }
  449. if (!loop)
  450. goto done;
  451. break;
  452. case TASKSTATS_TYPE_NULL:
  453. break;
  454. default:
  455. fprintf(stderr, "Unknown nested"
  456. " nla_type %d\n",
  457. na->nla_type);
  458. break;
  459. }
  460. len2 += NLA_ALIGN(na->nla_len);
  461. na = (struct nlattr *)((char *)na +
  462. NLA_ALIGN(na->nla_len));
  463. }
  464. break;
  465. case CGROUPSTATS_TYPE_CGROUP_STATS:
  466. print_cgroupstats(NLA_DATA(na));
  467. break;
  468. default:
  469. fprintf(stderr, "Unknown nla_type %d\n",
  470. na->nla_type);
  471. case TASKSTATS_TYPE_NULL:
  472. break;
  473. }
  474. na = (struct nlattr *) (GENLMSG_DATA(&msg) + len);
  475. }
  476. } while (loop);
  477. done:
  478. if (maskset) {
  479. rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
  480. TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK,
  481. &cpumask, strlen(cpumask) + 1);
  482. printf("Sent deregister mask, retval %d\n", rc);
  483. if (rc < 0)
  484. err(rc, "error sending deregister cpumask\n");
  485. }
  486. err:
  487. close(nl_sd);
  488. if (fd)
  489. close(fd);
  490. if (cfd)
  491. close(cfd);
  492. return 0;
  493. }