getdelays.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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/types.h>
  24. #include <signal.h>
  25. #include <linux/genetlink.h>
  26. #include <linux/taskstats.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. }
  73. /*
  74. * Create a raw netlink socket and bind
  75. */
  76. static int create_nl_socket(int protocol)
  77. {
  78. int fd;
  79. struct sockaddr_nl local;
  80. fd = socket(AF_NETLINK, SOCK_RAW, protocol);
  81. if (fd < 0)
  82. return -1;
  83. if (rcvbufsz)
  84. if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
  85. &rcvbufsz, sizeof(rcvbufsz)) < 0) {
  86. fprintf(stderr, "Unable to set socket rcv buf size "
  87. "to %d\n",
  88. rcvbufsz);
  89. return -1;
  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. 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. 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, 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. rep_len = recv(sd, &ans, sizeof(ans), 0);
  154. if (ans.n.nlmsg_type == NLMSG_ERROR ||
  155. (rep_len < 0) || !NLMSG_OK((&ans.n), rep_len))
  156. return 0;
  157. na = (struct nlattr *) GENLMSG_DATA(&ans);
  158. na = (struct nlattr *) ((char *) na + NLA_ALIGN(na->nla_len));
  159. if (na->nla_type == CTRL_ATTR_FAMILY_ID) {
  160. id = *(__u16 *) NLA_DATA(na);
  161. }
  162. return id;
  163. }
  164. void print_delayacct(struct taskstats *t)
  165. {
  166. printf("\n\nCPU %15s%15s%15s%15s\n"
  167. " %15llu%15llu%15llu%15llu\n"
  168. "IO %15s%15s\n"
  169. " %15llu%15llu\n"
  170. "MEM %15s%15s\n"
  171. " %15llu%15llu\n",
  172. "count", "real total", "virtual total", "delay total",
  173. t->cpu_count, t->cpu_run_real_total, t->cpu_run_virtual_total,
  174. t->cpu_delay_total,
  175. "count", "delay total",
  176. t->blkio_count, t->blkio_delay_total,
  177. "count", "delay total", t->swapin_count, t->swapin_delay_total);
  178. }
  179. void task_context_switch_counts(struct taskstats *t)
  180. {
  181. printf("\n\nTask %15s%15s\n"
  182. " %15lu%15lu\n",
  183. "voluntary", "nonvoluntary",
  184. t->nvcsw, t->nivcsw);
  185. }
  186. void print_ioacct(struct taskstats *t)
  187. {
  188. printf("%s: read=%llu, write=%llu, cancelled_write=%llu\n",
  189. t->ac_comm,
  190. (unsigned long long)t->read_bytes,
  191. (unsigned long long)t->write_bytes,
  192. (unsigned long long)t->cancelled_write_bytes);
  193. }
  194. int main(int argc, char *argv[])
  195. {
  196. int c, rc, rep_len, aggr_len, len2, cmd_type;
  197. __u16 id;
  198. __u32 mypid;
  199. struct nlattr *na;
  200. int nl_sd = -1;
  201. int len = 0;
  202. pid_t tid = 0;
  203. pid_t rtid = 0;
  204. int fd = 0;
  205. int count = 0;
  206. int write_file = 0;
  207. int maskset = 0;
  208. char *logfile = NULL;
  209. int loop = 0;
  210. struct msgtemplate msg;
  211. while (1) {
  212. c = getopt(argc, argv, "qdiw:r:m:t:p:vl");
  213. if (c < 0)
  214. break;
  215. switch (c) {
  216. case 'd':
  217. printf("print delayacct stats ON\n");
  218. print_delays = 1;
  219. break;
  220. case 'i':
  221. printf("printing IO accounting\n");
  222. print_io_accounting = 1;
  223. break;
  224. case 'q':
  225. printf("printing task/process context switch rates\n");
  226. print_task_context_switch_counts = 1;
  227. break;
  228. case 'w':
  229. logfile = strdup(optarg);
  230. printf("write to file %s\n", logfile);
  231. write_file = 1;
  232. break;
  233. case 'r':
  234. rcvbufsz = atoi(optarg);
  235. printf("receive buf size %d\n", rcvbufsz);
  236. if (rcvbufsz < 0)
  237. err(1, "Invalid rcv buf size\n");
  238. break;
  239. case 'm':
  240. strncpy(cpumask, optarg, sizeof(cpumask));
  241. maskset = 1;
  242. printf("cpumask %s maskset %d\n", cpumask, maskset);
  243. break;
  244. case 't':
  245. tid = atoi(optarg);
  246. if (!tid)
  247. err(1, "Invalid tgid\n");
  248. cmd_type = TASKSTATS_CMD_ATTR_TGID;
  249. break;
  250. case 'p':
  251. tid = atoi(optarg);
  252. if (!tid)
  253. err(1, "Invalid pid\n");
  254. cmd_type = TASKSTATS_CMD_ATTR_PID;
  255. break;
  256. case 'v':
  257. printf("debug on\n");
  258. dbg = 1;
  259. break;
  260. case 'l':
  261. printf("listen forever\n");
  262. loop = 1;
  263. break;
  264. default:
  265. usage();
  266. exit(-1);
  267. }
  268. }
  269. if (write_file) {
  270. fd = open(logfile, O_WRONLY | O_CREAT | O_TRUNC,
  271. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  272. if (fd == -1) {
  273. perror("Cannot open output file\n");
  274. exit(1);
  275. }
  276. }
  277. if ((nl_sd = create_nl_socket(NETLINK_GENERIC)) < 0)
  278. err(1, "error creating Netlink socket\n");
  279. mypid = getpid();
  280. id = get_family_id(nl_sd);
  281. if (!id) {
  282. fprintf(stderr, "Error getting family id, errno %d\n", errno);
  283. goto err;
  284. }
  285. PRINTF("family id %d\n", id);
  286. if (maskset) {
  287. rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
  288. TASKSTATS_CMD_ATTR_REGISTER_CPUMASK,
  289. &cpumask, strlen(cpumask) + 1);
  290. PRINTF("Sent register cpumask, retval %d\n", rc);
  291. if (rc < 0) {
  292. fprintf(stderr, "error sending register cpumask\n");
  293. goto err;
  294. }
  295. }
  296. if (tid) {
  297. rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
  298. cmd_type, &tid, sizeof(__u32));
  299. PRINTF("Sent pid/tgid, retval %d\n", rc);
  300. if (rc < 0) {
  301. fprintf(stderr, "error sending tid/tgid cmd\n");
  302. goto done;
  303. }
  304. }
  305. do {
  306. int i;
  307. rep_len = recv(nl_sd, &msg, sizeof(msg), 0);
  308. PRINTF("received %d bytes\n", rep_len);
  309. if (rep_len < 0) {
  310. fprintf(stderr, "nonfatal reply error: errno %d\n",
  311. errno);
  312. continue;
  313. }
  314. if (msg.n.nlmsg_type == NLMSG_ERROR ||
  315. !NLMSG_OK((&msg.n), rep_len)) {
  316. struct nlmsgerr *err = NLMSG_DATA(&msg);
  317. fprintf(stderr, "fatal reply error, errno %d\n",
  318. err->error);
  319. goto done;
  320. }
  321. PRINTF("nlmsghdr size=%d, nlmsg_len=%d, rep_len=%d\n",
  322. sizeof(struct nlmsghdr), msg.n.nlmsg_len, rep_len);
  323. rep_len = GENLMSG_PAYLOAD(&msg.n);
  324. na = (struct nlattr *) GENLMSG_DATA(&msg);
  325. len = 0;
  326. i = 0;
  327. while (len < rep_len) {
  328. len += NLA_ALIGN(na->nla_len);
  329. switch (na->nla_type) {
  330. case TASKSTATS_TYPE_AGGR_TGID:
  331. /* Fall through */
  332. case TASKSTATS_TYPE_AGGR_PID:
  333. aggr_len = NLA_PAYLOAD(na->nla_len);
  334. len2 = 0;
  335. /* For nested attributes, na follows */
  336. na = (struct nlattr *) NLA_DATA(na);
  337. done = 0;
  338. while (len2 < aggr_len) {
  339. switch (na->nla_type) {
  340. case TASKSTATS_TYPE_PID:
  341. rtid = *(int *) NLA_DATA(na);
  342. if (print_delays)
  343. printf("PID\t%d\n", rtid);
  344. break;
  345. case TASKSTATS_TYPE_TGID:
  346. rtid = *(int *) NLA_DATA(na);
  347. if (print_delays)
  348. printf("TGID\t%d\n", rtid);
  349. break;
  350. case TASKSTATS_TYPE_STATS:
  351. count++;
  352. if (print_delays)
  353. print_delayacct((struct taskstats *) NLA_DATA(na));
  354. if (print_io_accounting)
  355. print_ioacct((struct taskstats *) NLA_DATA(na));
  356. if (print_task_context_switch_counts)
  357. task_context_switch_counts((struct taskstats *) NLA_DATA(na));
  358. if (fd) {
  359. if (write(fd, NLA_DATA(na), na->nla_len) < 0) {
  360. err(1,"write error\n");
  361. }
  362. }
  363. if (!loop)
  364. goto done;
  365. break;
  366. default:
  367. fprintf(stderr, "Unknown nested"
  368. " nla_type %d\n",
  369. na->nla_type);
  370. break;
  371. }
  372. len2 += NLA_ALIGN(na->nla_len);
  373. na = (struct nlattr *) ((char *) na + len2);
  374. }
  375. break;
  376. default:
  377. fprintf(stderr, "Unknown nla_type %d\n",
  378. na->nla_type);
  379. break;
  380. }
  381. na = (struct nlattr *) (GENLMSG_DATA(&msg) + len);
  382. }
  383. } while (loop);
  384. done:
  385. if (maskset) {
  386. rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
  387. TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK,
  388. &cpumask, strlen(cpumask) + 1);
  389. printf("Sent deregister mask, retval %d\n", rc);
  390. if (rc < 0)
  391. err(rc, "error sending deregister cpumask\n");
  392. }
  393. err:
  394. close(nl_sd);
  395. if (fd)
  396. close(fd);
  397. return 0;
  398. }