]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/procstat/procstat_threads.c
This commit was generated by cvs2svn to compensate for changes in r174206,
[FreeBSD/FreeBSD.git] / usr.bin / procstat / procstat_threads.c
1 /*-
2  * Copyright (c) 2007 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include <sys/types.h>
30 #include <sys/sysctl.h>
31 #include <sys/user.h>
32
33 #include <err.h>
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "procstat.h"
40
41 void
42 procstat_threads(pid_t pid, struct kinfo_proc *kipp)
43 {
44         struct kinfo_proc *kip;
45         int error, i, name[4];
46         const char *str;
47         size_t len;
48
49         if (!hflag)
50                 printf("%5s %6s %-20s %2s %4s %-7s %-9s\n", "PID", "TID",
51                     "COMM", "CPU", "PRI", "STATE", "WCHAN");
52
53         /*
54          * We need to re-query for thread information, so don't use *kipp.
55          */
56         name[0] = CTL_KERN;
57         name[1] = KERN_PROC;
58         name[2] = KERN_PROC_PID | KERN_PROC_INC_THREAD;
59         name[3] = pid;
60
61         len = 0;
62         error = sysctl(name, 4, NULL, &len, NULL, 0);
63         if (error < 0 && errno != ESRCH) {
64                 warn("sysctl: kern.proc.pid: %d", pid);
65                 return;
66         }
67         if (error < 0)
68                 return;
69
70         kip = malloc(len);
71         if (kip == NULL)
72                 err(-1, "malloc");
73
74         if (sysctl(name, 4, kip, &len, NULL, 0) < 0) {
75                 warn("sysctl: kern.proc.pid: %d", pid);
76                 free(kip);
77                 return;
78         }
79
80         kinfo_proc_sort(kip, len / sizeof(*kipp));
81         for (i = 0; i < len / sizeof(*kipp); i++) {
82                 kipp = &kip[i];
83                 printf("%5d ", pid);
84                 printf("%6d ", kipp->ki_tid);
85                 printf("%-20s ", strlen(kipp->ki_comm) ?
86                     kipp->ki_comm : "-");
87                 if (kipp->ki_oncpu != 255)
88                         printf("%3d ", kipp->ki_oncpu);
89                 else if (kipp->ki_lastcpu != 255)
90                         printf("%3d ", kipp->ki_lastcpu);
91                 else
92                         printf("%3s ", "-");
93                 printf("%4d ", kipp->ki_pri.pri_level);
94                 switch (kipp->ki_stat) {
95                 case SRUN:
96                         str = "run";
97                         break;
98
99                 case SSTOP:
100                         str = "stop";
101                         break;
102
103                 case SSLEEP:
104                         str = "sleep";
105                         break;
106
107                 case SLOCK:
108                         str = "lock";
109                         break;
110
111                 case SWAIT:
112                         str = "wait";
113                         break;
114
115                 case SZOMB:
116                         str = "zomb";
117                         break;
118
119                 case SIDL:
120                         str = "idle";
121                         break;
122
123                 default:
124                         str = "??";
125                         break;
126                 }
127                 printf("%-7s ", str);
128                 if (kipp->ki_kiflag & KI_LOCKBLOCK) {
129                         printf("*%-8s ", strlen(kipp->ki_lockname) ?
130                             kipp->ki_lockname : "-");
131                 } else {
132                         printf("%-9s ", strlen(kipp->ki_wmesg) ?
133                             kipp->ki_wmesg : "-");
134                 }
135                 printf("\n");
136         }
137         free(kip);
138 }