procstat_threads.c revision 221807
1174199Srwatson/*- 2174199Srwatson * Copyright (c) 2007 Robert N. M. Watson 3174199Srwatson * All rights reserved. 4174199Srwatson * 5174199Srwatson * Redistribution and use in source and binary forms, with or without 6174199Srwatson * modification, are permitted provided that the following conditions 7174199Srwatson * are met: 8174199Srwatson * 1. Redistributions of source code must retain the above copyright 9174199Srwatson * notice, this list of conditions and the following disclaimer. 10174199Srwatson * 2. Redistributions in binary form must reproduce the above copyright 11174199Srwatson * notice, this list of conditions and the following disclaimer in the 12174199Srwatson * documentation and/or other materials provided with the distribution. 13174199Srwatson * 14174199Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15174199Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16174199Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17174199Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18174199Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19174199Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20174199Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21174199Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22174199Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23174199Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24174199Srwatson * SUCH DAMAGE. 25174199Srwatson * 26174199Srwatson * $FreeBSD: head/usr.bin/procstat/procstat_threads.c 221807 2011-05-12 10:11:39Z stas $ 27174199Srwatson */ 28174199Srwatson 29186567Srwatson#include <sys/param.h> 30174199Srwatson#include <sys/sysctl.h> 31174199Srwatson#include <sys/user.h> 32174199Srwatson 33174199Srwatson#include <err.h> 34174199Srwatson#include <errno.h> 35221807Sstas#include <libprocstat.h> 36174199Srwatson#include <stdio.h> 37174199Srwatson#include <stdlib.h> 38174199Srwatson#include <string.h> 39174199Srwatson 40174199Srwatson#include "procstat.h" 41174199Srwatson 42174199Srwatsonvoid 43221807Sstasprocstat_threads(struct kinfo_proc *kipp) 44174199Srwatson{ 45174199Srwatson struct kinfo_proc *kip; 46176107Sdwmalone int error, name[4]; 47176107Sdwmalone unsigned int i; 48174199Srwatson const char *str; 49174199Srwatson size_t len; 50174199Srwatson 51174199Srwatson if (!hflag) 52174230Srwatson printf("%5s %6s %-16s %-16s %2s %4s %-7s %-9s\n", "PID", 53174230Srwatson "TID", "COMM", "TDNAME", "CPU", "PRI", "STATE", "WCHAN"); 54174199Srwatson 55174199Srwatson /* 56174199Srwatson * We need to re-query for thread information, so don't use *kipp. 57174199Srwatson */ 58174199Srwatson name[0] = CTL_KERN; 59174199Srwatson name[1] = KERN_PROC; 60174199Srwatson name[2] = KERN_PROC_PID | KERN_PROC_INC_THREAD; 61221807Sstas name[3] = kipp->ki_pid; 62174199Srwatson 63174199Srwatson len = 0; 64174199Srwatson error = sysctl(name, 4, NULL, &len, NULL, 0); 65174199Srwatson if (error < 0 && errno != ESRCH) { 66221807Sstas warn("sysctl: kern.proc.pid: %d", kipp->ki_pid); 67174199Srwatson return; 68174199Srwatson } 69174199Srwatson if (error < 0) 70174199Srwatson return; 71174199Srwatson 72174199Srwatson kip = malloc(len); 73174199Srwatson if (kip == NULL) 74174199Srwatson err(-1, "malloc"); 75174199Srwatson 76174199Srwatson if (sysctl(name, 4, kip, &len, NULL, 0) < 0) { 77221807Sstas warn("sysctl: kern.proc.pid: %d", kipp->ki_pid); 78174199Srwatson free(kip); 79174199Srwatson return; 80174199Srwatson } 81174199Srwatson 82174199Srwatson kinfo_proc_sort(kip, len / sizeof(*kipp)); 83174199Srwatson for (i = 0; i < len / sizeof(*kipp); i++) { 84174199Srwatson kipp = &kip[i]; 85221807Sstas printf("%5d ", kipp->ki_pid); 86174199Srwatson printf("%6d ", kipp->ki_tid); 87174230Srwatson printf("%-16s ", strlen(kipp->ki_comm) ? 88174199Srwatson kipp->ki_comm : "-"); 89174230Srwatson printf("%-16s ", (strlen(kipp->ki_ocomm) && 90174230Srwatson (strcmp(kipp->ki_comm, kipp->ki_ocomm) != 0)) ? 91174230Srwatson kipp->ki_ocomm : "-"); 92174199Srwatson if (kipp->ki_oncpu != 255) 93174199Srwatson printf("%3d ", kipp->ki_oncpu); 94174199Srwatson else if (kipp->ki_lastcpu != 255) 95174199Srwatson printf("%3d ", kipp->ki_lastcpu); 96174199Srwatson else 97174199Srwatson printf("%3s ", "-"); 98174199Srwatson printf("%4d ", kipp->ki_pri.pri_level); 99174199Srwatson switch (kipp->ki_stat) { 100174199Srwatson case SRUN: 101174199Srwatson str = "run"; 102174199Srwatson break; 103174199Srwatson 104174199Srwatson case SSTOP: 105174199Srwatson str = "stop"; 106174199Srwatson break; 107174199Srwatson 108174199Srwatson case SSLEEP: 109174199Srwatson str = "sleep"; 110174199Srwatson break; 111174199Srwatson 112174199Srwatson case SLOCK: 113174199Srwatson str = "lock"; 114174199Srwatson break; 115174199Srwatson 116174199Srwatson case SWAIT: 117174199Srwatson str = "wait"; 118174199Srwatson break; 119174199Srwatson 120174199Srwatson case SZOMB: 121174199Srwatson str = "zomb"; 122174199Srwatson break; 123174199Srwatson 124174199Srwatson case SIDL: 125174199Srwatson str = "idle"; 126174199Srwatson break; 127174199Srwatson 128174199Srwatson default: 129174199Srwatson str = "??"; 130174199Srwatson break; 131174199Srwatson } 132174199Srwatson printf("%-7s ", str); 133174199Srwatson if (kipp->ki_kiflag & KI_LOCKBLOCK) { 134174199Srwatson printf("*%-8s ", strlen(kipp->ki_lockname) ? 135174199Srwatson kipp->ki_lockname : "-"); 136174199Srwatson } else { 137174199Srwatson printf("%-9s ", strlen(kipp->ki_wmesg) ? 138174199Srwatson kipp->ki_wmesg : "-"); 139174199Srwatson } 140174199Srwatson printf("\n"); 141174199Srwatson } 142174199Srwatson free(kip); 143174199Srwatson} 144