Changeset View
Changeset View
Standalone View
Standalone View
usr.bin/procstat/procstat_threads.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 29 Lines | |||||
#include "procstat.h" | #include "procstat.h" | ||||
void | void | ||||
procstat_threads(struct procstat *procstat, struct kinfo_proc *kipp) | procstat_threads(struct procstat *procstat, struct kinfo_proc *kipp) | ||||
{ | { | ||||
struct kinfo_proc *kip; | struct kinfo_proc *kip; | ||||
unsigned int count, i; | unsigned int count, i; | ||||
const char *str; | const char *str; | ||||
char *threadid; | |||||
if (!hflag) | if (!hflag) | ||||
printf("%5s %6s %-16s %-16s %2s %4s %-7s %-9s\n", "PID", | xo_emit("{T:/%5s %6s %-16s %-16s %2s %4s %-7s %-9s}\n", "PID", | ||||
"TID", "COMM", "TDNAME", "CPU", "PRI", "STATE", "WCHAN"); | "TID", "COMM", "TDNAME", "CPU", "PRI", "STATE", "WCHAN"); | ||||
xo_emit("{ek:process_id/%d}", kipp->ki_pid); | |||||
xo_emit("{e:command/%s}", strlen(kipp->ki_comm) ? | |||||
kipp->ki_comm : "-"); | |||||
xo_open_container("threads"); | |||||
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); | asprintf(&threadid, "%d", kipp->ki_tid); | ||||
printf("%6d ", kipp->ki_tid); | if (threadid == NULL) | ||||
printf("%-16s ", strlen(kipp->ki_comm) ? | xo_errc(1, ENOMEM, "Failed to allocate memory in " | ||||
"procstat_threads()"); | |||||
xo_open_container(threadid); | |||||
xo_emit("{dk:process_id/%5d/%d} ", kipp->ki_pid); | |||||
xo_emit("{:thread_id/%6d/%d} ", kipp->ki_tid); | |||||
xo_emit("{d: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} ", "-"); | ||||
printf("%4d ", kipp->ki_pri.pri_level); | xo_emit("{:priority/%4d/%d} ", kipp->ki_pri.pri_level); | ||||
switch (kipp->ki_stat) { | switch (kipp->ki_stat) { | ||||
case SRUN: | case SRUN: | ||||
str = "run"; | str = "run"; | ||||
break; | break; | ||||
case SSTOP: | case SSTOP: | ||||
str = "stop"; | str = "stop"; | ||||
break; | break; | ||||
Show All 17 Lines | for (i = 0; i < count; i++) { | ||||
case SIDL: | case SIDL: | ||||
str = "idle"; | str = "idle"; | ||||
break; | break; | ||||
default: | default: | ||||
str = "??"; | str = "??"; | ||||
break; | break; | ||||
} | } | ||||
printf("%-7s ", str); | xo_emit("{:run_state/%-7s/%s} ", str); | ||||
if (kipp->ki_kiflag & KI_LOCKBLOCK) { | if (kipp->ki_kiflag & KI_LOCKBLOCK) { | ||||
printf("*%-8s ", strlen(kipp->ki_lockname) ? | xo_emit("{:lock_name/*%-8s/%s} ", | ||||
strlen(kipp->ki_lockname) ? | |||||
kipp->ki_lockname : "-"); | kipp->ki_lockname : "-"); | ||||
} else { | } else { | ||||
printf("%-9s ", strlen(kipp->ki_wmesg) ? | xo_emit("{:wait_channel/%-9s/%s} ", | ||||
kipp->ki_wmesg : "-"); | strlen(kipp->ki_wmesg) ? kipp->ki_wmesg : "-"); | ||||
} | } | ||||
printf("\n"); | xo_close_container(threadid); | ||||
free(threadid); | |||||
xo_emit("\n"); | |||||
} | } | ||||
xo_close_container("threads"); | |||||
procstat_freeprocs(procstat, kip); | procstat_freeprocs(procstat, kip); | ||||
} | } |