getdelays.c 9.2 KB

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