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
19 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <sys/types.h>
25 #include <linux/genetlink.h>
26 #include <linux/taskstats.h>
29 * Generic macros for dealing with netlink sockets. Might be duplicated
30 * elsewhere. It is recommended that commercial grade applications use
31 * libnl or libnetlink and use the interfaces provided by the library
33 #define GENLMSG_DATA(glh) ((void *)(NLMSG_DATA(glh) + GENL_HDRLEN))
34 #define GENLMSG_PAYLOAD(glh) (NLMSG_PAYLOAD(glh, 0) - GENL_HDRLEN)
35 #define NLA_DATA(na) ((void *)((char*)(na) + NLA_HDRLEN))
36 #define NLA_PAYLOAD(len) (len - NLA_HDRLEN)
38 #define err(code, fmt, arg...) do { printf(fmt, ##arg); exit(code); } while (0)
43 int dbg
=0, print_delays
=0;
45 #define PRINTF(fmt, arg...) { \
51 /* Maximum size of response requested or message sent */
52 #define MAX_MSG_SIZE 1024
53 /* Maximum number of cpus expected to be specified in a cpumask */
55 /* Maximum length of pathname to log file */
56 #define MAX_FILENAME 256
61 char buf
[MAX_MSG_SIZE
];
64 char cpumask
[100+6*MAX_CPUS
];
67 * Create a raw netlink socket and bind
69 static int create_nl_socket(int protocol
)
72 struct sockaddr_nl local
;
74 fd
= socket(AF_NETLINK
, SOCK_RAW
, protocol
);
79 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
,
80 &rcvbufsz
, sizeof(rcvbufsz
)) < 0) {
81 printf("Unable to set socket rcv buf size to %d\n",
86 memset(&local
, 0, sizeof(local
));
87 local
.nl_family
= AF_NETLINK
;
89 if (bind(fd
, (struct sockaddr
*) &local
, sizeof(local
)) < 0)
99 int send_cmd(int sd
, __u16 nlmsg_type
, __u32 nlmsg_pid
,
100 __u8 genl_cmd
, __u16 nla_type
,
101 void *nla_data
, int nla_len
)
104 struct sockaddr_nl nladdr
;
108 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
;
114 msg
.n
.nlmsg_pid
= nlmsg_pid
;
115 msg
.g
.cmd
= genl_cmd
;
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
);
124 buflen
= msg
.n
.nlmsg_len
;
125 memset(&nladdr
, 0, sizeof(nladdr
));
126 nladdr
.nl_family
= AF_NETLINK
;
127 while ((r
= sendto(sd
, buf
, buflen
, 0, (struct sockaddr
*) &nladdr
,
128 sizeof(nladdr
))) < buflen
) {
132 } else if (errno
!= EAGAIN
)
140 * Probe the controller in genetlink to find the family id
141 * for the TASKSTATS family
143 int get_family_id(int sd
)
155 strcpy(name
, TASKSTATS_GENL_NAME
);
156 rc
= send_cmd(sd
, GENL_ID_CTRL
, getpid(), CTRL_CMD_GETFAMILY
,
157 CTRL_ATTR_FAMILY_NAME
, (void *)name
,
158 strlen(TASKSTATS_GENL_NAME
)+1);
160 rep_len
= recv(sd
, &ans
, sizeof(ans
), 0);
161 if (ans
.n
.nlmsg_type
== NLMSG_ERROR
||
162 (rep_len
< 0) || !NLMSG_OK((&ans
.n
), rep_len
))
165 na
= (struct nlattr
*) GENLMSG_DATA(&ans
);
166 na
= (struct nlattr
*) ((char *) na
+ NLA_ALIGN(na
->nla_len
));
167 if (na
->nla_type
== CTRL_ATTR_FAMILY_ID
) {
168 id
= *(__u16
*) NLA_DATA(na
);
173 void print_delayacct(struct taskstats
*t
)
175 printf("\n\nCPU %15s%15s%15s%15s\n"
176 " %15llu%15llu%15llu%15llu\n"
181 "count", "real total", "virtual total", "delay total",
182 t
->cpu_count
, t
->cpu_run_real_total
, t
->cpu_run_virtual_total
,
184 "count", "delay total",
185 t
->blkio_count
, t
->blkio_delay_total
,
186 "count", "delay total", t
->swapin_count
, t
->swapin_delay_total
);
189 int main(int argc
, char *argv
[])
191 int c
, rc
, rep_len
, aggr_len
, len2
, cmd_type
;
208 struct msgtemplate msg
;
211 c
= getopt(argc
, argv
, "dw:r:m:t:p:v:l");
217 printf("print delayacct stats ON\n");
221 strncpy(logfile
, optarg
, MAX_FILENAME
);
222 printf("write to file %s\n", logfile
);
226 rcvbufsz
= atoi(optarg
);
227 printf("receive buf size %d\n", rcvbufsz
);
229 err(1, "Invalid rcv buf size\n");
232 strncpy(cpumask
, optarg
, sizeof(cpumask
));
234 printf("cpumask %s maskset %d\n", cpumask
, maskset
);
239 err(1, "Invalid tgid\n");
240 cmd_type
= TASKSTATS_CMD_ATTR_TGID
;
246 err(1, "Invalid pid\n");
247 cmd_type
= TASKSTATS_CMD_ATTR_PID
;
251 printf("debug on\n");
255 printf("listen forever\n");
259 printf("Unknown option %d\n", c
);
265 fd
= open(logfile
, O_WRONLY
| O_CREAT
| O_TRUNC
,
266 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
);
268 perror("Cannot open output file\n");
273 if ((nl_sd
= create_nl_socket(NETLINK_GENERIC
)) < 0)
274 err(1, "error creating Netlink socket\n");
278 id
= get_family_id(nl_sd
);
280 printf("Error getting family id, errno %d", errno
);
283 PRINTF("family id %d\n", id
);
286 rc
= send_cmd(nl_sd
, id
, mypid
, TASKSTATS_CMD_GET
,
287 TASKSTATS_CMD_ATTR_REGISTER_CPUMASK
,
288 &cpumask
, strlen(cpumask
) + 1);
289 PRINTF("Sent register cpumask, retval %d\n", rc
);
291 printf("error sending register cpumask\n");
297 rc
= send_cmd(nl_sd
, id
, mypid
, TASKSTATS_CMD_GET
,
298 cmd_type
, &tid
, sizeof(__u32
));
299 PRINTF("Sent pid/tgid, retval %d\n", rc
);
301 printf("error sending tid/tgid cmd\n");
309 rep_len
= recv(nl_sd
, &msg
, sizeof(msg
), 0);
310 PRINTF("received %d bytes\n", rep_len
);
313 printf("nonfatal reply error: errno %d\n", errno
);
316 if (msg
.n
.nlmsg_type
== NLMSG_ERROR
||
317 !NLMSG_OK((&msg
.n
), rep_len
)) {
318 struct nlmsgerr
*err
= NLMSG_DATA(&msg
);
319 printf("fatal reply error, errno %d\n", err
->error
);
323 PRINTF("nlmsghdr size=%d, nlmsg_len=%d, rep_len=%d\n",
324 sizeof(struct nlmsghdr
), msg
.n
.nlmsg_len
, rep_len
);
327 rep_len
= GENLMSG_PAYLOAD(&msg
.n
);
329 na
= (struct nlattr
*) GENLMSG_DATA(&msg
);
332 while (len
< rep_len
) {
333 len
+= NLA_ALIGN(na
->nla_len
);
334 switch (na
->nla_type
) {
335 case TASKSTATS_TYPE_AGGR_TGID
:
337 case TASKSTATS_TYPE_AGGR_PID
:
338 aggr_len
= NLA_PAYLOAD(na
->nla_len
);
340 /* For nested attributes, na follows */
341 na
= (struct nlattr
*) NLA_DATA(na
);
343 while (len2
< aggr_len
) {
344 switch (na
->nla_type
) {
345 case TASKSTATS_TYPE_PID
:
346 rtid
= *(int *) NLA_DATA(na
);
348 printf("PID\t%d\n", rtid
);
350 case TASKSTATS_TYPE_TGID
:
351 rtid
= *(int *) NLA_DATA(na
);
353 printf("TGID\t%d\n", rtid
);
355 case TASKSTATS_TYPE_STATS
:
358 print_delayacct((struct taskstats
*) NLA_DATA(na
));
360 if (write(fd
, NLA_DATA(na
), na
->nla_len
) < 0) {
361 err(1,"write error\n");
368 printf("Unknown nested nla_type %d\n", na
->nla_type
);
371 len2
+= NLA_ALIGN(na
->nla_len
);
372 na
= (struct nlattr
*) ((char *) na
+ len2
);
377 printf("Unknown nla_type %d\n", na
->nla_type
);
380 na
= (struct nlattr
*) (GENLMSG_DATA(&msg
) + len
);
385 rc
= send_cmd(nl_sd
, id
, mypid
, TASKSTATS_CMD_GET
,
386 TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK
,
387 &cpumask
, strlen(cpumask
) + 1);
388 printf("Sent deregister mask, retval %d\n", rc
);
390 err(rc
, "error sending deregister cpumask\n");