getdelays.c 9.9 KB

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