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