Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.15.
  1// SPDX-License-Identifier: GPL-2.0
  2/* procacct.c
  3 *
  4 * Demonstrator of fetching resource data on task exit, as a way
  5 * to accumulate accurate program resource usage statistics, without
  6 * prior identification of the programs. For that, the fields for
  7 * device and inode of the program executable binary file are also
  8 * extracted in addition to the command string.
  9 *
 10 * The TGID together with the PID and the AGROUP flag allow
 11 * identification of threads in a process and single-threaded processes.
 12 * The ac_tgetime field gives proper whole-process walltime.
 13 *
 14 * Written (changed) by Thomas Orgis, University of Hamburg in 2022
 15 *
 16 * This is a cheap derivation (inheriting the style) of getdelays.c:
 17 *
 18 * Utility to get per-pid and per-tgid delay accounting statistics
 19 * Also illustrates usage of the taskstats interface
 20 *
 21 * Copyright (C) Shailabh Nagar, IBM Corp. 2005
 22 * Copyright (C) Balbir Singh, IBM Corp. 2006
 23 * Copyright (c) Jay Lan, SGI. 2006
 24 */
 25
 26#include <stdio.h>
 27#include <stdlib.h>
 28#include <errno.h>
 29#include <unistd.h>
 30#include <poll.h>
 31#include <string.h>
 32#include <fcntl.h>
 33#include <sys/types.h>
 34#include <sys/stat.h>
 35#include <sys/socket.h>
 36#include <sys/wait.h>
 37#include <signal.h>
 38
 39#include <linux/genetlink.h>
 40#include <linux/acct.h>
 41#include <linux/taskstats.h>
 42#include <linux/kdev_t.h>
 43
 44/*
 45 * Generic macros for dealing with netlink sockets. Might be duplicated
 46 * elsewhere. It is recommended that commercial grade applications use
 47 * libnl or libnetlink and use the interfaces provided by the library
 48 */
 49#define GENLMSG_DATA(glh)	((void *)(NLMSG_DATA(glh) + GENL_HDRLEN))
 50#define GENLMSG_PAYLOAD(glh)	(NLMSG_PAYLOAD(glh, 0) - GENL_HDRLEN)
 51#define NLA_DATA(na)		((void *)((char *)(na) + NLA_HDRLEN))
 52#define NLA_PAYLOAD(len)	(len - NLA_HDRLEN)
 53
 54#define err(code, fmt, arg...)			\
 55	do {					\
 56		fprintf(stderr, fmt, ##arg);	\
 57		exit(code);			\
 58	} while (0)
 59
 60int rcvbufsz;
 61char name[100];
 62int dbg;
 63int print_delays;
 64int print_io_accounting;
 65int print_task_context_switch_counts;
 66
 67#define PRINTF(fmt, arg...) {			\
 68		if (dbg) {			\
 69			printf(fmt, ##arg);	\
 70		}				\
 71	}
 72
 73/* Maximum size of response requested or message sent */
 74#define MAX_MSG_SIZE	1024
 75/* Maximum number of cpus expected to be specified in a cpumask */
 76#define MAX_CPUS	32
 77
 78struct msgtemplate {
 79	struct nlmsghdr n;
 80	struct genlmsghdr g;
 81	char buf[MAX_MSG_SIZE];
 82};
 83
 84char cpumask[100+6*MAX_CPUS];
 85
 86static void usage(void)
 87{
 88	fprintf(stderr, "procacct [-v] [-w logfile] [-r bufsize] [-m cpumask]\n");
 89	fprintf(stderr, "  -v: debug on\n");
 90}
 91
 92/*
 93 * Create a raw netlink socket and bind
 94 */
 95static int create_nl_socket(int protocol)
 96{
 97	int fd;
 98	struct sockaddr_nl local;
 99
100	fd = socket(AF_NETLINK, SOCK_RAW, protocol);
101	if (fd < 0)
102		return -1;
103
104	if (rcvbufsz)
105		if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
106				&rcvbufsz, sizeof(rcvbufsz)) < 0) {
107			fprintf(stderr, "Unable to set socket rcv buf size to %d\n",
108				rcvbufsz);
109			goto error;
110		}
111
112	memset(&local, 0, sizeof(local));
113	local.nl_family = AF_NETLINK;
114
115	if (bind(fd, (struct sockaddr *) &local, sizeof(local)) < 0)
116		goto error;
117
118	return fd;
119error:
120	close(fd);
121	return -1;
122}
123
124
125static int send_cmd(int sd, __u16 nlmsg_type, __u32 nlmsg_pid,
126	     __u8 genl_cmd, __u16 nla_type,
127	     void *nla_data, int nla_len)
128{
129	struct nlattr *na;
130	struct sockaddr_nl nladdr;
131	int r, buflen;
132	char *buf;
133
134	struct msgtemplate msg;
135
136	msg.n.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN);
137	msg.n.nlmsg_type = nlmsg_type;
138	msg.n.nlmsg_flags = NLM_F_REQUEST;
139	msg.n.nlmsg_seq = 0;
140	msg.n.nlmsg_pid = nlmsg_pid;
141	msg.g.cmd = genl_cmd;
142	msg.g.version = 0x1;
143	na = (struct nlattr *) GENLMSG_DATA(&msg);
144	na->nla_type = nla_type;
145	na->nla_len = nla_len + 1 + NLA_HDRLEN;
146	memcpy(NLA_DATA(na), nla_data, nla_len);
147	msg.n.nlmsg_len += NLMSG_ALIGN(na->nla_len);
148
149	buf = (char *) &msg;
150	buflen = msg.n.nlmsg_len;
151	memset(&nladdr, 0, sizeof(nladdr));
152	nladdr.nl_family = AF_NETLINK;
153	while ((r = sendto(sd, buf, buflen, 0, (struct sockaddr *) &nladdr,
154			   sizeof(nladdr))) < buflen) {
155		if (r > 0) {
156			buf += r;
157			buflen -= r;
158		} else if (errno != EAGAIN)
159			return -1;
160	}
161	return 0;
162}
163
164
165/*
166 * Probe the controller in genetlink to find the family id
167 * for the TASKSTATS family
168 */
169static int get_family_id(int sd)
170{
171	struct {
172		struct nlmsghdr n;
173		struct genlmsghdr g;
174		char buf[256];
175	} ans;
176
177	int id = 0, rc;
178	struct nlattr *na;
179	int rep_len;
180
181	strcpy(name, TASKSTATS_GENL_NAME);
182	rc = send_cmd(sd, GENL_ID_CTRL, getpid(), CTRL_CMD_GETFAMILY,
183			CTRL_ATTR_FAMILY_NAME, (void *)name,
184			strlen(TASKSTATS_GENL_NAME)+1);
185	if (rc < 0)
186		return 0;	/* sendto() failure? */
187
188	rep_len = recv(sd, &ans, sizeof(ans), 0);
189	if (ans.n.nlmsg_type == NLMSG_ERROR ||
190	    (rep_len < 0) || !NLMSG_OK((&ans.n), rep_len))
191		return 0;
192
193	na = (struct nlattr *) GENLMSG_DATA(&ans);
194	na = (struct nlattr *) ((char *) na + NLA_ALIGN(na->nla_len));
195	if (na->nla_type == CTRL_ATTR_FAMILY_ID)
196		id = *(__u16 *) NLA_DATA(na);
197
198	return id;
199}
200
201#define average_ms(t, c) (t / 1000000ULL / (c ? c : 1))
202
203static void print_procacct(struct taskstats *t)
204{
205	/* First letter: T is a mere thread, G the last in a group, U  unknown. */
206	printf(
207		"%c pid=%lu tgid=%lu uid=%lu wall=%llu gwall=%llu cpu=%llu vmpeak=%llu rsspeak=%llu dev=%lu:%lu inode=%llu comm=%s\n"
208	,	t->version >= 12 ? (t->ac_flag & AGROUP ? 'P' : 'T') : '?'
209	,	(unsigned long)t->ac_pid
210	,	(unsigned long)(t->version >= 12 ? t->ac_tgid : 0)
211	,	(unsigned long)t->ac_uid
212	,	(unsigned long long)t->ac_etime
213	,	(unsigned long long)(t->version >= 12 ? t->ac_tgetime : 0)
214	,	(unsigned long long)(t->ac_utime+t->ac_stime)
215	,	(unsigned long long)t->hiwater_vm
216	,	(unsigned long long)t->hiwater_rss
217	,	(unsigned long)(t->version >= 12 ? MAJOR(t->ac_exe_dev) : 0)
218	,	(unsigned long)(t->version >= 12 ? MINOR(t->ac_exe_dev) : 0)
219	,	(unsigned long long)(t->version >= 12 ? t->ac_exe_inode : 0)
220	,	t->ac_comm
221	);
222}
223
224void handle_aggr(int mother, struct nlattr *na, int fd)
225{
226	int aggr_len = NLA_PAYLOAD(na->nla_len);
227	int len2 = 0;
228	pid_t rtid = 0;
229
230	na = (struct nlattr *) NLA_DATA(na);
231	while (len2 < aggr_len) {
232		switch (na->nla_type) {
233		case TASKSTATS_TYPE_PID:
234			rtid = *(int *) NLA_DATA(na);
235			PRINTF("PID\t%d\n", rtid);
236			break;
237		case TASKSTATS_TYPE_TGID:
238			rtid = *(int *) NLA_DATA(na);
239			PRINTF("TGID\t%d\n", rtid);
240			break;
241		case TASKSTATS_TYPE_STATS:
242			if (mother == TASKSTATS_TYPE_AGGR_PID)
243				print_procacct((struct taskstats *) NLA_DATA(na));
244			if (fd) {
245				if (write(fd, NLA_DATA(na), na->nla_len) < 0)
246					err(1, "write error\n");
247			}
248			break;
249		case TASKSTATS_TYPE_NULL:
250			break;
251		default:
252			fprintf(stderr, "Unknown nested nla_type %d\n",
253				na->nla_type);
254			break;
255		}
256		len2 += NLA_ALIGN(na->nla_len);
257		na = (struct nlattr *)((char *)na +
258						 NLA_ALIGN(na->nla_len));
259	}
260}
261
262int main(int argc, char *argv[])
263{
264	int c, rc, rep_len;
265	__u16 id;
266	__u32 mypid;
267
268	struct nlattr *na;
269	int nl_sd = -1;
270	int len = 0;
271
272	int fd = 0;
273	int write_file = 0;
274	int maskset = 0;
275	char *logfile = NULL;
276	int cfd = 0;
277	int forking = 0;
278
279	struct msgtemplate msg;
280
281	while (!forking) {
282		c = getopt(argc, argv, "m:vr:");
283		if (c < 0)
284			break;
285
286		switch (c) {
287		case 'w':
288			logfile = strdup(optarg);
289			printf("write to file %s\n", logfile);
290			write_file = 1;
291			break;
292		case 'r':
293			rcvbufsz = atoi(optarg);
294			printf("receive buf size %d\n", rcvbufsz);
295			if (rcvbufsz < 0)
296				err(1, "Invalid rcv buf size\n");
297			break;
298		case 'm':
299			strncpy(cpumask, optarg, sizeof(cpumask));
300			cpumask[sizeof(cpumask) - 1] = '\0';
301			maskset = 1;
302			break;
303		case 'v':
304			printf("debug on\n");
305			dbg = 1;
306			break;
307		default:
308			usage();
309			exit(-1);
310		}
311	}
312	if (!maskset) {
313		maskset = 1;
314		strncpy(cpumask, "1", sizeof(cpumask));
315		cpumask[sizeof(cpumask) - 1] = '\0';
316	}
317	printf("cpumask %s maskset %d\n", cpumask, maskset);
318
319	if (write_file) {
320		fd = open(logfile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
321		if (fd == -1) {
322			perror("Cannot open output file\n");
323			exit(1);
324		}
325	}
326
327	nl_sd = create_nl_socket(NETLINK_GENERIC);
328	if (nl_sd < 0)
329		err(1, "error creating Netlink socket\n");
330
331	mypid = getpid();
332	id = get_family_id(nl_sd);
333	if (!id) {
334		fprintf(stderr, "Error getting family id, errno %d\n", errno);
335		goto err;
336	}
337	PRINTF("family id %d\n", id);
338
339	if (maskset) {
340		rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
341			      TASKSTATS_CMD_ATTR_REGISTER_CPUMASK,
342			      &cpumask, strlen(cpumask) + 1);
343		PRINTF("Sent register cpumask, retval %d\n", rc);
344		if (rc < 0) {
345			fprintf(stderr, "error sending register cpumask\n");
346			goto err;
347		}
348	}
349
350	do {
351		rep_len = recv(nl_sd, &msg, sizeof(msg), 0);
352		PRINTF("received %d bytes\n", rep_len);
353
354		if (rep_len < 0) {
355			fprintf(stderr, "nonfatal reply error: errno %d\n",
356				errno);
357			continue;
358		}
359		if (msg.n.nlmsg_type == NLMSG_ERROR ||
360		    !NLMSG_OK((&msg.n), rep_len)) {
361			struct nlmsgerr *err = NLMSG_DATA(&msg);
362
363			fprintf(stderr, "fatal reply error,  errno %d\n",
364				err->error);
365			goto done;
366		}
367
368		PRINTF("nlmsghdr size=%zu, nlmsg_len=%d, rep_len=%d\n",
369		       sizeof(struct nlmsghdr), msg.n.nlmsg_len, rep_len);
370
371
372		rep_len = GENLMSG_PAYLOAD(&msg.n);
373
374		na = (struct nlattr *) GENLMSG_DATA(&msg);
375		len = 0;
376		while (len < rep_len) {
377			len += NLA_ALIGN(na->nla_len);
378			int mother = na->nla_type;
379
380			PRINTF("mother=%i\n", mother);
381			switch (na->nla_type) {
382			case TASKSTATS_TYPE_AGGR_PID:
383			case TASKSTATS_TYPE_AGGR_TGID:
384				/* For nested attributes, na follows */
385				handle_aggr(mother, na, fd);
386				break;
387			default:
388				fprintf(stderr, "Unexpected nla_type %d\n",
389					na->nla_type);
390			case TASKSTATS_TYPE_NULL:
391				break;
392			}
393			na = (struct nlattr *) (GENLMSG_DATA(&msg) + len);
394		}
395	} while (1);
396done:
397	if (maskset) {
398		rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
399			      TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK,
400			      &cpumask, strlen(cpumask) + 1);
401		printf("Sent deregister mask, retval %d\n", rc);
402		if (rc < 0)
403			err(rc, "error sending deregister cpumask\n");
404	}
405err:
406	close(nl_sd);
407	if (fd)
408		close(fd);
409	if (cfd)
410		close(cfd);
411	return 0;
412}