2 * Copyright (c) 2012 Hudson River Trading LLC
3 * Written by: John H. Baldwin <jhb@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/sysctl.h>
35 #include <libprocstat.h>
47 { "maximum RSS", true, 1 },
48 { "integral shared memory", true, 1 },
49 { "integral unshared data", true, 1 },
50 { "integral unshared stack", true, 1 },
51 { "page reclaims", false, 0 },
52 { "page faults", false, 0 },
53 { "swaps", false, 0 },
54 { "block reads", false, 0 },
55 { "block writes", false, 0 },
56 { "messages sent", false, 0 },
57 { "messages received", false, 0 },
58 { "signals received", false, 0 },
59 { "voluntary context switches", false, 0 },
60 { "involuntary context switches", false, 0 }
63 /* xxx days hh:mm:ss.uuuuuu */
65 format_time(struct timeval *tv)
67 static char buffer[32];
68 int days, hours, minutes, seconds, used;
70 minutes = tv->tv_sec / 60;
71 seconds = tv->tv_sec % 60;
78 used += snprintf(buffer, sizeof(buffer), "1 day ");
80 used += snprintf(buffer, sizeof(buffer), "%u days ", days);
82 snprintf(buffer + used, sizeof(buffer) - used, "%02u:%02u:%02u.%06u ",
83 hours, minutes, seconds, (unsigned int)tv->tv_usec);
88 format_value(long value, bool humanize, int scale)
90 static char buffer[14];
95 humanize_number(buffer, sizeof(buffer), value, "B",
98 snprintf(buffer, sizeof(buffer), "%ld ", value);
103 print_prefix(struct kinfo_proc *kipp)
106 printf("%5d ", kipp->ki_pid);
108 printf("%6d ", kipp->ki_tid);
109 printf("%-16s ", kipp->ki_comm);
113 print_rusage(struct kinfo_proc *kipp)
119 printf("%-14s %32s\n", "user time",
120 format_time(&kipp->ki_rusage.ru_utime));
122 printf("%-14s %32s\n", "system time",
123 format_time(&kipp->ki_rusage.ru_stime));
124 lp = &kipp->ki_rusage.ru_maxrss;
125 for (i = 0; i < nitems(rusage_info); i++) {
127 printf("%-32s %14s\n", rusage_info[i].ri_name,
128 format_value(*lp, rusage_info[i].ri_humanize,
129 rusage_info[i].ri_scale));
135 procstat_rusage(struct procstat *procstat, struct kinfo_proc *kipp)
137 struct kinfo_proc *kip;
138 unsigned int count, i;
141 printf("%5s ", "PID");
143 printf("%6s ", "TID");
144 printf("%-16s %-32s %14s\n", "COMM", "RESOURCE",
153 kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD,
154 kipp->ki_pid, &count);
157 kinfo_proc_sort(kip, count);
158 for (i = 0; i < count; i++)
159 print_rusage(&kip[i]);
160 procstat_freeprocs(procstat, kip);