Changeset View
Changeset View
Standalone View
Standalone View
usr.bin/procstat/procstat_cs.c
/*- | /*- | ||||
* Copyright (c) 2007 Robert N. M. Watson | * Copyright (c) 2007 Robert N. M. Watson | ||||
* Copyright (c) 2015 Allan Jude <allanjude@freebsd.org> | |||||
* All rights reserved. | * All rights reserved. | ||||
* | * | ||||
* Redistribution and use in source and binary forms, with or without | * Redistribution and use in source and binary forms, with or without | ||||
* modification, are permitted provided that the following conditions | * modification, are permitted provided that the following conditions | ||||
* are met: | * are met: | ||||
* 1. Redistributions of source code must retain the above copyright | * 1. Redistributions of source code must retain the above copyright | ||||
* notice, this list of conditions and the following disclaimer. | * notice, this list of conditions and the following disclaimer. | ||||
* 2. Redistributions in binary form must reproduce the above copyright | * 2. Redistributions in binary form must reproduce the above copyright | ||||
Show All 12 Lines | |||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||||
* SUCH DAMAGE. | * SUCH DAMAGE. | ||||
* | * | ||||
* $FreeBSD$ | * $FreeBSD$ | ||||
*/ | */ | ||||
#include <sys/param.h> | #include <sys/param.h> | ||||
#include <sys/cpuset.h> | #include <sys/cpuset.h> | ||||
#include <sys/sbuf.h> | |||||
#include <sys/sysctl.h> | #include <sys/sysctl.h> | ||||
#include <sys/user.h> | #include <sys/user.h> | ||||
#include <err.h> | #include <err.h> | ||||
#include <errno.h> | #include <errno.h> | ||||
#include <libprocstat.h> | #include <libprocstat.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include "procstat.h" | #include "procstat.h" | ||||
void | void | ||||
procstat_cs(struct procstat *procstat, struct kinfo_proc *kipp) | procstat_cs(struct procstat *procstat, struct kinfo_proc *kipp) | ||||
{ | { | ||||
cpusetid_t cs; | cpusetid_t cs; | ||||
cpuset_t mask; | cpuset_t mask; | ||||
struct kinfo_proc *kip; | struct kinfo_proc *kip; | ||||
struct sbuf *cpusetbuf; | |||||
unsigned int count, i; | unsigned int count, i; | ||||
int once, twice, lastcpu, cpu; | int once, twice, lastcpu, cpu; | ||||
if (!hflag) | if (!hflag) | ||||
printf("%5s %6s %-16s %-16s %2s %4s %-7s\n", "PID", | xo_emit("{T:/%5s %6s %-16s %-16s %2s %4s %-7s}\n", "PID", | ||||
"TID", "COMM", "TDNAME", "CPU", "CSID", "CPU MASK"); | "TID", "COMM", "TDNAME", "CPU", "CSID", "CPU MASK"); | ||||
kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD, | kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD, | ||||
kipp->ki_pid, &count); | kipp->ki_pid, &count); | ||||
if (kip == NULL) | if (kip == NULL) | ||||
return; | return; | ||||
kinfo_proc_sort(kip, count); | kinfo_proc_sort(kip, count); | ||||
for (i = 0; i < count; i++) { | for (i = 0; i < count; i++) { | ||||
kipp = &kip[i]; | kipp = &kip[i]; | ||||
printf("%5d ", kipp->ki_pid); | xo_emit("{k:process_id/%5d/%d} ", kipp->ki_pid); | ||||
printf("%6d ", kipp->ki_tid); | xo_emit("{:thread_id/%6d/%d} ", kipp->ki_tid); | ||||
printf("%-16s ", strlen(kipp->ki_comm) ? | xo_emit("{:command/%-16s/%s} ", strlen(kipp->ki_comm) ? | ||||
kipp->ki_comm : "-"); | kipp->ki_comm : "-"); | ||||
printf("%-16s ", (strlen(kipp->ki_tdname) && | xo_emit("{:thread_name/%-16s/%s} ", (strlen(kipp->ki_tdname) && | ||||
(strcmp(kipp->ki_comm, kipp->ki_tdname) != 0)) ? | (strcmp(kipp->ki_comm, kipp->ki_tdname) != 0)) ? | ||||
kipp->ki_tdname : "-"); | kipp->ki_tdname : "-"); | ||||
if (kipp->ki_oncpu != 255) | if (kipp->ki_oncpu != 255) | ||||
printf("%3d ", kipp->ki_oncpu); | xo_emit("{:cpu/%3d/%d} ", kipp->ki_oncpu); | ||||
else if (kipp->ki_lastcpu != 255) | else if (kipp->ki_lastcpu != 255) | ||||
printf("%3d ", kipp->ki_lastcpu); | xo_emit("{:cpu/%3d/%d} ", kipp->ki_lastcpu); | ||||
else | else | ||||
printf("%3s ", "-"); | xo_emit("{:cpu/%3s/%s} ", "-"); | ||||
if (cpuset_getid(CPU_LEVEL_CPUSET, CPU_WHICH_TID, | if (cpuset_getid(CPU_LEVEL_CPUSET, CPU_WHICH_TID, | ||||
kipp->ki_tid, &cs) != 0) { | kipp->ki_tid, &cs) != 0) { | ||||
cs = CPUSET_INVALID; | cs = CPUSET_INVALID; | ||||
} | } | ||||
printf("%4d ", cs); | xo_emit("{:cpu_set_id/%4d/%d} ", cs); | ||||
if ((cs != CPUSET_INVALID) && | if ((cs != CPUSET_INVALID) && | ||||
(cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, | (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, | ||||
kipp->ki_tid, sizeof(mask), &mask) == 0)) { | kipp->ki_tid, sizeof(mask), &mask) == 0)) { | ||||
lastcpu = -1; | lastcpu = -1; | ||||
once = 0; | once = 0; | ||||
twice = 0; | twice = 0; | ||||
cpusetbuf = sbuf_new_auto(); | |||||
for (cpu = 0; cpu < CPU_SETSIZE; cpu++) { | for (cpu = 0; cpu < CPU_SETSIZE; cpu++) { | ||||
if (CPU_ISSET(cpu, &mask)) { | if (CPU_ISSET(cpu, &mask)) { | ||||
if (once == 0) { | if (once == 0) { | ||||
printf("%d", cpu); | sbuf_printf(cpusetbuf, "%d", cpu); | ||||
once = 1; | once = 1; | ||||
} else if (cpu == lastcpu + 1) { | } else if (cpu == lastcpu + 1) { | ||||
twice = 1; | twice = 1; | ||||
} else if (twice == 1) { | } else if (twice == 1) { | ||||
printf("-%d,%d", lastcpu, cpu); | sbuf_printf(cpusetbuf, "-%d,%d", lastcpu, cpu); | ||||
twice = 0; | twice = 0; | ||||
} else | } else | ||||
printf(",%d", cpu); | sbuf_printf(cpusetbuf, ",%d", cpu); | ||||
lastcpu = cpu; | lastcpu = cpu; | ||||
} | } | ||||
} | } | ||||
if (once && twice) | if (once && twice) | ||||
printf("-%d", lastcpu); | sbuf_printf(cpusetbuf, "-%d", lastcpu); | ||||
if (sbuf_finish(cpusetbuf) != 0) | |||||
xo_err(1, "Could not generate output"); | |||||
xo_emit("{:cpu_set/%s}", sbuf_data(cpusetbuf)); | |||||
sbuf_delete(cpusetbuf); | |||||
} | } | ||||
printf("\n"); | xo_emit("\n"); | ||||
} | } | ||||
procstat_freeprocs(procstat, kip); | procstat_freeprocs(procstat, kip); | ||||
} | } |