File: m_svr5.c

package info (click to toggle)
ptop 3.6.2-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,516 kB
  • sloc: ansic: 18,233; sh: 3,493; makefile: 124; awk: 44
file content (1439 lines) | stat: -rwxr-xr-x 32,295 bytes parent folder | download | duplicates (3)
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
/*
 * pg_top - a top PostgreSQL users display for Unix
 *
 * SYNOPSIS:  For Intel based System V Release 5 (Unixware7)
 *
 * DESCRIPTION:
 * System V release 5 for i[3456]86
 * Works for:
 * i586-sco-sysv5uw7  i386 SCO UNIX_SVR5 (UnixWare 7)
 *
 * LIBS:  -lelf -lmas
 *
 * CFLAGS: -DHAVE_GETOPT -DORDER
 *
 * AUTHORS: Mike Hopkirk	   <hops@sco.com>
 *			David Cutter	   <dpc@grail.com>
 *			Andrew Herbert	   <andrew@werple.apana.org.au>
 *			Robert Boucher	   <boucher@sofkin.ca>
 */

/* build config
 *	SHOW_NICE - process nice fields don't seem to be being updated so changed
 *	   default to display # of threads in use instead.
 *	   define this to display nice fields (values always 0)
 * #define SHOW_NICE 1
 */

#define _KMEMUSER
#define prpsinfo psinfo
#include <sys/procfs.h>

#define pr_state pr_lwp.pr_state
#define pr_nice pr_lwp.pr_nice
#define pr_pri pr_lwp.pr_pri
#define pr_onpro pr_lwp.pr_onpro
#define ZOMBIE(p)	((p)->pr_nlwp == 0)
#define SIZE_K(p)	pagetok((p)->pr_size)
#define RSS_K(p)	pagetok((p)->pr_rssize)


#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <dirent.h>
#include <nlist.h>
#include <string.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/sysmacros.h>
#include <vm/anon.h>
#include <sys/priocntl.h>
#include <sys/tspriocntl.h>
#include <sys/var.h>

#include "pg_top.h"
#include "machine.h"
#include "utils.h"

#define UNIX "/stand/unix"
#define KMEM "/dev/kmem"
#define PROCFS "/proc"
#define CPUSTATES	5

#ifndef PRIO_MAX
#define PRIO_MAX	20
#endif
#ifndef PRIO_MIN
#define PRIO_MIN	-20
#endif

#ifndef FSCALE
#define FSHIFT	8				/* bits to right of fixed binary point */
#define FSCALE	(1<<FSHIFT)
#endif

#define loaddouble(x) ((double)x/FSCALE)
#define pagetok(size) ((size) * pagesz) >> LOG1024

/* definitions for the index in the nlist array */
#define X_AVENRUN	0
#define X_V		1
#define X_MPID		2

static struct nlist nlst[] =
{
	{"avenrun"},				/* 0 */
	{"v"},						/* 1 */
	{"nextpid"},				/* 2 */
	{NULL}
};

static unsigned long avenrun_offset;
static unsigned long mpid_offset;

static unsigned int pagesz;

static void reallocproc(int n);
static int	maxprocs;

/* get_process_info passes back a handle.  This is what it looks like: */

struct handle
{
	struct prpsinfo **next_proc;	/* points to next valid proc pointer */
	int			remaining;		/* number of pointers remaining */
};

/*
 *	These definitions control the format of the per-process area
 */

static char header[] =
#ifdef SHOW_NICE
"  PID X        PRI NICE  SIZE   RES STATE   TIME      CPU  COMMAND";
#else
"  PID X        PRI  THR  SIZE   RES STATE   TIME      CPU  COMMAND";
#endif
/* 0123456	 -- field to fill in starts at header+6 */
#define UNAME_START 6
#define Proc_format \
	"%5d %-8.8s %3d %4d %5s %5s %-5s %6s %8.4f%% %.16s"

char	   *state_abbrev[] =
{"oncpu", "run", "sleep", "stop", "idle", "zombie"};

#define sZOMB 5
int			process_states[8];
char	   *procstatenames[] =
{
	" on cpu, ", " running, ", " sleeping, ", " stopped, ",
	" idling ", " zombie, ",
	NULL
};

int			cpu_states[CPUSTATES];
char	   *cpustatenames[] =
{"idle", "user", "kernel", "wait", NULL};


/* these are for detailing the memory statistics */
long		memory_stats[5];
char	   *memorynames[] =
{"K phys, ", "K used, ", "K free, ", "K swapUsed, ", "K swapFree", NULL};

/* these are names given to allowed sorting orders -- first is default */
char	   *ordernames[] = {
	"state", "cpu", "size", "res", "time", "pid", "uid", "rpid", "ruid", NULL
};

/* forward definitions for comparison functions */
int			proc_compare();
int			compare_cpu();
int			compare_size();
int			compare_res();
int			compare_time();
int			compare_pid();
int			compare_uid();
int			compare_rpid();
int			compare_ruid();

int			(*proc_compares[]) () =
{
	proc_compare,
	compare_cpu,
	compare_size,
	compare_res,
	compare_time,
	compare_pid,
	compare_uid,
	compare_rpid,
	compare_ruid,
	NULL
};


