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