machine.c revision 76079
1/* 2 * top - a top users display for Unix 3 * 4 * SYNOPSIS: For FreeBSD-2.x and later 5 * 6 * DESCRIPTION: 7 * Originally written for BSD4.4 system by Christos Zoulas. 8 * Ported to FreeBSD 2.x by Steven Wallace && Wolfram Schneider 9 * Order support hacked in from top-3.5beta6/machine/m_aix41.c 10 * by Monte Mitzelfelt (for latest top see http://www.groupsys.com/topinfo/) 11 * 12 * This is the machine-dependent module for FreeBSD 2.2 13 * Works for: 14 * FreeBSD 2.2.x, 3.x, 4.x, and probably FreeBSD 2.1.x 15 * 16 * LIBS: -lkvm 17 * 18 * AUTHOR: Christos Zoulas <christos@ee.cornell.edu> 19 * Steven Wallace <swallace@freebsd.org> 20 * Wolfram Schneider <wosch@FreeBSD.org> 21 * Thomas Moestl <tmoestl@gmx.net> 22 * 23 * $FreeBSD: head/usr.bin/top/machine.c 76079 2001-04-27 19:33:50Z jhb $ 24 */ 25 26 27#include <sys/time.h> 28#include <sys/types.h> 29#include <sys/signal.h> 30#include <sys/param.h> 31 32#include "os.h" 33#include <stdio.h> 34#include <nlist.h> 35#include <math.h> 36#include <kvm.h> 37#include <pwd.h> 38#include <sys/errno.h> 39#include <sys/sysctl.h> 40#include <sys/dkstat.h> 41#include <sys/file.h> 42#include <sys/time.h> 43#include <sys/proc.h> 44#include <sys/user.h> 45#include <sys/vmmeter.h> 46#include <sys/resource.h> 47#include <sys/rtprio.h> 48 49/* Swap */ 50#include <stdlib.h> 51#include <sys/conf.h> 52 53#include <unistd.h> 54#include <osreldate.h> /* for changes in kernel structures */ 55 56#include "top.h" 57#include "machine.h" 58#include "screen.h" 59#include "utils.h" 60 61static void getsysctl __P((char *, void *, size_t)); 62 63#define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var)) 64 65extern char* printable __P((char *)); 66int swapmode __P((int *retavail, int *retfree)); 67static int smpmode; 68static int namelength; 69static int cmdlengthdelta; 70 71/* Prototypes for top internals */ 72void quit __P((int)); 73 74/* get_process_info passes back a handle. This is what it looks like: */ 75 76struct handle 77{ 78 struct kinfo_proc **next_proc; /* points to next valid proc pointer */ 79 int remaining; /* number of pointers remaining */ 80}; 81 82/* declarations for load_avg */ 83#include "loadavg.h" 84 85/* define what weighted cpu is. */ 86#define weighted_cpu(pct, pp) ((pp)->ki_swtime == 0 ? 0.0 : \ 87 ((pct) / (1.0 - exp((pp)->ki_swtime * logcpu)))) 88 89/* what we consider to be process size: */ 90#define PROCSIZE(pp) ((pp)->ki_size / 1024) 91 92/* definitions for indices in the nlist array */ 93 94/* 95 * These definitions control the format of the per-process area 96 */ 97 98static char smp_header[] = 99 " PID %-*.*s PRI NICE SIZE RES STATE C TIME WCPU CPU COMMAND"; 100 101#define smp_Proc_format \ 102 "%5d %-*.*s %3d %4d%7s %6s %-6.6s %1x%7s %5.2f%% %5.2f%% %.*s" 103 104static char up_header[] = 105 " PID %-*.*s PRI NICE SIZE RES STATE TIME WCPU CPU COMMAND"; 106 107#define up_Proc_format \ 108 "%5d %-*.*s %3d %4d%7s %6s %-6.6s%.0d%7s %5.2f%% %5.2f%% %.*s" 109 110 111 112/* process state names for the "STATE" column of the display */ 113/* the extra nulls in the string "run" are for adding a slash and 114 the processor number when needed */ 115 116char *state_abbrev[] = 117{ 118 "", "START", "RUN\0\0\0", "SLEEP", "STOP", "ZOMB", "WAIT", "MUTEX" 119}; 120 121 122static kvm_t *kd; 123 124/* values that we stash away in _init and use in later routines */ 125 126static double logcpu; 127 128/* these are retrieved from the kernel in _init */ 129 130static load_avg ccpu; 131 132/* these are used in the get_ functions */ 133 134static int lastpid; 135 136/* these are for calculating cpu state percentages */ 137 138static long cp_time[CPUSTATES]; 139static long cp_old[CPUSTATES]; 140static long cp_diff[CPUSTATES]; 141 142/* these are for detailing the process states */ 143 144int process_states[8]; 145char *procstatenames[] = { 146 "", " starting, ", " running, ", " sleeping, ", " stopped, ", 147 " zombie, ", " waiting, ", " mutex, ", 148 NULL 149}; 150 151/* these are for detailing the cpu states */ 152 153int cpu_states[CPUSTATES]; 154char *cpustatenames[] = { 155 "user", "nice", "system", "interrupt", "idle", NULL 156}; 157 158/* these are for detailing the memory statistics */ 159 160int memory_stats[7]; 161char *memorynames[] = { 162 "K Active, ", "K Inact, ", "K Wired, ", "K Cache, ", "K Buf, ", "K Free", 163 NULL 164}; 165 166int swap_stats[7]; 167char *swapnames[] = { 168/* 0 1 2 3 4 5 */ 169 "K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out", 170 NULL 171}; 172 173 174/* these are for keeping track of the proc array */ 175 176static int nproc; 177static int onproc = -1; 178static int pref_len; 179static struct kinfo_proc *pbase; 180static struct kinfo_proc **pref; 181 182/* these are for getting the memory statistics */ 183 184static int pageshift; /* log base 2 of the pagesize */ 185 186/* define pagetok in terms of pageshift */ 187 188#define pagetok(size) ((size) << pageshift) 189 190/* useful externals */ 191long percentages(); 192 193#ifdef ORDER 194/* sorting orders. first is default */ 195char *ordernames[] = { 196 "cpu", "size", "res", "time", "pri", NULL 197}; 198#endif 199 200int 201machine_init(statics) 202 203struct statics *statics; 204 205{ 206 register int pagesize; 207 size_t modelen; 208 struct passwd *pw; 209 210 modelen = sizeof(smpmode); 211 if ((sysctlbyname("machdep.smp_active", &smpmode, &modelen, NULL, 0) < 0 && 212 sysctlbyname("kern.smp.active", &smpmode, &modelen, NULL, 0) < 0) || 213 modelen != sizeof(smpmode)) 214 smpmode = 0; 215 216 while ((pw = getpwent()) != NULL) { 217 if (strlen(pw->pw_name) > namelength) 218 namelength = strlen(pw->pw_name); 219 } 220 if (namelength < 8) 221 namelength = 8; 222 if (smpmode && namelength > 13) 223 namelength = 13; 224 else if (namelength > 15) 225 namelength = 15; 226 227 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY, "kvm_open")) == NULL) 228 return -1; 229 230 GETSYSCTL("kern.ccpu", ccpu); 231 232 /* this is used in calculating WCPU -- calculate it ahead of time */ 233 logcpu = log(loaddouble(ccpu)); 234 235 pbase = NULL; 236 pref = NULL; 237 nproc = 0; 238 onproc = -1; 239 /* get the page size with "getpagesize" and calculate pageshift from it */ 240 pagesize = getpagesize(); 241 pageshift = 0; 242 while (pagesize > 1) 243 { 244 pageshift++; 245 pagesize >>= 1; 246 } 247 248 /* we only need the amount of log(2)1024 for our conversion */ 249 pageshift -= LOG1024; 250 251 /* fill in the statics information */ 252 statics->procstate_names = procstatenames; 253 statics->cpustate_names = cpustatenames; 254 statics->memory_names = memorynames; 255 statics->swap_names = swapnames; 256#ifdef ORDER 257 statics->order_names = ordernames; 258#endif 259 260 /* all done! */ 261 return(0); 262} 263 264char *format_header(uname_field) 265 266register char *uname_field; 267 268{ 269 static char Header[128]; 270 271 snprintf(Header, sizeof(Header), smpmode ? smp_header : up_header, 272 namelength, namelength, uname_field); 273 274 cmdlengthdelta = strlen(Header) - 7; 275 276 return Header; 277} 278 279static int swappgsin = -1; 280static int swappgsout = -1; 281extern struct timeval timeout; 282 283void 284get_system_info(si) 285 286struct system_info *si; 287 288{ 289 long total; 290 struct loadavg sysload; 291 int mib[2]; 292 struct timeval boottime; 293 size_t bt_size; 294 295 /* get the cp_time array */ 296 GETSYSCTL("kern.cp_time", cp_time); 297 GETSYSCTL("vm.loadavg", sysload); 298 GETSYSCTL("kern.lastpid", lastpid); 299 300 /* convert load averages to doubles */ 301 { 302 register int i; 303 register double *infoloadp; 304 305 infoloadp = si->load_avg; 306 for (i = 0; i < 3; i++) 307 { 308#ifdef notyet 309 *infoloadp++ = ((double) sysload.ldavg[i]) / sysload.fscale; 310#endif 311 *infoloadp++ = loaddouble(sysload.ldavg[i]); 312 } 313 } 314 315 /* convert cp_time counts to percentages */ 316 total = percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff); 317 318 /* sum memory & swap statistics */ 319 { 320 static unsigned int swap_delay = 0; 321 static int swapavail = 0; 322 static int swapfree = 0; 323 static int bufspace = 0; 324 static int nspgsin, nspgsout; 325 326 GETSYSCTL("vfs.bufspace", bufspace); 327 GETSYSCTL("vm.stats.vm.v_active_count", memory_stats[0]); 328 GETSYSCTL("vm.stats.vm.v_inactive_count", memory_stats[1]); 329 GETSYSCTL("vm.stats.vm.v_wire_count", memory_stats[2]); 330 GETSYSCTL("vm.stats.vm.v_cache_count", memory_stats[3]); 331 GETSYSCTL("vm.stats.vm.v_free_count", memory_stats[5]); 332 GETSYSCTL("vm.stats.vm.v_swappgsin", nspgsin); 333 GETSYSCTL("vm.stats.vm.v_swappgsout", nspgsout); 334 /* convert memory stats to Kbytes */ 335 memory_stats[0] = pagetok(memory_stats[0]); 336 memory_stats[1] = pagetok(memory_stats[1]); 337 memory_stats[2] = pagetok(memory_stats[2]); 338 memory_stats[3] = pagetok(memory_stats[3]); 339 memory_stats[4] = bufspace / 1024; 340 memory_stats[5] = pagetok(memory_stats[5]); 341 memory_stats[6] = -1; 342 343 /* first interval */ 344 if (swappgsin < 0) { 345 swap_stats[4] = 0; 346 swap_stats[5] = 0; 347 } 348 349 /* compute differences between old and new swap statistic */ 350 else { 351 swap_stats[4] = pagetok(((nspgsin - swappgsin))); 352 swap_stats[5] = pagetok(((nspgsout - swappgsout))); 353 } 354 355 swappgsin = nspgsin; 356 swappgsout = nspgsout; 357 358 /* call CPU heavy swapmode() only for changes */ 359 if (swap_stats[4] > 0 || swap_stats[5] > 0 || swap_delay == 0) { 360 swap_stats[3] = swapmode(&swapavail, &swapfree); 361 swap_stats[0] = swapavail; 362 swap_stats[1] = swapavail - swapfree; 363 swap_stats[2] = swapfree; 364 } 365 swap_delay = 1; 366 swap_stats[6] = -1; 367 } 368 369 /* set arrays and strings */ 370 si->cpustates = cpu_states; 371 si->memory = memory_stats; 372 si->swap = swap_stats; 373 374 375 if(lastpid > 0) { 376 si->last_pid = lastpid; 377 } else { 378 si->last_pid = -1; 379 } 380 381 /* 382 * Print how long system has been up. 383 * (Found by looking getting "boottime" from the kernel) 384 */ 385 mib[0] = CTL_KERN; 386 mib[1] = KERN_BOOTTIME; 387 bt_size = sizeof(boottime); 388 if (sysctl(mib, 2, &boottime, &bt_size, NULL, 0) != -1 && 389 boottime.tv_sec != 0) { 390 si->boottime = boottime; 391 } else { 392 si->boottime.tv_sec = -1; 393 } 394} 395 396static struct handle handle; 397 398caddr_t get_process_info(si, sel, compare) 399 400struct system_info *si; 401struct process_select *sel; 402int (*compare)(); 403 404{ 405 register int i; 406 register int total_procs; 407 register int active_procs; 408 register struct kinfo_proc **prefp; 409 register struct kinfo_proc *pp; 410 411 /* these are copied out of sel for speed */ 412 int show_idle; 413 int show_self; 414 int show_system; 415 int show_uid; 416 int show_command; 417 418 419 pbase = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc); 420 if (nproc > onproc) 421 pref = (struct kinfo_proc **) realloc(pref, sizeof(struct kinfo_proc *) 422 * (onproc = nproc)); 423 if (pref == NULL || pbase == NULL) { 424 (void) fprintf(stderr, "top: Out of memory.\n"); 425 quit(23); 426 } 427 /* get a pointer to the states summary array */ 428 si->procstates = process_states; 429 430 /* set up flags which define what we are going to select */ 431 show_idle = sel->idle; 432 show_self = sel->self; 433 show_system = sel->system; 434 show_uid = sel->uid != -1; 435 show_command = sel->command != NULL; 436 437 /* count up process states and get pointers to interesting procs */ 438 total_procs = 0; 439 active_procs = 0; 440 memset((char *)process_states, 0, sizeof(process_states)); 441 prefp = pref; 442 for (pp = pbase, i = 0; i < nproc; pp++, i++) 443 { 444 /* 445 * Place pointers to each valid proc structure in pref[]. 446 * Process slots that are actually in use have a non-zero 447 * status field. Processes with P_SYSTEM set are system 448 * processes---these get ignored unless show_sysprocs is set. 449 */ 450 if (pp->ki_stat != 0 && 451 (show_self != pp->ki_pid) && 452 (show_system || ((pp->ki_flag & P_SYSTEM) == 0))) 453 { 454 total_procs++; 455 process_states[(unsigned char) pp->ki_stat]++; 456 if ((pp->ki_stat != SZOMB) && 457 (show_idle || (pp->ki_pctcpu != 0) || 458 (pp->ki_stat == SRUN)) && 459 (!show_uid || pp->ki_ruid == (uid_t)sel->uid)) 460 { 461 *prefp++ = pp; 462 active_procs++; 463 } 464 } 465 } 466 467 /* if requested, sort the "interesting" processes */ 468 if (compare != NULL) 469 { 470 qsort((char *)pref, active_procs, sizeof(struct kinfo_proc *), compare); 471 } 472 473 /* remember active and total counts */ 474 si->p_total = total_procs; 475 si->p_active = pref_len = active_procs; 476 477 /* pass back a handle */ 478 handle.next_proc = pref; 479 handle.remaining = active_procs; 480 return((caddr_t)&handle); 481} 482 483char fmt[128]; /* static area where result is built */ 484 485char *format_next_process(handle, get_userid) 486 487caddr_t handle; 488char *(*get_userid)(); 489 490{ 491 register struct kinfo_proc *pp; 492 register long cputime; 493 register double pct; 494 struct handle *hp; 495 char status[16]; 496 int state; 497 498 /* find and remember the next proc structure */ 499 hp = (struct handle *)handle; 500 pp = *(hp->next_proc++); 501 hp->remaining--; 502 503 /* get the process's command name */ 504 if ((pp->ki_sflag & PS_INMEM) == 0) { 505 /* 506 * Print swapped processes as <pname> 507 */ 508 char *comm = pp->ki_comm; 509#define COMSIZ sizeof(pp->ki_comm) 510 char buf[COMSIZ]; 511 (void) strncpy(buf, comm, COMSIZ); 512 comm[0] = '<'; 513 (void) strncpy(&comm[1], buf, COMSIZ - 2); 514 comm[COMSIZ - 2] = '\0'; 515 (void) strncat(comm, ">", COMSIZ - 1); 516 comm[COMSIZ - 1] = '\0'; 517 } 518 519 /* 520 * Convert the process's runtime from microseconds to seconds. This 521 * time includes the interrupt time although that is not wanted here. 522 * ps(1) is similarly sloppy. 523 */ 524 cputime = (pp->ki_runtime + 500000) / 1000000; 525 526 /* calculate the base for cpu percentages */ 527 pct = pctdouble(pp->ki_pctcpu); 528 529 /* generate "STATE" field */ 530 switch (state = pp->ki_stat) { 531 case SRUN: 532 if (smpmode && pp->ki_oncpu != 0xff) 533 sprintf(status, "CPU%d", pp->ki_oncpu); 534 else 535 strcpy(status, "RUN"); 536 break; 537 case SMTX: 538 if (pp->ki_kiflag & KI_MTXBLOCK) { 539 sprintf(status, "*%.6s", pp->ki_mtxname); 540 break; 541 } 542 /* fall through */ 543 case SSLEEP: 544 if (pp->ki_wmesg != NULL) { 545 sprintf(status, "%.6s", pp->ki_wmesg); 546 break; 547 } 548 /* fall through */ 549 default: 550 551 if (state >= 0 && 552 state < sizeof(state_abbrev) / sizeof(*state_abbrev)) 553 sprintf(status, "%.6s", state_abbrev[(unsigned char) state]); 554 else 555 sprintf(status, "?%5d", state); 556 break; 557 } 558 559 /* format this entry */ 560 sprintf(fmt, 561 smpmode ? smp_Proc_format : up_Proc_format, 562 pp->ki_pid, 563 namelength, namelength, 564 (*get_userid)(pp->ki_ruid), 565 pp->ki_pri.pri_level - PZERO, 566 567 /* 568 * normal time -> nice value -20 - +20 569 * real time 0 - 31 -> nice value -52 - -21 570 * idle time 0 - 31 -> nice value +21 - +52 571 */ 572 (pp->ki_pri.pri_class == PRI_TIMESHARE ? 573 pp->ki_nice - NZERO : 574 (PRI_IS_REALTIME(pp->ki_pri.pri_class) ? 575 (PRIO_MIN - 1 - (PRI_MAX_REALTIME - pp->ki_pri.pri_level)) : 576 (PRIO_MAX + 1 + pp->ki_pri.pri_level - PRI_MIN_IDLE))), 577 format_k2(PROCSIZE(pp)), 578 format_k2(pagetok(pp->ki_rssize)), 579 status, 580 smpmode ? pp->ki_lastcpu : 0, 581 format_time(cputime), 582 100.0 * weighted_cpu(pct, pp), 583 100.0 * pct, 584 screen_width > cmdlengthdelta ? 585 screen_width - cmdlengthdelta : 586 0, 587 printable(pp->ki_comm)); 588 589 /* return the result */ 590 return(fmt); 591} 592 593static void getsysctl (name, ptr, len) 594 595char *name; 596void *ptr; 597size_t len; 598 599{ 600 size_t nlen = len; 601 if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) { 602 fprintf(stderr, "top: sysctl(%s...) failed: %s\n", name, 603 strerror(errno)); 604 quit(23); 605 } 606 if (nlen != len) { 607 fprintf(stderr, "top: sysctl(%s...) expected %d, got %d\n", name, 608 len, nlen); 609 quit(23); 610 } 611} 612 613/* comparison routines for qsort */ 614 615/* 616 * proc_compare - comparison function for "qsort" 617 * Compares the resource consumption of two processes using five 618 * distinct keys. The keys (in descending order of importance) are: 619 * percent cpu, cpu ticks, state, resident set size, total virtual 620 * memory usage. The process states are ordered as follows (from least 621 * to most important): WAIT, zombie, sleep, stop, start, run. The 622 * array declaration below maps a process state index into a number 623 * that reflects this ordering. 624 */ 625 626static unsigned char sorted_state[] = 627{ 628 0, /* not used */ 629 3, /* sleep */ 630 1, /* ABANDONED (WAIT) */ 631 6, /* run */ 632 5, /* start */ 633 2, /* zombie */ 634 4 /* stop */ 635}; 636 637 638#define ORDERKEY_PCTCPU \ 639 if (lresult = (long) p2->ki_pctcpu - (long) p1->ki_pctcpu, \ 640 (result = lresult > 0 ? 1 : lresult < 0 ? -1 : 0) == 0) 641 642#define ORDERKEY_CPTICKS \ 643 if ((result = p2->ki_runtime > p1->ki_runtime ? 1 : \ 644 p2->ki_runtime < p1->ki_runtime ? -1 : 0) == 0) 645 646#define ORDERKEY_STATE \ 647 if ((result = sorted_state[(unsigned char) p2->ki_stat] - \ 648 sorted_state[(unsigned char) p1->ki_stat]) == 0) 649 650#define ORDERKEY_PRIO \ 651 if ((result = p2->ki_pri.pri_level - p1->ki_pri.pri_level) == 0) 652 653#define ORDERKEY_RSSIZE \ 654 if ((result = p2->ki_rssize - p1->ki_rssize) == 0) 655 656#define ORDERKEY_MEM \ 657 if ( (result = PROCSIZE(p2) - PROCSIZE(p1)) == 0 ) 658 659/* compare_cpu - the comparison function for sorting by cpu percentage */ 660 661int 662#ifdef ORDER 663compare_cpu(pp1, pp2) 664#else 665proc_compare(pp1, pp2) 666#endif 667 668struct proc **pp1; 669struct proc **pp2; 670 671{ 672 register struct kinfo_proc *p1; 673 register struct kinfo_proc *p2; 674 register int result; 675 register pctcpu lresult; 676 677 /* remove one level of indirection */ 678 p1 = *(struct kinfo_proc **) pp1; 679 p2 = *(struct kinfo_proc **) pp2; 680 681 ORDERKEY_PCTCPU 682 ORDERKEY_CPTICKS 683 ORDERKEY_STATE 684 ORDERKEY_PRIO 685 ORDERKEY_RSSIZE 686 ORDERKEY_MEM 687 ; 688 689 return(result); 690} 691 692#ifdef ORDER 693/* compare routines */ 694int compare_size(), compare_res(), compare_time(), compare_prio(); 695 696int (*proc_compares[])() = { 697 compare_cpu, 698 compare_size, 699 compare_res, 700 compare_time, 701 compare_prio, 702 NULL 703}; 704 705/* compare_size - the comparison function for sorting by total memory usage */ 706 707int 708compare_size(pp1, pp2) 709 710struct proc **pp1; 711struct proc **pp2; 712 713{ 714 register struct kinfo_proc *p1; 715 register struct kinfo_proc *p2; 716 register int result; 717 register pctcpu lresult; 718 719 /* remove one level of indirection */ 720 p1 = *(struct kinfo_proc **) pp1; 721 p2 = *(struct kinfo_proc **) pp2; 722 723 ORDERKEY_MEM 724 ORDERKEY_RSSIZE 725 ORDERKEY_PCTCPU 726 ORDERKEY_CPTICKS 727 ORDERKEY_STATE 728 ORDERKEY_PRIO 729 ; 730 731 return(result); 732} 733 734/* compare_res - the comparison function for sorting by resident set size */ 735 736int 737compare_res(pp1, pp2) 738 739struct proc **pp1; 740struct proc **pp2; 741 742{ 743 register struct kinfo_proc *p1; 744 register struct kinfo_proc *p2; 745 register int result; 746 register pctcpu lresult; 747 748 /* remove one level of indirection */ 749 p1 = *(struct kinfo_proc **) pp1; 750 p2 = *(struct kinfo_proc **) pp2; 751 752 ORDERKEY_RSSIZE 753 ORDERKEY_MEM 754 ORDERKEY_PCTCPU 755 ORDERKEY_CPTICKS 756 ORDERKEY_STATE 757 ORDERKEY_PRIO 758 ; 759 760 return(result); 761} 762 763/* compare_time - the comparison function for sorting by total cpu time */ 764 765int 766compare_time(pp1, pp2) 767 768struct proc **pp1; 769struct proc **pp2; 770 771{ 772 register struct kinfo_proc *p1; 773 register struct kinfo_proc *p2; 774 register int result; 775 register pctcpu lresult; 776 777 /* remove one level of indirection */ 778 p1 = *(struct kinfo_proc **) pp1; 779 p2 = *(struct kinfo_proc **) pp2; 780 781 ORDERKEY_CPTICKS 782 ORDERKEY_PCTCPU 783 ORDERKEY_STATE 784 ORDERKEY_PRIO 785 ORDERKEY_RSSIZE 786 ORDERKEY_MEM 787 ; 788 789 return(result); 790 } 791 792/* compare_prio - the comparison function for sorting by cpu percentage */ 793 794int 795compare_prio(pp1, pp2) 796 797struct proc **pp1; 798struct proc **pp2; 799 800{ 801 register struct kinfo_proc *p1; 802 register struct kinfo_proc *p2; 803 register int result; 804 register pctcpu lresult; 805 806 /* remove one level of indirection */ 807 p1 = *(struct kinfo_proc **) pp1; 808 p2 = *(struct kinfo_proc **) pp2; 809 810 ORDERKEY_PRIO 811 ORDERKEY_CPTICKS 812 ORDERKEY_PCTCPU 813 ORDERKEY_STATE 814 ORDERKEY_RSSIZE 815 ORDERKEY_MEM 816 ; 817 818 return(result); 819} 820#endif 821 822/* 823 * proc_owner(pid) - returns the uid that owns process "pid", or -1 if 824 * the process does not exist. 825 * It is EXTREMLY IMPORTANT that this function work correctly. 826 * If top runs setuid root (as in SVR4), then this function 827 * is the only thing that stands in the way of a serious 828 * security problem. It validates requests for the "kill" 829 * and "renice" commands. 830 */ 831 832int proc_owner(pid) 833 834int pid; 835 836{ 837 register int cnt; 838 register struct kinfo_proc **prefp; 839 register struct kinfo_proc *pp; 840 841 prefp = pref; 842 cnt = pref_len; 843 while (--cnt >= 0) 844 { 845 pp = *prefp++; 846 if (pp->ki_pid == (pid_t)pid) 847 { 848 return((int)pp->ki_ruid); 849 } 850 } 851 return(-1); 852} 853 854int 855swapmode(retavail, retfree) 856 int *retavail; 857 int *retfree; 858{ 859 int n; 860 int pagesize = getpagesize(); 861 struct kvm_swap swapary[1]; 862 863 *retavail = 0; 864 *retfree = 0; 865 866#define CONVERT(v) ((quad_t)(v) * pagesize / 1024) 867 868 n = kvm_getswapinfo(kd, swapary, 1, 0); 869 if (n < 0 || swapary[0].ksw_total == 0) 870 return(0); 871 872 *retavail = CONVERT(swapary[0].ksw_total); 873 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used); 874 875 n = (int)((double)swapary[0].ksw_used * 100.0 / 876 (double)swapary[0].ksw_total); 877 return(n); 878} 879 880