getdelays.c 13 KB

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