getdelays.c 13 KB

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