getdelays.c 9.6 KB

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