getdelays.c 14 KB

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