static int	kmem = -1;
static int	nproc;
static int	bytes;
static struct prpsinfo *pbase;
static struct prpsinfo **pref;
static DIR *procdir;

/* useful externals */
extern int	errno;
extern char *sys_errlist[];
extern char *myname;
extern long percentages();
extern int	check_nlist();
extern int	getkval();
extern void perror();
extern void getptable();
extern void quit();
extern int	nlist();

/* fwd dcls */
static int	kmet_init(void);
static int	get_cpustates(int *new);


int
machine_init(struct statics * statics)
{
	static struct var v;
	int			i;

	/* fill in the statics information */
	statics->procstate_names = procstatenames;
	statics->cpustate_names = cpustatenames;
	statics->memory_names = memorynames;
	statics->order_names = ordernames;

	/* get the list of symbols we want to access in the kernel */
	if (nlist(UNIX, nlst))
	{
		(void) fprintf(stderr, "Unable to nlist %s\n", UNIX);
		return (-1);
	}

	/* make sure they were all found */
	if (check_nlist(nlst) > 0)
		return (-1);

	/* open kernel memory */
	if ((kmem = open(KMEM, O_RDONLY)) == -1)
	{
		perror(KMEM);
		return (-1);
	}

	v.v_proc = 200;				/* arbitrary default */
	/* get the symbol values out of kmem */
	/* NPROC Tuning parameter for max number of processes */
	(void) getkval(nlst[X_V].n_value, &v, sizeof(struct var), nlst[X_V].n_name);
	nproc = v.v_proc;
	maxprocs = nproc;

	/* stash away certain offsets for later use */
	mpid_offset = nlst[X_MPID].n_value;
	avenrun_offset = nlst[X_AVENRUN].n_value;

	/* allocate space for proc structure array and array of pointers */
	bytes = nproc * sizeof(struct prpsinfo);
	pbase = (struct prpsinfo *) malloc(bytes);
	pref = (struct prpsinfo **) malloc(nproc * sizeof(struct prpsinfo *));

	pagesz = sysconf(_SC_PAGESIZE);


	/* Just in case ... */
	if (pbase == (struct prpsinfo *) NULL || pref == (struct prpsinfo **) NULL)
	{
		(void) fprintf(stderr, "%s: can't allocate sufficient memory\n", myname);
		return (-1);
	}

	if (!(procdir = opendir(PROCFS)))
	{
		(void) fprintf(stderr, "Unable to open %s\n", PROCFS);
		return (-1);
	}

	if (chdir(PROCFS))
	{							/* handy for later on when we're reading it */
		(void) fprintf(stderr, "Unable to chdir to %s\n", PROCFS);
		return (-1);
	}


	kmet_init();

	/* all done! */
	return (0);
}

char *
format_header(char *uname_field)
{
	register char *ptr;

	ptr = header + UNAME_START;
	while (*uname_field != '\0')
		*ptr++ = *uname_field++;

	return (header);
}

void
get_system_info(struct system_info * si)
{
	long		avenrun[3];
	long		mem;
	static time_t cp_old[CPUSTATES];
	static time_t cp_diff[CPUSTATES];	/* for cpu state percentages */
	register int i;
	static long swap_total;
	static long swap_free;
	int			new_states[CPUSTATES];

	get_cpustates(new_states);

	/* convert cp_time counts to percentages */
	(void) percentages(CPUSTATES, cpu_states, new_states, cp_old, cp_diff);


	si->last_pid = -1;

	/*
	 * get mpid -- process id of last process svr5 is nextpid - next pid to be
	 * assigned (already incremented)
	 */
	(void) getkval(mpid_offset, &(si->last_pid), sizeof(si->last_pid),
				   "nextpid");
	(si->last_pid)--;			/* so we shld decrement for display */


	/* get load average array */
	(void) getkval(avenrun_offset, (int *) avenrun, sizeof(avenrun), "avenrun");
	/* convert load averages to doubles */
	for (i = 0; i < 3; i++)
		si->load_avg[i] = loaddouble(avenrun[i]);

	mem = sysconf(_SC_TOTAL_MEMORY);	/* physical mem */
	memory_stats[0] = pagetok(mem);

	mem = kmet_get_freemem();	/* free mem */
	memory_stats[2] = pagetok(mem);

	/* mem = sysconf(_SC_GENERAL_MEMORY);	 */
	memory_stats[1] = memory_stats[0] - memory_stats[2];		/* active */

	get_swapinfo(&swap_total, &swap_free);
	memory_stats[3] = pagetok(swap_total - swap_free);
	memory_stats[4] = pagetok(swap_free);


	/* set arrays and strings */
	si->cpustates = cpu_states;
	si->memory = memory_stats;
}

static struct handle handle;

