getdelays.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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%15llums\n"
  173. "SWAP %15s%15s%15s\n"
  174. " %15llu%15llu%15llums\n"
  175. "RECLAIM %12s%15s%15s\n"
  176. " %15llu%15llu%15llums\n"
  177. "THRASHING%12s%15s%15s\n"
  178. " %15llu%15llu%15llums\n"
  179. "COMPACT %12s%15s%15s\n"
  180. " %15llu%15llu%15llums\n"
  181. "WPCOPY %12s%15s%15s\n"
  182. " %15llu%15llu%15llums\n",
  183. "count", "real total", "virtual total",
  184. "delay total", "delay average",
  185. (unsigned long long)t->cpu_count,
  186. (unsigned long long)t->cpu_run_real_total,
  187. (unsigned long long)t->cpu_run_virtual_total,
  188. (unsigned long long)t->cpu_delay_total,
  189. average_ms((double)t->cpu_delay_total, t->cpu_count),
  190. "count", "delay total", "delay average",
  191. (unsigned long long)t->blkio_count,
  192. (unsigned long long)t->blkio_delay_total,
  193. average_ms(t->blkio_delay_total, t->blkio_count),
  194. "count", "delay total", "delay average",
  195. (unsigned long long)t->swapin_count,
  196. (unsigned long long)t->swapin_delay_total,
  197. average_ms(t->swapin_delay_total, t->swapin_count),
  198. "count", "delay total", "delay average",
  199. (unsigned long long)t->freepages_count,
  200. (unsigned long long)t->freepages_delay_total,
  201. average_ms(t->freepages_delay_total, t->freepages_count),
  202. "count", "delay total", "delay average",
  203. (unsigned long long)t->thrashing_count,
  204. (unsigned long long)t->thrashing_delay_total,
  205. average_ms(t->thrashing_delay_total, t->thrashing_count),
  206. "count", "delay total", "delay average",
  207. (unsigned long long)t->compact_count,
  208. (unsigned long long)t->compact_delay_total,
  209. average_ms(t->compact_delay_total, t->compact_count),
  210. "count", "delay total", "delay average",
  211. (unsigned long long)t->wpcopy_count,
  212. (unsigned long long)t->wpcopy_delay_total,
  213. average_ms(t->wpcopy_delay_total, t->wpcopy_count));
  214. }
  215. static void task_context_switch_counts(struct taskstats *t)
  216. {
  217. printf("\n\nTask %15s%15s\n"
  218. " %15llu%15llu\n",
  219. "voluntary", "nonvoluntary",
  220. (unsigned long long)t->nvcsw, (unsigned long long)t->nivcsw);
  221. }
  222. static void print_cgroupstats(struct cgroupstats *c)
  223. {
  224. printf("sleeping %llu, blocked %llu, running %llu, stopped %llu, "
  225. "uninterruptible %llu\n", (unsigned long long)c->nr_sleeping,
  226. (unsigned long long)c->nr_io_wait,
  227. (unsigned long long)c->nr_running,
  228. (unsigned long long)c->nr_stopped,
  229. (unsigned long long)c->nr_uninterruptible);
  230. }
  231. static void print_ioacct(struct taskstats *t)
  232. {
  233. printf("%s: read=%llu, write=%llu, cancelled_write=%llu\n",
  234. t->ac_comm,
  235. (unsigned long long)t->read_bytes,
  236. (unsigned long long)t->write_bytes,
  237. (unsigned long long)t->cancelled_write_bytes);
  238. }
  239. int main(int argc, char *argv[])
  240. {
  241. int c, rc, rep_len, aggr_len, len2;
  242. int cmd_type = TASKSTATS_CMD_ATTR_UNSPEC;
  243. __u16 id;
  244. __u32 mypid;
  245. struct nlattr *na;
  246. int nl_sd = -1;
  247. int len = 0;
  248. pid_t tid = 0;
  249. pid_t rtid = 0;
  250. int fd = 0;
  251. int write_file = 0;
  252. int maskset = 0;
  253. char *logfile = NULL;
  254. int loop = 0;
  255. int containerset = 0;
  256. char *containerpath = NULL;
  257. int cfd = 0;
  258. int forking = 0;
  259. sigset_t sigset;
  260. struct msgtemplate msg;
  261. while (!forking) {
  262. c = getopt(argc, argv, "qdiw:r:m:t:p:vlC:c:");
  263. if (c < 0)
  264. break;
  265. switch (c) {
  266. case 'd':
  267. printf("print delayacct stats ON\n");
  268. print_delays = 1;
  269. break;
  270. case 'i':
  271. printf("printing IO accounting\n");
  272. print_io_accounting = 1;
  273. break;
  274. case 'q':
  275. printf("printing task/process context switch rates\n");
  276. print_task_context_switch_counts = 1;
  277. break;
  278. case 'C':
  279. containerset = 1;
  280. containerpath = optarg;
  281. break;
  282. case 'w':
  283. logfile = strdup(optarg);
  284. printf("write to file %s\n", logfile);
  285. write_file = 1;
  286. break;
  287. case 'r':
  288. rcvbufsz = atoi(optarg);
  289. printf("receive buf size %d\n", rcvbufsz);
  290. if (rcvbufsz < 0)
  291. err(1, "Invalid rcv buf size\n");
  292. break;
  293. case 'm':
  294. strncpy(cpumask, optarg, sizeof(cpumask));
  295. cpumask[sizeof(cpumask) - 1] = '\0';
  296. maskset = 1;
  297. printf("cpumask %s maskset %d\n", cpumask, maskset);
  298. break;
  299. case 't':
  300. tid = atoi(optarg);
  301. if (!tid)
  302. err(1, "Invalid tgid\n");
  303. cmd_type = TASKSTATS_CMD_ATTR_TGID;
  304. break;
  305. case 'p':
  306. tid = atoi(optarg);
  307. if (!tid)
  308. err(1, "Invalid pid\n");
  309. cmd_type = TASKSTATS_CMD_ATTR_PID;
  310. break;
  311. case 'c':
  312. /* Block SIGCHLD for sigwait() later */
  313. if (sigemptyset(&sigset) == -1)
  314. err(1, "Failed to empty sigset");
  315. if (sigaddset(&sigset, SIGCHLD))
  316. err(1, "Failed to set sigchld in sigset");
  317. sigprocmask(SIG_BLOCK, &sigset, NULL);
  318. /* fork/exec a child */
  319. tid = fork();
  320. if (tid < 0)
  321. err(1, "Fork failed\n");
  322. if (tid == 0)
  323. if (execvp(argv[optind - 1],
  324. &argv[optind - 1]) < 0)
  325. exit(-1);
  326. /* Set the command type and avoid further processing */
  327. cmd_type = TASKSTATS_CMD_ATTR_PID;
  328. forking = 1;
  329. break;
  330. case 'v':
  331. printf("debug on\n");
  332. dbg = 1;
  333. break;
  334. case 'l':
  335. printf("listen forever\n");
  336. loop = 1;
  337. break;
  338. default:
  339. usage();
  340. exit(-1);
  341. }
  342. }
  343. if (write_file) {
  344. fd = open(logfile, O_WRONLY | O_CREAT | O_TRUNC,
  345. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  346. if (fd == -1) {
  347. perror("Cannot open output file\n");
  348. exit(1);
  349. }
  350. }
  351. nl_sd = create_nl_socket(NETLINK_GENERIC);
  352. if (nl_sd < 0)
  353. err(1, "error creating Netlink socket\n");
  354. mypid = getpid();
  355. id = get_family_id(nl_sd);
  356. if (!id) {
  357. fprintf(stderr, "Error getting family id, errno %d\n", errno);
  358. goto err;
  359. }
  360. PRINTF("family id %d\n", id);
  361. if (maskset) {
  362. rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
  363. TASKSTATS_CMD_ATTR_REGISTER_CPUMASK,
  364. &cpumask, strlen(cpumask) + 1);
  365. PRINTF("Sent register cpumask, retval %d\n", rc);
  366. if (rc < 0) {
  367. fprintf(stderr, "error sending register cpumask\n");
  368. goto err;
  369. }
  370. }
  371. if (tid && containerset) {
  372. fprintf(stderr, "Select either -t or -C, not both\n");
  373. goto err;
  374. }
  375. /*
  376. * If we forked a child, wait for it to exit. Cannot use waitpid()
  377. * as all the delicious data would be reaped as part of the wait
  378. */
  379. if (tid && forking) {
  380. int sig_received;
  381. sigwait(&sigset, &sig_received);
  382. }
  383. if (tid) {
  384. rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
  385. cmd_type, &tid, sizeof(__u32));
  386. PRINTF("Sent pid/tgid, retval %d\n", rc);
  387. if (rc < 0) {
  388. fprintf(stderr, "error sending tid/tgid cmd\n");
  389. goto done;
  390. }
  391. }
  392. if (containerset) {
  393. cfd = open(containerpath, O_RDONLY);
  394. if (cfd < 0) {
  395. perror("error opening container file");
  396. goto err;
  397. }
  398. rc = send_cmd(nl_sd, id, mypid, CGROUPSTATS_CMD_GET,
  399. CGROUPSTATS_CMD_ATTR_FD, &cfd, sizeof(__u32));
  400. if (rc < 0) {
  401. perror("error sending cgroupstats command");
  402. goto err;
  403. }
  404. }
  405. if (!maskset && !tid && !containerset) {
  406. usage();
  407. goto err;
  408. }
  409. do {
  410. rep_len = recv(nl_sd, &msg, sizeof(msg), 0);
  411. PRINTF("received %d bytes\n", rep_len);
  412. if (rep_len < 0) {
  413. fprintf(stderr, "nonfatal reply error: errno %d\n",
  414. errno);
  415. continue;
  416. }
  417. if (msg.n.nlmsg_type == NLMSG_ERROR ||
  418. !NLMSG_OK((&msg.n), rep_len)) {
  419. struct nlmsgerr *err = NLMSG_DATA(&msg);
  420. fprintf(stderr, "fatal reply error, errno %d\n",
  421. err->error);
  422. goto done;
  423. }
  424. PRINTF("nlmsghdr size=%zu, nlmsg_len=%d, rep_len=%d\n",
  425. sizeof(struct nlmsghdr), msg.n.nlmsg_len, rep_len);
  426. rep_len = GENLMSG_PAYLOAD(&msg.n);
  427. na = (struct nlattr *) GENLMSG_DATA(&msg);
  428. len = 0;
  429. while (len < rep_len) {
  430. len += NLA_ALIGN(na->nla_len);
  431. switch (na->nla_type) {
  432. case TASKSTATS_TYPE_AGGR_TGID:
  433. /* Fall through */
  434. case TASKSTATS_TYPE_AGGR_PID:
  435. aggr_len = NLA_PAYLOAD(na->nla_len);
  436. len2 = 0;
  437. /* For nested attributes, na follows */
  438. na = (struct nlattr *) NLA_DATA(na);
  439. while (len2 < aggr_len) {
  440. switch (na->nla_type) {
  441. case TASKSTATS_TYPE_PID:
  442. rtid = *(int *) NLA_DATA(na);
  443. if (print_delays)
  444. printf("PID\t%d\n", rtid);
  445. break;
  446. case TASKSTATS_TYPE_TGID:
  447. rtid = *(int *) NLA_DATA(na);
  448. if (print_delays)
  449. printf("TGID\t%d\n", rtid);
  450. break;
  451. case TASKSTATS_TYPE_STATS:
  452. if (print_delays)
  453. print_delayacct((struct taskstats *) NLA_DATA(na));
  454. if (print_io_accounting)
  455. print_ioacct((struct taskstats *) NLA_DATA(na));
  456. if (print_task_context_switch_counts)
  457. task_context_switch_counts((struct taskstats *) NLA_DATA(na));
  458. if (fd) {
  459. if (write(fd, NLA_DATA(na), na->nla_len) < 0) {
  460. err(1,"write error\n");
  461. }
  462. }
  463. if (!loop)
  464. goto done;
  465. break;
  466. case TASKSTATS_TYPE_NULL:
  467. break;
  468. default:
  469. fprintf(stderr, "Unknown nested"
  470. " nla_type %d\n",
  471. na->nla_type);
  472. break;
  473. }
  474. len2 += NLA_ALIGN(na->nla_len);
  475. na = (struct nlattr *)((char *)na +
  476. NLA_ALIGN(na->nla_len));
  477. }
  478. break;
  479. case CGROUPSTATS_TYPE_CGROUP_STATS:
  480. print_cgroupstats(NLA_DATA(na));
  481. break;
  482. default:
  483. fprintf(stderr, "Unknown nla_type %d\n",
  484. na->nla_type);
  485. case TASKSTATS_TYPE_NULL:
  486. break;
  487. }
  488. na = (struct nlattr *) (GENLMSG_DATA(&msg) + len);
  489. }
  490. } while (loop);
  491. done:
  492. if (maskset) {
  493. rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
  494. TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK,
  495. &cpumask, strlen(cpumask) + 1);
  496. printf("Sent deregister mask, retval %d\n", rc);
  497. if (rc < 0)
  498. err(rc, "error sending deregister cpumask\n");
  499. }
  500. err:
  501. close(nl_sd);
  502. if (fd)
  503. close(fd);
  504. if (cfd)
  505. close(cfd);
  506. return 0;
  507. }