xref: /freebsd/usr.bin/procstat/procstat_cs.c (revision 4d846d26)
1 /*-
2  * Copyright (c) 2007 Robert N. M. Watson
3  * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/cpuset.h>
33 #include <sys/sbuf.h>
34 #include <sys/sysctl.h>
35 #include <sys/user.h>
36 
37 #include <err.h>
38 #include <errno.h>
39 #include <libprocstat.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 
44 #include "procstat.h"
45 
46 void
47 procstat_cs(struct procstat *procstat, struct kinfo_proc *kipp)
48 {
49 	cpusetid_t cs;
50 	cpuset_t mask;
51 	struct kinfo_proc *kip;
52 	struct sbuf *cpusetbuf;
53 	unsigned int count, i;
54 	int once, twice, lastcpu, cpu;
55 
56 	if ((procstat_opts & PS_OPT_NOHEADER) == 0)
57 		xo_emit("{T:/%5s %6s %-19s %-19s %2s %4s %-7s}\n", "PID",
58 		    "TID", "COMM", "TDNAME", "CPU", "CSID", "CPU MASK");
59 
60 	kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD,
61 	    kipp->ki_pid, &count);
62 	if (kip == NULL)
63 		return;
64 	kinfo_proc_sort(kip, count);
65 	for (i = 0; i < count; i++) {
66 		kipp = &kip[i];
67 		xo_emit("{k:process_id/%5d/%d} ", kipp->ki_pid);
68 		xo_emit("{:thread_id/%6d/%d} ", kipp->ki_tid);
69 		xo_emit("{:command/%-19s/%s} ", strlen(kipp->ki_comm) ?
70 		    kipp->ki_comm : "-");
71 		xo_emit("{:thread_name/%-19s/%s} ",
72                     kinfo_proc_thread_name(kipp));
73 		if (kipp->ki_oncpu != 255)
74 			xo_emit("{:cpu/%3d/%d} ", kipp->ki_oncpu);
75 		else if (kipp->ki_lastcpu != 255)
76 			xo_emit("{:cpu/%3d/%d} ", kipp->ki_lastcpu);
77 		else
78 			xo_emit("{:cpu/%3s/%s} ", "-");
79 		if (cpuset_getid(CPU_LEVEL_CPUSET, CPU_WHICH_TID,
80 		    kipp->ki_tid, &cs) != 0) {
81 			cs = CPUSET_INVALID;
82 		}
83 		xo_emit("{:cpu_set_id/%4d/%d} ", cs);
84 		if ((cs != CPUSET_INVALID) &&
85 		    (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID,
86 		    kipp->ki_tid, sizeof(mask), &mask) == 0)) {
87 			lastcpu = -1;
88 			once = 0;
89 			twice = 0;
90 			cpusetbuf = sbuf_new_auto();
91 			for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
92 				if (CPU_ISSET(cpu, &mask)) {
93 					if (once == 0) {
94 						sbuf_printf(cpusetbuf, "%d",
95 						    cpu);
96 						once = 1;
97 					} else if (cpu == lastcpu + 1) {
98 						twice = 1;
99 					} else if (twice == 1) {
100 						sbuf_printf(cpusetbuf, "-%d,%d",
101 						    lastcpu, cpu);
102 						twice = 0;
103 					} else
104 						sbuf_printf(cpusetbuf, ",%d",
105 						    cpu);
106 					lastcpu = cpu;
107 				}
108 			}
109 			if (once && twice)
110 				sbuf_printf(cpusetbuf, "-%d", lastcpu);
111 			if (sbuf_finish(cpusetbuf) != 0)
112 				xo_err(1, "Could not generate output");
113 			xo_emit("{:cpu_set/%s}", sbuf_data(cpusetbuf));
114 			sbuf_delete(cpusetbuf);
115 		}
116 		xo_emit("\n");
117 	}
118 	procstat_freeprocs(procstat, kip);
119 }
120