getdelays.c 9.0 KB

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