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