xref: /freebsd/usr.bin/procstat/procstat_vm.c (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2007 Robert N. M. Watson
5  * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #include <sys/param.h>
33 #include <sys/sysctl.h>
34 #include <sys/user.h>
35 
36 #include <err.h>
37 #include <errno.h>
38 #include <libprocstat.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <stdint.h>
42 #include <libutil.h>
43 
44 #include "procstat.h"
45 
46 void
47 procstat_vm(struct procstat *procstat, struct kinfo_proc *kipp)
48 {
49 	struct kinfo_vmentry *freep, *kve;
50 	int ptrwidth;
51 	int i, cnt;
52 	const char *str, *lstr;
53 
54 	ptrwidth = 2*sizeof(void *) + 2;
55 	if ((procstat_opts & PS_OPT_NOHEADER) == 0)
56 		xo_emit("{T:/%5s %*s %*s %3s %4s %4s %3s %3s %-5s %-2s %-s}\n",
57 		    "PID", ptrwidth, "START", ptrwidth, "END", "PRT", "RES",
58 		    "PRES", "REF", "SHD", "FLAG", "TP", "PATH");
59 
60 	xo_emit("{ek:process_id/%d}", kipp->ki_pid);
61 
62 	freep = procstat_getvmmap(procstat, kipp, &cnt);
63 	if (freep == NULL)
64 		return;
65 	xo_open_list("vm");
66 	for (i = 0; i < cnt; i++) {
67 		xo_open_instance("vm");
68 		kve = &freep[i];
69 		xo_emit("{dk:process_id/%5d} ", kipp->ki_pid);
70 		xo_emit("{d:kve_start/%#*jx} ", ptrwidth,
71 		    (uintmax_t)kve->kve_start);
72 		xo_emit("{d:kve_end/%#*jx} ", ptrwidth,
73 		    (uintmax_t)kve->kve_end);
74 		xo_emit("{e:kve_start/%#jx}", (uintmax_t)kve->kve_start);
75 		xo_emit("{e:kve_end/%#jx}", (uintmax_t)kve->kve_end);
76 		xo_emit("{d:read/%s}", kve->kve_protection & KVME_PROT_READ ?
77 		    "r" : "-");
78 		xo_emit("{d:write/%s}", kve->kve_protection & KVME_PROT_WRITE ?
79 		    "w" : "-");
80 		xo_emit("{d:exec/%s} ", kve->kve_protection & KVME_PROT_EXEC ?
81 		    "x" : "-");
82 		xo_open_container("kve_protection");
83 		xo_emit("{en:read/%s}", kve->kve_protection & KVME_PROT_READ ?
84 		    "true" : "false");
85 		xo_emit("{en:write/%s}", kve->kve_protection & KVME_PROT_WRITE ?
86 		    "true" : "false");
87 		xo_emit("{en:exec/%s}", kve->kve_protection & KVME_PROT_EXEC ?
88 		    "true" : "false");
89 		xo_close_container("kve_protection");
90 		xo_emit("{:kve_resident/%4d/%d} ", kve->kve_resident);
91 		xo_emit("{:kve_private_resident/%4d/%d} ",
92 		    kve->kve_private_resident);
93 		xo_emit("{:kve_ref_count/%3d/%d} ", kve->kve_ref_count);
94 		xo_emit("{:kve_shadow_count/%3d/%d} ", kve->kve_shadow_count);
95 		xo_emit("{d:copy_on_write/%-1s}", kve->kve_flags &
96 		    KVME_FLAG_COW ? "C" : "-");
97 		xo_emit("{d:need_copy/%-1s}", kve->kve_flags &
98 		    KVME_FLAG_NEEDS_COPY ? "N" : "-");
99 		xo_emit("{d:super_pages/%-1s}", kve->kve_flags &
100 		    KVME_FLAG_SUPER ? "S" : "-");
101 		xo_emit("{d:grows_down/%-1s}", kve->kve_flags &
102 		    KVME_FLAG_GROWS_UP ? "U" : kve->kve_flags &
103 		    KVME_FLAG_GROWS_DOWN ? "D" : "-");
104 		xo_emit("{d:wired/%-1s} ", kve->kve_flags &
105 		    KVME_FLAG_USER_WIRED ? "W" : "-");
106 		xo_open_container("kve_flags");
107 		xo_emit("{en:copy_on_write/%s}", kve->kve_flags &
108 		    KVME_FLAG_COW ? "true" : "false");
109 		xo_emit("{en:needs_copy/%s}", kve->kve_flags &
110 		    KVME_FLAG_NEEDS_COPY ? "true" : "false");
111 		xo_emit("{en:super_pages/%s}", kve->kve_flags &
112 		    KVME_FLAG_SUPER ? "true" : "false");
113 		xo_emit("{en:grows_up/%s}", kve->kve_flags &
114 		    KVME_FLAG_GROWS_UP ? "true" : "false");
115 		xo_emit("{en:grows_down/%s}", kve->kve_flags &
116 		    KVME_FLAG_GROWS_DOWN ? "true" : "false");
117 		xo_emit("{en:wired/%s}", kve->kve_flags &
118 		    KVME_FLAG_USER_WIRED ? "true" : "false");
119 		xo_close_container("kve_flags");
120 		switch (kve->kve_type) {
121 		case KVME_TYPE_NONE:
122 			str = "--";
123 			lstr = "none";
124 			break;
125 		case KVME_TYPE_DEFAULT:
126 			str = "df";
127 			lstr = "default";
128 			break;
129 		case KVME_TYPE_VNODE:
130 			str = "vn";
131 			lstr = "vnode";
132 			break;
133 		case KVME_TYPE_SWAP:
134 			str = "sw";
135 			lstr = "swap";
136 			break;
137 		case KVME_TYPE_DEVICE:
138 			str = "dv";
139 			lstr = "device";
140 			break;
141 		case KVME_TYPE_PHYS:
142 			str = "ph";
143 			lstr = "physical";
144 			break;
145 		case KVME_TYPE_DEAD:
146 			str = "dd";
147 			lstr = "dead";
148 			break;
149 		case KVME_TYPE_SG:
150 			str = "sg";
151 			lstr = "scatter/gather";
152 			break;
153 		case KVME_TYPE_MGTDEVICE:
154 			str = "md";
155 			lstr = "managed_device";
156 			break;
157 		case KVME_TYPE_UNKNOWN:
158 		default:
159 			str = "??";
160 			lstr = "unknown";
161 			break;
162 		}
163 		xo_emit("{d:kve_type/%-2s} ", str);
164 		xo_emit("{e:kve_type/%s}", lstr);
165 		xo_emit("{:kve_path/%-s/%s}\n", kve->kve_path);
166 		xo_close_instance("vm");
167 	}
168 	xo_close_list("vm");
169 	free(freep);
170 }
171