xref: /freebsd/usr.bin/procstat/procstat_rusage.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
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>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/sysctl.h>
32 #include <sys/user.h>
33 
34 #include <libprocstat.h>
35 #include <stdbool.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <libutil.h>
39 
40 #include "procstat.h"
41 
42 static struct {
43 	const char *ri_name;
44 	bool	ri_humanize;
45 	int	ri_scale;
46 } rusage_info[] = {
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 }
61 };
62 
63 /* xxx days hh:mm:ss.uuuuuu */
64 static const char *
65 format_time(struct timeval *tv)
66 {
67 	static char buffer[32];
68 	int days, hours, minutes, seconds, used;
69 
70 	minutes = tv->tv_sec / 60;
71 	seconds = tv->tv_sec % 60;
72 	hours = minutes / 60;
73 	minutes %= 60;
74 	days = hours / 24;
75 	hours %= 24;
76 	used = 0;
77 	if (days == 1)
78 		used += snprintf(buffer, sizeof(buffer), "1 day ");
79 	else if (days > 0)
80 		used += snprintf(buffer, sizeof(buffer), "%u days ", days);
81 
82 	snprintf(buffer + used, sizeof(buffer) - used, "%02u:%02u:%02u.%06u",
83 	    hours, minutes, seconds, (unsigned int)tv->tv_usec);
84 	return (buffer);
85 }
86 
87 static const char *
88 format_value(long value, bool humanize, int scale)
89 {
90 	static char buffer[14];
91 
92 	if (scale != 0)
93 		value <<= scale * 10;
94 	if (humanize)
95 		humanize_number(buffer, sizeof(buffer), value, "B",
96 		    scale, HN_DECIMAL);
97 	else
98 		snprintf(buffer, sizeof(buffer), "%ld   ", value);
99 	return (buffer);
100 }
101 
102 static void
103 print_prefix(struct kinfo_proc *kipp)
104 {
105 
106 	xo_emit("{d:process_id/%5d/%d} ", kipp->ki_pid);
107 	if ((procstat_opts & PS_OPT_PERTHREAD) != 0)
108 		xo_emit("{d:thread_id/%6d/%d} ", kipp->ki_tid);
109 	xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm);
110 }
111 
112 static void
113 print_rusage(struct kinfo_proc *kipp)
114 {
115 	long *lp;
116 	unsigned int i;
117 	char *field, *threadid;
118 
119 	print_prefix(kipp);
120 	xo_emit("{d:resource/%-14s} {d:usage/%29s}{P:   }\n", "user time",
121 	    format_time(&kipp->ki_rusage.ru_utime));
122 	print_prefix(kipp);
123 	xo_emit("{d:resource/%-14s} {d:usage/%29s}{P:   }\n", "system time",
124 	    format_time(&kipp->ki_rusage.ru_stime));
125 
126 	if ((procstat_opts & PS_OPT_PERTHREAD) != 0) {
127 		asprintf(&threadid, "%d", kipp->ki_tid);
128 		if (threadid == NULL)
129 			xo_errc(1, ENOMEM,
130 			    "Failed to allocate memory in print_rusage()");
131 		xo_open_container(threadid);
132 		xo_emit("{e:thread_id/%d}", kipp->ki_tid);
133 	} else {
134 		xo_emit("{e:process_id/%d}", kipp->ki_pid);
135 		xo_emit("{e:command/%s}", kipp->ki_comm);
136 	}
137 	xo_emit("{e:user time/%s}", format_time(&kipp->ki_rusage.ru_utime));
138 	xo_emit("{e:system time/%s}", format_time(&kipp->ki_rusage.ru_stime));
139 
140 	lp = &kipp->ki_rusage.ru_maxrss;
141 	for (i = 0; i < nitems(rusage_info); i++) {
142 		print_prefix(kipp);
143 		asprintf(&field, "{e:%s/%%D}", rusage_info[i].ri_name);
144 		if (field == NULL)
145 			xo_errc(1, ENOMEM,
146 			    "Failed to allocate memory in print_rusage()");
147 		xo_emit(field, *lp);
148 		free(field);
149 		xo_emit("{d:resource/%-32s} {d:usage/%14s}\n",
150 		    rusage_info[i].ri_name,
151 		    format_value(*lp, rusage_info[i].ri_humanize,
152 		    rusage_info[i].ri_scale));
153 		lp++;
154 	}
155 	if ((procstat_opts & PS_OPT_PERTHREAD) != 0) {
156 		xo_close_container(threadid);
157 		free(threadid);
158 	}
159 }
160 
161 void
162 procstat_rusage(struct procstat *procstat, struct kinfo_proc *kipp)
163 {
164 	struct kinfo_proc *kip;
165 	unsigned int count, i;
166 
167 	if ((procstat_opts & PS_OPT_NOHEADER) == 0) {
168 		xo_emit("{d:ta/%5s} ", "PID");
169 		if ((procstat_opts & PS_OPT_PERTHREAD) != 0)
170 			xo_emit("{d:tb/%6s} ", "TID");
171 		xo_emit("{d:tc/%-16s %-32s %14s}\n", "COMM", "RESOURCE",
172 		    "VALUE        ");
173 	}
174 
175 	if ((procstat_opts & PS_OPT_PERTHREAD) == 0) {
176 		print_rusage(kipp);
177 		return;
178 	}
179 
180 	xo_emit("{e:process_id/%d}", kipp->ki_pid);
181 	xo_emit("{e:command/%s}", kipp->ki_comm);
182 	xo_open_container("threads");
183 
184 	kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD,
185 	    kipp->ki_pid, &count);
186 	if (kip == NULL)
187 		return;
188 	kinfo_proc_sort(kip, count);
189 	for (i = 0; i < count; i++) {
190 		print_rusage(&kip[i]);
191 	}
192 
193 	xo_close_container("threads");
194 	procstat_freeprocs(procstat, kip);
195 }
196