2 * Copyright (c) 2007 Robert N. M. Watson
3 * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/param.h>
31 #include <sys/cpuset.h>
33 #include <sys/sysctl.h>
38 #include <libprocstat.h>
46 procstat_cs(struct procstat *procstat, struct kinfo_proc *kipp)
50 struct kinfo_proc *kip;
51 struct sbuf *cpusetbuf;
52 unsigned int count, i;
53 int once, twice, lastcpu, cpu;
55 if ((procstat_opts & PS_OPT_NOHEADER) == 0)
56 xo_emit("{T:/%5s %6s %-19s %-19s %2s %4s %-7s}\n", "PID",
57 "TID", "COMM", "TDNAME", "CPU", "CSID", "CPU MASK");
59 kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD,
60 kipp->ki_pid, &count);
63 kinfo_proc_sort(kip, count);
64 for (i = 0; i < count; 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) ?
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);
77 xo_emit("{:cpu/%3s/%s} ", "-");
78 if (cpuset_getid(CPU_LEVEL_CPUSET, CPU_WHICH_TID,
79 kipp->ki_tid, &cs) != 0) {
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)) {
89 cpusetbuf = sbuf_new_auto();
90 for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
91 if (CPU_ISSET(cpu, &mask)) {
93 sbuf_printf(cpusetbuf, "%d",
96 } else if (cpu == lastcpu + 1) {
98 } else if (twice == 1) {
99 sbuf_printf(cpusetbuf, "-%d,%d",
103 sbuf_printf(cpusetbuf, ",%d",
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);
117 procstat_freeprocs(procstat, kip);