getdelays.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. /*
  27. * Generic macros for dealing with netlink sockets. Might be duplicated
  28. * elsewhere. It is recommended that commercial grade applications use
  29. * libnl or libnetlink and use the interfaces provided by the library
  30. */
  31. #define GENLMSG_DATA(glh) ((void *)(NLMSG_DATA(glh) + GENL_HDRLEN))
  32. #define GENLMSG_PAYLOAD(glh) (NLMSG_PAYLOAD(glh, 0) - GENL_HDRLEN)
  33. #define NLA_DATA(na) ((void *)((char*)(na) + NLA_HDRLEN))
  34. #define NLA_PAYLOAD(len) (len - NLA_HDRLEN)
  35. #define err(code, fmt, arg...) \
  36. do { \
  37. fprintf(stderr, fmt, ##arg); \
  38. exit(code); \
  39. } while (0)
  40. int done;
  41. int rcvbufsz;
  42. char name[100];
  43. int dbg;
  44. int print_delays;
  45. int print_io_accounting;
  46. int print_task_context_switch_counts;
  47. __u64 stime, utime;
  48. #define PRINTF(fmt, arg...) { \
  49. if (dbg) { \
  50. printf(fmt, ##arg); \
  51. } \
  52. }
  53. /* Maximum size of response requested or message sent */
  54. #define MAX_MSG_SIZE 1024
  55. /* Maximum number of cpus expected to be specified in a cpumask */
  56. #define MAX_CPUS 32
  57. struct msgtemplate {
  58. struct nlmsghdr n;
  59. struct genlmsghdr g;
  60. char buf[MAX_MSG_SIZE];
  61. };
  62. char cpumask[100+6*MAX_CPUS];
  63. static void usage(void)
  64. {
  65. fprintf(stderr, "getdelays [-dilv] [-w logfile] [-r bufsize] "
  66. "[-m cpumask] [-t tgid] [-p pid]\n");
  67. fprintf(stderr, " -d: print delayacct stats\n");
  68. fprintf(stderr, " -i: print IO accounting (works only with -p)\n");
  69. fprintf(stderr, " -l: listen forever\n");
  70. fprintf(stderr, " -v: debug on\n");
  71. }
  72. /*
  73. * Create a raw netlink socket and bind
  74. */
  75. static int create_nl_socket(int protocol)
  76. {
  77. int fd;
  78. struct sockaddr_nl local;
  79. fd = socket(AF_NETLINK, SOCK_RAW, protocol);
  80. if (fd < 0)
  81. return -1;
  82. if (rcvbufsz)
  83. if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
  84. &rcvbufsz, sizeof(rcvbufsz)) < 0) {
  85. fprintf(stderr, "Unable to set socket rcv buf size "
  86. "to %d\n",
  87. rcvbufsz);
  88. return -1;
  89. }
  90. memset(&local, 0, sizeof(local));
  91. local.nl_family = AF_NETLINK;
  92. if (bind(fd, (struct sockaddr *) &local, sizeof(local)) < 0)
  93. goto error;
  94. return fd;
  95. error:
  96. close(fd);
  97. return -1;
  98. }
  99. int send_cmd(int sd, __u16 nlmsg_type, __u32 nlmsg_pid,
  100. __u8 genl_cmd, __u16 nla_type,
  101. void *nla_data, int nla_len)
  102. {
  103. struct nlattr *na;
  104. struct sockaddr_nl nladdr;
  105. int r, buflen;
  106. char *buf;
  107. struct msgtemplate msg;
  108. msg.n.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN);
  109. msg.n.nlmsg_type = nlmsg_type;
  110. msg.n.nlmsg_flags = NLM_F_REQUEST;
  111. msg.n.nlmsg_seq = 0;
  112. msg.n.nlmsg_pid = nlmsg_pid;
  113. msg.g.cmd = genl_cmd;
  114. msg.g.version = 0x1;
  115. na = (struct nlattr *) GENLMSG_DATA(&msg);
  116. na->nla_type = nla_type;
  117. na->nla_len = nla_len + 1 + NLA_HDRLEN;
  118. memcpy(NLA_DATA(na), nla_data, nla_len);
  119. msg.n.nlmsg_len += NLMSG_ALIGN(na->nla_len);
  120. buf = (char *) &msg;
  121. buflen = msg.n.nlmsg_len ;
  122. memset(&nladdr, 0, sizeof(nladdr));
  123. nladdr.nl_family = AF_NETLINK;
  124. while ((r = sendto(sd, buf, buflen, 0, (struct sockaddr *) &nladdr,
  125. sizeof(nladdr))) < buflen) {
  126. if (r > 0) {
  127. buf += r;
  128. buflen -= r;
  129. } else if (errno != EAGAIN)
  130. return -1;
  131. }
  132. return 0;
  133. }
  134. /*
  135. * Probe the controller in genetlink to find the family id
  136. * for the TASKSTATS family
  137. */
  138. int get_family_id(int sd)
  139. {
  140. struct {
  141. struct nlmsghdr n;
  142. struct genlmsghdr g;
  143. char buf[256];
  144. } ans;
  145. int id, rc;
  146. struct nlattr *na;
  147. int rep_len;
  148. strcpy(name, TASKSTATS_GENL_NAME);
  149. rc = send_cmd(sd, GENL_ID_CTRL, getpid(), CTRL_CMD_GETFAMILY,
  150. CTRL_ATTR_FAMILY_NAME, (void *)name,
  151. strlen(TASKSTATS_GENL_NAME)+1);
  152. rep_len = recv(sd, &ans, sizeof(ans), 0);
  153. if (ans.n.nlmsg_type == NLMSG_ERROR ||
  154. (rep_len < 0) || !NLMSG_OK((&ans.n), rep_len))
  155. return 0;
  156. na = (struct nlattr *) GENLMSG_DATA(&ans);
  157. na = (struct nlattr *) ((char *) na + NLA_ALIGN(na->nla_len));
  158. if (na->nla_type == CTRL_ATTR_FAMILY_ID) {
  159. id = *(__u16 *) NLA_DATA(na);
  160. }
  161. return id;
  162. }
  163. void print_delayacct(struct taskstats *t)
  164. {
  165. printf("\n\nCPU %15s%15s%15s%15s\n"
  166. " %15llu%15llu%15llu%15llu\n"
  167. "IO %15s%15s\n"
  168. " %15llu%15llu\n"
  169. "MEM %15s%15s\n"
  170. " %15llu%15llu\n",
  171. "count", "real total", "virtual total", "delay total",
  172. t->cpu_count, t->cpu_run_real_total, t->cpu_run_virtual_total,
  173. t->cpu_delay_total,
  174. "count", "delay total",
  175. t->blkio_count, t->blkio_delay_total,
  176. "count", "delay total", t->swapin_count, t->swapin_delay_total);
  177. }
  178. void task_context_switch_counts(struct taskstats *t)
  179. {
  180. printf("\n\nTask %15s%15s\n"
  181. " %15lu%15lu\n",
  182. "voluntary", "nonvoluntary",
  183. t->nvcsw, t->nivcsw);
  184. }
  185. void print_ioacct(struct taskstats *t)
  186. {
  187. printf("%s: read=%llu, write=%llu, cancelled_write=%llu\n",
  188. t->ac_comm,
  189. (unsigned long long)t->read_bytes,
  190. (unsigned long long)t->write_bytes,
  191. (unsigned long long)t->cancelled_write_bytes);
  192. }
  193. int main(int argc, char *argv[])
  194. {
  195. int c, rc, rep_len, aggr_len, len2, cmd_type;
  196. __u16 id;
  197. __u32 mypid;
  198. struct nlattr *na;
  199. int nl_sd = -1;
  200. int len = 0;
  201. pid_t tid = 0;
  202. pid_t rtid = 0;
  203. int fd = 0;
  204. int count = 0;
  205. int write_file = 0;
  206. int maskset = 0;
  207. char *logfile = NULL;
  208. int loop = 0;
  209. struct msgtemplate msg;
  210. while (1) {
  211. c = getopt(argc, argv, "qdiw:r:m:t:p:vl");
  212. if (c < 0)
  213. break;
  214. switch (c) {
  215. case 'd':
  216. printf("print delayacct stats ON\n");
  217. print_delays = 1;
  218. break;
  219. case 'i':
  220. printf("printing IO accounting\n");
  221. print_io_accounting = 1;
  222. break;
  223. case 'q':
  224. printf("printing task/process context switch rates\n");
  225. print_task_context_switch_counts = 1;
  226. break;
  227. case 'w':
  228. logfile = strdup(optarg);
  229. printf("write to file %s\n", logfile);
  230. write_file = 1;
  231. break;
  232. case 'r':
  233. rcvbufsz = atoi(optarg);
  234. printf("receive buf size %d\n", rcvbufsz);
  235. if (rcvbufsz < 0)
  236. err(1, "Invalid rcv buf size\n");
  237. break;
  238. case 'm':
  239. strncpy(cpumask, optarg, sizeof(cpumask));
  240. maskset = 1;
  241. printf("cpumask %s maskset %d\n", cpumask, maskset);
  242. break;
  243. case 't':
  244. tid = atoi(optarg);
  245. if (!tid)
  246. err(1, "Invalid tgid\n");
  247. cmd_type = TASKSTATS_CMD_ATTR_TGID;
  248. break;
  249. case 'p':
  250. tid = atoi(optarg);
  251. if (!tid)
  252. err(1, "Invalid pid\n");
  253. cmd_type = TASKSTATS_CMD_ATTR_PID;
  254. break;
  255. case 'v':
  256. printf("debug on\n");
  257. dbg = 1;
  258. break;
  259. case 'l':
  260. printf("listen forever\n");
  261. loop = 1;
  262. break;
  263. default:
  264. usage();
  265. exit(-1);
  266. }
  267. }
  268. if (write_file) {
  269. fd = open(logfile, O_WRONLY | O_CREAT | O_TRUNC,
  270. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  271. if (fd == -1) {
  272. perror("Cannot open output file\n");
  273. exit(1);
  274. }
  275. }
  276. if ((nl_sd = create_nl_socket(NETLINK_GENERIC)) < 0)
  277. err(1, "error creating Netlink socket\n");
  278. mypid = getpid();
  279. id = get_family_id(nl_sd);
  280. if (!id) {
  281. fprintf(stderr, "Error getting family id, errno %d\n", errno);
  282. goto err;
  283. }
  284. PRINTF("family id %d\n", id);
  285. if (maskset) {
  286. rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
  287. TASKSTATS_CMD_ATTR_REGISTER_CPUMASK,
  288. &cpumask, strlen(cpumask) + 1);
  289. PRINTF("Sent register cpumask, retval %d\n", rc);
  290. if (rc < 0) {
  291. fprintf(stderr, "error sending register cpumask\n");
  292. goto err;
  293. }
  294. }
  295. if (tid) {
  296. rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
  297. cmd_type, &tid, sizeof(__u32));
  298. PRINTF("Sent pid/tgid, retval %d\n", rc);
  299. if (rc < 0) {
  300. fprintf(stderr, "error sending tid/tgid cmd\n");
  301. goto done;
  302. }
  303. }
  304. do {
  305. int i;
  306. rep_len = recv(nl_sd, &msg, sizeof(msg), 0);
  307. PRINTF("received %d bytes\n", rep_len);
  308. if (rep_len < 0) {
  309. fprintf(stderr, "nonfatal reply error: errno %d\n",
  310. errno);
  311. continue;
  312. }
  313. if (msg.n.nlmsg_type == NLMSG_ERROR ||
  314. !NLMSG_OK((&msg.n), rep_len)) {
  315. struct nlmsgerr *err = NLMSG_DATA(&msg);
  316. fprintf(stderr, "fatal reply error, errno %d\n",
  317. err->error);
  318. goto done;
  319. }
  320. PRINTF("nlmsghdr size=%d, nlmsg_len=%d, rep_len=%d\n",
  321. sizeof(struct nlmsghdr), msg.n.nlmsg_len, rep_len);
  322. rep_len = GENLMSG_PAYLOAD(&msg.n);
  323. na = (struct nlattr *) GENLMSG_DATA(&msg);
  324. len = 0;
  325. i = 0;
  326. while (len < rep_len) {
  327. len += NLA_ALIGN(na->nla_len);
  328. switch (na->nla_type) {
  329. case TASKSTATS_TYPE_AGGR_TGID:
  330. /* Fall through */
  331. case TASKSTATS_TYPE_AGGR_PID:
  332. aggr_len = NLA_PAYLOAD(na->nla_len);
  333. len2 = 0;
  334. /* For nested attributes, na follows */
  335. na = (struct nlattr *) NLA_DATA(na);
  336. done = 0;
  337. while (len2 < aggr_len) {
  338. switch (na->nla_type) {
  339. case TASKSTATS_TYPE_PID:
  340. rtid = *(int *) NLA_DATA(na);
  341. if (print_delays)
  342. printf("PID\t%d\n", rtid);
  343. break;
  344. case TASKSTATS_TYPE_TGID:
  345. rtid = *(int *) NLA_DATA(na);
  346. if (print_delays)
  347. printf("TGID\t%d\n", rtid);
  348. break;
  349. case TASKSTATS_TYPE_STATS:
  350. count++;
  351. if (print_delays)
  352. print_delayacct((struct taskstats *) NLA_DATA(na));
  353. if (print_io_accounting)
  354. print_ioacct((struct taskstats *) NLA_DATA(na));
  355. if (print_task_context_switch_counts)
  356. task_context_switch_counts((struct taskstats *) NLA_DATA(na));
  357. if (fd) {
  358. if (write(fd, NLA_DATA(na), na->nla_len) < 0) {
  359. err(1,"write error\n");
  360. }
  361. }
  362. if (!loop)
  363. goto done;
  364. break;
  365. default:
  366. fprintf(stderr, "Unknown nested"
  367. " nla_type %d\n",
  368. na->nla_type);
  369. break;
  370. }
  371. len2 += NLA_ALIGN(na->nla_len);
  372. na = (struct nlattr *) ((char *) na + len2);
  373. }
  374. break;
  375. default:
  376. fprintf(stderr, "Unknown nla_type %d\n",
  377. na->nla_type);
  378. break;
  379. }
  380. na = (struct nlattr *) (GENLMSG_DATA(&msg) + len);
  381. }
  382. } while (loop);
  383. done:
  384. if (maskset) {
  385. rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
  386. TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK,
  387. &cpumask, strlen(cpumask) + 1);
  388. printf("Sent deregister mask, retval %d\n", rc);
  389. if (rc < 0)
  390. err(rc, "error sending deregister cpumask\n");
  391. }
  392. err:
  393. close(nl_sd);
  394. if (fd)
  395. close(fd);
  396. return 0;
  397. }