2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2021 Yoshihiro Ota <ota@j.email.ne.jp>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/param.h>
29 #include <sys/sysctl.h>
33 #include <libprocstat.h>
44 * vm objects of swappable types
46 static struct swapvm {
48 uint32_t swapped; /* in pages */
50 pid_t pid; /* to avoid double counting */
52 static int nswobj = 0;
54 static struct procstat *prstat = NULL;
56 *procstat_getvmmap() is an expensive call and the number of processes running
57 * may also be high. So, maintain an array of pointers for ease of expanding
58 * an array and also swapping pointers are faster than struct.
60 static struct proc_usage {
63 char command[COMMLEN + 1];
68 static int proc_compar(const void *, const void *);
71 display_proc_line(int idx, int y, uint64_t totalswappages)
83 uname = user_from_uid(pu[idx]->uid, 0);
84 swapbytes = ptoa(pu[idx]->pages);
86 snprintf(buf, sizeof(buf), "%6d %-10s %-10.10s", pu[idx]->pid, uname,
88 offset = 6 + 1 + 10 + 1 + 10 + 1;
89 mvwaddstr(wnd, y, 0, buf);
90 sysputuint64(wnd, y, offset, 4, swapbytes, 0);
92 mvwaddstr(wnd, y, offset, " / ");
94 sysputuint64(wnd, y, offset, 4, pu[idx]->total, 0);
97 rate = pu[idx]->total > 1 ? 100 * swapbytes / pu[idx]->total : 0;
98 snprintf(buf, sizeof(buf), "%3d%%", rate);
99 mvwaddstr(wnd, y, offset, buf);
100 if (rate > 100) /* avoid running over the screen */
102 sysputXs(wnd, y, offset + 5, rate / 10);
104 rate = 100 * pu[idx]->pages / totalswappages;
105 snprintf(buf, sizeof(buf), "%3d%%", rate);
106 mvwaddstr(wnd, y, offset + 16, buf);
107 if (rate > 100) /* avoid running over the screen */
109 sysputXs(wnd, y, offset + 21, rate / 10);
113 swobj_search(const void *a, const void *b)
115 const uint64_t *aa = a;
116 const struct swapvm *bb = b;
118 if (*aa == bb->kvo_me)
120 return (*aa > bb->kvo_me ? -1 : 1);
124 swobj_sort(const void *a, const void *b)
127 return ((((const struct swapvm *) a)->kvo_me >
128 ((const struct swapvm *) b)->kvo_me) ? -1 : 1);
132 get_swap_vmobjects(void)
135 int cnt, i, next_i, last_nswobj;
136 struct kinfo_vmobject *kvo;
139 kvo = kinfo_getswapvmobject(&cnt);
141 error("kinfo_getswapvmobject()");
145 for (i = next_i; i < cnt; i++) {
146 if (kvo[i].kvo_type != KVME_TYPE_DEFAULT &&
147 kvo[i].kvo_type != KVME_TYPE_SWAP)
149 if (nswobj < maxnobj) {
150 swobj[nswobj].kvo_me = kvo[i].kvo_me;
151 swobj[nswobj].swapped = kvo[i].kvo_swapped;
152 swobj[nswobj].next = kvo[i].kvo_backing_obj;
153 swobj[nswobj].pid = 0;
158 if (nswobj <= maxnobj)
160 /* allocate memory and fill skipped elements */
161 last_nswobj = maxnobj;
163 nswobj = last_nswobj;
164 /* allocate more memory and fill missed ones */
165 if ((swobj = reallocf(swobj, maxnobj * sizeof(*swobj))) ==
167 error("Out of memory");
170 } while (i <= cnt); /* extra safety guard */
173 qsort(swobj, nswobj, sizeof(swobj[0]), swobj_sort);
177 /* This returns the number of swap pages a process uses. */
179 per_proc_swap_usage(struct kinfo_proc *kipp)
184 struct kinfo_vmentry *freep, *kve;
187 freep = procstat_getvmmap(prstat, kipp, &cnt);
191 for (i = 0; i < cnt; i++) {
193 if (kve->kve_type == KVME_TYPE_DEFAULT ||
194 kve->kve_type == KVME_TYPE_SWAP) {
195 vmobj = kve->kve_obj;
197 vm = bsearch(&vmobj, swobj, nswobj,
198 sizeof(swobj[0]), swobj_search);
199 if (vm != NULL && vm->pid != kipp->ki_pid) {
200 pages += vm->swapped;
202 vm->pid = kipp->ki_pid;
205 } while (vmobj != 0);
213 procshow(int lcol, int hight, uint64_t totalswappages)
217 for (i = 0, y = lcol + 1 /* HEADING */; i < hight; i++, y++)
218 display_proc_line(i, y, totalswappages);
226 prstat = procstat_open_sysctl();
227 return (prstat != NULL);
233 static int maxnproc = 0;
236 struct kinfo_proc *kipp;
239 if ( ! get_swap_vmobjects() ) /* call failed or nothing is paged-out */
242 kipp = procstat_getprocs(prstat, KERN_PROC_PROC, 0, &cnt);
244 error("procstat_getprocs()");
247 if (maxnproc < cnt) {
248 if ((pu = realloc(pu, cnt * sizeof(*pu))) == NULL) {
249 error("Out of memory");
252 memset(&pu[maxnproc], 0, (cnt - maxnproc) * sizeof(pu[0]));
256 for (i = 0; i < cnt; i++) {
257 pages = per_proc_swap_usage(&kipp[i]);
260 if (pu[nproc] == NULL &&
261 (pu[nproc] = malloc(sizeof(**pu))) == NULL) {
262 error("Out of memory");
265 strlcpy(pu[nproc]->command, kipp[i].ki_comm,
266 sizeof(pu[nproc]->command));
267 pu[nproc]->pid = kipp[i].ki_pid;
268 pu[nproc]->uid = kipp[i].ki_uid;
269 pu[nproc]->pages = pages;
270 pu[nproc]->total = kipp[i].ki_size;
274 qsort(pu, nproc, sizeof(*pu), proc_compar);
283 mvwaddstr(wnd, lcol, 0,
284 "Pid Username Command Swap/Total "
285 "Per-Process Per-System");
289 proc_compar(const void *a, const void *b)
291 const struct proc_usage *aa = *((const struct proc_usage **)a);
292 const struct proc_usage *bb = *((const struct proc_usage **)b);
294 return (aa->pages > bb->pages ? -1 : 1);