Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

[PATCH] per task delay accounting taskstats interface: documentation fix

Change documentation and example program to reflect the flow control issues
being addressed by the cpumask changes.

Signed-off-by: Shailabh Nagar <nagar@watson.ibm.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>

authored by

Shailabh Nagar and committed by
Linus Torvalds
9e06d3f9 ad4ecbcb

+361 -301
+309 -289
Documentation/accounting/getdelays.c
··· 5 5 * 6 6 * Copyright (C) Shailabh Nagar, IBM Corp. 2005 7 7 * Copyright (C) Balbir Singh, IBM Corp. 2006 8 + * Copyright (c) Jay Lan, SGI. 2006 8 9 * 9 10 */ 10 11 ··· 37 36 38 37 #define err(code, fmt, arg...) do { printf(fmt, ##arg); exit(code); } while (0) 39 38 int done = 0; 39 + int rcvbufsz=0; 40 + 41 + char name[100]; 42 + int dbg=0, print_delays=0; 43 + __u64 stime, utime; 44 + #define PRINTF(fmt, arg...) { \ 45 + if (dbg) { \ 46 + printf(fmt, ##arg); \ 47 + } \ 48 + } 49 + 50 + /* Maximum size of response requested or message sent */ 51 + #define MAX_MSG_SIZE 256 52 + /* Maximum number of cpus expected to be specified in a cpumask */ 53 + #define MAX_CPUS 32 54 + /* Maximum length of pathname to log file */ 55 + #define MAX_FILENAME 256 56 + 57 + struct msgtemplate { 58 + struct nlmsghdr n; 59 + struct genlmsghdr g; 60 + char buf[MAX_MSG_SIZE]; 61 + }; 62 + 63 + char cpumask[100+6*MAX_CPUS]; 40 64 41 65 /* 42 66 * Create a raw netlink socket and bind 43 67 */ 44 - static int create_nl_socket(int protocol, int groups) 68 + static int create_nl_socket(int protocol) 45 69 { 46 - socklen_t addr_len; 47 - int fd; 48 - struct sockaddr_nl local; 70 + int fd; 71 + struct sockaddr_nl local; 49 72 50 - fd = socket(AF_NETLINK, SOCK_RAW, protocol); 51 - if (fd < 0) 73 + fd = socket(AF_NETLINK, SOCK_RAW, protocol); 74 + if (fd < 0) 75 + return -1; 76 + 77 + if (rcvbufsz) 78 + if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, 79 + &rcvbufsz, sizeof(rcvbufsz)) < 0) { 80 + printf("Unable to set socket rcv buf size to %d\n", 81 + rcvbufsz); 82 + return -1; 83 + } 84 + 85 + memset(&local, 0, sizeof(local)); 86 + local.nl_family = AF_NETLINK; 87 + 88 + if (bind(fd, (struct sockaddr *) &local, sizeof(local)) < 0) 89 + goto error; 90 + 91 + return fd; 92 + error: 93 + close(fd); 52 94 return -1; 53 - 54 - memset(&local, 0, sizeof(local)); 55 - local.nl_family = AF_NETLINK; 56 - local.nl_groups = groups; 57 - 58 - if (bind(fd, (struct sockaddr *) &local, sizeof(local)) < 0) 59 - goto error; 60 - 61 - return fd; 62 - error: 63 - close(fd); 64 - return -1; 65 95 } 66 96 67 - int sendto_fd(int s, const char *buf, int bufLen) 97 + 98 + int send_cmd(int sd, __u16 nlmsg_type, __u32 nlmsg_pid, 99 + __u8 genl_cmd, __u16 nla_type, 100 + void *nla_data, int nla_len) 68 101 { 69 - struct sockaddr_nl nladdr; 70 - int r; 102 + struct nlattr *na; 103 + struct sockaddr_nl nladdr; 104 + int r, buflen; 105 + char *buf; 71 106 72 - memset(&nladdr, 0, sizeof(nladdr)); 73 - nladdr.nl_family = AF_NETLINK; 107 + struct msgtemplate msg; 74 108 75 - while ((r = sendto(s, buf, bufLen, 0, (struct sockaddr *) &nladdr, 76 - sizeof(nladdr))) < bufLen) { 77 - if (r > 0) { 78 - buf += r; 79 - bufLen -= r; 80 - } else if (errno != EAGAIN) 81 - return -1; 82 - } 83 - return 0; 109 + msg.n.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN); 110 + msg.n.nlmsg_type = nlmsg_type; 111 + msg.n.nlmsg_flags = NLM_F_REQUEST; 112 + msg.n.nlmsg_seq = 0; 113 + msg.n.nlmsg_pid = nlmsg_pid; 114 + msg.g.cmd = genl_cmd; 115 + msg.g.version = 0x1; 116 + na = (struct nlattr *) GENLMSG_DATA(&msg); 117 + na->nla_type = nla_type; 118 + na->nla_len = nla_len + 1 + NLA_HDRLEN; 119 + memcpy(NLA_DATA(na), nla_data, nla_len); 120 + msg.n.nlmsg_len += NLMSG_ALIGN(na->nla_len); 121 + 122 + buf = (char *) &msg; 123 + buflen = msg.n.nlmsg_len ; 124 + memset(&nladdr, 0, sizeof(nladdr)); 125 + nladdr.nl_family = AF_NETLINK; 126 + while ((r = sendto(sd, buf, buflen, 0, (struct sockaddr *) &nladdr, 127 + sizeof(nladdr))) < buflen) { 128 + if (r > 0) { 129 + buf += r; 130 + buflen -= r; 131 + } else if (errno != EAGAIN) 132 + return -1; 133 + } 134 + return 0; 84 135 } 136 + 85 137 86 138 /* 87 139 * Probe the controller in genetlink to find the family id ··· 142 88 */ 143 89 int get_family_id(int sd) 144 90 { 145 - struct { 146 - struct nlmsghdr n; 147 - struct genlmsghdr g; 148 - char buf[256]; 149 - } family_req; 150 - struct { 151 - struct nlmsghdr n; 152 - struct genlmsghdr g; 153 - char buf[256]; 154 - } ans; 91 + struct { 92 + struct nlmsghdr n; 93 + struct genlmsghdr g; 94 + char buf[256]; 95 + } ans; 155 96 156 - int id; 157 - struct nlattr *na; 158 - int rep_len; 97 + int id, rc; 98 + struct nlattr *na; 99 + int rep_len; 159 100 160 - /* Get family name */ 161 - family_req.n.nlmsg_type = GENL_ID_CTRL; 162 - family_req.n.nlmsg_flags = NLM_F_REQUEST; 163 - family_req.n.nlmsg_seq = 0; 164 - family_req.n.nlmsg_pid = getpid(); 165 - family_req.n.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN); 166 - family_req.g.cmd = CTRL_CMD_GETFAMILY; 167 - family_req.g.version = 0x1; 168 - na = (struct nlattr *) GENLMSG_DATA(&family_req); 169 - na->nla_type = CTRL_ATTR_FAMILY_NAME; 170 - na->nla_len = strlen(TASKSTATS_GENL_NAME) + 1 + NLA_HDRLEN; 171 - strcpy(NLA_DATA(na), TASKSTATS_GENL_NAME); 172 - family_req.n.nlmsg_len += NLMSG_ALIGN(na->nla_len); 101 + strcpy(name, TASKSTATS_GENL_NAME); 102 + rc = send_cmd(sd, GENL_ID_CTRL, getpid(), CTRL_CMD_GETFAMILY, 103 + CTRL_ATTR_FAMILY_NAME, (void *)name, 104 + strlen(TASKSTATS_GENL_NAME)+1); 173 105 174 - if (sendto_fd(sd, (char *) &family_req, family_req.n.nlmsg_len) < 0) 175 - err(1, "error sending message via Netlink\n"); 106 + rep_len = recv(sd, &ans, sizeof(ans), 0); 107 + if (ans.n.nlmsg_type == NLMSG_ERROR || 108 + (rep_len < 0) || !NLMSG_OK((&ans.n), rep_len)) 109 + return 0; 176 110 177 - rep_len = recv(sd, &ans, sizeof(ans), 0); 178 - 179 - if (rep_len < 0) 180 - err(1, "error receiving reply message via Netlink\n"); 181 - 182 - 183 - /* Validate response message */ 184 - if (!NLMSG_OK((&ans.n), rep_len)) 185 - err(1, "invalid reply message received via Netlink\n"); 186 - 187 - if (ans.n.nlmsg_type == NLMSG_ERROR) { /* error */ 188 - printf("error received NACK - leaving\n"); 189 - exit(1); 190 - } 191 - 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; 111 + na = (struct nlattr *) GENLMSG_DATA(&ans); 112 + na = (struct nlattr *) ((char *) na + NLA_ALIGN(na->nla_len)); 113 + if (na->nla_type == CTRL_ATTR_FAMILY_ID) { 114 + id = *(__u16 *) NLA_DATA(na); 115 + } 116 + return id; 199 117 } 200 118 201 - void print_taskstats(struct taskstats *t) 119 + void print_delayacct(struct taskstats *t) 202 120 { 203 - printf("\n\nCPU %15s%15s%15s%15s\n" 204 - " %15llu%15llu%15llu%15llu\n" 205 - "IO %15s%15s\n" 206 - " %15llu%15llu\n" 207 - "MEM %15s%15s\n" 208 - " %15llu%15llu\n\n", 209 - "count", "real total", "virtual total", "delay total", 210 - t->cpu_count, t->cpu_run_real_total, t->cpu_run_virtual_total, 211 - t->cpu_delay_total, 212 - "count", "delay total", 213 - t->blkio_count, t->blkio_delay_total, 214 - "count", "delay total", t->swapin_count, t->swapin_delay_total); 215 - } 216 - 217 - void sigchld(int sig) 218 - { 219 - done = 1; 121 + printf("\n\nCPU %15s%15s%15s%15s\n" 122 + " %15llu%15llu%15llu%15llu\n" 123 + "IO %15s%15s\n" 124 + " %15llu%15llu\n" 125 + "MEM %15s%15s\n" 126 + " %15llu%15llu\n\n", 127 + "count", "real total", "virtual total", "delay total", 128 + t->cpu_count, t->cpu_run_real_total, t->cpu_run_virtual_total, 129 + t->cpu_delay_total, 130 + "count", "delay total", 131 + t->blkio_count, t->blkio_delay_total, 132 + "count", "delay total", t->swapin_count, t->swapin_delay_total); 220 133 } 221 134 222 135 int main(int argc, char *argv[]) 223 136 { 224 - int rc; 225 - int sk_nl; 226 - struct nlmsghdr *nlh; 227 - struct genlmsghdr *genlhdr; 228 - char *buf; 229 - struct taskstats_cmd_param *param; 230 - __u16 id; 231 - struct nlattr *na; 137 + int c, rc, rep_len, aggr_len, len2, cmd_type; 138 + __u16 id; 139 + __u32 mypid; 232 140 233 - /* For receiving */ 234 - struct sockaddr_nl kern_nla, from_nla; 235 - socklen_t from_nla_len; 236 - int recv_len; 237 - struct taskstats_reply *reply; 141 + struct nlattr *na; 142 + int nl_sd = -1; 143 + int len = 0; 144 + pid_t tid = 0; 145 + pid_t rtid = 0; 238 146 239 - struct { 240 - struct nlmsghdr n; 241 - struct genlmsghdr g; 242 - char buf[256]; 243 - } req; 147 + int fd = 0; 148 + int count = 0; 149 + int write_file = 0; 150 + int maskset = 0; 151 + char logfile[128]; 152 + int loop = 0; 244 153 245 - struct { 246 - struct nlmsghdr n; 247 - struct genlmsghdr g; 248 - char buf[256]; 249 - } ans; 154 + struct msgtemplate msg; 250 155 251 - int nl_sd = -1; 252 - int rep_len; 253 - int len = 0; 254 - int aggr_len, len2; 255 - struct sockaddr_nl nladdr; 256 - pid_t tid = 0; 257 - pid_t rtid = 0; 258 - int cmd_type = TASKSTATS_TYPE_TGID; 259 - int c, status; 260 - int forking = 0; 261 - struct sigaction act = { 262 - .sa_handler = SIG_IGN, 263 - .sa_mask = SA_NOMASK, 264 - }; 265 - struct sigaction tact ; 156 + while (1) { 157 + c = getopt(argc, argv, "dw:r:m:t:p:v:l"); 158 + if (c < 0) 159 + break; 266 160 267 - if (argc < 3) { 268 - printf("usage %s [-t tgid][-p pid][-c cmd]\n", argv[0]); 269 - exit(-1); 270 - } 271 - 272 - tact.sa_handler = sigchld; 273 - sigemptyset(&tact.sa_mask); 274 - if (sigaction(SIGCHLD, &tact, NULL) < 0) 275 - err(1, "sigaction failed for SIGCHLD\n"); 276 - 277 - while (1) { 278 - 279 - c = getopt(argc, argv, "t:p:c:"); 280 - if (c < 0) 281 - break; 282 - 283 - switch (c) { 284 - case 't': 285 - tid = atoi(optarg); 286 - if (!tid) 287 - err(1, "Invalid tgid\n"); 288 - cmd_type = TASKSTATS_CMD_ATTR_TGID; 289 - break; 290 - case 'p': 291 - tid = atoi(optarg); 292 - if (!tid) 293 - err(1, "Invalid pid\n"); 294 - cmd_type = TASKSTATS_CMD_ATTR_TGID; 295 - break; 296 - case 'c': 297 - opterr = 0; 298 - tid = fork(); 299 - if (tid < 0) 300 - err(1, "fork failed\n"); 301 - 302 - if (tid == 0) { /* child process */ 303 - if (execvp(argv[optind - 1], &argv[optind - 1]) < 0) { 304 - exit(-1); 161 + switch (c) { 162 + case 'd': 163 + printf("print delayacct stats ON\n"); 164 + print_delays = 1; 165 + break; 166 + case 'w': 167 + strncpy(logfile, optarg, MAX_FILENAME); 168 + printf("write to file %s\n", logfile); 169 + write_file = 1; 170 + break; 171 + case 'r': 172 + rcvbufsz = atoi(optarg); 173 + printf("receive buf size %d\n", rcvbufsz); 174 + if (rcvbufsz < 0) 175 + err(1, "Invalid rcv buf size\n"); 176 + break; 177 + case 'm': 178 + strncpy(cpumask, optarg, sizeof(cpumask)); 179 + maskset = 1; 180 + printf("cpumask %s maskset %d\n", cpumask, maskset); 181 + break; 182 + case 't': 183 + tid = atoi(optarg); 184 + if (!tid) 185 + err(1, "Invalid tgid\n"); 186 + cmd_type = TASKSTATS_CMD_ATTR_TGID; 187 + print_delays = 1; 188 + break; 189 + case 'p': 190 + tid = atoi(optarg); 191 + if (!tid) 192 + err(1, "Invalid pid\n"); 193 + cmd_type = TASKSTATS_CMD_ATTR_PID; 194 + print_delays = 1; 195 + break; 196 + case 'v': 197 + printf("debug on\n"); 198 + dbg = 1; 199 + break; 200 + case 'l': 201 + printf("listen forever\n"); 202 + loop = 1; 203 + break; 204 + default: 205 + printf("Unknown option %d\n", c); 206 + exit(-1); 305 207 } 306 - } 307 - forking = 1; 308 - break; 309 - default: 310 - printf("usage %s [-t tgid][-p pid][-c cmd]\n", argv[0]); 311 - exit(-1); 312 - break; 313 - } 314 - if (c == 'c') 315 - break; 316 - } 317 - 318 - /* Construct Netlink request message */ 319 - 320 - /* Send Netlink request message & get reply */ 321 - 322 - if ((nl_sd = 323 - create_nl_socket(NETLINK_GENERIC, TASKSTATS_LISTEN_GROUP)) < 0) 324 - err(1, "error creating Netlink socket\n"); 325 - 326 - 327 - id = get_family_id(nl_sd); 328 - 329 - /* Send command needed */ 330 - req.n.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN); 331 - req.n.nlmsg_type = id; 332 - req.n.nlmsg_flags = NLM_F_REQUEST; 333 - req.n.nlmsg_seq = 0; 334 - req.n.nlmsg_pid = tid; 335 - req.g.cmd = TASKSTATS_CMD_GET; 336 - na = (struct nlattr *) GENLMSG_DATA(&req); 337 - na->nla_type = cmd_type; 338 - na->nla_len = sizeof(unsigned int) + NLA_HDRLEN; 339 - *(__u32 *) NLA_DATA(na) = tid; 340 - req.n.nlmsg_len += NLMSG_ALIGN(na->nla_len); 341 - 342 - 343 - if (!forking && sendto_fd(nl_sd, (char *) &req, req.n.nlmsg_len) < 0) 344 - err(1, "error sending message via Netlink\n"); 345 - 346 - act.sa_handler = SIG_IGN; 347 - sigemptyset(&act.sa_mask); 348 - if (sigaction(SIGINT, &act, NULL) < 0) 349 - err(1, "sigaction failed for SIGINT\n"); 350 - 351 - do { 352 - int i; 353 - struct pollfd pfd; 354 - int pollres; 355 - 356 - pfd.events = 0xffff & ~POLLOUT; 357 - pfd.fd = nl_sd; 358 - pollres = poll(&pfd, 1, 5000); 359 - if (pollres < 0 || done) { 360 - break; 361 208 } 362 209 363 - rep_len = recv(nl_sd, &ans, sizeof(ans), 0); 364 - nladdr.nl_family = AF_NETLINK; 365 - nladdr.nl_groups = TASKSTATS_LISTEN_GROUP; 366 - 367 - if (ans.n.nlmsg_type == NLMSG_ERROR) { /* error */ 368 - printf("error received NACK - leaving\n"); 369 - exit(1); 210 + if (write_file) { 211 + fd = open(logfile, O_WRONLY | O_CREAT | O_TRUNC, 212 + S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 213 + if (fd == -1) { 214 + perror("Cannot open output file\n"); 215 + exit(1); 216 + } 370 217 } 371 218 372 - if (rep_len < 0) { 373 - err(1, "error receiving reply message via Netlink\n"); 374 - break; 219 + if ((nl_sd = create_nl_socket(NETLINK_GENERIC)) < 0) 220 + err(1, "error creating Netlink socket\n"); 221 + 222 + 223 + mypid = getpid(); 224 + id = get_family_id(nl_sd); 225 + if (!id) { 226 + printf("Error getting family id, errno %d", errno); 227 + goto err; 228 + } 229 + PRINTF("family id %d\n", id); 230 + 231 + if (maskset) { 232 + rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET, 233 + TASKSTATS_CMD_ATTR_REGISTER_CPUMASK, 234 + &cpumask, sizeof(cpumask)); 235 + PRINTF("Sent register cpumask, retval %d\n", rc); 236 + if (rc < 0) { 237 + printf("error sending register cpumask\n"); 238 + goto err; 239 + } 375 240 } 376 241 377 - /* Validate response message */ 378 - if (!NLMSG_OK((&ans.n), rep_len)) 379 - err(1, "invalid reply message received via Netlink\n"); 242 + if (tid) { 243 + rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET, 244 + cmd_type, &tid, sizeof(__u32)); 245 + PRINTF("Sent pid/tgid, retval %d\n", rc); 246 + if (rc < 0) { 247 + printf("error sending tid/tgid cmd\n"); 248 + goto done; 249 + } 250 + } 380 251 381 - rep_len = GENLMSG_PAYLOAD(&ans.n); 252 + do { 253 + int i; 382 254 383 - na = (struct nlattr *) GENLMSG_DATA(&ans); 384 - len = 0; 385 - i = 0; 386 - while (len < rep_len) { 387 - len += NLA_ALIGN(na->nla_len); 388 - switch (na->nla_type) { 389 - case TASKSTATS_TYPE_AGGR_PID: 390 - /* Fall through */ 391 - case TASKSTATS_TYPE_AGGR_TGID: 392 - aggr_len = NLA_PAYLOAD(na->nla_len); 393 - len2 = 0; 394 - /* For nested attributes, na follows */ 395 - na = (struct nlattr *) NLA_DATA(na); 396 - done = 0; 397 - while (len2 < aggr_len) { 398 - switch (na->nla_type) { 399 - case TASKSTATS_TYPE_PID: 400 - rtid = *(int *) NLA_DATA(na); 401 - break; 402 - case TASKSTATS_TYPE_TGID: 403 - rtid = *(int *) NLA_DATA(na); 404 - break; 405 - case TASKSTATS_TYPE_STATS: 406 - if (rtid == tid) { 407 - print_taskstats((struct taskstats *) 408 - NLA_DATA(na)); 409 - done = 1; 255 + rep_len = recv(nl_sd, &msg, sizeof(msg), 0); 256 + PRINTF("received %d bytes\n", rep_len); 257 + 258 + if (rep_len < 0) { 259 + printf("nonfatal reply error: errno %d\n", errno); 260 + continue; 261 + } 262 + if (msg.n.nlmsg_type == NLMSG_ERROR || 263 + !NLMSG_OK((&msg.n), rep_len)) { 264 + printf("fatal reply error, errno %d\n", errno); 265 + goto done; 266 + } 267 + 268 + PRINTF("nlmsghdr size=%d, nlmsg_len=%d, rep_len=%d\n", 269 + sizeof(struct nlmsghdr), msg.n.nlmsg_len, rep_len); 270 + 271 + 272 + rep_len = GENLMSG_PAYLOAD(&msg.n); 273 + 274 + na = (struct nlattr *) GENLMSG_DATA(&msg); 275 + len = 0; 276 + i = 0; 277 + while (len < rep_len) { 278 + len += NLA_ALIGN(na->nla_len); 279 + switch (na->nla_type) { 280 + case TASKSTATS_TYPE_AGGR_TGID: 281 + /* Fall through */ 282 + case TASKSTATS_TYPE_AGGR_PID: 283 + aggr_len = NLA_PAYLOAD(na->nla_len); 284 + len2 = 0; 285 + /* For nested attributes, na follows */ 286 + na = (struct nlattr *) NLA_DATA(na); 287 + done = 0; 288 + while (len2 < aggr_len) { 289 + switch (na->nla_type) { 290 + case TASKSTATS_TYPE_PID: 291 + rtid = *(int *) NLA_DATA(na); 292 + if (print_delays) 293 + printf("PID\t%d\n", rtid); 294 + break; 295 + case TASKSTATS_TYPE_TGID: 296 + rtid = *(int *) NLA_DATA(na); 297 + if (print_delays) 298 + printf("TGID\t%d\n", rtid); 299 + break; 300 + case TASKSTATS_TYPE_STATS: 301 + count++; 302 + if (print_delays) 303 + print_delayacct((struct taskstats *) NLA_DATA(na)); 304 + if (fd) { 305 + if (write(fd, NLA_DATA(na), na->nla_len) < 0) { 306 + err(1,"write error\n"); 307 + } 308 + } 309 + if (!loop) 310 + goto done; 311 + break; 312 + default: 313 + printf("Unknown nested nla_type %d\n", na->nla_type); 314 + break; 315 + } 316 + len2 += NLA_ALIGN(na->nla_len); 317 + na = (struct nlattr *) ((char *) na + len2); 318 + } 319 + break; 320 + 321 + default: 322 + printf("Unknown nla_type %d\n", na->nla_type); 323 + break; 410 324 } 411 - break; 412 - } 413 - len2 += NLA_ALIGN(na->nla_len); 414 - na = (struct nlattr *) ((char *) na + len2); 415 - if (done) 416 - break; 325 + na = (struct nlattr *) (GENLMSG_DATA(&msg) + len); 417 326 } 418 - } 419 - na = (struct nlattr *) (GENLMSG_DATA(&ans) + len); 420 - if (done) 421 - break; 327 + } while (loop); 328 + done: 329 + if (maskset) { 330 + rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET, 331 + TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK, 332 + &cpumask, sizeof(cpumask)); 333 + printf("Sent deregister mask, retval %d\n", rc); 334 + if (rc < 0) 335 + err(rc, "error sending deregister cpumask\n"); 422 336 } 423 - if (done) 424 - break; 425 - } 426 - while (1); 427 - 428 - close(nl_sd); 429 - return 0; 337 + err: 338 + close(nl_sd); 339 + if (fd) 340 + close(fd); 341 + return 0; 430 342 }
+52 -12
Documentation/accounting/taskstats.txt
··· 26 26 Usage 27 27 ----- 28 28 29 - To get statistics during task's lifetime, userspace opens a unicast netlink 29 + To get statistics during a task's lifetime, userspace opens a unicast netlink 30 30 socket (NETLINK_GENERIC family) and sends commands specifying a pid or a tgid. 31 31 The response contains statistics for a task (if pid is specified) or the sum of 32 32 statistics for all tasks of the process (if tgid is specified). 33 33 34 - To obtain statistics for tasks which are exiting, userspace opens a multicast 35 - netlink socket. Each time a task exits, its per-pid statistics is always sent 36 - by the kernel to each listener on the multicast socket. In addition, if it is 37 - the last thread exiting its thread group, an additional record containing the 38 - per-tgid stats are also sent. The latter contains the sum of per-pid stats for 39 - all threads in the thread group, both past and present. 34 + To obtain statistics for tasks which are exiting, the userspace listener 35 + sends a register command and specifies a cpumask. Whenever a task exits on 36 + one of the cpus in the cpumask, its per-pid statistics are sent to the 37 + registered listener. Using cpumasks allows the data received by one listener 38 + to be limited and assists in flow control over the netlink interface and is 39 + explained in more detail below. 40 + 41 + If the exiting task is the last thread exiting its thread group, 42 + an additional record containing the per-tgid stats is also sent to userspace. 43 + The latter contains the sum of per-pid stats for all threads in the thread 44 + group, both past and present. 40 45 41 46 getdelays.c is a simple utility demonstrating usage of the taskstats interface 42 - for reporting delay accounting statistics. 47 + for reporting delay accounting statistics. Users can register cpumasks, 48 + send commands and process responses, listen for per-tid/tgid exit data, 49 + write the data received to a file and do basic flow control by increasing 50 + receive buffer sizes. 43 51 44 52 Interface 45 53 --------- ··· 74 66 75 67 The taskstats payload is one of the following three kinds: 76 68 77 - 1. Commands: Sent from user to kernel. The payload is one attribute, of type 78 - TASKSTATS_CMD_ATTR_PID/TGID, containing a u32 pid or tgid in the attribute 79 - payload. The pid/tgid denotes the task/process for which userspace wants 80 - statistics. 69 + 1. Commands: Sent from user to kernel. Commands to get data on 70 + a pid/tgid consist of one attribute, of type TASKSTATS_CMD_ATTR_PID/TGID, 71 + containing a u32 pid or tgid in the attribute payload. The pid/tgid denotes 72 + the task/process for which userspace wants statistics. 73 + 74 + Commands to register/deregister interest in exit data from a set of cpus 75 + consist of one attribute, of type 76 + TASKSTATS_CMD_ATTR_REGISTER/DEREGISTER_CPUMASK and contain a cpumask in the 77 + attribute payload. The cpumask is specified as an ascii string of 78 + comma-separated cpu ranges e.g. to listen to exit data from cpus 1,2,3,5,7,8 79 + the cpumask would be "1-3,5,7-8". If userspace forgets to deregister interest 80 + in cpus before closing the listening socket, the kernel cleans up its interest 81 + set over time. However, for the sake of efficiency, an explicit deregistration 82 + is advisable. 81 83 82 84 2. Response for a command: sent from the kernel in response to a userspace 83 85 command. The payload is a series of three attributes of type: ··· 155 137 struct too much, requiring disparate userspace accounting utilities to 156 138 unnecessarily receive large structures whose fields are of no interest, then 157 139 extending the attributes structure would be worthwhile. 140 + 141 + Flow control for taskstats 142 + -------------------------- 143 + 144 + When the rate of task exits becomes large, a listener may not be able to keep 145 + up with the kernel's rate of sending per-tid/tgid exit data leading to data 146 + loss. This possibility gets compounded when the taskstats structure gets 147 + extended and the number of cpus grows large. 148 + 149 + To avoid losing statistics, userspace should do one or more of the following: 150 + 151 + - increase the receive buffer sizes for the netlink sockets opened by 152 + listeners to receive exit data. 153 + 154 + - create more listeners and reduce the number of cpus being listened to by 155 + each listener. In the extreme case, there could be one listener for each cpu. 156 + Users may also consider setting the cpu affinity of the listener to the subset 157 + of cpus to which it listens, especially if they are listening to just one cpu. 158 + 159 + Despite these measures, if the userspace receives ENOBUFS error messages 160 + indicated overflow of receive buffers, it should take measures to handle the 161 + loss of data. 158 162 159 163 ----