xref: /freebsd/usr.bin/procstat/procstat_vm.c (revision d6b92ffa)
1 /*-
2  * Copyright (c) 2007 Robert N. M. Watson
3  * Copyright (c) 2015 Allan Jude <allanjude@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  * $FreeBSD$
28  */
29 
30 #include <sys/param.h>
31 #include <sys/sysctl.h>
32 #include <sys/user.h>
33 
34 #include <err.h>
35 #include <errno.h>
36 #include <libprocstat.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <stdint.h>
40 #include <libutil.h>
41 
42 #include "procstat.h"
43 
44 void
45 procstat_vm(struct procstat *procstat, struct kinfo_proc *kipp)
46 {
47 	struct kinfo_vmentry *freep, *kve;
48 	int ptrwidth;
49 	int i, cnt;
50 	const char *str, *lstr;
51 
52 	ptrwidth = 2*sizeof(void *) + 2;
53 	if (!hflag)
54 		xo_emit("{T:/%5s %*s %*s %3s %4s %4s %3s %3s %-4s %-2s %-s}\n",
55 		    "PID", ptrwidth, "START", ptrwidth, "END", "PRT", "RES",
56 		    "PRES", "REF", "SHD", "FLAG", "TP", "PATH");
57 
58 	xo_emit("{ek:process_id/%d}", kipp->ki_pid);
59 
60 	freep = procstat_getvmmap(procstat, kipp, &cnt);
61 	if (freep == NULL)
62 		return;
63 	xo_open_list("vm");
64 	for (i = 0; i < cnt; i++) {
65 		xo_open_instance("vm");
66 		kve = &freep[i];
67 		xo_emit("{dk:process_id/%5d} ", kipp->ki_pid);
68 		xo_emit("{d:kve_start/%#*jx} ", ptrwidth,
69 		    (uintmax_t)kve->kve_start);
70 		xo_emit("{d:kve_end/%#*jx} ", ptrwidth,
71 		    (uintmax_t)kve->kve_end);
72 		xo_emit("{e:kve_start/%#jx}", (uintmax_t)kve->kve_start);
73 		xo_emit("{e:kve_end/%#jx}", (uintmax_t)kve->kve_end);
74 		xo_emit("{d:read/%s}", kve->kve_protection & KVME_PROT_READ ?
75 		    "r" : "-");
76 		xo_emit("{d:write/%s}", kve->kve_protection & KVME_PROT_WRITE ?
77 		    "w" : "-");
78 		xo_emit("{d:exec/%s} ", kve->kve_protection & KVME_PROT_EXEC ?
79 		    "x" : "-");
80 		xo_open_container("kve_protection");
81 		xo_emit("{en:read/%s}", kve->kve_protection & KVME_PROT_READ ?
82 		    "true" : "false");
83 		xo_emit("{en:write/%s}", kve->kve_protection & KVME_PROT_WRITE ?
84 		    "true" : "false");
85 		xo_emit("{en:exec/%s}", kve->kve_protection & KVME_PROT_EXEC ?
86 		    "true" : "false");
87 		xo_close_container("kve_protection");
88 		xo_emit("{:kve_resident/%4d/%d} ", kve->kve_resident);
89 		xo_emit("{:kve_private_resident/%4d/%d} ",
90 		    kve->kve_private_resident);
91 		xo_emit("{:kve_ref_count/%3d/%d} ", kve->kve_ref_count);
92 		xo_emit("{:kve_shadow_count/%3d/%d} ", kve->kve_shadow_count);
93 		xo_emit("{d:copy_on_write/%-1s}", kve->kve_flags &
94 		    KVME_FLAG_COW ? "C" : "-");
95 		xo_emit("{d:need_copy/%-1s}", kve->kve_flags &
96 		    KVME_FLAG_NEEDS_COPY ? "N" : "-");
97 		xo_emit("{d:super_pages/%-1s}", kve->kve_flags &
98 		    KVME_FLAG_SUPER ? "S" : "-");
99 		xo_emit("{d:grows_down/%-1s} ", kve->kve_flags &
100 		    KVME_FLAG_GROWS_UP ? "U" : kve->kve_flags &
101 		    KVME_FLAG_GROWS_DOWN ? "D" : "-");
102 		xo_open_container("kve_flags");
103 		xo_emit("{en:copy_on_write/%s}", kve->kve_flags &
104 		    KVME_FLAG_COW ? "true" : "false");
105 		xo_emit("{en:needs_copy/%s}", kve->kve_flags &
106 		    KVME_FLAG_NEEDS_COPY ? "true" : "false");
107 		xo_emit("{en:super_pages/%s}", kve->kve_flags &
108 		    KVME_FLAG_SUPER ? "true" : "false");
109 		xo_emit("{en:grows_up/%s}", kve->kve_flags &
110 		    KVME_FLAG_GROWS_UP ? "true" : "false");
111 		xo_emit("{en:grows_down/%s}", kve->kve_flags &
112 		    KVME_FLAG_GROWS_DOWN ? "true" : "false");
113 		xo_close_container("kve_flags");
114 		switch (kve->kve_type) {
115 		case KVME_TYPE_NONE:
116 			str = "--";
117 			lstr = "none";
118 			break;
119 		case KVME_TYPE_DEFAULT:
120 			str = "df";
121 			lstr = "default";
122 			break;
123 		case KVME_TYPE_VNODE:
124 			str = "vn";
125 			lstr = "vnode";
126 			break;
127 		case KVME_TYPE_SWAP:
128 			str = "sw";
129 			lstr = "swap";
130 			break;
131 		case KVME_TYPE_DEVICE:
132 			str = "dv";
133 			lstr = "device";
134 			break;
135 		case KVME_TYPE_PHYS:
136 			str = "ph";
137 			lstr = "physical";
138 			break;
139 		case KVME_TYPE_DEAD:
140 			str = "dd";
141 			lstr = "dead";
142 			break;
143 		case KVME_TYPE_SG:
144 			str = "sg";
145 			lstr = "scatter/gather";
146 			break;
147 		case KVME_TYPE_MGTDEVICE:
148 			str = "md";
149 			lstr = "managed_device";
150 			break;
151 		case KVME_TYPE_UNKNOWN:
152 		default:
153 			str = "??";
154 			lstr = "unknown";
155 			break;
156 		}
157 		xo_emit("{d:kve_type/%-2s} ", str);
158 		xo_emit("{e:kve_type/%s}", lstr);
159 		xo_emit("{:kve_path/%-s/%s}\n", kve->kve_path);
160 		xo_close_instance("vm");
161 	}
162 	xo_close_list("vm");
163 	free(freep);
164 }
165