getdelays.c 11.9 KB
Newer Older
1 2 3 4 5 6 7
/* getdelays.c
 *
 * Utility to get per-pid and per-tgid delay accounting statistics
 * Also illustrates usage of the taskstats interface
 *
 * Copyright (C) Shailabh Nagar, IBM Corp. 2005
 * Copyright (C) Balbir Singh, IBM Corp. 2006
8
 * Copyright (c) Jay Lan, SGI. 2006
9
 *
A
Andrew Morton 已提交
10 11
 * Compile with
 *	gcc -I/usr/src/linux/include getdelays.c -o getdelays
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <poll.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <signal.h>

#include <linux/genetlink.h>
#include <linux/taskstats.h>
B
Balbir Singh 已提交
28
#include <linux/cgroupstats.h>
29 30 31 32 33 34 35 36 37 38 39

/*
 * Generic macros for dealing with netlink sockets. Might be duplicated
 * elsewhere. It is recommended that commercial grade applications use
 * libnl or libnetlink and use the interfaces provided by the library
 */
#define GENLMSG_DATA(glh)	((void *)(NLMSG_DATA(glh) + GENL_HDRLEN))
#define GENLMSG_PAYLOAD(glh)	(NLMSG_PAYLOAD(glh, 0) - GENL_HDRLEN)
#define NLA_DATA(na)		((void *)((char*)(na) + NLA_HDRLEN))
#define NLA_PAYLOAD(len)	(len - NLA_HDRLEN)

A
Andrew Morton 已提交
40 41 42 43 44 45 46 47 48 49 50
#define err(code, fmt, arg...)			\
	do {					\
		fprintf(stderr, fmt, ##arg);	\
		exit(code);			\
	} while (0)

int done;
int rcvbufsz;
char name[100];
int dbg;
int print_delays;
51
int print_io_accounting;
52
int print_task_context_switch_counts;
53
__u64 stime, utime;
A
Andrew Morton 已提交
54

55 56 57 58 59 60 61
#define PRINTF(fmt, arg...) {			\
	    if (dbg) {				\
		printf(fmt, ##arg);		\
	    }					\
	}

/* Maximum size of response requested or message sent */
62
#define MAX_MSG_SIZE	1024
63 64 65 66 67 68 69 70 71 72
/* Maximum number of cpus expected to be specified in a cpumask */
#define MAX_CPUS	32

struct msgtemplate {
	struct nlmsghdr n;
	struct genlmsghdr g;
	char buf[MAX_MSG_SIZE];
};

char cpumask[100+6*MAX_CPUS];
73

74 75 76 77 78 79 80 81
static void usage(void)
{
	fprintf(stderr, "getdelays [-dilv] [-w logfile] [-r bufsize] "
			"[-m cpumask] [-t tgid] [-p pid]\n");
	fprintf(stderr, "  -d: print delayacct stats\n");
	fprintf(stderr, "  -i: print IO accounting (works only with -p)\n");
	fprintf(stderr, "  -l: listen forever\n");
	fprintf(stderr, "  -v: debug on\n");
B
Balbir Singh 已提交
82
	fprintf(stderr, "  -C: container path\n");
83 84
}

85 86 87
/*
 * Create a raw netlink socket and bind
 */
88
static int create_nl_socket(int protocol)
89
{
90 91 92 93 94 95 96 97 98 99
	int fd;
	struct sockaddr_nl local;

	fd = socket(AF_NETLINK, SOCK_RAW, protocol);
	if (fd < 0)
		return -1;

	if (rcvbufsz)
		if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
				&rcvbufsz, sizeof(rcvbufsz)) < 0) {
A
Andrew Morton 已提交
100 101 102
			fprintf(stderr, "Unable to set socket rcv buf size "
					"to %d\n",
				rcvbufsz);
103 104
			return -1;
		}
105

106 107
	memset(&local, 0, sizeof(local));
	local.nl_family = AF_NETLINK;
108

109 110
	if (bind(fd, (struct sockaddr *) &local, sizeof(local)) < 0)
		goto error;
111

112 113 114 115
	return fd;
error:
	close(fd);
	return -1;
116 117
}

118 119 120 121

int send_cmd(int sd, __u16 nlmsg_type, __u32 nlmsg_pid,
	     __u8 genl_cmd, __u16 nla_type,
	     void *nla_data, int nla_len)
