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