]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - usr.bin/procstat/procstat_rusage.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / usr.bin / procstat / procstat_rusage.c
1 /*-
2  * Copyright (c) 2012 Hudson River Trading LLC
3  * Written by: John H. Baldwin <jhb@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/sysctl.h>
33 #include <sys/user.h>
34
35 #include <libprocstat.h>
36 #include <stdbool.h>
37 #include <stdio.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         printf("%5d ", kipp->ki_pid);
107         if (Hflag)
108                 printf("%6d ", kipp->ki_tid);
109         printf("%-16s ", kipp->ki_comm);
110 }
111
112 static void
113 print_rusage(struct kinfo_proc *kipp)
114 {
115         long *lp;
116         unsigned int i;
117
118         print_prefix(kipp);
119         printf("%-14s %32s\n", "user time",
120             format_time(&kipp->ki_rusage.ru_utime));
121         print_prefix(kipp);
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++) {
126                 print_prefix(kipp);
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));
130                 lp++;
131         }
132 }
133
134 void
135 procstat_rusage(struct procstat *procstat, struct kinfo_proc *kipp)
136 {
137         struct kinfo_proc *kip;
138         unsigned int count, i;
139
140         if (!hflag) {
141                 printf("%5s ", "PID");
142                 if (Hflag)
143                         printf("%6s ", "TID");
144                 printf("%-16s %-32s %14s\n", "COMM", "RESOURCE",
145                     "VALUE        ");
146         }
147
148         if (!Hflag) {
149                 print_rusage(kipp);
150                 return;
151         }
152
153         kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD,
154             kipp->ki_pid, &count);
155         if (kip == NULL)
156                 return;
157         kinfo_proc_sort(kip, count);
158         for (i = 0; i < count; i++)
159                 print_rusage(&kip[i]);
160         procstat_freeprocs(procstat, kip);
161 }