getdelays.c 14 KB

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