summaryrefslogtreecommitdiff
path: root/usr.bin/systat/ps.c
blob: 83a2ce2a389496bf6dda62569d86f9b33290dcea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*      $NetBSD: ps.c,v 1.40 2021/04/17 08:34:27 maya Exp $  */

/*-
 * Copyright (c) 1999
 *      The NetBSD Foundation, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by the NetBSD Foundation.
 * 4. Neither the name of the Foundation nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * XXX Notes XXX
 * showps -- print data needed at each refresh
 * fetchps -- get data used by mode (done at each refresh)
 * labelps -- print labels (ie info not needing refreshing)
 * initps -- prepare once-only data structures for mode
 * openps -- prepare per-run information for mode, return window
 * closeps -- close mode to prepare to switch modes
 * cmdps -- optional, handle commands
 */

#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: ps.c,v 1.40 2021/04/17 08:34:27 maya Exp $");
#endif /* not lint */

#include <sys/param.h>
#include <sys/sched.h>
#include <sys/sysctl.h>
#include <sys/stat.h>

#include <curses.h>
#include <math.h>
#include <pwd.h>
#include <stdlib.h>
#include <string.h>
#include <tzfile.h>
#include <unistd.h>

#include "systat.h"
#include "extern.h"
#include "ps.h"

int compare_pctcpu_noidle(const void *, const void *);
char *state2str(struct kinfo_proc2 *);
char *tty2str(struct kinfo_proc2 *);
int rss2int(struct kinfo_proc2 *);
int vsz2int(struct kinfo_proc2 *);
char *comm2str(struct kinfo_proc2 *);
double pmem2float(struct kinfo_proc2 *);
char *start2str(struct kinfo_proc2 *);
char *time2str(struct kinfo_proc2 *);

static time_t now;

#define SHOWUSER_ANY	(uid_t)-1
static uid_t showuser = SHOWUSER_ANY;

#ifndef P_ZOMBIE
#define P_ZOMBIE(p)	((p)->p_stat == SZOMB)
#endif

void
labelps(void)
{
	mvwaddstr(wnd, 0, 0, "USER      PID %CPU %MEM    VSZ   RSS TT  STAT STARTED       TIME COMMAND");
}

void
showps(void)
{
	int i, k, y, vsz, rss;
	const char *user, *comm, *state, *tty, *start, *time_str;
	pid_t pid;
	double pctcpu, pctmem;
	struct kinfo_proc2 *kp;

	now = 0;	/* force start2str to reget current time */

	qsort(pt, nproc + 1, sizeof (struct p_times), compare_pctcpu_noidle);

	y = 1;
	i = nproc + 1;
	if (i > getmaxy(wnd)-2)
		i = getmaxy(wnd)-1;
	for (k = 0; i > 0 ; k++) {
		if (pt[k].pt_kp == NULL) /* We're all the way down to the imaginary idle proc */
			break;

		kp = pt[k].pt_kp;
		if (showuser != SHOWUSER_ANY && kp->p_uid != showuser)
			continue;
		user = user_from_uid(kp->p_uid, 0);
		pid = kp->p_pid;
		pctcpu = 100.0 * pt[k].pt_pctcpu;
		pctmem = pmem2float(pt[k].pt_kp);
		vsz = vsz2int(pt[k].pt_kp);
		rss = rss2int(pt[k].pt_kp);
		tty = tty2str(pt[k].pt_kp);
		state = state2str(pt[k].pt_kp);
		start = start2str(pt[k].pt_kp);
		time_str = time2str(pt[k].pt_kp);
		comm = comm2str(pt[k].pt_kp);
		/*comm = pt[k].pt_kp->kp_proc.p_comm; */

		wmove(wnd, y, 0);
		wclrtoeol(wnd);
		mvwprintw(wnd, y++, 0,
		    "%-8.8s%5d %4.1f %4.1f %6d %5d %-3s %-4s %7s %10.10s %s",
		    user, pid, pctcpu, pctmem, vsz, rss, tty, state, start,
		    time_str, comm);
		i--;
	}
	wmove(wnd, y, 0);
	wclrtobot(wnd);
}

int
compare_pctcpu_noidle(const void *a, const void *b)
{
	if (((const struct p_times *) a)->pt_kp == NULL)
		return 1;

	if (((const struct p_times *) b)->pt_kp == NULL)
	 	return -1;

	return (((const struct p_times *) a)->pt_pctcpu >
		((const struct p_times *) b)->pt_pctcpu)? -1: 1;
}

/* from here down adapted from .../src/usr.bin/ps/print.c .  Any mistakes are my own, however. */
char *
state2str(struct kinfo_proc2 *kp)
{       
	int flag; 
	char *cp;
	char buf[5];
	static char statestr[4];

	flag = kp->p_flag;
	cp = buf;

	switch (kp->p_stat) {
	case LSSTOP:
		*cp = 'T';
		break;

	case LSSLEEP:
		if (flag & L_SINTR)     /* interruptable (long) */
			*cp = kp->p_slptime >= (uint32_t)maxslp ? 'I' : 'S';
		else
			*cp = 'D';
		break;

	case LSRUN:
	case LSIDL:
	case LSONPROC:
		*cp = 'R';
		break;

	case LSZOMB:
#ifdef LSDEAD
	case LSDEAD:
#endif
		*cp = 'Z';
		break;

	default:
		*cp = '?';
	}
	cp++;
	if (kp->p_nice < NZERO)
		*cp++ = '<';
	else if (kp->p_nice > NZERO)
		*cp++ = 'N';
	if (flag & P_TRACED)
		*cp++ = 'X';
	if (flag & P_WEXIT &&
	    /* XXX - I don't like this */
	    (kp->p_stat != LSZOMB))
		*cp++ = 'E';
	if (flag & P_PPWAIT)
		*cp++ = 'V';
	if (kp->p_eflag & EPROC_SLEADER)
		*cp++ = 's'; 
	if ((flag & P_CONTROLT) && kp->p__pgid == kp->p_tpgid)
		*cp++ = '+';
	*cp = '\0';
	snprintf(statestr, sizeof(statestr), "%-s",  buf);

	return statestr;
}

