getdelays.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. __u64 stime, utime;
  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 "
  89. "to %d\n",
  90. rcvbufsz);
  91. return -1;
  92. }
  93. memset(&local, 0, sizeof(local));
  94. local.nl_family = AF_NETLINK;
  95. if (bind(fd, (struct sockaddr *) &local, sizeof(local)) < 0)
  96. goto error;
  97. return fd;
  98. error:
  99. close(fd);
  100. return -1;
  101. }
  102. static int send_cmd(int sd, __u16 nlmsg_type, __u32 nlmsg_pid,
  103. __u8 genl_cmd, __u16 nla_type,
  104. void *nla_data, int nla_len)
  105. {
  106. struct nlattr *na;
  107. struct sockaddr_nl nladdr;
  108. int r, buflen;
  109. char *buf;
  110. struct msgtemplate msg;
  111. msg.n.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN);
  112. msg.n.nlmsg_type = nlmsg_type;
  113. msg.n.nlmsg_flags = NLM_F_REQUEST;
  114. msg.n.nlmsg_seq = 0;
  115. msg.n.nlmsg_pid = nlmsg_pid;
  116. msg.g.cmd = genl_cmd;
  117. msg.g.version = 0x1;
  118. na = (struct nlattr *) GENLMSG_DATA(&msg);
  119. na->nla_type = nla_type;
  120. na->nla_len = nla_len + 1 + NLA_HDRLEN;
  121. memcpy(NLA_DATA(na), nla_data, nla_len);
  122. msg.n.nlmsg_len += NLMSG_ALIGN(na->nla_len);
  123. buf = (char *) &msg;
  124. buflen = msg.n.nlmsg_len ;
  125. memset(&nladdr, 0, sizeof(nladdr));
  126. nladdr.nl_family = AF_NETLINK;
  127. while ((r = sendto(sd, buf, buflen, 0, (struct sockaddr *) &nladdr,
  128. sizeof(nladdr))) < buflen) {
  129. if (r > 0) {
  130. buf += r;
  131. buflen -= r;
  132. } else if (errno != EAGAIN)
  133. return -1;
  134. }
  135. return 0;
  136. }
  137. /*
  138. * Probe the controller in genetlink to find the family id
  139. * for the TASKSTATS family
  140. */
  141. static int get_family_id(int sd)
  142. {
  143. struct {
  144. struct nlmsghdr n;
  145. struct genlmsghdr g;
  146. char buf[256];
  147. } ans;
  148. int id = 0, rc;
  149. struct nlattr *na;
  150. int rep_len;
  151. strcpy(name, TASKSTATS_GENL_NAME);
  152. rc = send_cmd(sd, GENL_ID_CTRL, getpid(), CTRL_CMD_GETFAMILY,
  153. CTRL_ATTR_FAMILY_NAME, (void *)name,
  154. strlen(TASKSTATS_GENL_NAME)+1);
  155. if (rc < 0)
  156. return 0; /* sendto() failure? */
  157. rep_len = recv(sd, &ans, sizeof(ans), 0);
  158. if (ans.n.nlmsg_type == NLMSG_ERROR ||
  159. (rep_len < 0) || !NLMSG_OK((&ans.n), rep_len))
  160. return 0;
  161. na = (struct nlattr *) GENLMSG_DATA(&ans);
  162. na = (struct nlattr *) ((char *) na + NLA_ALIGN(na->nla_len));
  163. if (na->nla_type == CTRL_ATTR_FAMILY_ID) {
  164. id = *(__u16 *) NLA_DATA(na);
  165. }
  166. return id;
  167. }
  168. #define average_ms(t, c) (t / 1000000ULL / (c ? c : 1))
  169. static void print_delayacct(struct taskstats *t)
  170. {
  171. printf("\n\nCPU %15s%15s%15s%15s%15s\n"
  172. " %15llu%15llu%15llu%15llu%15.3fms\n"
  173. "IO %15s%15s%15s\n"
  174. " %15llu%15llu%15llums\n"
  175. "SWAP %15s%15s%15s\n"
  176. " %15llu%15llu%15llums\n"
  177. "RECLAIM %12s%15s%15s\n"
  178. " %15llu%15llu%15llums\n",
  179. "count", "real total", "virtual total",
  180. "delay total", "delay average",
  181. (unsigned long long)t->cpu_count,
  182. (unsigned long long)t->cpu_run_real_total,
  183. (unsigned long long)t->cpu_run_virtual_total,
  184. (unsigned long long)t->cpu_delay_total,
  185. average_ms((double)t->cpu_delay_total, t->cpu_count),
  186. "count", "delay total", "delay average",
  187. (unsigned long long)t->blkio_count,
  188. (unsigned long long)t->blkio_delay_total,
  189. average_ms(t->blkio_delay_total, t->blkio_count),
  190. "count", "delay total", "delay average",
  191. (unsigned long long)t->swapin_count,
  192. (unsigned long long)t->swapin_delay_total,
  193. average_ms(t->swapin_delay_total, t->swapin_count),
  194. "count", "delay total", "delay average",
  195. (unsigned long long)t->freepages_count,
  196. (unsigned long long)t->freepages_delay_total,
  197. average_ms(t->freepages_delay_total, t->freepages_count));
  198. }
  199. static void task_context_switch_counts(struct taskstats *t)
  200. {
  201. printf("\n\nTask %15s%15s\n"
  202. " %15llu%15llu\n",
  203. "voluntary", "nonvoluntary",
  204. (unsigned long long)t->nvcsw, (unsigned long long)t->nivcsw);
  205. }
  206. static void print_cgroupstats(struct cgroupstats *c)
  207. {
  208. printf("sleeping %llu, blocked %llu, running %llu, stopped %llu, "
  209. "uninterruptible %llu\n", (unsigned long long)c->nr_sleeping,
  210. (unsigned long long)c->nr_io_wait,
  211. (unsigned long long)c->nr_running,
  212. (unsigned long long)c->nr_stopped,
  213. (unsigned long long)c->nr_uninterruptible);
  214. }
  215. static void print_ioacct(struct taskstats *t)
  216. {
  217. printf("%s: read=%llu, write=%llu, cancelled_write=%llu\n",
  218. t->ac_comm,
  219. (unsigned long long)t->read_bytes,
  220. (unsigned long long)t->write_bytes,
  221. (unsigned long long)t->cancelled_write_bytes);
  222. }
  223. int main(int argc, char *argv[])
  224. {
  225. int c, rc, rep_len, aggr_len, len2;
  226. int cmd_type = TASKSTATS_CMD_ATTR_UNSPEC;
  227. __u16 id;
  228. __u32 mypid;
  229. struct nlattr *na;
  230. int nl_sd = -1;
  231. int len = 0;
  232. pid_t tid = 0;
  233. pid_t rtid = 0;
  234. int fd = 0;
  235. int count = 0;
  236. int write_file = 0;
  237. int maskset = 0;
  238. char *logfile = NULL;
  239. int loop = 0;
  240. int containerset = 0;
  241. char containerpath[1024];
  242. int cfd = 0;
  243. int forking = 0;
  244. sigset_t sigset;
  245. struct msgtemplate msg;
  246. while (!forking) {
  247. c = getopt(argc, argv, "qdiw:r:m:t:p:vlC:c:");
  248. if (c < 0)
  249. break;
  250. switch (c) {
  251. case 'd':
  252. printf("print delayacct stats ON\n");
  253. print_delays = 1;
  254. break;
  255. case 'i':
  256. printf("printing IO accounting\n");
  257. print_io_accounting = 1;
  258. break;
  259. case 'q':
  260. printf("printing task/process context switch rates\n");
  261. print_task_context_switch_counts = 1;
  262. break;
  263. case 'C':
  264. containerset = 1;
  265. strncpy(containerpath, optarg, strlen(optarg) + 1);
  266. break;
  267. case 'w':
  268. logfile = strdup(optarg);
  269. printf("write to file %s\n", logfile);
  270. write_file = 1;
  271. break;
  272. case 'r':
  273. rcvbufsz = atoi(optarg);
  274. printf("receive buf size %d\n", rcvbufsz);
  275. if (rcvbufsz < 0)
  276. err(1, "Invalid rcv buf size\n");
  277. break;
  278. case 'm':
  279. strncpy(cpumask, optarg, sizeof(cpumask));
  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. if ((nl_sd = create_nl_socket(NETLINK_GENERIC)) < 0)
  336. err(1, "error creating Netlink socket\n");
  337. mypid = getpid();
  338. id = get_family_id(nl_sd);
  339. if (!id) {
  340. fprintf(stderr, "Error getting family id, errno %d\n", errno);
  341. goto err;
  342. }
  343. PRINTF("family id %d\n", id);
  344. if (maskset) {
  345. rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
  346. TASKSTATS_CMD_ATTR_REGISTER_CPUMASK,
  347. &cpumask, strlen(cpumask) + 1);
  348. PRINTF("Sent register cpumask, retval %d\n", rc);
  349. if (rc < 0) {
  350. fprintf(stderr, "error sending register cpumask\n");
  351. goto err;
  352. }
  353. }
  354. if (tid && containerset) {
  355. fprintf(stderr, "Select either -t or -C, not both\n");
  356. goto err;
  357. }
  358. /*
  359. * If we forked a child, wait for it to exit. Cannot use waitpid()
  360. * as all the delicious data would be reaped as part of the wait
  361. */
  362. if (tid && forking) {
  363. int sig_received;
  364. sigwait(&sigset, &sig_received);
  365. }
  366. if (tid) {
  367. rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
  368. cmd_type, &tid, sizeof(__u32));
  369. PRINTF("Sent pid/tgid, retval %d\n", rc);
  370. if (rc < 0) {
  371. fprintf(stderr, "error sending tid/tgid cmd\n");
  372. goto done;
  373. }
  374. }
  375. if (containerset) {
  376. cfd = open(containerpath, O_RDONLY);
  377. if (cfd < 0) {
  378. perror("error opening container file");
  379. goto err;
  380. }
  381. rc = send_cmd(nl_sd, id, mypid, CGROUPSTATS_CMD_GET,
  382. CGROUPSTATS_CMD_ATTR_FD, &cfd, sizeof(__u32));
  383. if (rc < 0) {
  384. perror("error sending cgroupstats command");
  385. goto err;
  386. }
  387. }
  388. if (!maskset && !tid && !containerset) {
  389. usage();
  390. goto err;
  391. }
  392. do {
  393. rep_len = recv(nl_sd, &msg, sizeof(msg), 0);
  394. PRINTF("received %d bytes\n", rep_len);
  395. if (rep_len < 0) {
  396. fprintf(stderr, "nonfatal reply error: errno %d\n",
  397. errno);
  398. continue;
  399. }
  400. if (msg.n.nlmsg_type == NLMSG_ERROR ||
  401. !NLMSG_OK((&msg.n), rep_len)) {
  402. struct nlmsgerr *err = NLMSG_DATA(&msg);
  403. fprintf(stderr, "fatal reply error, errno %d\n",
  404. err->error);
  405. goto done;
  406. }
  407. PRINTF("nlmsghdr size=%zu, nlmsg_len=%d, rep_len=%d\n",
  408. sizeof(struct nlmsghdr), msg.n.nlmsg_len, rep_len);
  409. rep_len = GENLMSG_PAYLOAD(&msg.n);
  410. na = (struct nlattr *) GENLMSG_DATA(&msg);
  411. len = 0;
  412. while (len < rep_len) {
  413. len += NLA_ALIGN(na->nla_len);
  414. switch (na->nla_type) {
  415. case TASKSTATS_TYPE_AGGR_TGID:
  416. /* Fall through */
  417. case TASKSTATS_TYPE_AGGR_PID:
  418. aggr_len = NLA_PAYLOAD(na->nla_len);
  419. len2 = 0;
  420. /* For nested attributes, na follows */
  421. na = (struct nlattr *) NLA_DATA(na);
  422. done = 0;
  423. while (len2 < aggr_len) {
  424. switch (na->nla_type) {
  425. case TASKSTATS_TYPE_PID:
  426. rtid = *(int *) NLA_DATA(na);
  427. if (print_delays)
  428. printf("PID\t%d\n", rtid);
  429. break;
  430. case TASKSTATS_TYPE_TGID:
  431. rtid = *(int *) NLA_DATA(na);
  432. if (print_delays)
  433. printf("TGID\t%d\n", rtid);
  434. break;
  435. case TASKSTATS_TYPE_STATS:
  436. count++;
  437. if (print_delays)
  438. print_delayacct((struct taskstats *) NLA_DATA(na));
  439. if (print_io_accounting)
  440. print_ioacct((struct taskstats *) NLA_DATA(na));
  441. if (print_task_context_switch_counts)
  442. task_context_switch_counts((struct taskstats *) NLA_DATA(na));
  443. if (fd) {
  444. if (write(fd, NLA_DATA(na), na->nla_len) < 0) {
  445. err(1,"write error\n");
  446. }
  447. }
  448. if (!loop)
  449. goto done;
  450. break;
  451. default:
  452. fprintf(stderr, "Unknown nested"
  453. " nla_type %d\n",
  454. na->nla_type);
  455. break;
  456. }
  457. len2 += NLA_ALIGN(na->nla_len);
  458. na = (struct nlattr *) ((char *) na + len2);
  459. }
  460. break;
  461. case CGROUPSTATS_TYPE_CGROUP_STATS:
  462. print_cgroupstats(NLA_DATA(na));
  463. break;
  464. default:
  465. fprintf(stderr, "Unknown nla_type %d\n",
  466. na->nla_type);
  467. case TASKSTATS_TYPE_NULL:
  468. break;
  469. }
  470. na = (struct nlattr *) (GENLMSG_DATA(&msg) + len);
  471. }
  472. } while (loop);
  473. done:
  474. if (maskset) {
  475. rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
  476. TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK,
  477. &cpumask, strlen(cpumask) + 1);
  478. printf("Sent deregister mask, retval %d\n", rc);
  479. if (rc < 0)
  480. err(rc, "error sending deregister cpumask\n");
  481. }
  482. err:
  483. close(nl_sd);
  484. if (fd)
  485. close(fd);
  486. if (cfd)
  487. close(cfd);
  488. return 0;
  489. }