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
11 * gcc -I/usr/src/linux/include getdelays.c -o getdelays
21 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <sys/types.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...) \
42 fprintf(stderr, fmt, ##arg); \
53 #define PRINTF(fmt, arg...) { \
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 */
63 /* Maximum length of pathname to log file */
64 #define MAX_FILENAME 256
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)
80 struct sockaddr_nl local;
82 fd = socket(AF_NETLINK, SOCK_RAW, protocol);
87 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
88 &rcvbufsz, sizeof(rcvbufsz)) < 0) {
89 fprintf(stderr, "Unable to set socket rcv buf size "
95 memset(&local, 0, sizeof(local));
96 local.nl_family = AF_NETLINK;
98 if (bind(fd, (struct sockaddr *) &local, sizeof(local)) < 0)
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)
113 struct sockaddr_nl nladdr;
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;
123 msg.n.nlmsg_pid = nlmsg_pid;
124 msg.g.cmd = genl_cmd;
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);
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) {
141 } else if (errno != EAGAIN)
149 * Probe the controller in genetlink to find the family id
150 * for the TASKSTATS family
152 int get_family_id(int sd)
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))
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);
182 void print_delayacct(struct taskstats *t)
184 printf("\n\nCPU %15s%15s%15s%15s\n"
185 " %15llu%15llu%15llu%15llu\n"
190 "count", "real total", "virtual total", "delay total",
191 t->cpu_count, t->cpu_run_real_total, t->cpu_run_virtual_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;
217 struct msgtemplate msg;
220 c = getopt(argc, argv, "dw:r:m:t:p:v:l");
226 printf("print delayacct stats ON\n");
230 strncpy(logfile, optarg, MAX_FILENAME);
231 printf("write to file %s\n", logfile);
235 rcvbufsz = atoi(optarg);
236 printf("receive buf size %d\n", rcvbufsz);
238 err(1, "Invalid rcv buf size\n");
241 strncpy(cpumask, optarg, sizeof(cpumask));
243 printf("cpumask %s maskset %d\n", cpumask, maskset);
248 err(1, "Invalid tgid\n");
249 cmd_type = TASKSTATS_CMD_ATTR_TGID;
255 err(1, "Invalid pid\n");
256 cmd_type = TASKSTATS_CMD_ATTR_PID;
260 printf("debug on\n");
264 printf("listen forever\n");
268 printf("Unknown option %d\n", c);
274 fd = open(logfile, O_WRONLY | O_CREAT | O_TRUNC,
275 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
277 perror("Cannot open output file\n");
282 if ((nl_sd = create_nl_socket(NETLINK_GENERIC)) < 0)
283 err(1, "error creating Netlink socket\n");
287 id = get_family_id(nl_sd);
289 fprintf(stderr, "Error getting family id, errno %d\n", errno);
292 PRINTF("family id %d\n", id);
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);
300 fprintf(stderr, "error sending register cpumask\n");
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);
310 fprintf(stderr, "error sending tid/tgid cmd\n");
318 rep_len = recv(nl_sd, &msg, sizeof(msg), 0);
319 PRINTF("received %d bytes\n", rep_len);
322 fprintf(stderr, "nonfatal reply error: errno %d\n",
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",
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);
343 while (len < rep_len) {
344 len += NLA_ALIGN(na->nla_len);
345 switch (na->nla_type) {
346 case TASKSTATS_TYPE_AGGR_TGID:
348 case TASKSTATS_TYPE_AGGR_PID:
349 aggr_len = NLA_PAYLOAD(na->nla_len);
351 /* For nested attributes, na follows */
352 na = (struct nlattr *) NLA_DATA(na);
354 while (len2 < aggr_len) {
355 switch (na->nla_type) {
356 case TASKSTATS_TYPE_PID:
357 rtid = *(int *) NLA_DATA(na);
359 printf("PID\t%d\n", rtid);
361 case TASKSTATS_TYPE_TGID:
362 rtid = *(int *) NLA_DATA(na);
364 printf("TGID\t%d\n", rtid);
366 case TASKSTATS_TYPE_STATS:
369 print_delayacct((struct taskstats *) NLA_DATA(na));
371 if (write(fd, NLA_DATA(na), na->nla_len) < 0) {
372 err(1,"write error\n");
379 fprintf(stderr, "Unknown nested"
384 len2 += NLA_ALIGN(na->nla_len);
385 na = (struct nlattr *) ((char *) na + len2);
390 fprintf(stderr, "Unknown nla_type %d\n",
394 na = (struct nlattr *) (GENLMSG_DATA(&msg) + len);
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);
404 err(rc, "error sending deregister cpumask\n");