caddr_t
get_process_info(
				 struct system_info * si,
				 struct process_select * sel,
				 int idx)
{
	register int i;
	register int total_procs;
	register int active_procs;
	register struct prpsinfo **prefp;
	register struct prpsinfo *pp;

	/* these are copied out of sel for speed */
	int			show_idle;
	int			show_system;
	int			show_uid;

	/* Get current number of processes */

	/* read all the proc structures */
	getptable(pbase);

	/* get a pointer to the states summary array */
	si->procstates = process_states;

	/* set up flags which define what we are going to select */
	show_idle = sel->idle;
	show_system = sel->system;
	show_uid = sel->uid != -1;

	nproc = kmet_get_nproc();

	/* count up process states and get pointers to interesting procs */
	total_procs = 0;
	active_procs = 0;
	(void) memset(process_states, 0, sizeof(process_states));
	prefp = pref;

	for (pp = pbase, i = 0; i < nproc; pp++, i++)
	{
		/*
		 * Place pointers to each valid proc structure in pref[]. Process
		 * slots that are actually in use have a non-zero status field.
		 * Processes with PR_ISSYS set are system processes---these get
		 * ignored unless show_sysprocs is set.
		 */
		if ((pp->pr_state >= SONPROC && pp->pr_state <= SIDL) &&
			(show_system || ((pp->pr_flag & PR_ISSYS) == 0)))
		{
			total_procs++;
			process_states[pp->pr_state]++;
			if ((!ZOMBIE(pp)) &&
				(show_idle || (pp->pr_state == SRUN) || (pp->pr_state == SONPROC)) &&
				(!show_uid || pp->pr_uid == (uid_t) sel->uid))
			{
				*prefp++ = pp;
				active_procs++;
			}
			if (ZOMBIE(pp))
				process_states[sZOMB]++;		/* invented */

		}
	}

	/* if requested, sort the "interesting" processes */
	qsort((char *) pref, active_procs, sizeof(struct prpsinfo *),
		  proc_compares[idx]);

	/* remember active and total counts */
	si->p_total = total_procs;
	si->P_ACTIVE = active_procs;

	/* pass back a handle */
	handle.next_proc = pref;
	handle.remaining = active_procs;
	return ((caddr_t) & handle);
}

/*
 * cpu percentage calculation is as fm ps.c
 * seems to be ratio of (sys+user time used)/(elapsed time)
 * i.e percent of cpu utilised when on cpu
 */
static double
percent_cpu(struct prpsinfo * pp)
{
	static time_t tim = 0L;
	time_t		starttime;
	time_t		ctime;
	time_t		etime;

	/* if (tim == 0L) */
	tim = time((time_t *) 0);
	starttime = pp->pr_start.tv_sec;
	if (pp->pr_start.tv_nsec > 500000000)
		starttime++;
	etime = (tim - starttime);
	ctime = pp->pr_time.tv_sec;
	if (pp->pr_time.tv_nsec > 500000000)
		ctime++;
	if (etime)
	{
		/* return  (float)(ctime * 100) / (unsigned)etime; */

		/*
		 * this was ocasionally giving vals >100 for some unknown reason so
		 * the below normalises it
		 */

		double		pct;

		pct = (float) (ctime * 100) / (unsigned) etime;
		return (pct < 100.0) ? pct : 100.00;
	}
	return 0.00;
}


char		fmt[MAX_COLS];		/* static area where result is built */

char *
format_next_process(
					caddr_t handle,
					char *(*get_userid) ())
{
	register struct prpsinfo *pp;
	struct handle *hp;
	register long cputime;
	register double pctcpu;

	/* find and remember the next proc structure */
	hp = (struct handle *) handle;
	pp = *(hp->next_proc++);
	hp->remaining--;

	/* get the cpu usage and calculate the cpu percentages */
	cputime = pp->pr_time.tv_sec;
	pctcpu = percent_cpu(pp);


	/* format this entry */
	(void) sprintf(fmt,
				   Proc_format,
				   pp->pr_pid,
				   (*get_userid) (pp->pr_uid),
				   pp->pr_pri,
#ifdef SHOW_NICE
				   pp->pr_nice,
#else
				   (u_short) pp->pr_nlwp < 999 ? (u_short) pp->pr_nlwp : 999,
#endif
				   format_k(SIZE_K(pp)),
				   format_k(RSS_K(pp)),
				   (ZOMBIE(pp)) ? state_abbrev[sZOMB]
				   : state_abbrev[pp->pr_state],
				   format_time(cputime),
					/* 100.0 * */ pctcpu,
				   printable(pp->pr_fname));

	/* return the result */
	return (fmt);
}

/*
 * check_nlist(nlst) - checks the nlist to see if any symbols were not
 *		found.	For every symbol that was not found, a one-line
 *		message is printed to stderr.  The routine returns the
 *		number of symbols NOT found.
 */
int
check_nlist(register struct nlist * nlst)
{
	register int i;

	/* check to see if we got ALL the symbols we requested */
	/* this will write one line to stderr for every symbol not found */

	i = 0;
	while (nlst->n_name != NULL)
	{
		if (nlst->n_value == 0)
		{
			/* this one wasn't found */
			(void) fprintf(stderr, "kernel: no symbol named `%s'\n", nlst->n_name);
			i = 1;
		}
		nlst++;
	}
	return (i);
}