char *
tty2str(struct kinfo_proc2 *kp)
{
	static char ttystr[4];
	char *tty_name;

	if (kp->p_tdev == (uint32_t)NODEV ||
	    (tty_name = devname(kp->p_tdev, S_IFCHR)) == NULL)
		strlcpy(ttystr, "??", sizeof(ttystr));
	else {
		if (strncmp(tty_name, "tty", 3) == 0 ||
		    strncmp(tty_name, "dty", 3) == 0)
			tty_name += 3;
		snprintf(ttystr, sizeof(ttystr), "%s%c", tty_name,
		    kp->p_eflag & EPROC_CTTY ? ' ' : '-');
	}

	return ttystr;
}

#define pgtok(a)	(((a)*getpagesize())/1024)

int
vsz2int(struct kinfo_proc2 *kp)
{
	int     i;

	i = pgtok(kp->p_vm_msize);

	return ((i < 0) ? 0 : i);
}

int
rss2int(struct kinfo_proc2 *kp)
{
	int	i;
 
	i = pgtok(kp->p_vm_rssize);

	/* XXX don't have info about shared */
	return ((i < 0) ? 0 : i);
}

char *
comm2str(struct kinfo_proc2 *kp)
{
	char **argv;
	static char commstr[41];

	commstr[0]='\0';

	argv = kvm_getargv2(kd, kp, 40);
	if (argv != NULL) {
		while (*argv) {
			strlcat(commstr, *argv, sizeof(commstr));
			argv++;
			strlcat(commstr, " ", sizeof(commstr));
		}
	} else {
		const char *fmt;

		/*
		 * Commands that don't set an argv vector are printed with
		 * square brackets if they are system commands.  Otherwise
		 * they are printed within parentheses.
		 */
		if (kp->p_flag & P_SYSTEM)
			fmt = "[]";
		else
			fmt = "()";

		snprintf(commstr, sizeof(commstr), "%c%s%c", fmt[0],
		    kp->p_comm, fmt[1]);
	}

	return commstr;
}

double
pmem2float(struct kinfo_proc2 *kp)
{	                       
	double fracmem;
	static int szptudot = -1;

	/*
	 * XXX want pmap ptpages, segtab, etc. (per architecture),
	 * not just the uarea.
	 */
	if (szptudot < 0) {
		int mib[2];
		size_t size;
		int uspace;

		mib[0] = CTL_VM;
		mib[1] = VM_USPACE;
		size = sizeof(uspace);
		if (sysctl(mib, 2, &uspace, &size, NULL, 0) == 0) {
			szptudot = uspace / getpagesize();
		} else {
			/* pick a vaguely useful default */
			szptudot = getpagesize();
		}
	}

	/* XXX don't have info about shared */
	fracmem = ((double)kp->p_vm_rssize + szptudot) / mempages;
	return (fracmem >= 0) ? 100.0 * fracmem : 0;
}

char *
start2str(struct kinfo_proc2 *kp)
{
	struct timeval u_start;
	struct tm *tp;
	time_t startt;
	static char startstr[10];

	u_start.tv_sec = kp->p_ustart_sec;
	u_start.tv_usec = kp->p_ustart_usec;

	startt = u_start.tv_sec;
	tp = localtime(&startt);
	if (now == 0)
	        time(&now);
	if (now - u_start.tv_sec < 24 * SECSPERHOUR) {
	        strftime(startstr, sizeof(startstr) - 1, "%l:%M%p", tp);
	} else if (now - u_start.tv_sec < 7 * SECSPERDAY) {
	        strftime(startstr, sizeof(startstr) - 1, "%a%I%p", tp);
	} else  
	        strftime(startstr, sizeof(startstr) - 1, "%e%b%y", tp);

	return startstr;
}

char *    
time2str(struct kinfo_proc2 *kp)
{	       
	long secs;
	long psecs;     /* "parts" of a second. first micro, then centi */
	static char timestr[18];

	/* XXX - I don't like this. */
	if (kp->p_stat == SZOMB) {
	        secs = 0;
	        psecs = 0;
	} else {
	        /*
	         * This counts time spent handling interrupts.  We could
	         * fix this, but it is not 100% trivial (and interrupt
	         * time fractions only work on the sparc anyway).       XXX
	         */
	        secs = kp->p_rtime_sec;
	        psecs = kp->p_rtime_usec;
#if 0
	        if (sumrusage) {
	                secs += k->ki_u.u_cru.ru_utime.tv_sec +
	                        k->ki_u.u_cru.ru_stime.tv_sec;
	                psecs += k->ki_u.u_cru.ru_utime.tv_usec +
	                        k->ki_u.u_cru.ru_stime.tv_usec;
	        }
#endif
	        /*
	         * round and scale to 100's
	         */
	        psecs = (psecs + 5000) / 10000;
	        secs += psecs / 100;
	        psecs = psecs % 100;
	}
	snprintf(timestr, sizeof(timestr), "%3ld:%02ld.%02ld", secs/60,
	    secs%60, psecs);

	return timestr;
}

void
ps_user(char *args)
{
	uid_t uid;

	if (args == NULL || *args == 0 || strcmp(args, "+") == 0) {
		uid = SHOWUSER_ANY;
	} else if (uid_from_user(args, &uid) != 0) {
		error("%s: unknown user", args);
		return;
	}

	showuser = uid;
	display(0);
}