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 %-16s %-16s %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/%-16s/%s} ", strlen(kipp->ki_comm) ? 69 kipp->ki_comm : "-"); 70 xo_emit("{:thread_name/%-16s/%s} ", (strlen(kipp->ki_tdname) && 71 (strcmp(kipp->ki_comm, kipp->ki_tdname) != 0)) ? 72 kipp->ki_tdname : "-"); 73 if (kipp->ki_oncpu != 255) 74 xo_emit("{:cpu/%3d/%d} ", kipp->ki_oncpu); 75 else if (kipp->ki_lastcpu != 255) 76 xo_emit("{:cpu/%3d/%d} ", kipp->ki_lastcpu); 77 else 78 xo_emit("{:cpu/%3s/%s} ", "-"); 79 if (cpuset_getid(CPU_LEVEL_CPUSET, CPU_WHICH_TID, 80 kipp->ki_tid, &cs) != 0) { 81 cs = CPUSET_INVALID; 82 } 83 xo_emit("{:cpu_set_id/%4d/%d} ", cs); 84 if ((cs != CPUSET_INVALID) && 85 (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, 86 kipp->ki_tid, sizeof(mask), &mask) == 0)) { 87 lastcpu = -1; 88 once = 0; 89 twice = 0; 90 cpusetbuf = sbuf_new_auto(); 91 for (cpu = 0; cpu < CPU_SETSIZE; cpu++) { 92 if (CPU_ISSET(cpu, &mask)) { 93 if (once == 0) { 94 sbuf_printf(cpusetbuf, "%d", 95 cpu); 96 once = 1; 97 } else if (cpu == lastcpu + 1) { 98 twice = 1; 99 } else if (twice == 1) { 100 sbuf_printf(cpusetbuf, "-%d,%d", 101 lastcpu, cpu); 102 twice = 0; 103 } else 104 sbuf_printf(cpusetbuf, ",%d", 105 cpu); 106 lastcpu = cpu; 107 } 108 } 109 if (once && twice) 110 sbuf_printf(cpusetbuf, "-%d", lastcpu); 111 if (sbuf_finish(cpusetbuf) != 0) 112 xo_err(1, "Could not generate output"); 113 xo_emit("{:cpu_set/%s}", sbuf_data(cpusetbuf)); 114 sbuf_delete(cpusetbuf); 115 } 116 xo_emit("\n"); 117 } 118 procstat_freeprocs(procstat, kip); 119 } 120