1279842Sscottl/*-
2279842Sscottl * Copyright (c) 2007 Robert N. M. Watson
3287486Sallanjude * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org>
4279842Sscottl * All rights reserved.
5279842Sscottl *
6279842Sscottl * Redistribution and use in source and binary forms, with or without
7279842Sscottl * modification, are permitted provided that the following conditions
8279842Sscottl * are met:
9279842Sscottl * 1. Redistributions of source code must retain the above copyright
10279842Sscottl *    notice, this list of conditions and the following disclaimer.
11279842Sscottl * 2. Redistributions in binary form must reproduce the above copyright
12279842Sscottl *    notice, this list of conditions and the following disclaimer in the
13279842Sscottl *    documentation and/or other materials provided with the distribution.
14279842Sscottl *
15279842Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16279842Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17279842Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18279842Sscottl * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19279842Sscottl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20279842Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21279842Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22279842Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23279842Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24279842Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25279842Sscottl * SUCH DAMAGE.
26279842Sscottl *
27279842Sscottl * $FreeBSD: stable/11/usr.bin/procstat/procstat_cs.c 310120 2016-12-15 16:51:33Z vangyzen $
28279842Sscottl */
29279842Sscottl
30279842Sscottl#include <sys/param.h>
31279842Sscottl#include <sys/cpuset.h>
32287486Sallanjude#include <sys/sbuf.h>
33279842Sscottl#include <sys/sysctl.h>
34279842Sscottl#include <sys/user.h>
35279842Sscottl
36279842Sscottl#include <err.h>
37279842Sscottl#include <errno.h>
38279842Sscottl#include <libprocstat.h>
39279842Sscottl#include <stdio.h>
40279842Sscottl#include <stdlib.h>
41279842Sscottl#include <string.h>
42279842Sscottl
43279842Sscottl#include "procstat.h"
44279842Sscottl
45279842Sscottlvoid
46279842Sscottlprocstat_cs(struct procstat *procstat, struct kinfo_proc *kipp)
47279842Sscottl{
48279842Sscottl	cpusetid_t cs;
49279842Sscottl	cpuset_t mask;
50279842Sscottl	struct kinfo_proc *kip;
51287486Sallanjude	struct sbuf *cpusetbuf;
52279842Sscottl	unsigned int count, i;
53279842Sscottl	int once, twice, lastcpu, cpu;
54279842Sscottl
55279842Sscottl	if (!hflag)
56310120Svangyzen		xo_emit("{T:/%5s %6s %-19s %-19s %2s %4s %-7s}\n", "PID",
57279842Sscottl		    "TID", "COMM", "TDNAME", "CPU", "CSID", "CPU MASK");
58279842Sscottl
59279842Sscottl	kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD,
60279842Sscottl	    kipp->ki_pid, &count);
61279842Sscottl	if (kip == NULL)
62279842Sscottl		return;
63279842Sscottl	kinfo_proc_sort(kip, count);
64279842Sscottl	for (i = 0; i < count; i++) {
65279842Sscottl		kipp = &kip[i];
66287486Sallanjude		xo_emit("{k:process_id/%5d/%d} ", kipp->ki_pid);
67287486Sallanjude		xo_emit("{:thread_id/%6d/%d} ", kipp->ki_tid);
68310120Svangyzen		xo_emit("{:command/%-19s/%s} ", strlen(kipp->ki_comm) ?
69279842Sscottl		    kipp->ki_comm : "-");
70310120Svangyzen		xo_emit("{:thread_name/%-19s/%s} ",
71310120Svangyzen                    kinfo_proc_thread_name(kipp));
72279842Sscottl		if (kipp->ki_oncpu != 255)
73287486Sallanjude			xo_emit("{:cpu/%3d/%d} ", kipp->ki_oncpu);
74279842Sscottl		else if (kipp->ki_lastcpu != 255)
75287486Sallanjude			xo_emit("{:cpu/%3d/%d} ", kipp->ki_lastcpu);
76279842Sscottl		else
77287486Sallanjude			xo_emit("{:cpu/%3s/%s} ", "-");
78279842Sscottl		if (cpuset_getid(CPU_LEVEL_CPUSET, CPU_WHICH_TID,
79279842Sscottl		    kipp->ki_tid, &cs) != 0) {
80279842Sscottl			cs = CPUSET_INVALID;
81279842Sscottl		}
82287486Sallanjude		xo_emit("{:cpu_set_id/%4d/%d} ", cs);
83279842Sscottl		if ((cs != CPUSET_INVALID) &&
84279842Sscottl		    (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID,
85279842Sscottl		    kipp->ki_tid, sizeof(mask), &mask) == 0)) {
86279842Sscottl			lastcpu = -1;
87279842Sscottl			once = 0;
88279842Sscottl			twice = 0;
89287486Sallanjude			cpusetbuf = sbuf_new_auto();
90279842Sscottl			for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
91279842Sscottl				if (CPU_ISSET(cpu, &mask)) {
92279842Sscottl					if (once == 0) {
93287486Sallanjude						sbuf_printf(cpusetbuf, "%d",
94287486Sallanjude						    cpu);
95279842Sscottl						once = 1;
96279842Sscottl					} else if (cpu == lastcpu + 1) {
97279842Sscottl						twice = 1;
98279842Sscottl					} else if (twice == 1) {
99287486Sallanjude						sbuf_printf(cpusetbuf, "-%d,%d",
100287486Sallanjude						    lastcpu, cpu);
101279842Sscottl						twice = 0;
102279842Sscottl					} else
103287486Sallanjude						sbuf_printf(cpusetbuf, ",%d",
104287486Sallanjude						    cpu);
105279842Sscottl					lastcpu = cpu;
106279842Sscottl				}
107279842Sscottl			}
108279842Sscottl			if (once && twice)
109287486Sallanjude				sbuf_printf(cpusetbuf, "-%d", lastcpu);
110287486Sallanjude			if (sbuf_finish(cpusetbuf) != 0)
111287486Sallanjude				xo_err(1, "Could not generate output");
112287486Sallanjude			xo_emit("{:cpu_set/%s}", sbuf_data(cpusetbuf));
113287486Sallanjude			sbuf_delete(cpusetbuf);
114279842Sscottl		}
115287486Sallanjude		xo_emit("\n");
116279842Sscottl	}
117279842Sscottl	procstat_freeprocs(procstat, kip);
118279842Sscottl}
119