1279842Sscottl/*-
2279842Sscottl * Copyright (c) 2007 Robert N. M. Watson
3279842Sscottl * All rights reserved.
4279842Sscottl *
5279842Sscottl * Redistribution and use in source and binary forms, with or without
6279842Sscottl * modification, are permitted provided that the following conditions
7279842Sscottl * are met:
8279842Sscottl * 1. Redistributions of source code must retain the above copyright
9279842Sscottl *    notice, this list of conditions and the following disclaimer.
10279842Sscottl * 2. Redistributions in binary form must reproduce the above copyright
11279842Sscottl *    notice, this list of conditions and the following disclaimer in the
12279842Sscottl *    documentation and/or other materials provided with the distribution.
13279842Sscottl *
14279842Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15279842Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16279842Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17279842Sscottl * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18279842Sscottl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19279842Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20279842Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21279842Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22279842Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23279842Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24279842Sscottl * SUCH DAMAGE.
25279842Sscottl *
26279842Sscottl * $FreeBSD: stable/10/usr.bin/procstat/procstat_cs.c 310121 2016-12-15 16:52:17Z vangyzen $
27279842Sscottl */
28279842Sscottl
29279842Sscottl#include <sys/param.h>
30279842Sscottl#include <sys/cpuset.h>
31279842Sscottl#include <sys/sysctl.h>
32279842Sscottl#include <sys/user.h>
33279842Sscottl
34279842Sscottl#include <err.h>
35279842Sscottl#include <errno.h>
36279842Sscottl#include <libprocstat.h>
37279842Sscottl#include <stdio.h>
38279842Sscottl#include <stdlib.h>
39279842Sscottl#include <string.h>
40279842Sscottl
41279842Sscottl#include "procstat.h"
42279842Sscottl
43279842Sscottlvoid
44279842Sscottlprocstat_cs(struct procstat *procstat, struct kinfo_proc *kipp)
45279842Sscottl{
46279842Sscottl	cpusetid_t cs;
47279842Sscottl	cpuset_t mask;
48279842Sscottl	struct kinfo_proc *kip;
49279842Sscottl	unsigned int count, i;
50279842Sscottl	int once, twice, lastcpu, cpu;
51279842Sscottl
52279842Sscottl	if (!hflag)
53310121Svangyzen		printf("%5s %6s %-19s %-19s %2s %4s %-7s\n", "PID",
54279842Sscottl		    "TID", "COMM", "TDNAME", "CPU", "CSID", "CPU MASK");
55279842Sscottl
56279842Sscottl	kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD,
57279842Sscottl	    kipp->ki_pid, &count);
58279842Sscottl	if (kip == NULL)
59279842Sscottl		return;
60279842Sscottl	kinfo_proc_sort(kip, count);
61279842Sscottl	for (i = 0; i < count; i++) {
62279842Sscottl		kipp = &kip[i];
63279842Sscottl		printf("%5d ", kipp->ki_pid);
64279842Sscottl		printf("%6d ", kipp->ki_tid);
65310121Svangyzen		printf("%-19s ", strlen(kipp->ki_comm) ?
66279842Sscottl		    kipp->ki_comm : "-");
67310121Svangyzen		printf("%-19s ", kinfo_proc_thread_name(kipp));
68279842Sscottl		if (kipp->ki_oncpu != 255)
69279842Sscottl			printf("%3d ", kipp->ki_oncpu);
70279842Sscottl		else if (kipp->ki_lastcpu != 255)
71279842Sscottl			printf("%3d ", kipp->ki_lastcpu);
72279842Sscottl		else
73279842Sscottl			printf("%3s ", "-");
74279842Sscottl		if (cpuset_getid(CPU_LEVEL_CPUSET, CPU_WHICH_TID,
75279842Sscottl		    kipp->ki_tid, &cs) != 0) {
76279842Sscottl			cs = CPUSET_INVALID;
77279842Sscottl		}
78279842Sscottl		printf("%4d ", cs);
79279842Sscottl		if ((cs != CPUSET_INVALID) &&
80279842Sscottl		    (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID,
81279842Sscottl		    kipp->ki_tid, sizeof(mask), &mask) == 0)) {
82279842Sscottl			lastcpu = -1;
83279842Sscottl			once = 0;
84279842Sscottl			twice = 0;
85279842Sscottl			for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
86279842Sscottl				if (CPU_ISSET(cpu, &mask)) {
87279842Sscottl					if (once == 0) {
88279842Sscottl						printf("%d", cpu);
89279842Sscottl						once = 1;
90279842Sscottl					} else if (cpu == lastcpu + 1) {
91279842Sscottl						twice = 1;
92279842Sscottl					} else if (twice == 1) {
93279842Sscottl						printf("-%d,%d", lastcpu, cpu);
94279842Sscottl						twice = 0;
95279842Sscottl					} else
96279842Sscottl						printf(",%d", cpu);
97279842Sscottl					lastcpu = cpu;
98279842Sscottl				}
99279842Sscottl			}
100279842Sscottl			if (once && twice)
101279842Sscottl				printf("-%d", lastcpu);
102279842Sscottl		}
103279842Sscottl		printf("\n");
104279842Sscottl	}
105279842Sscottl	procstat_freeprocs(procstat, kip);
106279842Sscottl}
107