/*
 *	getkval(offset, ptr, size, refstr) - get a value out of the kernel.
 *	"offset" is the byte offset into the kernel for the desired value,
 *		"ptr" points to a buffer into which the value is retrieved,
 *		"size" is the size of the buffer (and the object to retrieve),
 *		"refstr" is a reference string used when printing error meessages,
 *		if "refstr" starts with a '!', then a failure on read will not
 *			be fatal (this may seem like a silly way to do things, but I
 *			really didn't want the overhead of another argument).
 *
 */
int
getkval(
		unsigned long offset,
		int *ptr,
		int size,
		char *refstr)
{
	if (lseek(kmem, (long) offset, 0) == -1)
	{
		if (*refstr == '!')
			refstr++;
		(void) fprintf(stderr, "%s: lseek to %s: %s\n",
					   myname, refstr, sys_errlist[errno]);
		quit(22);
	}
	if (read(kmem, (char *) ptr, size) == -1)
		if (*refstr == '!')
			/* we lost the race with the kernel, process isn't in memory */
			return (0);
		else
		{
			(void) fprintf(stderr, "%s: reading %s: %s\n",
						   myname, refstr, sys_errlist[errno]);
			quit(23);
		}
	return (1);
}

/* ----------------- comparison routines for qsort ---------------- */

/* First, the possible comparison keys.  These are defined in such a way
   that they can be merely listed in the source code to define the actual
   desired ordering.
 */

#define ORDERKEY_PCTCPU  if (dresult = percent_cpu (p2) - percent_cpu (p1),\
				 (result = dresult > 0.0 ? 1 : \
				 dresult < 0.0 ? -1 : 0) == 0)

#define ORDERKEY_CPTICKS if ((result = p2->pr_time.tv_sec - p1->pr_time.tv_sec) == 0)
#define ORDERKEY_STATE	 if ((result = (long) (sorted_state[p2->pr_state] - \
				   sorted_state[p1->pr_state])) == 0)

#define ORDERKEY_PRIO	 if ((result = p2->pr_pri	 - p1->pr_pri)	  == 0)
#define ORDERKEY_RSSIZE  if ((result = p2->pr_rssize - p1->pr_rssize) == 0)
#define ORDERKEY_MEM	 if ((result = (p2->pr_size  - p1->pr_size))  == 0)

#define ORDERKEY_PID	 if ((result = (p2->pr_pid	- p1->pr_pid))	== 0)
#define ORDERKEY_UID	 if ((result = (p2->pr_uid	- p1->pr_uid))	== 0)
#define ORDERKEY_RPID	 if ((result = (p1->pr_pid	- p2->pr_pid))	== 0)
#define ORDERKEY_RUID	 if ((result = (p1->pr_uid	- p2->pr_uid))	== 0)

/* states enum {SONPROC, SRUN, SSLEEP, SSTOP, SIDL}  */
unsigned char sorted_state[] =
{
	7,							/* onproc		*/
	6,							/* run				*/
	5,							/* sleep		*/
	4,							/* stop				*/
	3,							/* idle			*/
	2,							/* zombie		*/
	0,							/* unused				*/
	0							/* unused			*/
};

#if 0
/*
 *	proc_compare - original singleton comparison function for "qsort"
 *	Compares the resource consumption of two processes using five
 *		distinct keys.	The keys (in descending order of importance) are:
 *		percent cpu, cpu ticks, state, resident set size, total virtual
 *		memory usage.  The process states are ordered as follows (from least
 *		to most important):  WAIT, zombie, sleep, stop, start, run.  The
 *		array declaration below maps a process state index into a number
 *		that reflects this ordering.
 */
 /* default comparison rtn */
int
original_proc_compare(
					  struct prpsinfo ** pp1,
					  struct prpsinfo ** pp2)
{
	register struct prpsinfo *p1;
	register struct prpsinfo *p2;
	register long result;
	double		dresult;

	/* remove one level of indirection */
	p1 = *pp1;
	p2 = *pp2;

	/* compare percent cpu (pctcpu) */
	dresult = percent_cpu(p2) - percent_cpu(p1);
	result = dresult > 0.0 ? 1 :
		dresult < 0.0 ? -1 : 0;
	if (result)
	{
		/* use cpticks to break the tie */
		if ((result = p2->pr_time.tv_sec - p1->pr_time.tv_sec) == 0)
		{
			/* use process state to break the tie */
			if ((result = (long) (sorted_state[p2->pr_state] -
								  sorted_state[p1->pr_state])) == 0)
			{
				/* use priority to break the tie */
				if ((result = p2->pr_pri - p1->pr_pri) == 0)
				{
					/* use resident set size (rssize) to break the tie */
					if ((result = p2->pr_rssize - p1->pr_rssize) == 0)
					{
						/* use total memory to break the tie */
						result = (p2->pr_size - p1->pr_size);
					}
				}
			}
		}
	}
	return (result);
}
#endif   /* original comparison rtn */

/* compare_state - comparison function for sorting by state,pri,time,size */
int
proc_compare(
			 struct prpsinfo ** pp1,
			 struct prpsinfo ** pp2)
{
	register struct prpsinfo *p1;
	register struct prpsinfo *p2;
	register long result;
	double		dresult;

	/* remove one level of indirection */
	p1 = *pp1;
	p2 = *pp2;

