10e0af57eSDr. Thomas Orgis // SPDX-License-Identifier: GPL-2.0
20e0af57eSDr. Thomas Orgis /* procacct.c
30e0af57eSDr. Thomas Orgis *
40e0af57eSDr. Thomas Orgis * Demonstrator of fetching resource data on task exit, as a way
50e0af57eSDr. Thomas Orgis * to accumulate accurate program resource usage statistics, without
60e0af57eSDr. Thomas Orgis * prior identification of the programs. For that, the fields for
70e0af57eSDr. Thomas Orgis * device and inode of the program executable binary file are also
80e0af57eSDr. Thomas Orgis * extracted in addition to the command string.
90e0af57eSDr. Thomas Orgis *
100e0af57eSDr. Thomas Orgis * The TGID together with the PID and the AGROUP flag allow
110e0af57eSDr. Thomas Orgis * identification of threads in a process and single-threaded processes.
120e0af57eSDr. Thomas Orgis * The ac_tgetime field gives proper whole-process walltime.
130e0af57eSDr. Thomas Orgis *
140e0af57eSDr. Thomas Orgis * Written (changed) by Thomas Orgis, University of Hamburg in 2022
150e0af57eSDr. Thomas Orgis *
160e0af57eSDr. Thomas Orgis * This is a cheap derivation (inheriting the style) of getdelays.c:
170e0af57eSDr. Thomas Orgis *
180e0af57eSDr. Thomas Orgis * Utility to get per-pid and per-tgid delay accounting statistics
190e0af57eSDr. Thomas Orgis * Also illustrates usage of the taskstats interface
200e0af57eSDr. Thomas Orgis *
210e0af57eSDr. Thomas Orgis * Copyright (C) Shailabh Nagar, IBM Corp. 2005
220e0af57eSDr. Thomas Orgis * Copyright (C) Balbir Singh, IBM Corp. 2006
230e0af57eSDr. Thomas Orgis * Copyright (c) Jay Lan, SGI. 2006
240e0af57eSDr. Thomas Orgis */
250e0af57eSDr. Thomas Orgis
260e0af57eSDr. Thomas Orgis #include <stdio.h>
270e0af57eSDr. Thomas Orgis #include <stdlib.h>
280e0af57eSDr. Thomas Orgis #include <errno.h>
290e0af57eSDr. Thomas Orgis #include <unistd.h>
300e0af57eSDr. Thomas Orgis #include <poll.h>
310e0af57eSDr. Thomas Orgis #include <string.h>
320e0af57eSDr. Thomas Orgis #include <fcntl.h>
330e0af57eSDr. Thomas Orgis #include <sys/types.h>
340e0af57eSDr. Thomas Orgis #include <sys/stat.h>
350e0af57eSDr. Thomas Orgis #include <sys/socket.h>
360e0af57eSDr. Thomas Orgis #include <sys/wait.h>
370e0af57eSDr. Thomas Orgis #include <signal.h>
380e0af57eSDr. Thomas Orgis
390e0af57eSDr. Thomas Orgis #include <linux/genetlink.h>
400e0af57eSDr. Thomas Orgis #include <linux/acct.h>
410e0af57eSDr. Thomas Orgis #include <linux/taskstats.h>
420e0af57eSDr. Thomas Orgis #include <linux/kdev_t.h>
430e0af57eSDr. Thomas Orgis
440e0af57eSDr. Thomas Orgis /*
450e0af57eSDr. Thomas Orgis * Generic macros for dealing with netlink sockets. Might be duplicated
460e0af57eSDr. Thomas Orgis * elsewhere. It is recommended that commercial grade applications use
470e0af57eSDr. Thomas Orgis * libnl or libnetlink and use the interfaces provided by the library
480e0af57eSDr. Thomas Orgis */
490e0af57eSDr. Thomas Orgis #define GENLMSG_DATA(glh) ((void *)(NLMSG_DATA(glh) + GENL_HDRLEN))
500e0af57eSDr. Thomas Orgis #define GENLMSG_PAYLOAD(glh) (NLMSG_PAYLOAD(glh, 0) - GENL_HDRLEN)
510e0af57eSDr. Thomas Orgis #define NLA_DATA(na) ((void *)((char *)(na) + NLA_HDRLEN))
520e0af57eSDr. Thomas Orgis #define NLA_PAYLOAD(len) (len - NLA_HDRLEN)
530e0af57eSDr. Thomas Orgis
540e0af57eSDr. Thomas Orgis #define err(code, fmt, arg...) \
550e0af57eSDr. Thomas Orgis do { \
560e0af57eSDr. Thomas Orgis fprintf(stderr, fmt, ##arg); \
570e0af57eSDr. Thomas Orgis exit(code); \
580e0af57eSDr. Thomas Orgis } while (0)
590e0af57eSDr. Thomas Orgis
600e0af57eSDr. Thomas Orgis int rcvbufsz;
610e0af57eSDr. Thomas Orgis char name[100];
620e0af57eSDr. Thomas Orgis int dbg;
630e0af57eSDr. Thomas Orgis int print_delays;
640e0af57eSDr. Thomas Orgis int print_io_accounting;
650e0af57eSDr. Thomas Orgis int print_task_context_switch_counts;
660e0af57eSDr. Thomas Orgis
670e0af57eSDr. Thomas Orgis #define PRINTF(fmt, arg...) { \
680e0af57eSDr. Thomas Orgis if (dbg) { \
690e0af57eSDr. Thomas Orgis printf(fmt, ##arg); \
700e0af57eSDr. Thomas Orgis } \
710e0af57eSDr. Thomas Orgis }
720e0af57eSDr. Thomas Orgis
730e0af57eSDr. Thomas Orgis /* Maximum size of response requested or message sent */
740e0af57eSDr. Thomas Orgis #define MAX_MSG_SIZE 1024
750e0af57eSDr. Thomas Orgis /* Maximum number of cpus expected to be specified in a cpumask */
760e0af57eSDr. Thomas Orgis #define MAX_CPUS 32
770e0af57eSDr. Thomas Orgis
780e0af57eSDr. Thomas Orgis struct msgtemplate {
790e0af57eSDr. Thomas Orgis struct nlmsghdr n;
800e0af57eSDr. Thomas Orgis struct genlmsghdr g;
810e0af57eSDr. Thomas Orgis char buf[MAX_MSG_SIZE];
820e0af57eSDr. Thomas Orgis };
830e0af57eSDr. Thomas Orgis
840e0af57eSDr. Thomas Orgis char cpumask[100+6*MAX_CPUS];
850e0af57eSDr. Thomas Orgis
usage(void)860e0af57eSDr. Thomas Orgis static void usage(void)
870e0af57eSDr. Thomas Orgis {
880e0af57eSDr. Thomas Orgis fprintf(stderr, "procacct [-v] [-w logfile] [-r bufsize] [-m cpumask]\n");
890e0af57eSDr. Thomas Orgis fprintf(stderr, " -v: debug on\n");
900e0af57eSDr. Thomas Orgis }
910e0af57eSDr. Thomas Orgis
920e0af57eSDr. Thomas Orgis /*
930e0af57eSDr. Thomas Orgis * Create a raw netlink socket and bind
940e0af57eSDr. Thomas Orgis */
create_nl_socket(int protocol)950e0af57eSDr. Thomas Orgis static int create_nl_socket(int protocol)
960e0af57eSDr. Thomas Orgis {
970e0af57eSDr. Thomas Orgis int fd;
980e0af57eSDr. Thomas Orgis struct sockaddr_nl local;
990e0af57eSDr. Thomas Orgis
1000e0af57eSDr. Thomas Orgis fd = socket(AF_NETLINK, SOCK_RAW, protocol);
1010e0af57eSDr. Thomas Orgis if (fd < 0)
1020e0af57eSDr. Thomas Orgis return -1;
1030e0af57eSDr. Thomas Orgis
1040e0af57eSDr. Thomas Orgis if (rcvbufsz)
1050e0af57eSDr. Thomas Orgis if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
1060e0af57eSDr. Thomas Orgis &rcvbufsz, sizeof(rcvbufsz)) < 0) {
1070e0af57eSDr. Thomas Orgis fprintf(stderr, "Unable to set socket rcv buf size to %d\n",
1080e0af57eSDr. Thomas Orgis rcvbufsz);
1090e0af57eSDr. Thomas Orgis goto error;
1100e0af57eSDr. Thomas Orgis }
1110e0af57eSDr. Thomas Orgis
1120e0af57eSDr. Thomas Orgis memset(&local, 0, sizeof(local));
1130e0af57eSDr. Thomas Orgis local.nl_family = AF_NETLINK;
1140e0af57eSDr. Thomas Orgis
1150e0af57eSDr. Thomas Orgis if (bind(fd, (struct sockaddr *) &local, sizeof(local)) < 0)
1160e0af57eSDr. Thomas Orgis goto error;
1170e0af57eSDr. Thomas Orgis
1180e0af57eSDr. Thomas Orgis return fd;
1190e0af57eSDr. Thomas Orgis error:
1200e0af57eSDr. Thomas Orgis close(fd);
1210e0af57eSDr. Thomas Orgis return -1;
1220e0af57eSDr. Thomas Orgis }
1230e0af57eSDr. Thomas Orgis
1240e0af57eSDr. Thomas Orgis
send_cmd(int sd,__u16 nlmsg_type,__u32 nlmsg_pid,__u8 genl_cmd,__u16 nla_type,void * nla_data,int nla_len)1250e0af57eSDr. Thomas Orgis static int send_cmd(int sd, __u16 nlmsg_type, __u32 nlmsg_pid,
1260e0af57eSDr. Thomas Orgis __u8 genl_cmd, __u16 nla_type,
1270e0af57eSDr. Thomas Orgis void *nla_data, int nla_len)
1280e0af57eSDr. Thomas Orgis {
1290e0af57eSDr. Thomas Orgis struct nlattr *na;
1300e0af57eSDr. Thomas Orgis struct sockaddr_nl nladdr;
1310e0af57eSDr. Thomas Orgis int r, buflen;
1320e0af57eSDr. Thomas Orgis char *buf;
1330e0af57eSDr. Thomas Orgis
1340e0af57eSDr. Thomas Orgis struct msgtemplate msg;
1350e0af57eSDr. Thomas Orgis
1360e0af57eSDr. Thomas Orgis msg.n.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN);
1370e0af57eSDr. Thomas Orgis msg.n.nlmsg_type = nlmsg_type;
1380e0af57eSDr. Thomas Orgis msg.n.nlmsg_flags = NLM_F_REQUEST;
1390e0af57eSDr. Thomas Orgis msg.n.nlmsg_seq = 0;
1400e0af57eSDr. Thomas Orgis msg.n.nlmsg_pid = nlmsg_pid;
1410e0af57eSDr. Thomas Orgis msg.g.cmd = genl_cmd;
1420e0af57eSDr. Thomas Orgis msg.g.version = 0x1;
1430e0af57eSDr. Thomas Orgis na = (struct nlattr *) GENLMSG_DATA(&msg);
1440e0af57eSDr. Thomas Orgis na->nla_type = nla_type;
1450e0af57eSDr. Thomas Orgis na->nla_len = nla_len + 1 + NLA_HDRLEN;
1460e0af57eSDr. Thomas Orgis memcpy(NLA_DATA(na), nla_data, nla_len);
1470e0af57eSDr. Thomas Orgis msg.n.nlmsg_len += NLMSG_ALIGN(na->nla_len);
1480e0af57eSDr. Thomas Orgis
1490e0af57eSDr. Thomas Orgis buf = (char *) &msg;
1500e0af57eSDr. Thomas Orgis buflen = msg.n.nlmsg_len;
1510e0af57eSDr. Thomas Orgis memset(&nladdr, 0, sizeof(nladdr));
1520e0af57eSDr. Thomas Orgis nladdr.nl_family = AF_NETLINK;
1530e0af57eSDr. Thomas Orgis while ((r = sendto(sd, buf, buflen, 0, (struct sockaddr *) &nladdr,
1540e0af57eSDr. Thomas Orgis sizeof(nladdr))) < buflen) {
1550e0af57eSDr. Thomas Orgis if (r > 0) {
1560e0af57eSDr. Thomas Orgis buf += r;
1570e0af57eSDr. Thomas Orgis buflen -= r;
1580e0af57eSDr. Thomas Orgis } else if (errno != EAGAIN)
1590e0af57eSDr. Thomas Orgis return -1;
1600e0af57eSDr. Thomas Orgis }
1610e0af57eSDr. Thomas Orgis return 0;
1620e0af57eSDr. Thomas Orgis }
1630e0af57eSDr. Thomas Orgis
1640e0af57eSDr. Thomas Orgis
1650e0af57eSDr. Thomas Orgis /*
1660e0af57eSDr. Thomas Orgis * Probe the controller in genetlink to find the family id
1670e0af57eSDr. Thomas Orgis * for the TASKSTATS family
1680e0af57eSDr. Thomas Orgis */
get_family_id(int sd)1690e0af57eSDr. Thomas Orgis static int get_family_id(int sd)
1700e0af57eSDr. Thomas Orgis {
1710e0af57eSDr. Thomas Orgis struct {
1720e0af57eSDr. Thomas Orgis struct nlmsghdr n;
1730e0af57eSDr. Thomas Orgis struct genlmsghdr g;
1740e0af57eSDr. Thomas Orgis char buf[256];
1750e0af57eSDr. Thomas Orgis } ans;
1760e0af57eSDr. Thomas Orgis
1770e0af57eSDr. Thomas Orgis int id = 0, rc;
1780e0af57eSDr. Thomas Orgis struct nlattr *na;
1790e0af57eSDr. Thomas Orgis int rep_len;
1800e0af57eSDr. Thomas Orgis
1810e0af57eSDr. Thomas Orgis strcpy(name, TASKSTATS_GENL_NAME);
1820e0af57eSDr. Thomas Orgis rc = send_cmd(sd, GENL_ID_CTRL, getpid(), CTRL_CMD_GETFAMILY,
1830e0af57eSDr. Thomas Orgis CTRL_ATTR_FAMILY_NAME, (void *)name,
1840e0af57eSDr. Thomas Orgis strlen(TASKSTATS_GENL_NAME)+1);
1850e0af57eSDr. Thomas Orgis if (rc < 0)
1860e0af57eSDr. Thomas Orgis return 0; /* sendto() failure? */
1870e0af57eSDr. Thomas Orgis
1880e0af57eSDr. Thomas Orgis rep_len = recv(sd, &ans, sizeof(ans), 0);
1890e0af57eSDr. Thomas Orgis if (ans.n.nlmsg_type == NLMSG_ERROR ||
1900e0af57eSDr. Thomas Orgis (rep_len < 0) || !NLMSG_OK((&ans.n), rep_len))
1910e0af57eSDr. Thomas Orgis return 0;
1920e0af57eSDr. Thomas Orgis
1930e0af57eSDr. Thomas Orgis na = (struct nlattr *) GENLMSG_DATA(&ans);
1940e0af57eSDr. Thomas Orgis na = (struct nlattr *) ((char *) na + NLA_ALIGN(na->nla_len));
1950e0af57eSDr. Thomas Orgis if (na->nla_type == CTRL_ATTR_FAMILY_ID)
1960e0af57eSDr. Thomas Orgis id = *(__u16 *) NLA_DATA(na);
1970e0af57eSDr. Thomas Orgis
1980e0af57eSDr. Thomas Orgis return id;
1990e0af57eSDr. Thomas Orgis }
2000e0af57eSDr. Thomas Orgis
2010e0af57eSDr. Thomas Orgis #define average_ms(t, c) (t / 1000000ULL / (c ? c : 1))
2020e0af57eSDr. Thomas Orgis
print_procacct(struct taskstats * t)2030e0af57eSDr. Thomas Orgis static void print_procacct(struct taskstats *t)
2040e0af57eSDr. Thomas Orgis {
2050e0af57eSDr. Thomas Orgis /* First letter: T is a mere thread, G the last in a group, U unknown. */
2060e0af57eSDr. Thomas Orgis printf(
2070e0af57eSDr. Thomas Orgis "%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"
2080e0af57eSDr. Thomas Orgis , t->version >= 12 ? (t->ac_flag & AGROUP ? 'P' : 'T') : '?'
2090e0af57eSDr. Thomas Orgis , (unsigned long)t->ac_pid
2100e0af57eSDr. Thomas Orgis , (unsigned long)(t->version >= 12 ? t->ac_tgid : 0)
2110e0af57eSDr. Thomas Orgis , (unsigned long)t->ac_uid
2120e0af57eSDr. Thomas Orgis , (unsigned long long)t->ac_etime
2130e0af57eSDr. Thomas Orgis , (unsigned long long)(t->version >= 12 ? t->ac_tgetime : 0)
2140e0af57eSDr. Thomas Orgis , (unsigned long long)(t->ac_utime+t->ac_stime)
2150e0af57eSDr. Thomas Orgis , (unsigned long long)t->hiwater_vm
2160e0af57eSDr. Thomas Orgis , (unsigned long long)t->hiwater_rss
2170e0af57eSDr. Thomas Orgis , (unsigned long)(t->version >= 12 ? MAJOR(t->ac_exe_dev) : 0)
2180e0af57eSDr. Thomas Orgis , (unsigned long)(t->version >= 12 ? MINOR(t->ac_exe_dev) : 0)
2190e0af57eSDr. Thomas Orgis , (unsigned long long)(t->version >= 12 ? t->ac_exe_inode : 0)
2200e0af57eSDr. Thomas Orgis , t->ac_comm
2210e0af57eSDr. Thomas Orgis );
2220e0af57eSDr. Thomas Orgis }
2230e0af57eSDr. Thomas Orgis
handle_aggr(int mother,struct nlattr * na,int fd)2240e0af57eSDr. Thomas Orgis void handle_aggr(int mother, struct nlattr *na, int fd)
2250e0af57eSDr. Thomas Orgis {
2260e0af57eSDr. Thomas Orgis int aggr_len = NLA_PAYLOAD(na->nla_len);
2270e0af57eSDr. Thomas Orgis int len2 = 0;
2280e0af57eSDr. Thomas Orgis pid_t rtid = 0;
2290e0af57eSDr. Thomas Orgis
2300e0af57eSDr. Thomas Orgis na = (struct nlattr *) NLA_DATA(na);
2310e0af57eSDr. Thomas Orgis while (len2 < aggr_len) {
2320e0af57eSDr. Thomas Orgis switch (na->nla_type) {
2330e0af57eSDr. Thomas Orgis case TASKSTATS_TYPE_PID:
2340e0af57eSDr. Thomas Orgis rtid = *(int *) NLA_DATA(na);
2350e0af57eSDr. Thomas Orgis PRINTF("PID\t%d\n", rtid);
2360e0af57eSDr. Thomas Orgis break;
2370e0af57eSDr. Thomas Orgis case TASKSTATS_TYPE_TGID:
2380e0af57eSDr. Thomas Orgis rtid = *(int *) NLA_DATA(na);
2390e0af57eSDr. Thomas Orgis PRINTF("TGID\t%d\n", rtid);
2400e0af57eSDr. Thomas Orgis break;
2410e0af57eSDr. Thomas Orgis case TASKSTATS_TYPE_STATS:
2420e0af57eSDr. Thomas Orgis if (mother == TASKSTATS_TYPE_AGGR_PID)
2430e0af57eSDr. Thomas Orgis print_procacct((struct taskstats *) NLA_DATA(na));
2440e0af57eSDr. Thomas Orgis if (fd) {
2450e0af57eSDr. Thomas Orgis if (write(fd, NLA_DATA(na), na->nla_len) < 0)
2460e0af57eSDr. Thomas Orgis err(1, "write error\n");
2470e0af57eSDr. Thomas Orgis }
2480e0af57eSDr. Thomas Orgis break;
2490e0af57eSDr. Thomas Orgis case TASKSTATS_TYPE_NULL:
2500e0af57eSDr. Thomas Orgis break;
2510e0af57eSDr. Thomas Orgis default:
2520e0af57eSDr. Thomas Orgis fprintf(stderr, "Unknown nested nla_type %d\n",
2530e0af57eSDr. Thomas Orgis na->nla_type);
2540e0af57eSDr. Thomas Orgis break;
2550e0af57eSDr. Thomas Orgis }
2560e0af57eSDr. Thomas Orgis len2 += NLA_ALIGN(na->nla_len);
2570e0af57eSDr. Thomas Orgis na = (struct nlattr *)((char *)na +
2580e0af57eSDr. Thomas Orgis NLA_ALIGN(na->nla_len));
2590e0af57eSDr. Thomas Orgis }
2600e0af57eSDr. Thomas Orgis }
2610e0af57eSDr. Thomas Orgis
main(int argc,char * argv[])2620e0af57eSDr. Thomas Orgis int main(int argc, char *argv[])
2630e0af57eSDr. Thomas Orgis {
264*9629c2b9SXiongfeng Wang int c, rc, rep_len;
2650e0af57eSDr. Thomas Orgis __u16 id;
2660e0af57eSDr. Thomas Orgis __u32 mypid;
2670e0af57eSDr. Thomas Orgis
2680e0af57eSDr. Thomas Orgis struct nlattr *na;
2690e0af57eSDr. Thomas Orgis int nl_sd = -1;
2700e0af57eSDr. Thomas Orgis int len = 0;
2710e0af57eSDr. Thomas Orgis
2720e0af57eSDr. Thomas Orgis int fd = 0;
2730e0af57eSDr. Thomas Orgis int write_file = 0;
2740e0af57eSDr. Thomas Orgis int maskset = 0;
2750e0af57eSDr. Thomas Orgis char *logfile = NULL;
2760e0af57eSDr. Thomas Orgis int cfd = 0;
2770e0af57eSDr. Thomas Orgis int forking = 0;
2780e0af57eSDr. Thomas Orgis
2790e0af57eSDr. Thomas Orgis struct msgtemplate msg;
2800e0af57eSDr. Thomas Orgis
2810e0af57eSDr. Thomas Orgis while (!forking) {
2820e0af57eSDr. Thomas Orgis c = getopt(argc, argv, "m:vr:");
2830e0af57eSDr. Thomas Orgis if (c < 0)
2840e0af57eSDr. Thomas Orgis break;
2850e0af57eSDr. Thomas Orgis
2860e0af57eSDr. Thomas Orgis switch (c) {
2870e0af57eSDr. Thomas Orgis case 'w':
2880e0af57eSDr. Thomas Orgis logfile = strdup(optarg);
2890e0af57eSDr. Thomas Orgis printf("write to file %s\n", logfile);
2900e0af57eSDr. Thomas Orgis write_file = 1;
2910e0af57eSDr. Thomas Orgis break;
2920e0af57eSDr. Thomas Orgis case 'r':
2930e0af57eSDr. Thomas Orgis rcvbufsz = atoi(optarg);
2940e0af57eSDr. Thomas Orgis printf("receive buf size %d\n", rcvbufsz);
2950e0af57eSDr. Thomas Orgis if (rcvbufsz < 0)
2960e0af57eSDr. Thomas Orgis err(1, "Invalid rcv buf size\n");
2970e0af57eSDr. Thomas Orgis break;
2980e0af57eSDr. Thomas Orgis case 'm':
2990e0af57eSDr. Thomas Orgis strncpy(cpumask, optarg, sizeof(cpumask));
3000e0af57eSDr. Thomas Orgis cpumask[sizeof(cpumask) - 1] = '\0';
3010e0af57eSDr. Thomas Orgis maskset = 1;
3020e0af57eSDr. Thomas Orgis break;
3030e0af57eSDr. Thomas Orgis case 'v':
3040e0af57eSDr. Thomas Orgis printf("debug on\n");
3050e0af57eSDr. Thomas Orgis dbg = 1;
3060e0af57eSDr. Thomas Orgis break;
3070e0af57eSDr. Thomas Orgis default:
3080e0af57eSDr. Thomas Orgis usage();
3090e0af57eSDr. Thomas Orgis exit(-1);
3100e0af57eSDr. Thomas Orgis }
3110e0af57eSDr. Thomas Orgis }
3120e0af57eSDr. Thomas Orgis if (!maskset) {
3130e0af57eSDr. Thomas Orgis maskset = 1;
3140e0af57eSDr. Thomas Orgis strncpy(cpumask, "1", sizeof(cpumask));
3150e0af57eSDr. Thomas Orgis cpumask[sizeof(cpumask) - 1] = '\0';
3160e0af57eSDr. Thomas Orgis }
3170e0af57eSDr. Thomas Orgis printf("cpumask %s maskset %d\n", cpumask, maskset);
3180e0af57eSDr. Thomas Orgis
3190e0af57eSDr. Thomas Orgis if (write_file) {
3200e0af57eSDr. Thomas Orgis fd = open(logfile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
3210e0af57eSDr. Thomas Orgis if (fd == -1) {
3220e0af57eSDr. Thomas Orgis perror("Cannot open output file\n");
3230e0af57eSDr. Thomas Orgis exit(1);
3240e0af57eSDr. Thomas Orgis }
3250e0af57eSDr. Thomas Orgis }
3260e0af57eSDr. Thomas Orgis
3270e0af57eSDr. Thomas Orgis nl_sd = create_nl_socket(NETLINK_GENERIC);
3280e0af57eSDr. Thomas Orgis if (nl_sd < 0)
3290e0af57eSDr. Thomas Orgis err(1, "error creating Netlink socket\n");
3300e0af57eSDr. Thomas Orgis
3310e0af57eSDr. Thomas Orgis mypid = getpid();
3320e0af57eSDr. Thomas Orgis id = get_family_id(nl_sd);
3330e0af57eSDr. Thomas Orgis if (!id) {
3340e0af57eSDr. Thomas Orgis fprintf(stderr, "Error getting family id, errno %d\n", errno);
3350e0af57eSDr. Thomas Orgis goto err;
3360e0af57eSDr. Thomas Orgis }
3370e0af57eSDr. Thomas Orgis PRINTF("family id %d\n", id);
3380e0af57eSDr. Thomas Orgis
3390e0af57eSDr. Thomas Orgis if (maskset) {
3400e0af57eSDr. Thomas Orgis rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
3410e0af57eSDr. Thomas Orgis TASKSTATS_CMD_ATTR_REGISTER_CPUMASK,
3420e0af57eSDr. Thomas Orgis &cpumask, strlen(cpumask) + 1);
3430e0af57eSDr. Thomas Orgis PRINTF("Sent register cpumask, retval %d\n", rc);
3440e0af57eSDr. Thomas Orgis if (rc < 0) {
3450e0af57eSDr. Thomas Orgis fprintf(stderr, "error sending register cpumask\n");
3460e0af57eSDr. Thomas Orgis goto err;
3470e0af57eSDr. Thomas Orgis }
3480e0af57eSDr. Thomas Orgis }
3490e0af57eSDr. Thomas Orgis
3500e0af57eSDr. Thomas Orgis do {
3510e0af57eSDr. Thomas Orgis rep_len = recv(nl_sd, &msg, sizeof(msg), 0);
3520e0af57eSDr. Thomas Orgis PRINTF("received %d bytes\n", rep_len);
3530e0af57eSDr. Thomas Orgis
3540e0af57eSDr. Thomas Orgis if (rep_len < 0) {
3550e0af57eSDr. Thomas Orgis fprintf(stderr, "nonfatal reply error: errno %d\n",
3560e0af57eSDr. Thomas Orgis errno);
3570e0af57eSDr. Thomas Orgis continue;
3580e0af57eSDr. Thomas Orgis }
3590e0af57eSDr. Thomas Orgis if (msg.n.nlmsg_type == NLMSG_ERROR ||
3600e0af57eSDr. Thomas Orgis !NLMSG_OK((&msg.n), rep_len)) {
3610e0af57eSDr. Thomas Orgis struct nlmsgerr *err = NLMSG_DATA(&msg);
3620e0af57eSDr. Thomas Orgis
3630e0af57eSDr. Thomas Orgis fprintf(stderr, "fatal reply error, errno %d\n",
3640e0af57eSDr. Thomas Orgis err->error);
3650e0af57eSDr. Thomas Orgis goto done;
3660e0af57eSDr. Thomas Orgis }
3670e0af57eSDr. Thomas Orgis
3680e0af57eSDr. Thomas Orgis PRINTF("nlmsghdr size=%zu, nlmsg_len=%d, rep_len=%d\n",
3690e0af57eSDr. Thomas Orgis sizeof(struct nlmsghdr), msg.n.nlmsg_len, rep_len);
3700e0af57eSDr. Thomas Orgis
3710e0af57eSDr. Thomas Orgis
3720e0af57eSDr. Thomas Orgis rep_len = GENLMSG_PAYLOAD(&msg.n);
3730e0af57eSDr. Thomas Orgis
3740e0af57eSDr. Thomas Orgis na = (struct nlattr *) GENLMSG_DATA(&msg);
3750e0af57eSDr. Thomas Orgis len = 0;
3760e0af57eSDr. Thomas Orgis while (len < rep_len) {
3770e0af57eSDr. Thomas Orgis len += NLA_ALIGN(na->nla_len);
3780e0af57eSDr. Thomas Orgis int mother = na->nla_type;
3790e0af57eSDr. Thomas Orgis
3800e0af57eSDr. Thomas Orgis PRINTF("mother=%i\n", mother);
3810e0af57eSDr. Thomas Orgis switch (na->nla_type) {
3820e0af57eSDr. Thomas Orgis case TASKSTATS_TYPE_AGGR_PID:
3830e0af57eSDr. Thomas Orgis case TASKSTATS_TYPE_AGGR_TGID:
3840e0af57eSDr. Thomas Orgis /* For nested attributes, na follows */
3850e0af57eSDr. Thomas Orgis handle_aggr(mother, na, fd);
3860e0af57eSDr. Thomas Orgis break;
3870e0af57eSDr. Thomas Orgis default:
3880e0af57eSDr. Thomas Orgis fprintf(stderr, "Unexpected nla_type %d\n",
3890e0af57eSDr. Thomas Orgis na->nla_type);
3900e0af57eSDr. Thomas Orgis case TASKSTATS_TYPE_NULL:
3910e0af57eSDr. Thomas Orgis break;
3920e0af57eSDr. Thomas Orgis }
3930e0af57eSDr. Thomas Orgis na = (struct nlattr *) (GENLMSG_DATA(&msg) + len);
3940e0af57eSDr. Thomas Orgis }
3950e0af57eSDr. Thomas Orgis } while (1);
3960e0af57eSDr. Thomas Orgis done:
3970e0af57eSDr. Thomas Orgis if (maskset) {
3980e0af57eSDr. Thomas Orgis rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
3990e0af57eSDr. Thomas Orgis TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK,
4000e0af57eSDr. Thomas Orgis &cpumask, strlen(cpumask) + 1);
4010e0af57eSDr. Thomas Orgis printf("Sent deregister mask, retval %d\n", rc);
4020e0af57eSDr. Thomas Orgis if (rc < 0)
4030e0af57eSDr. Thomas Orgis err(rc, "error sending deregister cpumask\n");
4040e0af57eSDr. Thomas Orgis }
4050e0af57eSDr. Thomas Orgis err:
4060e0af57eSDr. Thomas Orgis close(nl_sd);
4070e0af57eSDr. Thomas Orgis if (fd)
4080e0af57eSDr. Thomas Orgis close(fd);
4090e0af57eSDr. Thomas Orgis if (cfd)
4100e0af57eSDr. Thomas Orgis close(cfd);
4110e0af57eSDr. Thomas Orgis return 0;
4120e0af57eSDr. Thomas Orgis }
413