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