	ORDERKEY_STATE
		ORDERKEY_PRIO
		ORDERKEY_CPTICKS
		ORDERKEY_RSSIZE
		ORDERKEY_MEM
		ORDERKEY_PCTCPU
		;

	return (result);
}


/* compare_cpu - the comparison function for sorting by cpu % (deflt) */
int
compare_cpu(
			struct prpsinfo ** pp1,
			struct prpsinfo ** pp2)
{
	register struct prpsinfo *p1;
	register struct prpsinfo *p2;
	register long result;
	double		dresult;

	/* remove one level of indirection */
	p1 = *pp1;
	p2 = *pp2;

	ORDERKEY_PCTCPU
		ORDERKEY_CPTICKS
		ORDERKEY_STATE
		ORDERKEY_PRIO
		ORDERKEY_RSSIZE
		ORDERKEY_MEM
		;

	return (result);
}

/* compare_size - the comparison function for sorting by total memory usage */
int
compare_size(
			 struct prpsinfo ** pp1,
			 struct prpsinfo ** pp2)
{
	register struct prpsinfo *p1;
	register struct prpsinfo *p2;
	register long result;
	double		dresult;

	/* remove one level of indirection */
	p1 = *pp1;
	p2 = *pp2;

	ORDERKEY_MEM
		ORDERKEY_RSSIZE
		ORDERKEY_PCTCPU
		ORDERKEY_CPTICKS
		ORDERKEY_STATE
		ORDERKEY_PRIO
		;

	return (result);
}

/* compare_res - the comparison function for sorting by resident set size */
int
compare_res(
			struct prpsinfo ** pp1,
			struct prpsinfo ** pp2)
{
	register struct prpsinfo *p1;
	register struct prpsinfo *p2;
	register long result;
	double		dresult;

	/* remove one level of indirection */
	p1 = *pp1;
	p2 = *pp2;

	ORDERKEY_RSSIZE
		ORDERKEY_MEM
		ORDERKEY_PCTCPU
		ORDERKEY_CPTICKS
		ORDERKEY_STATE
		ORDERKEY_PRIO
		;

	return (result);
}

/* compare_time - the comparison function for sorting by total cpu time */
int
compare_time(
			 struct prpsinfo ** pp1,
			 struct prpsinfo ** pp2)
{
	register struct prpsinfo *p1;
	register struct prpsinfo *p2;
	register long result;
	double		dresult;

	/* remove one level of indirection */
	p1 = *pp1;
	p2 = *pp2;

	ORDERKEY_CPTICKS
		ORDERKEY_PCTCPU
		ORDERKEY_STATE
		ORDERKEY_PRIO
		ORDERKEY_MEM
		ORDERKEY_RSSIZE
		;

	return (result);
}

/* compare_pid - the comparison function for sorting by pid */
int
compare_pid(
			struct prpsinfo ** pp1,
			struct prpsinfo ** pp2)
{
	register struct prpsinfo *p1;
	register struct prpsinfo *p2;
	register long result;
	double		dresult;

	/* remove one level of indirection */
	p1 = *pp1;
	p2 = *pp2;

	ORDERKEY_PID
		ORDERKEY_CPTICKS
		ORDERKEY_PCTCPU
		ORDERKEY_STATE
		ORDERKEY_PRIO
		ORDERKEY_MEM
		ORDERKEY_RSSIZE
		;

	return (result);
}

/* compare_uid - the comparison function for sorting by user ID */
int
compare_uid(
			struct prpsinfo ** pp1,
			struct prpsinfo ** pp2)
{
	register struct prpsinfo *p1;
	register struct prpsinfo *p2;
	register long result;
	double		dresult;

	/* remove one level of indirection */
	p1 = *pp1;
	p2 = *pp2;

	ORDERKEY_UID
		ORDERKEY_CPTICKS
		ORDERKEY_PCTCPU
		ORDERKEY_STATE
		ORDERKEY_PRIO
		ORDERKEY_MEM
		ORDERKEY_RSSIZE
		;

	return (result);
}

/* compare_rpid - the comparison function for sorting by pid ascending */
int
compare_rpid(
			 struct prpsinfo ** pp1,
			 struct prpsinfo ** pp2)
{
	register struct prpsinfo *p1;
	register struct prpsinfo *p2;
	register long result;
	double		dresult;

	/* remove one level of indirection */
	p1 = *pp1;
	p2 = *pp2;

	ORDERKEY_RPID
		ORDERKEY_CPTICKS
		ORDERKEY_PCTCPU
		ORDERKEY_STATE
		ORDERKEY_PRIO
		ORDERKEY_MEM
		ORDERKEY_RSSIZE
		;

	return (result);
}

/* compare_uid - the comparison function for sorting by user ID ascending */
int
compare_ruid(
			 struct prpsinfo ** pp1,
			 struct prpsinfo ** pp2)
{
	register struct prpsinfo *p1;
	register struct prpsinfo *p2;
	register long result;
	double		dresult;

	/* remove one level of indirection */
	p1 = *pp1;
	p2 = *pp2;