122
{
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	struct nlattr *na;
	struct sockaddr_nl nladdr;
	int r, buflen;
	char *buf;

	struct msgtemplate msg;

	msg.n.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN);
	msg.n.nlmsg_type = nlmsg_type;
	msg.n.nlmsg_flags = NLM_F_REQUEST;
	msg.n.nlmsg_seq = 0;
	msg.n.nlmsg_pid = nlmsg_pid;
	msg.g.cmd = genl_cmd;
	msg.g.version = 0x1;
	na = (struct nlattr *) GENLMSG_DATA(&msg);
	na->nla_type = nla_type;
	na->nla_len = nla_len + 1 + NLA_HDRLEN;
	memcpy(NLA_DATA(na), nla_data, nla_len);
	msg.n.nlmsg_len += NLMSG_ALIGN(na->nla_len);

	buf = (char *) &msg;
	buflen = msg.n.nlmsg_len ;
	memset(&nladdr, 0, sizeof(nladdr));
	nladdr.nl_family = AF_NETLINK;
	while ((r = sendto(sd, buf, buflen, 0, (struct sockaddr *) &nladdr,
			   sizeof(nladdr))) < buflen) {
		if (r > 0) {
			buf += r;
			buflen -= r;
		} else if (errno != EAGAIN)
			return -1;
	}
	return 0;
156 157
}

158

159 160 161 162 163 164
/*
 * Probe the controller in genetlink to find the family id
 * for the TASKSTATS family
 */
int get_family_id(int sd)
{
165 166 167 168 169 170
	struct {
		struct nlmsghdr n;
		struct genlmsghdr g;
		char buf[256];
	} ans;

R
Randy Dunlap 已提交
171
	int id = 0, rc;
172 173 174 175 176 177 178 179 180 181 182 183
	struct nlattr *na;
	int rep_len;

	strcpy(name, TASKSTATS_GENL_NAME);
	rc = send_cmd(sd, GENL_ID_CTRL, getpid(), CTRL_CMD_GETFAMILY,
			CTRL_ATTR_FAMILY_NAME, (void *)name,
			strlen(TASKSTATS_GENL_NAME)+1);

	rep_len = recv(sd, &ans, sizeof(ans), 0);
	if (ans.n.nlmsg_type == NLMSG_ERROR ||
	    (rep_len < 0) || !NLMSG_OK((&ans.n), rep_len))
		return 0;
184

185 186 187 188 189 190
	na = (struct nlattr *) GENLMSG_DATA(&ans);
	na = (struct nlattr *) ((char *) na + NLA_ALIGN(na->nla_len));
	if (na->nla_type == CTRL_ATTR_FAMILY_ID) {
		id = *(__u16 *) NLA_DATA(na);
	}
	return id;
191 192
}

193
void print_delayacct(struct taskstats *t)
194
{
195 196 197 198
	printf("\n\nCPU   %15s%15s%15s%15s\n"
	       "      %15llu%15llu%15llu%15llu\n"
	       "IO    %15s%15s\n"
	       "      %15llu%15llu\n"
199 200 201
	       "SWAP  %15s%15s\n"
	       "      %15llu%15llu\n"
	       "RECLAIM  %12s%15s\n"
202
	       "      %15llu%15llu\n",
203
	       "count", "real total", "virtual total", "delay total",
204 205 206 207
	       (unsigned long long)t->cpu_count,
	       (unsigned long long)t->cpu_run_real_total,
	       (unsigned long long)t->cpu_run_virtual_total,
	       (unsigned long long)t->cpu_delay_total,
208
	       "count", "delay total",
209 210
	       (unsigned long long)t->blkio_count,
	       (unsigned long long)t->blkio_delay_total,
211
	       "count", "delay total",
212 213 214 215 216
	       (unsigned long long)t->swapin_count,
	       (unsigned long long)t->swapin_delay_total,
	       "count", "delay total",
	       (unsigned long long)t->freepages_count,
	       (unsigned long long)t->freepages_delay_total);
217 218
}

219 220 221
void task_context_switch_counts(struct taskstats *t)
{
	printf("\n\nTask   %15s%15s\n"
R
Randy Dunlap 已提交
222
	       "       %15llu%15llu\n",
223
	       "voluntary", "nonvoluntary",
224
	       (unsigned long long)t->nvcsw, (unsigned long long)t->nivcsw);
225 226
}

B
Balbir Singh 已提交
227 228 229
void print_cgroupstats(struct cgroupstats *c)
{
	printf("sleeping %llu, blocked %llu, running %llu, stopped %llu, "
230 231 232 233 234
		"uninterruptible %llu\n", (unsigned long long)c->nr_sleeping,
		(unsigned long long)c->nr_io_wait,
		(unsigned long long)c->nr_running,
		(unsigned long long)c->nr_stopped,
		(unsigned long long)c->nr_uninterruptible);
B
Balbir Singh 已提交
235 236 237
}


