PageRenderTime 32ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Documentation/accounting/getdelays.c

https://github.com/dmitriy103/bravo_kernel-2.6.35
C | 506 lines | 424 code | 54 blank | 28 comment | 59 complexity | 771449b4f4767aadd98c5d91c103e6e5 MD5 | raw file
  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 <signal.h>
  24. #include <linux/genetlink.h>
  25. #include <linux/taskstats.h>
  26. #include <linux/cgroupstats.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. int print_task_context_switch_counts;
  48. __u64 stime, utime;
  49. #define PRINTF(fmt, arg...) { \
  50. if (dbg) { \
  51. printf(fmt, ##arg); \
  52. } \
  53. }
  54. /* Maximum size of response requested or message sent */
  55. #define MAX_MSG_SIZE 1024
  56. /* Maximum number of cpus expected to be specified in a cpumask */
  57. #define MAX_CPUS 32
  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. static void usage(void)
  65. {
  66. fprintf(stderr, "getdelays [-dilv] [-w logfile] [-r bufsize] "
  67. "[-m cpumask] [-t tgid] [-p pid]\n");
  68. fprintf(stderr, " -d: print delayacct stats\n");
  69. fprintf(stderr, " -i: print IO accounting (works only with -p)\n");
  70. fprintf(stderr, " -l: listen forever\n");
  71. fprintf(stderr, " -v: debug on\n");
  72. fprintf(stderr, " -C: container path\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. static 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. static 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 = 0, 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. static 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. "SWAP %15s%15s\n"
  172. " %15llu%15llu\n"
  173. "RECLAIM %12s%15s\n"
  174. " %15llu%15llu\n",
  175. "count", "real total", "virtual total", "delay total",
  176. (unsigned long long)t->cpu_count,
  177. (unsigned long long)t->cpu_run_real_total,
  178. (unsigned long long)t->cpu_run_virtual_total,
  179. (unsigned long long)t->cpu_delay_total,
  180. "count", "delay total",
  181. (unsigned long long)t->blkio_count,
  182. (unsigned long long)t->blkio_delay_total,
  183. "count", "delay total",
  184. (unsigned long long)t->swapin_count,
  185. (unsigned long long)t->swapin_delay_total,
  186. "count", "delay total",
  187. (unsigned long long)t->freepages_count,
  188. (unsigned long long)t->freepages_delay_total);
  189. }
  190. static void task_context_switch_counts(struct taskstats *t)
  191. {
  192. printf("\n\nTask %15s%15s\n"
  193. " %15llu%15llu\n",
  194. "voluntary", "nonvoluntary",
  195. (unsigned long long)t->nvcsw, (unsigned long long)t->nivcsw);
  196. }
  197. static void print_cgroupstats(struct cgroupstats *c)
  198. {
  199. printf("sleeping %llu, blocked %llu, running %llu, stopped %llu, "
  200. "uninterruptible %llu\n", (unsigned long long)c->nr_sleeping,
  201. (unsigned long long)c->nr_io_wait,
  202. (unsigned long long)c->nr_running,
  203. (unsigned long long)c->nr_stopped,
  204. (unsigned long long)c->nr_uninterruptible);
  205. }
  206. static void print_ioacct(struct taskstats *t)
  207. {
  208. printf("%s: read=%llu, write=%llu, cancelled_write=%llu\n",
  209. t->ac_comm,
  210. (unsigned long long)t->read_bytes,
  211. (unsigned long long)t->write_bytes,
  212. (unsigned long long)t->cancelled_write_bytes);
  213. }
  214. int main(int argc, char *argv[])
  215. {
  216. int c, rc, rep_len, aggr_len, len2;
  217. int cmd_type = TASKSTATS_CMD_ATTR_UNSPEC;
  218. __u16 id;
  219. __u32 mypid;
  220. struct nlattr *na;
  221. int nl_sd = -1;
  222. int len = 0;
  223. pid_t tid = 0;
  224. pid_t rtid = 0;
  225. int fd = 0;
  226. int count = 0;
  227. int write_file = 0;
  228. int maskset = 0;
  229. char *logfile = NULL;
  230. int loop = 0;
  231. int containerset = 0;
  232. char containerpath[1024];
  233. int cfd = 0;
  234. struct msgtemplate msg;
  235. while (1) {
  236. c = getopt(argc, argv, "qdiw:r:m:t:p:vlC:");
  237. if (c < 0)
  238. break;
  239. switch (c) {
  240. case 'd':
  241. printf("print delayacct stats ON\n");
  242. print_delays = 1;
  243. break;
  244. case 'i':
  245. printf("printing IO accounting\n");
  246. print_io_accounting = 1;
  247. break;
  248. case 'q':
  249. printf("printing task/process context switch rates\n");
  250. print_task_context_switch_counts = 1;
  251. break;
  252. case 'C':
  253. containerset = 1;
  254. strncpy(containerpath, optarg, strlen(optarg) + 1);
  255. break;
  256. case 'w':
  257. logfile = strdup(optarg);
  258. printf("write to file %s\n", logfile);
  259. write_file = 1;
  260. break;
  261. case 'r':
  262. rcvbufsz = atoi(optarg);
  263. printf("receive buf size %d\n", rcvbufsz);
  264. if (rcvbufsz < 0)
  265. err(1, "Invalid rcv buf size\n");
  266. break;
  267. case 'm':
  268. strncpy(cpumask, optarg, sizeof(cpumask));
  269. maskset = 1;
  270. printf("cpumask %s maskset %d\n", cpumask, maskset);
  271. break;
  272. case 't':
  273. tid = atoi(optarg);
  274. if (!tid)
  275. err(1, "Invalid tgid\n");
  276. cmd_type = TASKSTATS_CMD_ATTR_TGID;
  277. break;
  278. case 'p':
  279. tid = atoi(optarg);
  280. if (!tid)
  281. err(1, "Invalid pid\n");
  282. cmd_type = TASKSTATS_CMD_ATTR_PID;
  283. break;
  284. case 'v':
  285. printf("debug on\n");
  286. dbg = 1;
  287. break;
  288. case 'l':
  289. printf("listen forever\n");
  290. loop = 1;
  291. break;
  292. default:
  293. usage();
  294. exit(-1);
  295. }
  296. }
  297. if (write_file) {
  298. fd = open(logfile, O_WRONLY | O_CREAT | O_TRUNC,
  299. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  300. if (fd == -1) {
  301. perror("Cannot open output file\n");
  302. exit(1);
  303. }
  304. }
  305. if ((nl_sd = create_nl_socket(NETLINK_GENERIC)) < 0)
  306. err(1, "error creating Netlink socket\n");
  307. mypid = getpid();
  308. id = get_family_id(nl_sd);
  309. if (!id) {
  310. fprintf(stderr, "Error getting family id, errno %d\n", errno);
  311. goto err;
  312. }
  313. PRINTF("family id %d\n", id);
  314. if (maskset) {
  315. rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
  316. TASKSTATS_CMD_ATTR_REGISTER_CPUMASK,
  317. &cpumask, strlen(cpumask) + 1);
  318. PRINTF("Sent register cpumask, retval %d\n", rc);
  319. if (rc < 0) {
  320. fprintf(stderr, "error sending register cpumask\n");
  321. goto err;
  322. }
  323. }
  324. if (tid && containerset) {
  325. fprintf(stderr, "Select either -t or -C, not both\n");
  326. goto err;
  327. }
  328. if (tid) {
  329. rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
  330. cmd_type, &tid, sizeof(__u32));
  331. PRINTF("Sent pid/tgid, retval %d\n", rc);
  332. if (rc < 0) {
  333. fprintf(stderr, "error sending tid/tgid cmd\n");
  334. goto done;
  335. }
  336. }
  337. if (containerset) {
  338. cfd = open(containerpath, O_RDONLY);
  339. if (cfd < 0) {
  340. perror("error opening container file");
  341. goto err;
  342. }
  343. rc = send_cmd(nl_sd, id, mypid, CGROUPSTATS_CMD_GET,
  344. CGROUPSTATS_CMD_ATTR_FD, &cfd, sizeof(__u32));
  345. if (rc < 0) {
  346. perror("error sending cgroupstats command");
  347. goto err;
  348. }
  349. }
  350. if (!maskset && !tid && !containerset) {
  351. usage();
  352. goto err;
  353. }
  354. do {
  355. int i;
  356. rep_len = recv(nl_sd, &msg, sizeof(msg), 0);
  357. PRINTF("received %d bytes\n", rep_len);
  358. if (rep_len < 0) {
  359. fprintf(stderr, "nonfatal reply error: errno %d\n",
  360. errno);
  361. continue;
  362. }
  363. if (msg.n.nlmsg_type == NLMSG_ERROR ||
  364. !NLMSG_OK((&msg.n), rep_len)) {
  365. struct nlmsgerr *err = NLMSG_DATA(&msg);
  366. fprintf(stderr, "fatal reply error, errno %d\n",
  367. err->error);
  368. goto done;
  369. }
  370. PRINTF("nlmsghdr size=%zu, nlmsg_len=%d, rep_len=%d\n",
  371. sizeof(struct nlmsghdr), msg.n.nlmsg_len, rep_len);
  372. rep_len = GENLMSG_PAYLOAD(&msg.n);
  373. na = (struct nlattr *) GENLMSG_DATA(&msg);
  374. len = 0;
  375. i = 0;
  376. while (len < rep_len) {
  377. len += NLA_ALIGN(na->nla_len);
  378. switch (na->nla_type) {
  379. case TASKSTATS_TYPE_AGGR_TGID:
  380. /* Fall through */
  381. case TASKSTATS_TYPE_AGGR_PID:
  382. aggr_len = NLA_PAYLOAD(na->nla_len);
  383. len2 = 0;
  384. /* For nested attributes, na follows */
  385. na = (struct nlattr *) NLA_DATA(na);
  386. done = 0;
  387. while (len2 < aggr_len) {
  388. switch (na->nla_type) {
  389. case TASKSTATS_TYPE_PID:
  390. rtid = *(int *) NLA_DATA(na);
  391. if (print_delays)
  392. printf("PID\t%d\n", rtid);
  393. break;
  394. case TASKSTATS_TYPE_TGID:
  395. rtid = *(int *) NLA_DATA(na);
  396. if (print_delays)
  397. printf("TGID\t%d\n", rtid);
  398. break;
  399. case TASKSTATS_TYPE_STATS:
  400. count++;
  401. if (print_delays)
  402. print_delayacct((struct taskstats *) NLA_DATA(na));
  403. if (print_io_accounting)
  404. print_ioacct((struct taskstats *) NLA_DATA(na));
  405. if (print_task_context_switch_counts)
  406. task_context_switch_counts((struct taskstats *) NLA_DATA(na));
  407. if (fd) {
  408. if (write(fd, NLA_DATA(na), na->nla_len) < 0) {
  409. err(1,"write error\n");
  410. }
  411. }
  412. if (!loop)
  413. goto done;
  414. break;
  415. default:
  416. fprintf(stderr, "Unknown nested"
  417. " nla_type %d\n",
  418. na->nla_type);
  419. break;
  420. }
  421. len2 += NLA_ALIGN(na->nla_len);
  422. na = (struct nlattr *) ((char *) na + len2);
  423. }
  424. break;
  425. case CGROUPSTATS_TYPE_CGROUP_STATS:
  426. print_cgroupstats(NLA_DATA(na));
  427. break;
  428. default:
  429. fprintf(stderr, "Unknown nla_type %d\n",
  430. na->nla_type);
  431. break;
  432. }
  433. na = (struct nlattr *) (GENLMSG_DATA(&msg) + len);
  434. }
  435. } while (loop);
  436. done:
  437. if (maskset) {
  438. rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
  439. TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK,
  440. &cpumask, strlen(cpumask) + 1);
  441. printf("Sent deregister mask, retval %d\n", rc);
  442. if (rc < 0)
  443. err(rc, "error sending deregister cpumask\n");
  444. }
  445. err:
  446. close(nl_sd);
  447. if (fd)
  448. close(fd);
  449. if (cfd)
  450. close(cfd);
  451. return 0;
  452. }