	ORDERKEY_RUID
		ORDERKEY_CPTICKS
		ORDERKEY_PCTCPU
		ORDERKEY_STATE
		ORDERKEY_PRIO
		ORDERKEY_MEM
		ORDERKEY_RSSIZE
		;

	return (result);
}


/* ---------------- helper rtns ---------------- */

/*
 * get process table
 */
void
getptable(struct prpsinfo * baseptr)
{
	struct prpsinfo *currproc;	/* pointer to current proc structure	*/
	int			numprocs = 0;
	struct dirent *direntp;

	currproc = baseptr;
	for (rewinddir(procdir); direntp = readdir(procdir);)
	{
		int			fd;
		char		buf[30];

		sprintf(buf, "%s/psinfo", direntp->d_name);

		if ((fd = open(buf, O_RDONLY)) < 0)
			continue;

		if (read(fd, currproc, sizeof(psinfo_t)) != sizeof(psinfo_t))
		{
			(void) close(fd);
			continue;
		}

		numprocs++;
		currproc++;

		(void) close(fd);

		/* Atypical place for growth */
		if (numprocs >= maxprocs)
		{
			reallocproc(2 * numprocs);
			currproc = (struct prpsinfo *)
				((char *) baseptr + sizeof(psinfo_t) * numprocs);
		}

	}

	if (nproc != numprocs)
		nproc = numprocs;
}

/* return the owner of the specified process, for use in commands.c as we're
   running setuid root */
int
proc_owner(int pid)
{
	register struct prpsinfo *p;
	int			i;

	for (i = 0, p = pbase; i < nproc; i++, p++)
		if (p->pr_pid == (pid_t) pid)
			return ((int) (p->pr_uid));

	return (-1);
}

int
setpriority(int dummy, int who, int niceval)
{
	int			scale;
	int			prio;
	pcinfo_t	pcinfo;
	pcparms_t	pcparms;
	tsparms_t  *tsparms;

	strcpy(pcinfo.pc_clname, "TS");
	if (priocntl(0, 0, PC_GETCID, (caddr_t) & pcinfo) == -1)
		return (-1);

	prio = niceval;
	if (prio > PRIO_MAX)
		prio = PRIO_MAX;
	else if (prio < PRIO_MIN)
		prio = PRIO_MIN;

	tsparms = (tsparms_t *) pcparms.pc_clparms;
	scale = ((tsinfo_t *) pcinfo.pc_clinfo)->ts_maxupri;
	tsparms->ts_uprilim = tsparms->ts_upri = -(scale * prio) / 20;
	pcparms.pc_cid = pcinfo.pc_cid;

	if (priocntl(P_PID, who, PC_SETPARMS, (caddr_t) & pcparms) == -1)
		return (-1);

	return (0);
}


get_swapinfo(long *total, long *fr)
{
	register int cnt,
				i;
	register long t,
				f;
	struct swaptable *swt;
	struct swapent *ste;
	static char path[256];

	/* get total number of swap entries */
	cnt = swapctl(SC_GETNSWP, 0);

	/* allocate enough space to hold count + n swapents */
	swt = (struct swaptable *) malloc(sizeof(int) +
									  cnt * sizeof(struct swapent));
	if (swt == NULL)
	{
		*total = 0;
		*fr = 0;
		return;
	}
	swt->swt_n = cnt;

	/*
	 * fill in ste_path pointers: we don't care about the paths, so we point
	 * them all to the same buffer
	 */
	ste = &(swt->swt_ent[0]);
	i = cnt;
	while (--i >= 0)
	{
		ste++->ste_path = path;
	}

	/* grab all swap info */
	swapctl(SC_LIST, swt);

	/* walk thru the structs and sum up the fields */
	t = f = 0;
	ste = &(swt->swt_ent[0]);
	i = cnt;
	while (--i >= 0)
	{
		/* dont count slots being deleted */
		if (!(ste->ste_flags & ST_INDEL))
		{
			t += ste->ste_pages;
			f += ste->ste_free;
		}
		ste++;
	}

	/* fill in the results */
	*total = t;
	*fr = f;
	free(swt);
}


/*
 * When we reach a proc limit, we need to realloc the stuff.
 */
static void
reallocproc(int n)
{
	int			bytes;
	struct oldproc *op,
			   *endbase;

	if (n < maxprocs)
		return;

	maxprocs = n;

	/* allocate space for proc structure array and array of pointers */
	bytes = maxprocs * sizeof(psinfo_t);
	pbase = (struct prpsinfo *) realloc(pbase, bytes);
	pref = (struct prpsinfo **) realloc(pref,
										maxprocs * sizeof(struct prpsinfo *));

	/* Just in case ... */
	if (pbase == (struct prpsinfo *) NULL || pref == (struct prpsinfo **) NULL)
	{
		fprintf(stderr, "%s: can't allocate sufficient memory\n", myname);
		quit(1);
	}
}

/* ---------------------------------------------------------------- */
/* Access kernel Metrics
 * SVR5 uses metreg inteface to Kernel statistics (metrics)
 *	see /usr/include/mas.h, /usr/include/metreg.h
 */

#include <sys/mman.h>
#include <sys/dl.h>
#include <mas.h>
#include <metreg.h>

