xref: /freebsd/usr.bin/procstat/procstat_cs.c (revision 315ee00f)
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 #include <sys/param.h>
30 #include <sys/cpuset.h>
31 #include <sys/sbuf.h>
32 #include <sys/sysctl.h>
33 #include <sys/user.h>
34 
35 #include <err.h>
36 #include <errno.h>
37 #include <libprocstat.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 
42 #include "procstat.h"
43 
44 void
45 procstat_cs(struct procstat *procstat, struct kinfo_proc *kipp)
46 {
47 	cpusetid_t cs;
48 	cpuset_t mask;
49 	struct kinfo_proc *kip;
50 	struct sbuf *cpusetbuf;
51 	unsigned int count, i;
52 	int once, twice, lastcpu, cpu;
53 
54 	if ((procstat_opts & PS_OPT_NOHEADER) == 0)
55 		xo_emit("{T:/%5s %6s %-19s %-19s %2s %4s %-7s}\n", "PID",
56 		    "TID", "COMM", "TDNAME", "CPU", "CSID", "CPU MASK");
57 
58 	kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD,
59 	    kipp->ki_pid, &count);
60 	if (kip == NULL)
61 		return;
62 	kinfo_proc_sort(kip, count);
63 	for (i = 0; i < count; i++) {
64 		kipp = &kip[i];
65 		xo_emit("{k:process_id/%5d/%d} ", kipp->ki_pid);
66 		xo_emit("{:thread_id/%6d/%d} ", kipp->ki_tid);
67 		xo_emit("{:command/%-19s/%s} ", strlen(kipp->ki_comm) ?
68 		    kipp->ki_comm : "-");
69 		xo_emit("{:thread_name/%-19s/%s} ",
70                     kinfo_proc_thread_name(kipp));
71 		if (kipp->ki_oncpu != 255)
72 			xo_emit("{:cpu/%3d/%d} ", kipp->ki_oncpu);
73 		else if (kipp->ki_lastcpu != 255)
74 			xo_emit("{:cpu/%3d/%d} ", kipp->ki_lastcpu);
75 		else
76 			xo_emit("{:cpu/%3s/%s} ", "-");
77 		if (cpuset_getid(CPU_LEVEL_CPUSET, CPU_WHICH_TID,
78 		    kipp->ki_tid, &cs) != 0) {
79 			cs = CPUSET_INVALID;
80 		}
81 		xo_emit("{:cpu_set_id/%4d/%d} ", cs);
82 		if ((cs != CPUSET_INVALID) &&
83 		    (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID,
84 		    kipp->ki_tid, sizeof(mask), &mask) == 0)) {
85 			lastcpu = -1;
86 			once = 0;
87 			twice = 0;
88 			cpusetbuf = sbuf_new_auto();
89 			for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
90 				if (CPU_ISSET(cpu, &mask)) {
91 					if (once == 0) {
92 						sbuf_printf(cpusetbuf, "%d",
93 						    cpu);
94 						once = 1;
95 					} else if (cpu == lastcpu + 1) {
96 						twice = 1;
97 					} else if (twice == 1) {
98 						sbuf_printf(cpusetbuf, "-%d,%d",
99 						    lastcpu, cpu);
100 						twice = 0;
101 					} else
102 						sbuf_printf(cpusetbuf, ",%d",
103 						    cpu);
104 					lastcpu = cpu;
105 				}
106 			}
107 			if (once && twice)
108 				sbuf_printf(cpusetbuf, "-%d", lastcpu);
109 			if (sbuf_finish(cpusetbuf) != 0)
110 				xo_err(1, "Could not generate output");
111 			xo_emit("{:cpu_set/%s}", sbuf_data(cpusetbuf));
112 			sbuf_delete(cpusetbuf);
113 		}
114 		xo_emit("\n");
115 	}
116 	procstat_freeprocs(procstat, kip);
117 }
118