2 * Copyright (c) 2012 Hudson River Trading LLC
3 * Written by: John H. Baldwin <jhb@FreeBSD.org>
4 * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/sysctl.h>
36 #include <libprocstat.h>
49 { "maximum RSS", true, 1 },
50 { "integral shared memory", true, 1 },
51 { "integral unshared data", true, 1 },
52 { "integral unshared stack", true, 1 },
53 { "page reclaims", false, 0 },
54 { "page faults", false, 0 },
55 { "swaps", false, 0 },
56 { "block reads", false, 0 },
57 { "block writes", false, 0 },
58 { "messages sent", false, 0 },
59 { "messages received", false, 0 },
60 { "signals received", false, 0 },
61 { "voluntary context switches", false, 0 },
62 { "involuntary context switches", false, 0 }
65 /* xxx days hh:mm:ss.uuuuuu */
67 format_time(struct timeval *tv)
69 static char buffer[32];
70 int days, hours, minutes, seconds, used;
72 minutes = tv->tv_sec / 60;
73 seconds = tv->tv_sec % 60;
80 used += snprintf(buffer, sizeof(buffer), "1 day ");
82 used += snprintf(buffer, sizeof(buffer), "%u days ", days);
84 snprintf(buffer + used, sizeof(buffer) - used, "%02u:%02u:%02u.%06u",
85 hours, minutes, seconds, (unsigned int)tv->tv_usec);
90 format_value(long value, bool humanize, int scale)
92 static char buffer[14];
97 humanize_number(buffer, sizeof(buffer), value, "B",
100 snprintf(buffer, sizeof(buffer), "%ld ", value);
105 print_prefix(struct kinfo_proc *kipp)
108 xo_emit("{d:process_id/%5d/%d} ", kipp->ki_pid);
109 if ((procstat_opts & PS_OPT_PERTHREAD) != 0)
110 xo_emit("{d:thread_id/%6d/%d} ", kipp->ki_tid);
111 xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm);
115 print_rusage(struct kinfo_proc *kipp)
119 char *field, *threadid;
122 xo_emit("{d:resource/%-14s} {d:usage/%29s}{P: }\n", "user time",
123 format_time(&kipp->ki_rusage.ru_utime));
125 xo_emit("{d:resource/%-14s} {d:usage/%29s}{P: }\n", "system time",
126 format_time(&kipp->ki_rusage.ru_stime));
128 if ((procstat_opts & PS_OPT_PERTHREAD) != 0) {
129 asprintf(&threadid, "%d", kipp->ki_tid);
130 if (threadid == NULL)
132 "Failed to allocate memory in print_rusage()");
133 xo_open_container(threadid);
134 xo_emit("{e:thread_id/%d}", kipp->ki_tid);
136 xo_emit("{e:process_id/%d}", kipp->ki_pid);
137 xo_emit("{e:command/%s}", kipp->ki_comm);
139 xo_emit("{e:user time/%s}", format_time(&kipp->ki_rusage.ru_utime));
140 xo_emit("{e:system time/%s}", format_time(&kipp->ki_rusage.ru_stime));
142 lp = &kipp->ki_rusage.ru_maxrss;
143 for (i = 0; i < nitems(rusage_info); i++) {
145 asprintf(&field, "{e:%s/%%D}", rusage_info[i].ri_name);
148 "Failed to allocate memory in print_rusage()");
151 xo_emit("{d:resource/%-32s} {d:usage/%14s}\n",
152 rusage_info[i].ri_name,
153 format_value(*lp, rusage_info[i].ri_humanize,
154 rusage_info[i].ri_scale));
157 if ((procstat_opts & PS_OPT_PERTHREAD) != 0) {
158 xo_close_container(threadid);
164 procstat_rusage(struct procstat *procstat, struct kinfo_proc *kipp)
166 struct kinfo_proc *kip;
167 unsigned int count, i;
169 if ((procstat_opts & PS_OPT_NOHEADER) == 0) {
170 xo_emit("{d:ta/%5s} ", "PID");
171 if ((procstat_opts & PS_OPT_PERTHREAD) != 0)
172 xo_emit("{d:tb/%6s} ", "TID");
173 xo_emit("{d:tc/%-16s %-32s %14s}\n", "COMM", "RESOURCE",
177 if ((procstat_opts & PS_OPT_PERTHREAD) == 0) {
182 xo_emit("{e:process_id/%d}", kipp->ki_pid);
183 xo_emit("{e:command/%s}", kipp->ki_comm);
184 xo_open_container("threads");
186 kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD,
187 kipp->ki_pid, &count);
190 kinfo_proc_sort(kip, count);
191 for (i = 0; i < count; i++) {
192 print_rusage(&kip[i]);
195 xo_close_container("threads");
196 procstat_freeprocs(procstat, kip);