[PATCH] getdelays: various fixes
[linux-2.6/verdex.git] / Documentation / accounting / getdelays.c
blob93dd04f86a27ac3e7db7aa90d1a463d32ebb3091
1 /* getdelays.c
3 * Utility to get per-pid and per-tgid delay accounting statistics
4 * Also illustrates usage of the taskstats interface
6 * Copyright (C) Shailabh Nagar, IBM Corp. 2005
7 * Copyright (C) Balbir Singh, IBM Corp. 2006
8 * Copyright (c) Jay Lan, SGI. 2006
10 * Compile with
11 * gcc -I/usr/src/linux/include getdelays.c -o getdelays
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <errno.h>
17 #include <unistd.h>
18 #include <poll.h>
19 #include <string.h>
20 #include <fcntl.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <sys/socket.h>
24 #include <sys/types.h>
25 #include <signal.h>
27 #include <linux/genetlink.h>
28 #include <linux/taskstats.h>
31 * Generic macros for dealing with netlink sockets. Might be duplicated
32 * elsewhere. It is recommended that commercial grade applications use
33 * libnl or libnetlink and use the interfaces provided by the library
35 #define GENLMSG_DATA(glh) ((void *)(NLMSG_DATA(glh) + GENL_HDRLEN))
36 #define GENLMSG_PAYLOAD(glh) (NLMSG_PAYLOAD(glh, 0) - GENL_HDRLEN)
37 #define NLA_DATA(na) ((void *)((char*)(na) + NLA_HDRLEN))
38 #define NLA_PAYLOAD(len) (len - NLA_HDRLEN)
40 #define err(code, fmt, arg...) \
41 do { \
42 fprintf(stderr, fmt, ##arg); \
43 exit(code); \
44 } while (0)
46 int done;
47 int rcvbufsz;
48 char name[100];
49 int dbg;
50 int print_delays;
51 __u64 stime, utime;
53 #define PRINTF(fmt, arg...) { \
54 if (dbg) { \
55 printf(fmt, ##arg); \
56 } \
59 /* Maximum size of response requested or message sent */
60 #define MAX_MSG_SIZE 1024
61 /* Maximum number of cpus expected to be specified in a cpumask */
62 #define MAX_CPUS 32
63 /* Maximum length of pathname to log file */
64 #define MAX_FILENAME 256
66 struct msgtemplate {
67 struct nlmsghdr n;
68 struct genlmsghdr g;
69 char buf[MAX_MSG_SIZE];
72 char cpumask[100+6*MAX_CPUS];
75 * Create a raw netlink socket and bind
77 static int create_nl_socket(int protocol)
79 int fd;
80 struct sockaddr_nl local;
82 fd = socket(AF_NETLINK, SOCK_RAW, protocol);
83 if (fd < 0)
84 return -1;
86 if (rcvbufsz)
87 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
88 &rcvbufsz, sizeof(rcvbufsz)) < 0) {
89 fprintf(stderr, "Unable to set socket rcv buf size "
90 "to %d\n",
91 rcvbufsz);
92 return -1;
95 memset(&local, 0, sizeof(local));
96 local.nl_family = AF_NETLINK;
98 if (bind(fd, (struct sockaddr *) &local, sizeof(local)) < 0)
99 goto error;
101 return fd;
102 error:
103 close(fd);
104 return -1;
108 int send_cmd(int sd, __u16 nlmsg_type, __u32 nlmsg_pid,
109 __u8 genl_cmd, __u16 nla_type,
110 void *nla_data, int nla_len)
112 struct nlattr *na;
113 struct sockaddr_nl nladdr;
114 int r, buflen;
115 char *buf;
117 struct msgtemplate msg;
119 msg.n.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN);
120 msg.n.nlmsg_type = nlmsg_type;
121 msg.n.nlmsg_flags = NLM_F_REQUEST;
122 msg.n.nlmsg_seq = 0;
123 msg.n.nlmsg_pid = nlmsg_pid;
124 msg.g.cmd = genl_cmd;
125 msg.g.version = 0x1;
126 na = (struct nlattr *) GENLMSG_DATA(&msg);
127 na->nla_type = nla_type;
128 na->nla_len = nla_len + 1 + NLA_HDRLEN;
129 memcpy(NLA_DATA(na), nla_data, nla_len);
130 msg.n.nlmsg_len += NLMSG_ALIGN(na->nla_len);
132 buf = (char *) &msg;
133 buflen = msg.n.nlmsg_len ;
134 memset(&nladdr, 0, sizeof(nladdr));
135 nladdr.nl_family = AF_NETLINK;
136 while ((r = sendto(sd, buf, buflen, 0, (struct sockaddr *) &nladdr,
137 sizeof(nladdr))) < buflen) {
138 if (r > 0) {
139 buf += r;
140 buflen -= r;
141 } else if (errno != EAGAIN)
142 return -1;
144 return 0;
149 * Probe the controller in genetlink to find the family id
150 * for the TASKSTATS family
152 int get_family_id(int sd)
154 struct {
155 struct nlmsghdr n;
156 struct genlmsghdr g;
157 char buf[256];
158 } ans;
160 int id, rc;
161 struct nlattr *na;
162 int rep_len;
164 strcpy(name, TASKSTATS_GENL_NAME);
165 rc = send_cmd(sd, GENL_ID_CTRL, getpid(), CTRL_CMD_GETFAMILY,
166 CTRL_ATTR_FAMILY_NAME, (void *)name,
167 strlen(TASKSTATS_GENL_NAME)+1);
169 rep_len = recv(sd, &ans, sizeof(ans), 0);
170 if (ans.n.nlmsg_type == NLMSG_ERROR ||
171 (rep_len < 0) || !NLMSG_OK((&ans.n), rep_len))
172 return 0;
174 na = (struct nlattr *) GENLMSG_DATA(&ans);
175 na = (struct nlattr *) ((char *) na + NLA_ALIGN(na->nla_len));
176 if (na->nla_type == CTRL_ATTR_FAMILY_ID) {
177 id = *(__u16 *) NLA_DATA(na);
179 return id;
182 void print_delayacct(struct taskstats *t)
184 printf("\n\nCPU %15s%15s%15s%15s\n"
185 " %15llu%15llu%15llu%15llu\n"
186 "IO %15s%15s\n"
187 " %15llu%15llu\n"
188 "MEM %15s%15s\n"
189 " %15llu%15llu\n\n",
190 "count", "real total", "virtual total", "delay total",
191 t->cpu_count, t->cpu_run_real_total, t->cpu_run_virtual_total,
192 t->cpu_delay_total,
193 "count", "delay total",
194 t->blkio_count, t->blkio_delay_total,
195 "count", "delay total", t->swapin_count, t->swapin_delay_total);
198 int main(int argc, char *argv[])
200 int c, rc, rep_len, aggr_len, len2, cmd_type;
201 __u16 id;
202 __u32 mypid;
204 struct nlattr *na;
205 int nl_sd = -1;
206 int len = 0;
207 pid_t tid = 0;
208 pid_t rtid = 0;
210 int fd = 0;
211 int count = 0;
212 int write_file = 0;
213 int maskset = 0;
214 char logfile[128];
215 int loop = 0;
217 struct msgtemplate msg;
219 while (1) {
220 c = getopt(argc, argv, "dw:r:m:t:p:v:l");
221 if (c < 0)
222 break;
224 switch (c) {
225 case 'd':
226 printf("print delayacct stats ON\n");
227 print_delays = 1;
228 break;
229 case 'w':
230 strncpy(logfile, optarg, MAX_FILENAME);
231 printf("write to file %s\n", logfile);
232 write_file = 1;
233 break;
234 case 'r':
235 rcvbufsz = atoi(optarg);
236 printf("receive buf size %d\n", rcvbufsz);
237 if (rcvbufsz < 0)
238 err(1, "Invalid rcv buf size\n");
239 break;
240 case 'm':
241 strncpy(cpumask, optarg, sizeof(cpumask));
242 maskset = 1;
243 printf("cpumask %s maskset %d\n", cpumask, maskset);
244 break;
245 case 't':
246 tid = atoi(optarg);
247 if (!tid)
248 err(1, "Invalid tgid\n");
249 cmd_type = TASKSTATS_CMD_ATTR_TGID;
250 print_delays = 1;
251 break;
252 case 'p':
253 tid = atoi(optarg);
254 if (!tid)
255 err(1, "Invalid pid\n");
256 cmd_type = TASKSTATS_CMD_ATTR_PID;
257 print_delays = 1;
258 break;
259 case 'v':
260 printf("debug on\n");
261 dbg = 1;
262 break;
263 case 'l':
264 printf("listen forever\n");
265 loop = 1;
266 break;
267 default:
268 printf("Unknown option %d\n", c);
269 exit(-1);
273 if (write_file) {
274 fd = open(logfile, O_WRONLY | O_CREAT | O_TRUNC,
275 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
276 if (fd == -1) {
277 perror("Cannot open output file\n");
278 exit(1);
282 if ((nl_sd = create_nl_socket(NETLINK_GENERIC)) < 0)
283 err(1, "error creating Netlink socket\n");
286 mypid = getpid();
287 id = get_family_id(nl_sd);
288 if (!id) {
289 fprintf(stderr, "Error getting family id, errno %d\n", errno);
290 goto err;
292 PRINTF("family id %d\n", id);
294 if (maskset) {
295 rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
296 TASKSTATS_CMD_ATTR_REGISTER_CPUMASK,
297 &cpumask, strlen(cpumask) + 1);
298 PRINTF("Sent register cpumask, retval %d\n", rc);
299 if (rc < 0) {
300 fprintf(stderr, "error sending register cpumask\n");
301 goto err;
305 if (tid) {
306 rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
307 cmd_type, &tid, sizeof(__u32));
308 PRINTF("Sent pid/tgid, retval %d\n", rc);
309 if (rc < 0) {
310 fprintf(stderr, "error sending tid/tgid cmd\n");
311 goto done;
315 do {
316 int i;
318 rep_len = recv(nl_sd, &msg, sizeof(msg), 0);
319 PRINTF("received %d bytes\n", rep_len);
321 if (rep_len < 0) {
322 fprintf(stderr, "nonfatal reply error: errno %d\n",
323 errno);
324 continue;
326 if (msg.n.nlmsg_type == NLMSG_ERROR ||
327 !NLMSG_OK((&msg.n), rep_len)) {
328 struct nlmsgerr *err = NLMSG_DATA(&msg);
329 fprintf(stderr, "fatal reply error, errno %d\n",
330 err->error);
331 goto done;
334 PRINTF("nlmsghdr size=%d, nlmsg_len=%d, rep_len=%d\n",
335 sizeof(struct nlmsghdr), msg.n.nlmsg_len, rep_len);
338 rep_len = GENLMSG_PAYLOAD(&msg.n);
340 na = (struct nlattr *) GENLMSG_DATA(&msg);
341 len = 0;
342 i = 0;
343 while (len < rep_len) {
344 len += NLA_ALIGN(na->nla_len);
345 switch (na->nla_type) {
346 case TASKSTATS_TYPE_AGGR_TGID:
347 /* Fall through */
348 case TASKSTATS_TYPE_AGGR_PID:
349 aggr_len = NLA_PAYLOAD(na->nla_len);
350 len2 = 0;
351 /* For nested attributes, na follows */
352 na = (struct nlattr *) NLA_DATA(na);
353 done = 0;
354 while (len2 < aggr_len) {
355 switch (na->nla_type) {
356 case TASKSTATS_TYPE_PID:
357 rtid = *(int *) NLA_DATA(na);
358 if (print_delays)
359 printf("PID\t%d\n", rtid);
360 break;
361 case TASKSTATS_TYPE_TGID:
362 rtid = *(int *) NLA_DATA(na);
363 if (print_delays)
364 printf("TGID\t%d\n", rtid);
365 break;
366 case TASKSTATS_TYPE_STATS:
367 count++;
368 if (print_delays)
369 print_delayacct((struct taskstats *) NLA_DATA(na));
370 if (fd) {
371 if (write(fd, NLA_DATA(na), na->nla_len) < 0) {
372 err(1,"write error\n");
375 if (!loop)
376 goto done;
377 break;
378 default:
379 fprintf(stderr, "Unknown nested"
380 " nla_type %d\n",
381 na->nla_type);
382 break;
384 len2 += NLA_ALIGN(na->nla_len);
385 na = (struct nlattr *) ((char *) na + len2);
387 break;
389 default:
390 fprintf(stderr, "Unknown nla_type %d\n",
391 na->nla_type);
392 break;
394 na = (struct nlattr *) (GENLMSG_DATA(&msg) + len);
396 } while (loop);
397 done:
398 if (maskset) {
399 rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
400 TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK,
401 &cpumask, strlen(cpumask) + 1);
402 printf("Sent deregister mask, retval %d\n", rc);
403 if (rc < 0)
404 err(rc, "error sending deregister cpumask\n");
406 err:
407 close(nl_sd);
408 if (fd)
409 close(fd);
410 return 0;