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