238 239 240 241 242 243 244 245 246
void print_ioacct(struct taskstats *t)
{
	printf("%s: read=%llu, write=%llu, cancelled_write=%llu\n",
		t->ac_comm,
		(unsigned long long)t->read_bytes,
		(unsigned long long)t->write_bytes,
		(unsigned long long)t->cancelled_write_bytes);
}

247 248
int main(int argc, char *argv[])
{
249 250 251 252 253 254 255 256 257 258 259 260 261 262
	int c, rc, rep_len, aggr_len, len2, cmd_type;
	__u16 id;
	__u32 mypid;

	struct nlattr *na;
	int nl_sd = -1;
	int len = 0;
	pid_t tid = 0;
	pid_t rtid = 0;

	int fd = 0;
	int count = 0;
	int write_file = 0;
	int maskset = 0;
S
Scott Wiersdorf 已提交
263
	char *logfile = NULL;
264
	int loop = 0;
B
Balbir Singh 已提交
265 266 267
	int containerset = 0;
	char containerpath[1024];
	int cfd = 0;
268 269 270 271

	struct msgtemplate msg;

	while (1) {
B
Balbir Singh 已提交
272
		c = getopt(argc, argv, "qdiw:r:m:t:p:vlC:");
273 274
		if (c < 0)
			break;
275

276 277 278 279 280
		switch (c) {
		case 'd':
			printf("print delayacct stats ON\n");
			print_delays = 1;
			break;
281 282 283 284
		case 'i':
			printf("printing IO accounting\n");
			print_io_accounting = 1;
			break;
285 286 287 288
		case 'q':
			printf("printing task/process context switch rates\n");
			print_task_context_switch_counts = 1;
			break;
B
Balbir Singh 已提交
289 290 291 292
		case 'C':
			containerset = 1;
			strncpy(containerpath, optarg, strlen(optarg) + 1);
			break;
293
		case 'w':
S
Scott Wiersdorf 已提交
294
			logfile = strdup(optarg);
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
			printf("write to file %s\n", logfile);
			write_file = 1;
			break;
		case 'r':
			rcvbufsz = atoi(optarg);
			printf("receive buf size %d\n", rcvbufsz);
			if (rcvbufsz < 0)
				err(1, "Invalid rcv buf size\n");
			break;
		case 'm':
			strncpy(cpumask, optarg, sizeof(cpumask));
			maskset = 1;
			printf("cpumask %s maskset %d\n", cpumask, maskset);
			break;
		case 't':
			tid = atoi(optarg);
			if (!tid)
				err(1, "Invalid tgid\n");
			cmd_type = TASKSTATS_CMD_ATTR_TGID;
			break;
		case 'p':
			tid = atoi(optarg);
			if (!tid)
				err(1, "Invalid pid\n");
			cmd_type = TASKSTATS_CMD_ATTR_PID;
			break;
		case 'v':
			printf("debug on\n");
			dbg = 1;
			break;
		case 'l':
			printf("listen forever\n");
			loop = 1;
			break;
		default:
330
			usage();
331
			exit(-1);
332 333 334
		}
	}

335 336 337 338 339 340 341 342
	if (write_file) {
		fd = open(logfile, O_WRONLY | O_CREAT | O_TRUNC,
			  S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
		if (fd == -1) {
			perror("Cannot open output file\n");
			exit(1);
		}
	}
343

344 345
	if ((nl_sd = create_nl_socket(NETLINK_GENERIC)) < 0)
		err(1, "error creating Netlink socket\n");
346 347


348 349 350
	mypid = getpid();
	id = get_family_id(nl_sd);
	if (!id) {
A
Andrew Morton 已提交
351
		fprintf(stderr, "Error getting family id, errno %d\n", errno);
352
		goto err;
353
	}
354 355 356 357 358
	PRINTF("family id %d\n", id);

	if (maskset) {
		rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
			      TASKSTATS_CMD_ATTR_REGISTER_CPUMASK,
359
			      &cpumask, strlen(cpumask) + 1);
360 361
		PRINTF("Sent register cpumask, retval %d\n", rc);
		if (rc < 0) {
A
Andrew Morton 已提交
362
			fprintf(stderr, "error sending register cpumask\n");
363 364
			goto err;
		}
365 366
	}

B
Balbir Singh 已提交
367 368 369 370 371
	if (tid && containerset) {
		fprintf(stderr, "Select either -t or -C, not both\n");
		goto err;
	}

372 373 374 375 376
	if (tid) {
		rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
			      cmd_type, &tid, sizeof(__u32));
		PRINTF("Sent pid/tgid, retval %d\n", rc);
		if (rc < 0) {
A
Andrew Morton 已提交
377
			fprintf(stderr, "error sending tid/tgid cmd\n");
378 379
			goto done;
		}
380 381
	}

