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