getdelays.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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 <signal.h>
  24. #include <linux/genetlink.h>
  25. #include <linux/taskstats.h>
  26. #include <linux/cgroupstats.h>
  27. /*
  28. * Generic macros for dealing with netlink sockets. Might be duplicated
  29. * elsewhere. It is recommended that commercial grade applications use
  30. * libnl or libnetlink and use the interfaces provided by the library
  31. */
  32. #define GENLMSG_DATA(glh) ((void *)(NLMSG_DATA(glh) + GENL_HDRLEN))
  33. #define GENLMSG_PAYLOAD(glh) (NLMSG_PAYLOAD(glh, 0) - GENL_HDRLEN)
  34. #define NLA_DATA(na) ((void *)((char*)(na) + NLA_HDRLEN))
  35. #define NLA_PAYLOAD(len) (len - NLA_HDRLEN)
  36. #define err(code, fmt, arg...) \
  37. do { \
  38. fprintf(stderr, fmt, ##arg); \
  39. exit(code); \
  40. } while (0)
  41. int done;
  42. int rcvbufsz;
  43. char name[100];
  44. int dbg;
  45. int print_delays;
  46. int print_io_accounting;
  47. int print_task_context_switch_counts;
  48. __u64 stime, utime;
  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 "
  88. "to %d\n",
  89. rcvbufsz);
  90. return -1;
  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. 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 + 1 + 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. 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. rep_len = recv(sd, &ans, sizeof(ans), 0);
  155. if (ans.n.nlmsg_type == NLMSG_ERROR ||
  156. (rep_len < 0) || !NLMSG_OK((&ans.n), rep_len))
  157. return 0;
  158. na = (struct nlattr *) GENLMSG_DATA(&ans);
  159. na = (struct nlattr *) ((char *) na + NLA_ALIGN(na->nla_len));
  160. if (na->nla_type == CTRL_ATTR_FAMILY_ID) {
  161. id = *(__u16 *) NLA_DATA(na);
  162. }
  163. return id;
  164. }
  165. void print_delayacct(struct taskstats *t)
  166. {
  167. printf("\n\nCPU %15s%15s%15s%15s\n"
  168. " %15llu%15llu%15llu%15llu\n"
  169. "IO %15s%15s\n"
  170. " %15llu%15llu\n"
  171. "SWAP %15s%15s\n"
  172. " %15llu%15llu\n"
  173. "RECLAIM %12s%15s\n"
  174. " %15llu%15llu\n",
  175. "count", "real total", "virtual total", "delay total",
  176. (unsigned long long)t->cpu_count,
  177. (unsigned long long)t->cpu_run_real_total,
  178. (unsigned long long)t->cpu_run_virtual_total,
  179. (unsigned long long)t->cpu_delay_total,
  180. "count", "delay total",
  181. (unsigned long long)t->blkio_count,
  182. (unsigned long long)t->blkio_delay_total,
  183. "count", "delay total",
  184. (unsigned long long)t->swapin_count,
  185. (unsigned long long)t->swapin_delay_total,
  186. "count", "delay total",
  187. (unsigned long long)t->freepages_count,
  188. (unsigned long long)t->freepages_delay_total);
  189. }
  190. void task_context_switch_counts(struct taskstats *t)
  191. {
  192. printf("\n\nTask %15s%15s\n"
  193. " %15llu%15llu\n",
  194. "voluntary", "nonvoluntary",
  195. (unsigned long long)t->nvcsw, (unsigned long long)t->nivcsw);
  196. }
  197. void print_cgroupstats(struct cgroupstats *c)
  198. {
  199. printf("sleeping %llu, blocked %llu, running %llu, stopped %llu, "
  200. "uninterruptible %llu\n", (unsigned long long)c->nr_sleeping,
  201. (unsigned long long)c->nr_io_wait,
  202. (unsigned long long)c->nr_running,
  203. (unsigned long long)c->nr_stopped,
  204. (unsigned long long)c->nr_uninterruptible);
  205. }
  206. void print_ioacct(struct taskstats *t)
  207. {
  208. printf("%s: read=%llu, write=%llu, cancelled_write=%llu\n",
  209. t->ac_comm,
  210. (unsigned long long)t->read_bytes,
  211. (unsigned long long)t->write_bytes,
  212. (unsigned long long)t->cancelled_write_bytes);
  213. }
  214. int main(int argc, char *argv[])
  215. {
  216. int c, rc, rep_len, aggr_len, len2, cmd_type;
  217. __u16 id;
  218. __u32 mypid;
  219. struct nlattr *na;
  220. int nl_sd = -1;
  221. int len = 0;
  222. pid_t tid = 0;
  223. pid_t rtid = 0;
  224. int fd = 0;
  225. int count = 0;
  226. int write_file = 0;
  227. int maskset = 0;
  228. char *logfile = NULL;
  229. int loop = 0;
  230. int containerset = 0;
  231. char containerpath[1024];
  232. int cfd = 0;
  233. struct msgtemplate msg;
  234. while (1) {
  235. c = getopt(argc, argv, "qdiw:r:m:t:p:vlC:");
  236. if (c < 0)
  237. break;
  238. switch (c) {
  239. case 'd':
  240. printf("print delayacct stats ON\n");
  241. print_delays = 1;
  242. break;
  243. case 'i':
  244. printf("printing IO accounting\n");
  245. print_io_accounting = 1;
  246. break;
  247. case 'q':
  248. printf("printing task/process context switch rates\n");
  249. print_task_context_switch_counts = 1;
  250. break;
  251. case 'C':
  252. containerset = 1;
  253. strncpy(containerpath, optarg, strlen(optarg) + 1);
  254. break;
  255. case 'w':
  256. logfile = strdup(optarg);
  257. printf("write to file %s\n", logfile);
  258. write_file = 1;
  259. break;
  260. case 'r':
  261. rcvbufsz = atoi(optarg);
  262. printf("receive buf size %d\n", rcvbufsz);
  263. if (rcvbufsz < 0)
  264. err(1, "Invalid rcv buf size\n");
  265. break;
  266. case 'm':
  267. strncpy(cpumask, optarg, sizeof(cpumask));
  268. maskset = 1;
  269. printf("cpumask %s maskset %d\n", cpumask, maskset);
  270. break;
  271. case 't':
  272. tid = atoi(optarg);
  273. if (!tid)
  274. err(1, "Invalid tgid\n");
  275. cmd_type = TASKSTATS_CMD_ATTR_TGID;
  276. break;
  277. case 'p':
  278. tid = atoi(optarg);
  279. if (!tid)
  280. err(1, "Invalid pid\n");
  281. cmd_type = TASKSTATS_CMD_ATTR_PID;
  282. break;
  283. case 'v':
  284. printf("debug on\n");
  285. dbg = 1;
  286. break;
  287. case 'l':
  288. printf("listen forever\n");
  289. loop = 1;
  290. break;
  291. default:
  292. usage();
  293. exit(-1);
  294. }
  295. }
  296. if (write_file) {
  297. fd = open(logfile, O_WRONLY | O_CREAT | O_TRUNC,
  298. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  299. if (fd == -1) {
  300. perror("Cannot open output file\n");
  301. exit(1);
  302. }
  303. }
  304. if ((nl_sd = create_nl_socket(NETLINK_GENERIC)) < 0)
  305. err(1, "error creating Netlink socket\n");
  306. mypid = getpid();
  307. id = get_family_id(nl_sd);
  308. if (!id) {
  309. fprintf(stderr, "Error getting family id, errno %d\n", errno);
  310. goto err;
  311. }
  312. PRINTF("family id %d\n", id);
  313. if (maskset) {
  314. rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
  315. TASKSTATS_CMD_ATTR_REGISTER_CPUMASK,
  316. &cpumask, strlen(cpumask) + 1);
  317. PRINTF("Sent register cpumask, retval %d\n", rc);
  318. if (rc < 0) {
  319. fprintf(stderr, "error sending register cpumask\n");
  320. goto err;
  321. }
  322. }
  323. if (tid && containerset) {
  324. fprintf(stderr, "Select either -t or -C, not both\n");
  325. goto err;
  326. }
  327. if (tid) {
  328. rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
  329. cmd_type, &tid, sizeof(__u32));
  330. PRINTF("Sent pid/tgid, retval %d\n", rc);
  331. if (rc < 0) {
  332. fprintf(stderr, "error sending tid/tgid cmd\n");
  333. goto done;
  334. }
  335. }
  336. if (containerset) {
  337. cfd = open(containerpath, O_RDONLY);
  338. if (cfd < 0) {
  339. perror("error opening container file");
  340. goto err;
  341. }
  342. rc = send_cmd(nl_sd, id, mypid, CGROUPSTATS_CMD_GET,
  343. CGROUPSTATS_CMD_ATTR_FD, &cfd, sizeof(__u32));
  344. if (rc < 0) {
  345. perror("error sending cgroupstats command");
  346. goto err;
  347. }
  348. }
  349. if (!maskset && !tid && !containerset) {
  350. usage();
  351. goto err;
  352. }
  353. do {
  354. int i;
  355. rep_len = recv(nl_sd, &msg, sizeof(msg), 0);
  356. PRINTF("received %d bytes\n", rep_len);
  357. if (rep_len < 0) {
  358. fprintf(stderr, "nonfatal reply error: errno %d\n",
  359. errno);
  360. continue;
  361. }
  362. if (msg.n.nlmsg_type == NLMSG_ERROR ||
  363. !NLMSG_OK((&msg.n), rep_len)) {
  364. struct nlmsgerr *err = NLMSG_DATA(&msg);
  365. fprintf(stderr, "fatal reply error, errno %d\n",
  366. err->error);
  367. goto done;
  368. }
  369. PRINTF("nlmsghdr size=%zu, nlmsg_len=%d, rep_len=%d\n",
  370. sizeof(struct nlmsghdr), msg.n.nlmsg_len, rep_len);
  371. rep_len = GENLMSG_PAYLOAD(&msg.n);
  372. na = (struct nlattr *) GENLMSG_DATA(&msg);
  373. len = 0;
  374. i = 0;
  375. while (len < rep_len) {
  376. len += NLA_ALIGN(na->nla_len);
  377. switch (na->nla_type) {
  378. case TASKSTATS_TYPE_AGGR_TGID:
  379. /* Fall through */
  380. case TASKSTATS_TYPE_AGGR_PID:
  381. aggr_len = NLA_PAYLOAD(na->nla_len);
  382. len2 = 0;
  383. /* For nested attributes, na follows */
  384. na = (struct nlattr *) NLA_DATA(na);
  385. done = 0;
  386. while (len2 < aggr_len) {
  387. switch (na->nla_type) {
  388. case TASKSTATS_TYPE_PID:
  389. rtid = *(int *) NLA_DATA(na);
  390. if (print_delays)
  391. printf("PID\t%d\n", rtid);
  392. break;
  393. case TASKSTATS_TYPE_TGID:
  394. rtid = *(int *) NLA_DATA(na);
  395. if (print_delays)
  396. printf("TGID\t%d\n", rtid);
  397. break;
  398. case TASKSTATS_TYPE_STATS:
  399. count++;
  400. if (print_delays)
  401. print_delayacct((struct taskstats *) NLA_DATA(na));
  402. if (print_io_accounting)
  403. print_ioacct((struct taskstats *) NLA_DATA(na));
  404. if (print_task_context_switch_counts)
  405. task_context_switch_counts((struct taskstats *) NLA_DATA(na));
  406. if (fd) {
  407. if (write(fd, NLA_DATA(na), na->nla_len) < 0) {
  408. err(1,"write error\n");
  409. }
  410. }
  411. if (!loop)
  412. goto done;
  413. break;
  414. default:
  415. fprintf(stderr, "Unknown nested"
  416. " nla_type %d\n",
  417. na->nla_type);
  418. break;
  419. }
  420. len2 += NLA_ALIGN(na->nla_len);
  421. na = (struct nlattr *) ((char *) na + len2);
  422. }
  423. break;
  424. case CGROUPSTATS_TYPE_CGROUP_STATS:
  425. print_cgroupstats(NLA_DATA(na));
  426. break;
  427. default:
  428. fprintf(stderr, "Unknown nla_type %d\n",
  429. na->nla_type);
  430. break;
  431. }
  432. na = (struct nlattr *) (GENLMSG_DATA(&msg) + len);
  433. }
  434. } while (loop);
  435. done:
  436. if (maskset) {
  437. rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
  438. TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK,
  439. &cpumask, strlen(cpumask) + 1);
  440. printf("Sent deregister mask, retval %d\n", rc);
  441. if (rc < 0)
  442. err(rc, "error sending deregister cpumask\n");
  443. }
  444. err:
  445. close(nl_sd);
  446. if (fd)
  447. close(fd);
  448. if (cfd)
  449. close(cfd);
  450. return 0;
  451. }