static int	md;					/* metric descriptor handle */
static uint32 ncpu;				/* number of processors in system */

/* fwd dcls */
static uint32 kmet_get_cpu(int type, char *desc);
static void kmet_verify(
			uint32 md, metid_t id, units_t units, type_t mettype,
			uint32 metsz, uint32 nobj, uint32 nlocs, resource_t res_id,
			uint32 ressz);


static int
get_cpustates(int *new)
{
	new[0] = (int) kmet_get_cpu(MPC_CPU_IDLE, "idle");
	new[1] = (int) kmet_get_cpu(MPC_CPU_USR, "usr");
	new[2] = (int) kmet_get_cpu(MPC_CPU_SYS, "sys");
	new[3] = (int) kmet_get_cpu(MPC_CPU_WIO, "wio");
}


/* initialises kernel metrics access and gets #cpus */
static int
kmet_init()
{
	uint32	   *ncpu_p;

	/* open (and map in) the metric access file and assoc data structures */
	if ((md = mas_open(MAS_FILE, MAS_MMAP_ACCESS)) < 0)
	{
		(void) fprintf(stderr, "mas_open failed\n");
		mas_perror();
		quit(10);
	}

	/* verify the NCPU metric is everything we expect */
	kmet_verify(md, NCPU, CPUS, CONFIGURABLE, sizeof(short),
				1, 1, MAS_SYSTEM, sizeof(uint32));

	/* get the number of cpu's on the system */
	if ((ncpu_p = (uint32 *) mas_get_met(md, NCPU, 0)) == NULL)
	{
		(void) fprintf(stderr, "mas_get_met of ncpu failed\n");
		mas_perror();
		quit(12);
	}
	ncpu = (uint32) (*(short *) ncpu_p);

	/*
	 * check that MPC_CPU_IDLE is of the form we expect ( paranoically we
	 * should check the rest as well but ... )
	 */
	kmet_verify(md, MPC_CPU_IDLE, TIX, PROFILE, sizeof(uint32),
				1, ncpu, NCPU, sizeof(short));

	kmet_verify(md, PROCUSE, PROCESSES, COUNT, sizeof(uint32),
				1, 1, MAS_SYSTEM, sizeof(uint32));
	nproc = kmet_get_nproc();

	return 0;
}

/* done with kernel metrics access */
static int
kmet_done()
{
	if (mas_close(md) < 0)
	{
		(void) fprintf(stderr, "mas_close failed\n");
		mas_perror();
		quit(14);
	}
}


static uint32
kmet_get_cpu(int type, char *desc)
{
	int			i;
	uint32		r = 0,
				rtot = 0;

	for (i = 0; i < ncpu; i++)
	{
		r = *(uint32 *) mas_get_met(md, (metid_t) type, 0);
		if (!r)
		{
			(void) fprintf(stderr, "mas_get_met of %s failed\n", desc);
			mas_perror();
			quit(12);
		}
		rtot += r;				/* sum them for multi cpus */
	}
	return rtot /* /ncpu */ ;
}

static int
kmet_get_freemem()
{
	dl_t	   *fm_p,
				fm,
				fmc,
				denom;
	time_t		td1;
	static time_t td0;
	static dl_t fm_old;


	td1 = time(NULL);
	if ((fm_p = (dl_t *) mas_get_met(md, FREEMEM, 0)) == NULL)
	{
		(void) fprintf(stderr, "mas_get_met of freemem failed\n");
		mas_perror();
		quit(12);
	}
	fm = *fm_p;

	denom.dl_hop = 0;
	denom.dl_lop = (long) (td1 - td0);
	td0 = td1;

	/*
	 * calculate the freemem difference divided by the time diff giving the
	 * freemem in that time sample (new - old) / (time_between_samples)
	 */
	fmc = lsub(fm, fm_old);
	fm_old = fm;

	fmc = ldivide(fmc, denom);
	return fmc.dl_lop;
}

/*
 * return # of processes currently executing on system
 */
static int
kmet_get_nproc()
{
	uint32	   *p;

	if ((p = (uint32 *) mas_get_met(md, PROCUSE, 0)) == NULL)
	{
		(void) fprintf(stderr, "mas_get_met of procuse failed\n");
		mas_perror();
		quit(11);
	}
	nproc = (int) *p;
}


/*
 * Function:	kmet_verify
 * renamed from mas_usrtime example verify_met() fm Doug Souders
 *
 * Description: Verify the registration data associated with this metric
 *		match what are expected.  Cautious consumer applications
 *		should do this sort of verification before using metrics.
 */
