getdelays.c 11 KB

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