B
Balbir Singh 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394
	if (containerset) {
		cfd = open(containerpath, O_RDONLY);
		if (cfd < 0) {
			perror("error opening container file");
			goto err;
		}
		rc = send_cmd(nl_sd, id, mypid, CGROUPSTATS_CMD_GET,
			      CGROUPSTATS_CMD_ATTR_FD, &cfd, sizeof(__u32));
		if (rc < 0) {
			perror("error sending cgroupstats command");
			goto err;
		}
	}
395 396 397 398
	if (!maskset && !tid && !containerset) {
		usage();
		goto err;
	}
B
Balbir Singh 已提交
399

400 401
	do {
		int i;
402

403 404
		rep_len = recv(nl_sd, &msg, sizeof(msg), 0);
		PRINTF("received %d bytes\n", rep_len);
405

406
		if (rep_len < 0) {
A
Andrew Morton 已提交
407 408
			fprintf(stderr, "nonfatal reply error: errno %d\n",
				errno);
409 410 411 412
			continue;
		}
		if (msg.n.nlmsg_type == NLMSG_ERROR ||
		    !NLMSG_OK((&msg.n), rep_len)) {
413
			struct nlmsgerr *err = NLMSG_DATA(&msg);
A
Andrew Morton 已提交
414 415
			fprintf(stderr, "fatal reply error,  errno %d\n",
				err->error);
416 417 418
			goto done;
		}

R
Randy Dunlap 已提交
419
		PRINTF("nlmsghdr size=%zu, nlmsg_len=%d, rep_len=%d\n",
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
		       sizeof(struct nlmsghdr), msg.n.nlmsg_len, rep_len);


		rep_len = GENLMSG_PAYLOAD(&msg.n);

		na = (struct nlattr *) GENLMSG_DATA(&msg);
		len = 0;
		i = 0;
		while (len < rep_len) {
			len += NLA_ALIGN(na->nla_len);
			switch (na->nla_type) {
			case TASKSTATS_TYPE_AGGR_TGID:
				/* Fall through */
			case TASKSTATS_TYPE_AGGR_PID:
				aggr_len = NLA_PAYLOAD(na->nla_len);
				len2 = 0;
				/* For nested attributes, na follows */
				na = (struct nlattr *) NLA_DATA(na);
				done = 0;
				while (len2 < aggr_len) {
					switch (na->nla_type) {
					case TASKSTATS_TYPE_PID:
						rtid = *(int *) NLA_DATA(na);
						if (print_delays)
							printf("PID\t%d\n", rtid);
						break;
					case TASKSTATS_TYPE_TGID:
						rtid = *(int *) NLA_DATA(na);
						if (print_delays)
							printf("TGID\t%d\n", rtid);
						break;
					case TASKSTATS_TYPE_STATS:
						count++;
						if (print_delays)
							print_delayacct((struct taskstats *) NLA_DATA(na));
455 456
						if (print_io_accounting)
							print_ioacct((struct taskstats *) NLA_DATA(na));
457 458
						if (print_task_context_switch_counts)
							task_context_switch_counts((struct taskstats *) NLA_DATA(na));
459 460 461 462 463 464 465 466 467
						if (fd) {
							if (write(fd, NLA_DATA(na), na->nla_len) < 0) {
								err(1,"write error\n");
							}
						}
						if (!loop)
							goto done;
						break;
					default:
A
Andrew Morton 已提交
468 469 470
						fprintf(stderr, "Unknown nested"
							" nla_type %d\n",
							na->nla_type);
471 472 473 474 475 476 477
						break;
					}
					len2 += NLA_ALIGN(na->nla_len);
					na = (struct nlattr *) ((char *) na + len2);
				}
				break;

B
Balbir Singh 已提交
478 479 480
			case CGROUPSTATS_TYPE_CGROUP_STATS:
				print_cgroupstats(NLA_DATA(na));
				break;
481
			default:
A
Andrew Morton 已提交
482 483
				fprintf(stderr, "Unknown nla_type %d\n",
					na->nla_type);
484
				break;
485
			}
486
			na = (struct nlattr *) (GENLMSG_DATA(&msg) + len);
487
		}
488 489 490 491 492
	} while (loop);
done:
	if (maskset) {
		rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
			      TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK,
493
			      &cpumask, strlen(cpumask) + 1);
494 495 496
		printf("Sent deregister mask, retval %d\n", rc);
		if (rc < 0)
			err(rc, "error sending deregister cpumask\n");
497
	}
498 499 500 501
err:
	close(nl_sd);
	if (fd)
		close(fd);
B
Balbir Singh 已提交
502 503
	if (cfd)
		close(cfd);
504
	return 0;
505
}