static void
kmet_verify(
			uint32 md,			/* metric descriptor				*/
			metid_t id,			/* metric id number					*/
			units_t units,		/* expected units of metric			*/
			type_t mettype,		/* expected type of metric			*/
			uint32 metsz,		/* expected object size of metric	*/
			uint32 nobj,		/* expected number of array elements */
			uint32 nlocs,		/* expected number of instances		*/
			resource_t res_id,	/* expected resource id number		*/
			uint32 ressz		/* expected resource object size	*/
)
{

	char	   *name;			/* the name of the metric	*/
	units_t    *units_p;		/* the units of the metric	*/
	type_t	   *mettype_p;		/* type field of the metric */
	uint32	   *objsz_p;		/* size of each element in met	*/
	uint32	   *nobj_p;			/* num of elements >1 then array */
	uint32	   *nlocs_p;		/* total number of instances	*/
	uint32	   *status_p;		/* status word (update|avail)	*/
	resource_t *resource_p;		/* the resource list of the met */
	uint32	   *resval_p;		/* pointer to resource		*/
	uint32	   *ressz_p;		/* size of the resource met */

	if (!(name = mas_get_met_name(md, id)))
	{
		(void) fprintf(stderr, "mas_get_met_name failed\n");
		mas_perror();
		quit(11);
	}

	if (!(status_p = mas_get_met_status(md, id)))
	{
		(void) fprintf(stderr, "mas_get_met_status of %s failed\n",
					   name);
		mas_perror();
		quit(11);
	}
	if (*status_p != MAS_AVAILABLE)
	{
		(void) fprintf(stderr, "unexpected status word for %s\n"
					   "- expected %u got %u\n",
					   name, MAS_AVAILABLE, *status_p);
		quit(11);
	}
	if (!(units_p = mas_get_met_units(md, id)))
	{
		(void) fprintf(stderr, "mas_get_met_units of %s failed\n",
					   name);
		mas_perror();
		quit(11);
	}
	if (units != *units_p)
	{
		(void) fprintf(stderr, "unexpected units for %s\n"
					   "- expected %u got %u\n",
					   name, units, *units_p);
		quit(11);
	}

	if (!(mettype_p = mas_get_met_type(md, id)))
	{
		(void) fprintf(stderr, "mas_get_met_type of %s failed\n",
					   name);
		mas_perror();
		quit(11);
	}
	if (mettype != *mettype_p)
	{
		(void) fprintf(stderr, "unexpected metric type for %s\n"
					   "- expected %u got %u\n",
					   name, mettype, *mettype_p);
		quit(11);
	}

	if (!(objsz_p = mas_get_met_objsz(md, id)))
	{
		(void) fprintf(stderr, "mas_get_met_objsz of %s failed\n", name);
		mas_perror();
		quit(11);
	}
	if (*objsz_p != metsz)
	{
		(void) fprintf(stderr, "unexpected object size for %s\n"
					   "- expected %u got %u\n",
					   name, metsz, *objsz_p);
		quit(11);
	}

	if (!(nobj_p = mas_get_met_nobj(md, id)))
	{
		(void) fprintf(stderr, "mas_get_met_nobj of %s failed\n", name);
		mas_perror();
		quit(11);
	}
	if (nobj != *nobj_p)
	{
		(void) fprintf(stderr, "unexpected number of objects for %s\n"
					   "- expected %u got %u\n",
					   name, nobj, *nobj_p);
		quit(11);
	}

	/* get the number of instances that libmas thinks it knows about  */
	if (!(nlocs_p = mas_get_met_nlocs(md, id)))
	{
		(void) fprintf(stderr, "mas_get_met_nlocs of %s failed\n", name);
		mas_perror();
		quit(11);
	}
	if (nlocs != *nlocs_p)
	{
		(void) fprintf(stderr, "unexpected number of instances for %s"
					   " - expected %u got %u\n",
					   name, nlocs, *nlocs_p);
		quit(11);

	}
	/* get the resource list for the metric */
	if (!(resource_p = mas_get_met_resources(md, id)))
	{
		(void) fprintf(stderr, "mas_get_met_resources of %s failed\n", name);
		mas_perror();
		quit(11);
	}
	if (*resource_p != res_id)
	{
		(void) fprintf(stderr, "unexpected resource id for %s\n"
					   "- expected %u got %u\n",
					   name, res_id, *resource_p);
		quit(11);
	}
	/* get the size of the resource  */
	if (!(ressz_p = mas_get_met_objsz(md, (metid_t) (*resource_p))))
	{
		(void) fprintf(stderr, "mas_get_met_objsz of resource failed\n");
		mas_perror();
		quit(11);
	}
	if (*ressz_p != ressz)
	{
		(void) fprintf(stderr, "unexpected resource size for %s\n"
					   "- expected %u got %u\n",
					   name, ressz, *ressz_p);
		quit(11);
	}
/*
 *	get the address of the resource
 */
	if (!(resval_p = (uint32 *) mas_get_met(md, *resource_p, 0)))
	{
		(void) fprintf(stderr, "mas_get_met of resource failed\n");
		mas_perror();
		quit(11);
	}
	if (ressz == sizeof(short))
	{
		if ((uint32) (*(short *) resval_p) != nlocs)
		{
			(void) fprintf(stderr, "unexpected resource value for %s\n"
						   "- expected %u got %u\n",
						   name, nlocs, (uint32) (*(short *) resval_p));
			quit(11);
		}
	}
	else
	{							/* assume size of uint32 */
		if (*resval_p != nlocs)
		{
			(void) fprintf(stderr, "unexpected resource value for %s\n"
						   "- expected %u got %u\n",
						   name, nlocs, *resval_p);
			quit(11);
		}
	}
	return;
}