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
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/wait.h>
25#include <signal.h>
26
27#include <linux/genetlink.h>
28#include <linux/taskstats.h>
29#include <linux/cgroupstats.h>
30
31/*
32 * Generic macros for dealing with netlink sockets. Might be duplicated
33 * elsewhere. It is recommended that commercial grade applications use
34 * libnl or libnetlink and use the interfaces provided by the library
35 */
36#define GENLMSG_DATA(glh)	((void *)(NLMSG_DATA(glh) + GENL_HDRLEN))
37#define GENLMSG_PAYLOAD(glh)	(NLMSG_PAYLOAD(glh, 0) - GENL_HDRLEN)
38#define NLA_DATA(na)		((void *)((char*)(na) + NLA_HDRLEN))
39#define NLA_PAYLOAD(len)	(len - NLA_HDRLEN)
40
41#define err(code, fmt, arg...)			\
42	do {					\
43		fprintf(stderr, fmt, ##arg);	\
44		exit(code);			\
45	} while (0)
46
47int done;
48int rcvbufsz;
49char name[100];
50int dbg;
51int print_delays;
52int print_io_accounting;
53int print_task_context_switch_counts;
54
55#define PRINTF(fmt, arg...) {			\
56	    if (dbg) {				\
57		printf(fmt, ##arg);		\
58	    }					\
59	}
60
61/* Maximum size of response requested or message sent */
62#define MAX_MSG_SIZE	1024
63/* Maximum number of cpus expected to be specified in a cpumask */
64#define MAX_CPUS	32
65
66struct msgtemplate {
67	struct nlmsghdr n;
68	struct genlmsghdr g;
69	char buf[MAX_MSG_SIZE];
70};
71
72char cpumask[100+6*MAX_CPUS];
73
74static void usage(void)
75{
76	fprintf(stderr, "getdelays [-dilv] [-w logfile] [-r bufsize] "
77			"[-m cpumask] [-t tgid] [-p pid]\n");
78	fprintf(stderr, "  -d: print delayacct stats\n");
79	fprintf(stderr, "  -i: print IO accounting (works only with -p)\n");
80	fprintf(stderr, "  -l: listen forever\n");
81	fprintf(stderr, "  -v: debug on\n");
82	fprintf(stderr, "  -C: container path\n");
83}
84
85/*
86 * Create a raw netlink socket and bind
87 */
88static int create_nl_socket(int protocol)
89{
90	int fd;
91	struct sockaddr_nl local;
92
93	fd = socket(AF_NETLINK, SOCK_RAW, protocol);
94	if (fd < 0)
95		return -1;
96
97	if (rcvbufsz)
98		if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
99				&rcvbufsz, sizeof(rcvbufsz)) < 0) {
100			fprintf(stderr, "Unable to set socket rcv buf size to %d\n",
101				rcvbufsz);
102			goto error;
103		}
104
105	memset(&local, 0, sizeof(local));
106	local.nl_family = AF_NETLINK;
107
108	if (bind(fd, (struct sockaddr *) &local, sizeof(local)) < 0)
109		goto error;
110
111	return fd;
112error:
113	close(fd);
114	return -1;
115}
116
117
118static int send_cmd(int sd, __u16 nlmsg_type, __u32 nlmsg_pid,
119	     __u8 genl_cmd, __u16 nla_type,
120	     void *nla_data, int nla_len)
121{
122	struct nlattr *na;
123	struct sockaddr_nl nladdr;
124	int r, buflen;
125	char *buf;
126
127	struct msgtemplate msg;
128
129	msg.n.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN);
130	msg.n.nlmsg_type = nlmsg_type;
131	msg.n.nlmsg_flags = NLM_F_REQUEST;
132	msg.n.nlmsg_seq = 0;
133	msg.n.nlmsg_pid = nlmsg_pid;
134	msg.g.cmd = genl_cmd;
135	msg.g.version = 0x1;
136	na = (struct nlattr *) GENLMSG_DATA(&msg);
137	na->nla_type = nla_type;
138	na->nla_len = nla_len + 1 + NLA_HDRLEN;
139	memcpy(NLA_DATA(na), nla_data, nla_len);
140	msg.n.nlmsg_len += NLMSG_ALIGN(na->nla_len);
141
142	buf = (char *) &msg;
143	buflen = msg.n.nlmsg_len ;
144	memset(&nladdr, 0, sizeof(nladdr));
145	nladdr.nl_family = AF_NETLINK;
146	while ((r = sendto(sd, buf, buflen, 0, (struct sockaddr *) &nladdr,
147			   sizeof(nladdr))) < buflen) {
148		if (r > 0) {
149			buf += r;
150			buflen -= r;
151		} else if (errno != EAGAIN)
152			return -1;
153	}
154	return 0;
155}
156
157
158/*
159 * Probe the controller in genetlink to find the family id
160 * for the TASKSTATS family
161 */
162static int get_family_id(int sd)
163{
164	struct {
165		struct nlmsghdr n;
166		struct genlmsghdr g;
167		char buf[256];
168	} ans;
169
170	int id = 0, rc;
171	struct nlattr *na;
172	int rep_len;
173
174	strcpy(name, TASKSTATS_GENL_NAME);
175	rc = send_cmd(sd, GENL_ID_CTRL, getpid(), CTRL_CMD_GETFAMILY,
176			CTRL_ATTR_FAMILY_NAME, (void *)name,
177			strlen(TASKSTATS_GENL_NAME)+1);
178	if (rc < 0)
179		return 0;	/* sendto() failure? */
180
181	rep_len = recv(sd, &ans, sizeof(ans), 0);
182	if (ans.n.nlmsg_type == NLMSG_ERROR ||
183	    (rep_len < 0) || !NLMSG_OK((&ans.n), rep_len))
184		return 0;
185
186	na = (struct nlattr *) GENLMSG_DATA(&ans);
187	na = (struct nlattr *) ((char *) na + NLA_ALIGN(na->nla_len));
188	if (na->nla_type == CTRL_ATTR_FAMILY_ID) {
189		id = *(__u16 *) NLA_DATA(na);
190	}
191	return id;
192}
193
194#define average_ms(t, c) (t / 1000000ULL / (c ? c : 1))
195
196static void print_delayacct(struct taskstats *t)
197{
198	printf("\n\nCPU   %15s%15s%15s%15s%15s\n"
199	       "      %15llu%15llu%15llu%15llu%15.3fms\n"
200	       "IO    %15s%15s%15s\n"
201	       "      %15llu%15llu%15llums\n"
202	       "SWAP  %15s%15s%15s\n"
203	       "      %15llu%15llu%15llums\n"
204	       "RECLAIM  %12s%15s%15s\n"
205	       "      %15llu%15llu%15llums\n",
206	       "count", "real total", "virtual total",
207	       "delay total", "delay average",
208	       (unsigned long long)t->cpu_count,
209	       (unsigned long long)t->cpu_run_real_total,
210	       (unsigned long long)t->cpu_run_virtual_total,
211	       (unsigned long long)t->cpu_delay_total,
212	       average_ms((double)t->cpu_delay_total, t->cpu_count),
213	       "count", "delay total", "delay average",
214	       (unsigned long long)t->blkio_count,
215	       (unsigned long long)t->blkio_delay_total,
216	       average_ms(t->blkio_delay_total, t->blkio_count),
217	       "count", "delay total", "delay average",
218	       (unsigned long long)t->swapin_count,
219	       (unsigned long long)t->swapin_delay_total,
220	       average_ms(t->swapin_delay_total, t->swapin_count),
221	       "count", "delay total", "delay average",
222	       (unsigned long long)t->freepages_count,
223	       (unsigned long long)t->freepages_delay_total,
224	       average_ms(t->freepages_delay_total, t->freepages_count));
225}
226
227static void task_context_switch_counts(struct taskstats *t)
228{
229	printf("\n\nTask   %15s%15s\n"
230	       "       %15llu%15llu\n",
231	       "voluntary", "nonvoluntary",
232	       (unsigned long long)t->nvcsw, (unsigned long long)t->nivcsw);
233}
234
235static void print_cgroupstats(struct cgroupstats *c)
236{
237	printf("sleeping %llu, blocked %llu, running %llu, stopped %llu, "
238		"uninterruptible %llu\n", (unsigned long long)c->nr_sleeping,
239		(unsigned long long)c->nr_io_wait,
240		(unsigned long long)c->nr_running,
241		(unsigned long long)c->nr_stopped,
242		(unsigned long long)c->nr_uninterruptible);
243}
244
245
246static void print_ioacct(struct taskstats *t)
247{
248	printf("%s: read=%llu, write=%llu, cancelled_write=%llu\n",
249		t->ac_comm,
250		(unsigned long long)t->read_bytes,
251		(unsigned long long)t->write_bytes,
252		(unsigned long long)t->cancelled_write_bytes);
253}
254
255int main(int argc, char *argv[])
256{
257	int c, rc, rep_len, aggr_len, len2;
258	int cmd_type = TASKSTATS_CMD_ATTR_UNSPEC;
259	__u16 id;
260	__u32 mypid;
261
262	struct nlattr *na;
263	int nl_sd = -1;
264	int len = 0;
265	pid_t tid = 0;
266	pid_t rtid = 0;
267
268	int fd = 0;
269	int count = 0;
270	int write_file = 0;
271	int maskset = 0;
272	char *logfile = NULL;
273	int loop = 0;
274	int containerset = 0;
275	char *containerpath = NULL;
276	int cfd = 0;
277	int forking = 0;
278	sigset_t sigset;
279
280	struct msgtemplate msg;
281
282	while (!forking) {
283		c = getopt(argc, argv, "qdiw:r:m:t:p:vlC:c:");
284		if (c < 0)
285			break;
286
287		switch (c) {
288		case 'd':
289			printf("print delayacct stats ON\n");
290			print_delays = 1;
291			break;
292		case 'i':
293			printf("printing IO accounting\n");
294			print_io_accounting = 1;
295			break;
296		case 'q':
297			printf("printing task/process context switch rates\n");
298			print_task_context_switch_counts = 1;
299			break;
300		case 'C':
301			containerset = 1;
302			containerpath = optarg;
303			break;
304		case 'w':
305			logfile = strdup(optarg);
306			printf("write to file %s\n", logfile);
307			write_file = 1;
308			break;
309		case 'r':
310			rcvbufsz = atoi(optarg);
311			printf("receive buf size %d\n", rcvbufsz);
312			if (rcvbufsz < 0)
313				err(1, "Invalid rcv buf size\n");
314			break;
315		case 'm':
316			strncpy(cpumask, optarg, sizeof(cpumask));
317			cpumask[sizeof(cpumask) - 1] = '\0';
318			maskset = 1;
319			printf("cpumask %s maskset %d\n", cpumask, maskset);
320			break;
321		case 't':
322			tid = atoi(optarg);
323			if (!tid)
324				err(1, "Invalid tgid\n");
325			cmd_type = TASKSTATS_CMD_ATTR_TGID;
326			break;
327		case 'p':
328			tid = atoi(optarg);
329			if (!tid)
330				err(1, "Invalid pid\n");
331			cmd_type = TASKSTATS_CMD_ATTR_PID;
332			break;
333		case 'c':
334
335			/* Block SIGCHLD for sigwait() later */
336			if (sigemptyset(&sigset) == -1)
337				err(1, "Failed to empty sigset");
338			if (sigaddset(&sigset, SIGCHLD))
339				err(1, "Failed to set sigchld in sigset");
340			sigprocmask(SIG_BLOCK, &sigset, NULL);
341
342			/* fork/exec a child */
343			tid = fork();
344			if (tid < 0)
345				err(1, "Fork failed\n");
346			if (tid == 0)
347				if (execvp(argv[optind - 1],
348				    &argv[optind - 1]) < 0)
349					exit(-1);
350
351			/* Set the command type and avoid further processing */
352			cmd_type = TASKSTATS_CMD_ATTR_PID;
353			forking = 1;
354			break;
355		case 'v':
356			printf("debug on\n");
357			dbg = 1;
358			break;
359		case 'l':
360			printf("listen forever\n");
361			loop = 1;
362			break;
363		default:
364			usage();
365			exit(-1);
366		}
367	}
368
369	if (write_file) {
370		fd = open(logfile, O_WRONLY | O_CREAT | O_TRUNC,
371			  S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
372		if (fd == -1) {
373			perror("Cannot open output file\n");
374			exit(1);
375		}
376	}
377
378	if ((nl_sd = create_nl_socket(NETLINK_GENERIC)) < 0)
379		err(1, "error creating Netlink socket\n");
380
381
382	mypid = getpid();
383	id = get_family_id(nl_sd);
384	if (!id) {
385		fprintf(stderr, "Error getting family id, errno %d\n", errno);
386		goto err;
387	}
388	PRINTF("family id %d\n", id);
389
390	if (maskset) {
391		rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
392			      TASKSTATS_CMD_ATTR_REGISTER_CPUMASK,
393			      &cpumask, strlen(cpumask) + 1);
394		PRINTF("Sent register cpumask, retval %d\n", rc);
395		if (rc < 0) {
396			fprintf(stderr, "error sending register cpumask\n");
397			goto err;
398		}
399	}
400
401	if (tid && containerset) {
402		fprintf(stderr, "Select either -t or -C, not both\n");
403		goto err;
404	}
405
406	/*
407	 * If we forked a child, wait for it to exit. Cannot use waitpid()
408	 * as all the delicious data would be reaped as part of the wait
409	 */
410	if (tid && forking) {
411		int sig_received;
412		sigwait(&sigset, &sig_received);
413	}
414
415	if (tid) {
416		rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
417			      cmd_type, &tid, sizeof(__u32));
418		PRINTF("Sent pid/tgid, retval %d\n", rc);
419		if (rc < 0) {
420			fprintf(stderr, "error sending tid/tgid cmd\n");
421			goto done;
422		}
423	}
424
425	if (containerset) {
426		cfd = open(containerpath, O_RDONLY);
427		if (cfd < 0) {
428			perror("error opening container file");
429			goto err;
430		}
431		rc = send_cmd(nl_sd, id, mypid, CGROUPSTATS_CMD_GET,
432			      CGROUPSTATS_CMD_ATTR_FD, &cfd, sizeof(__u32));
433		if (rc < 0) {
434			perror("error sending cgroupstats command");
435			goto err;
436		}
437	}
438	if (!maskset && !tid && !containerset) {
439		usage();
440		goto err;
441	}
442
443	do {
444		rep_len = recv(nl_sd, &msg, sizeof(msg), 0);
445		PRINTF("received %d bytes\n", rep_len);
446
447		if (rep_len < 0) {
448			fprintf(stderr, "nonfatal reply error: errno %d\n",
449				errno);
450			continue;
451		}
452		if (msg.n.nlmsg_type == NLMSG_ERROR ||
453		    !NLMSG_OK((&msg.n), rep_len)) {
454			struct nlmsgerr *err = NLMSG_DATA(&msg);
455			fprintf(stderr, "fatal reply error,  errno %d\n",
456				err->error);
457			goto done;
458		}
459
460		PRINTF("nlmsghdr size=%zu, nlmsg_len=%d, rep_len=%d\n",
461		       sizeof(struct nlmsghdr), msg.n.nlmsg_len, rep_len);
462
463
464		rep_len = GENLMSG_PAYLOAD(&msg.n);
465
466		na = (struct nlattr *) GENLMSG_DATA(&msg);
467		len = 0;
468		while (len < rep_len) {
469			len += NLA_ALIGN(na->nla_len);
470			switch (na->nla_type) {
471			case TASKSTATS_TYPE_AGGR_TGID:
472				/* Fall through */
473			case TASKSTATS_TYPE_AGGR_PID:
474				aggr_len = NLA_PAYLOAD(na->nla_len);
475				len2 = 0;
476				/* For nested attributes, na follows */
477				na = (struct nlattr *) NLA_DATA(na);
478				done = 0;
479				while (len2 < aggr_len) {
480					switch (na->nla_type) {
481					case TASKSTATS_TYPE_PID:
482						rtid = *(int *) NLA_DATA(na);
483						if (print_delays)
484							printf("PID\t%d\n", rtid);
485						break;
486					case TASKSTATS_TYPE_TGID:
487						rtid = *(int *) NLA_DATA(na);
488						if (print_delays)
489							printf("TGID\t%d\n", rtid);
490						break;
491					case TASKSTATS_TYPE_STATS:
492						count++;
493						if (print_delays)
494							print_delayacct((struct taskstats *) NLA_DATA(na));
495						if (print_io_accounting)
496							print_ioacct((struct taskstats *) NLA_DATA(na));
497						if (print_task_context_switch_counts)
498							task_context_switch_counts((struct taskstats *) NLA_DATA(na));
499						if (fd) {
500							if (write(fd, NLA_DATA(na), na->nla_len) < 0) {
501								err(1,"write error\n");
502							}
503						}
504						if (!loop)
505							goto done;
506						break;
507					default:
508						fprintf(stderr, "Unknown nested"
509							" nla_type %d\n",
510							na->nla_type);
511						break;
512					}
513					len2 += NLA_ALIGN(na->nla_len);
514					na = (struct nlattr *) ((char *) na + len2);
515				}
516				break;
517
518			case CGROUPSTATS_TYPE_CGROUP_STATS:
519				print_cgroupstats(NLA_DATA(na));
520				break;
521			default:
522				fprintf(stderr, "Unknown nla_type %d\n",
523					na->nla_type);
524			case TASKSTATS_TYPE_NULL:
525				break;
526			}
527			na = (struct nlattr *) (GENLMSG_DATA(&msg) + len);
528		}
529	} while (loop);
530done:
531	if (maskset) {
532		rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
533			      TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK,
534			      &cpumask, strlen(cpumask) + 1);
535		printf("Sent deregister mask, retval %d\n", rc);
536		if (rc < 0)
537			err(rc, "error sending deregister cpumask\n");
538	}
539err:
540	close(nl_sd);
541	if (fd)
542		close(fd);
543	if (cfd)
544		close(cfd);
545	return 0;
546}
547