getdelays.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. __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\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 print_ioacct(struct taskstats *t)
  179. {
  180. printf("%s: read=%llu, write=%llu, cancelled_write=%llu\n",
  181. t->ac_comm,
  182. (unsigned long long)t->read_bytes,
  183. (unsigned long long)t->write_bytes,
  184. (unsigned long long)t->cancelled_write_bytes);
  185. }
  186. int main(int argc, char *argv[])
  187. {
  188. int c, rc, rep_len, aggr_len, len2, cmd_type;
  189. __u16 id;
  190. __u32 mypid;
  191. struct nlattr *na;
  192. int nl_sd = -1;
  193. int len = 0;
  194. pid_t tid = 0;
  195. pid_t rtid = 0;
  196. int fd = 0;
  197. int count = 0;
  198. int write_file = 0;
  199. int maskset = 0;
  200. char *logfile = NULL;
  201. int loop = 0;
  202. struct msgtemplate msg;
  203. while (1) {
  204. c = getopt(argc, argv, "diw:r:m:t:p:vl");
  205. if (c < 0)
  206. break;
  207. switch (c) {
  208. case 'd':
  209. printf("print delayacct stats ON\n");
  210. print_delays = 1;
  211. break;
  212. case 'i':
  213. printf("printing IO accounting\n");
  214. print_io_accounting = 1;
  215. break;
  216. case 'w':
  217. logfile = strdup(optarg);
  218. printf("write to file %s\n", logfile);
  219. write_file = 1;
  220. break;
  221. case 'r':
  222. rcvbufsz = atoi(optarg);
  223. printf("receive buf size %d\n", rcvbufsz);
  224. if (rcvbufsz < 0)
  225. err(1, "Invalid rcv buf size\n");
  226. break;
  227. case 'm':
  228. strncpy(cpumask, optarg, sizeof(cpumask));
  229. maskset = 1;
  230. printf("cpumask %s maskset %d\n", cpumask, maskset);
  231. break;
  232. case 't':
  233. tid = atoi(optarg);
  234. if (!tid)
  235. err(1, "Invalid tgid\n");
  236. cmd_type = TASKSTATS_CMD_ATTR_TGID;
  237. break;
  238. case 'p':
  239. tid = atoi(optarg);
  240. if (!tid)
  241. err(1, "Invalid pid\n");
  242. cmd_type = TASKSTATS_CMD_ATTR_PID;
  243. break;
  244. case 'v':
  245. printf("debug on\n");
  246. dbg = 1;
  247. break;
  248. case 'l':
  249. printf("listen forever\n");
  250. loop = 1;
  251. break;
  252. default:
  253. usage();
  254. exit(-1);
  255. }
  256. }
  257. if (write_file) {
  258. fd = open(logfile, O_WRONLY | O_CREAT | O_TRUNC,
  259. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  260. if (fd == -1) {
  261. perror("Cannot open output file\n");
  262. exit(1);
  263. }
  264. }
  265. if ((nl_sd = create_nl_socket(NETLINK_GENERIC)) < 0)
  266. err(1, "error creating Netlink socket\n");
  267. mypid = getpid();
  268. id = get_family_id(nl_sd);
  269. if (!id) {
  270. fprintf(stderr, "Error getting family id, errno %d\n", errno);
  271. goto err;
  272. }
  273. PRINTF("family id %d\n", id);
  274. if (maskset) {
  275. rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
  276. TASKSTATS_CMD_ATTR_REGISTER_CPUMASK,
  277. &cpumask, strlen(cpumask) + 1);
  278. PRINTF("Sent register cpumask, retval %d\n", rc);
  279. if (rc < 0) {
  280. fprintf(stderr, "error sending register cpumask\n");
  281. goto err;
  282. }
  283. }
  284. if (tid) {
  285. rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
  286. cmd_type, &tid, sizeof(__u32));
  287. PRINTF("Sent pid/tgid, retval %d\n", rc);
  288. if (rc < 0) {
  289. fprintf(stderr, "error sending tid/tgid cmd\n");
  290. goto done;
  291. }
  292. }
  293. do {
  294. int i;
  295. rep_len = recv(nl_sd, &msg, sizeof(msg), 0);
  296. PRINTF("received %d bytes\n", rep_len);
  297. if (rep_len < 0) {
  298. fprintf(stderr, "nonfatal reply error: errno %d\n",
  299. errno);
  300. continue;
  301. }
  302. if (msg.n.nlmsg_type == NLMSG_ERROR ||
  303. !NLMSG_OK((&msg.n), rep_len)) {
  304. struct nlmsgerr *err = NLMSG_DATA(&msg);
  305. fprintf(stderr, "fatal reply error, errno %d\n",
  306. err->error);
  307. goto done;
  308. }
  309. PRINTF("nlmsghdr size=%d, nlmsg_len=%d, rep_len=%d\n",
  310. sizeof(struct nlmsghdr), msg.n.nlmsg_len, rep_len);
  311. rep_len = GENLMSG_PAYLOAD(&msg.n);
  312. na = (struct nlattr *) GENLMSG_DATA(&msg);
  313. len = 0;
  314. i = 0;
  315. while (len < rep_len) {
  316. len += NLA_ALIGN(na->nla_len);
  317. switch (na->nla_type) {
  318. case TASKSTATS_TYPE_AGGR_TGID:
  319. /* Fall through */
  320. case TASKSTATS_TYPE_AGGR_PID:
  321. aggr_len = NLA_PAYLOAD(na->nla_len);
  322. len2 = 0;
  323. /* For nested attributes, na follows */
  324. na = (struct nlattr *) NLA_DATA(na);
  325. done = 0;
  326. while (len2 < aggr_len) {
  327. switch (na->nla_type) {
  328. case TASKSTATS_TYPE_PID:
  329. rtid = *(int *) NLA_DATA(na);
  330. if (print_delays)
  331. printf("PID\t%d\n", rtid);
  332. break;
  333. case TASKSTATS_TYPE_TGID:
  334. rtid = *(int *) NLA_DATA(na);
  335. if (print_delays)
  336. printf("TGID\t%d\n", rtid);
  337. break;
  338. case TASKSTATS_TYPE_STATS:
  339. count++;
  340. if (print_delays)
  341. print_delayacct((struct taskstats *) NLA_DATA(na));
  342. if (print_io_accounting)
  343. print_ioacct((struct taskstats *) NLA_DATA(na));
  344. if (fd) {
  345. if (write(fd, NLA_DATA(na), na->nla_len) < 0) {
  346. err(1,"write error\n");
  347. }
  348. }
  349. if (!loop)
  350. goto done;
  351. break;
  352. default:
  353. fprintf(stderr, "Unknown nested"
  354. " nla_type %d\n",
  355. na->nla_type);
  356. break;
  357. }
  358. len2 += NLA_ALIGN(na->nla_len);
  359. na = (struct nlattr *) ((char *) na + len2);
  360. }
  361. break;
  362. default:
  363. fprintf(stderr, "Unknown nla_type %d\n",
  364. na->nla_type);
  365. break;
  366. }
  367. na = (struct nlattr *) (GENLMSG_DATA(&msg) + len);
  368. }
  369. } while (loop);
  370. done:
  371. if (maskset) {
  372. rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
  373. TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK,
  374. &cpumask, strlen(cpumask) + 1);
  375. printf("Sent deregister mask, retval %d\n", rc);
  376. if (rc < 0)
  377. err(rc, "error sending deregister cpumask\n");
  378. }
  379. err:
  380. close(nl_sd);
  381. if (fd)
  382. close(fd);
  383. return 0;
  384. }