machine.c revision 72951
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 72951 2001-02-23 18:52:37Z rwatson $ 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 *, int)); 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 int modelen; 208 struct passwd *pw; 209 210 modelen = sizeof(smpmode); 211 if ((sysctlbyname("machdep.smp_active", &smpmode, &modelen, NULL, 0) < 0 && 212 sysctlbyname("smp.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 (namelength > 15) 223 namelength = 15; 224 225 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY, "kvm_open")) == NULL) 226 return -1; 227 228 GETSYSCTL("kern.ccpu", ccpu); 229 230 /* this is used in calculating WCPU -- calculate it ahead of time */ 231 logcpu = log(loaddouble(ccpu)); 232 233 pbase = NULL; 234 pref = NULL; 235 nproc = 0; 236 onproc = -1; 237 /* get the page size with "getpagesize" and calculate pageshift from it */ 238 pagesize = getpagesize(); 239 pageshift = 0; 240 while (pagesize > 1) 241 { 242 pageshift++; 243 pagesize >>= 1; 244 } 245 246 /* we only need the amount of log(2)1024 for our conversion */ 247 pageshift -= LOG1024; 248 249 /* fill in the statics information */ 250 statics->procstate_names = procstatenames; 251 statics->cpustate_names = cpustatenames; 252 statics->memory_names = memorynames; 253 statics->swap_names = swapnames; 254#ifdef ORDER 255 statics->order_names = ordernames; 256#endif 257 258 /* all done! */ 259 return(0); 260} 261 262char *format_header(uname_field) 263 264register char *uname_field; 265 266{ 267 static char Header[128]; 268 269 snprintf(Header, sizeof(Header), smpmode ? smp_header : up_header, 270 namelength, namelength, uname_field); 271 272 cmdlengthdelta = strlen(Header) - 7; 273 274 return Header; 275} 276 277static int swappgsin = -1; 278static int swappgsout = -1; 279extern struct timeval timeout; 280 281void 282get_system_info(si) 283 284struct system_info *si; 285 286{ 287 long total; 288 load_avg avenrun[4]; /* 3 values and FSCALE. */ 289 int mib[2]; 290 struct timeval boottime; 291 size_t bt_size; 292 293 /* get the cp_time array */ 294 GETSYSCTL("kern.cp_time", cp_time); 295 GETSYSCTL("vm.loadavg", avenrun); 296 GETSYSCTL("kern.lastpid", lastpid); 297 298 /* convert load averages to doubles */ 299 { 300 register int i; 301 register double *infoloadp; 302 load_avg *avenrunp; 303 304#ifdef notyet 305 struct loadavg sysload; 306 int size; 307 getkerninfo(KINFO_LOADAVG, &sysload, &size, 0); 308#endif 309 310 infoloadp = si->load_avg; 311 avenrunp = avenrun; 312 for (i = 0; i < 3; i++) 313 { 314#ifdef notyet 315 *infoloadp++ = ((double) sysload.ldavg[i]) / sysload.fscale; 316#endif 317 *infoloadp++ = loaddouble(*avenrunp++); 318 } 319 } 320 321 /* convert cp_time counts to percentages */ 322 total = percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff); 323 324 /* sum memory & swap statistics */ 325 { 326 static unsigned int swap_delay = 0; 327 static int swapavail = 0; 328 static int swapfree = 0; 329 static int bufspace = 0; 330 static int nspgsin, nspgsout; 331 332 GETSYSCTL("vfs.bufspace", bufspace); 333 GETSYSCTL("vm.stats.vm.v_active_count", memory_stats[0]); 334 GETSYSCTL("vm.stats.vm.v_inactive_count", memory_stats[1]); 335 GETSYSCTL("vm.stats.vm.v_wire_count", memory_stats[2]); 336 GETSYSCTL("vm.stats.vm.v_cache_count", memory_stats[3]); 337 GETSYSCTL("vm.stats.vm.v_free_count", memory_stats[5]); 338 GETSYSCTL("vm.stats.vm.v_swappgsin", nspgsin); 339 GETSYSCTL("vm.stats.vm.v_swappgsout", nspgsout); 340 /* convert memory stats to Kbytes */ 341 memory_stats[4] = bufspace / 1024; 342 memory_stats[6] = -1; 343 344 /* first interval */ 345 if (swappgsin < 0) { 346 swap_stats[4] = 0; 347 swap_stats[5] = 0; 348 } 349 350 /* compute differences between old and new swap statistic */ 351 else { 352 swap_stats[4] = pagetok(((nspgsin - swappgsin))); 353 swap_stats[5] = pagetok(((nspgsout - swappgsout))); 354 } 355 356 swappgsin = nspgsin; 357 swappgsout = nspgsout; 358 359 /* call CPU heavy swapmode() only for changes */ 360 if (swap_stats[4] > 0 || swap_stats[5] > 0 || swap_delay == 0) { 361 swap_stats[3] = swapmode(&swapavail, &swapfree); 362 swap_stats[0] = swapavail; 363 swap_stats[1] = swapavail - swapfree; 364 swap_stats[2] = swapfree; 365 } 366 swap_delay = 1; 367 swap_stats[6] = -1; 368 } 369 370 /* set arrays and strings */ 371 si->cpustates = cpu_states; 372 si->memory = memory_stats; 373 si->swap = swap_stats; 374 375 376 if(lastpid > 0) { 377 si->last_pid = lastpid; 378 } else { 379 si->last_pid = -1; 380 } 381 382 /* 383 * Print how long system has been up. 384 * (Found by looking getting "boottime" from the kernel) 385 */ 386 mib[0] = CTL_KERN; 387 mib[1] = KERN_BOOTTIME; 388 bt_size = sizeof(boottime); 389 if (sysctl(mib, 2, &boottime, &bt_size, NULL, 0) != -1 && 390 boottime.tv_sec != 0) { 391 si->boottime = boottime; 392 } else { 393 si->boottime.tv_sec = -1; 394 } 395} 396 397static struct handle handle; 398 399caddr_t get_process_info(si, sel, compare) 400 401struct system_info *si; 402struct process_select *sel; 403int (*compare)(); 404 405{ 406 register int i; 407 register int total_procs; 408 register int active_procs; 409 register struct kinfo_proc **prefp; 410 register struct kinfo_proc *pp; 411 412 /* these are copied out of sel for speed */ 413 int show_idle; 414 int show_self; 415 int show_system; 416 int show_uid; 417 int show_command; 418 419 420 pbase = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc); 421 if (nproc > onproc) 422 pref = (struct kinfo_proc **) realloc(pref, sizeof(struct kinfo_proc *) 423 * (onproc = nproc)); 424 if (pref == NULL || pbase == NULL) { 425 (void) fprintf(stderr, "top: Out of memory.\n"); 426 quit(23); 427 } 428 /* get a pointer to the states summary array */ 429 si->procstates = process_states; 430 431 /* set up flags which define what we are going to select */ 432 show_idle = sel->idle; 433 show_self = sel->self; 434 show_system = sel->system; 435 show_uid = sel->uid != -1; 436 show_command = sel->command != NULL; 437 438 /* count up process states and get pointers to interesting procs */ 439 total_procs = 0; 440 active_procs = 0; 441 memset((char *)process_states, 0, sizeof(process_states)); 442 prefp = pref; 443 for (pp = pbase, i = 0; i < nproc; pp++, i++) 444 { 445 /* 446 * Place pointers to each valid proc structure in pref[]. 447 * Process slots that are actually in use have a non-zero 448 * status field. Processes with P_SYSTEM set are system 449 * processes---these get ignored unless show_sysprocs is set. 450 */ 451 if (pp->ki_stat != 0 && 452 (show_self != pp->ki_pid) && 453 (show_system || ((pp->ki_flag & P_SYSTEM) == 0))) 454 { 455 total_procs++; 456 process_states[(unsigned char) pp->ki_stat]++; 457 if ((pp->ki_stat != SZOMB) && 458 (show_idle || (pp->ki_pctcpu != 0) || 459 (pp->ki_stat == SRUN)) && 460 (!show_uid || pp->ki_ruid == (uid_t)sel->uid)) 461 { 462 *prefp++ = pp; 463 active_procs++; 464 } 465 } 466 } 467 468 /* if requested, sort the "interesting" processes */ 469 if (compare != NULL) 470 { 471 qsort((char *)pref, active_procs, sizeof(struct kinfo_proc *), compare); 472 } 473 474 /* remember active and total counts */ 475 si->p_total = total_procs; 476 si->p_active = pref_len = active_procs; 477 478 /* pass back a handle */ 479 handle.next_proc = pref; 480 handle.remaining = active_procs; 481 return((caddr_t)&handle); 482} 483 484char fmt[128]; /* static area where result is built */ 485 486char *format_next_process(handle, get_userid) 487 488caddr_t handle; 489char *(*get_userid)(); 490 491{ 492 register struct kinfo_proc *pp; 493 register long cputime; 494 register double pct; 495 struct handle *hp; 496 char status[16]; 497 int state; 498 499 /* find and remember the next proc structure */ 500 hp = (struct handle *)handle; 501 pp = *(hp->next_proc++); 502 hp->remaining--; 503 504 /* get the process's command name */ 505 if ((pp->ki_sflag & PS_INMEM) == 0) { 506 /* 507 * Print swapped processes as <pname> 508 */ 509 char *comm = pp->ki_comm; 510#define COMSIZ sizeof(pp->ki_comm) 511 char buf[COMSIZ]; 512 (void) strncpy(buf, comm, COMSIZ); 513 comm[0] = '<'; 514 (void) strncpy(&comm[1], buf, COMSIZ - 2); 515 comm[COMSIZ - 2] = '\0'; 516 (void) strncat(comm, ">", COMSIZ - 1); 517 comm[COMSIZ - 1] = '\0'; 518 } 519 520 /* 521 * Convert the process's runtime from microseconds to seconds. This 522 * time includes the interrupt time although that is not wanted here. 523 * ps(1) is similarly sloppy. 524 */ 525 cputime = (pp->ki_runtime + 500000) / 1000000; 526 527 /* calculate the base for cpu percentages */ 528 pct = pctdouble(pp->ki_pctcpu); 529 530 /* generate "STATE" field */ 531 switch (state = pp->ki_stat) { 532 case SRUN: 533 if (smpmode && pp->ki_oncpu != 0xff) 534 sprintf(status, "CPU%d", pp->ki_oncpu); 535 else 536 strcpy(status, "RUN"); 537 break; 538 case SMTX: 539 if (pp->ki_kiflag & KI_MTXBLOCK) { 540 sprintf(status, "*%.6s", pp->ki_mtxname); 541 break; 542 } 543 /* fall through */ 544 case SSLEEP: 545 if (pp->ki_wmesg != NULL) { 546 sprintf(status, "%.6s", pp->ki_wmesg); 547 break; 548 } 549 /* fall through */ 550 default: 551 552 if (state >= 0 && 553 state < sizeof(state_abbrev) / sizeof(*state_abbrev)) 554 sprintf(status, "%.6s", state_abbrev[(unsigned char) state]); 555 else 556 sprintf(status, "?%5d", state); 557 break; 558 } 559 560 /* format this entry */ 561 sprintf(fmt, 562 smpmode ? smp_Proc_format : up_Proc_format, 563 pp->ki_pid, 564 namelength, namelength, 565 (*get_userid)(pp->ki_ruid), 566 pp->ki_pri.pri_level - PZERO, 567 568 /* 569 * normal time -> nice value -20 - +20 570 * real time 0 - 31 -> nice value -52 - -21 571 * idle time 0 - 31 -> nice value +21 - +52 572 */ 573 (pp->ki_pri.pri_class == PRI_TIMESHARE ? 574 pp->ki_nice - NZERO : 575 (PRI_IS_REALTIME(pp->ki_pri.pri_class) ? 576 (PRIO_MIN - 1 - (PRI_MAX_REALTIME - pp->ki_pri.pri_level)) : 577 (PRIO_MAX + 1 + pp->ki_pri.pri_level - PRI_MIN_IDLE))), 578 format_k2(PROCSIZE(pp)), 579 format_k2(pagetok(pp->ki_rssize)), 580 status, 581 smpmode ? pp->ki_lastcpu : 0, 582 format_time(cputime), 583 100.0 * weighted_cpu(pct, pp), 584 100.0 * pct, 585 screen_width > cmdlengthdelta ? 586 screen_width - cmdlengthdelta : 587 0, 588 printable(pp->ki_comm)); 589 590 /* return the result */ 591 return(fmt); 592} 593 594static void getsysctl (name, ptr, len) 595 596char *name; 597void *ptr; 598int len; 599 600{ 601 int nlen = len; 602 if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) { 603 fprintf(stderr, "top: sysctl(%s...) failed: %s\n", name, 604 strerror(errno)); 605 quit(23); 606 } 607 if (nlen != len) { 608 fprintf(stderr, "top: sysctl(%s...) expected %d, got %d\n", name, 609 len, nlen); 610 quit(23); 611 } 612} 613 614/* comparison routines for qsort */ 615 616/* 617 * proc_compare - comparison function for "qsort" 618 * Compares the resource consumption of two processes using five 619 * distinct keys. The keys (in descending order of importance) are: 620 * percent cpu, cpu ticks, state, resident set size, total virtual 621 * memory usage. The process states are ordered as follows (from least 622 * to most important): WAIT, zombie, sleep, stop, start, run. The 623 * array declaration below maps a process state index into a number 624 * that reflects this ordering. 625 */ 626 627static unsigned char sorted_state[] = 628{ 629 0, /* not used */ 630 3, /* sleep */ 631 1, /* ABANDONED (WAIT) */ 632 6, /* run */ 633 5, /* start */ 634 2, /* zombie */ 635 4 /* stop */ 636}; 637 638 639#define ORDERKEY_PCTCPU \ 640 if (lresult = (long) p2->ki_pctcpu - (long) p1->ki_pctcpu, \ 641 (result = lresult > 0 ? 1 : lresult < 0 ? -1 : 0) == 0) 642 643#define ORDERKEY_CPTICKS \ 644 if ((result = p2->ki_runtime > p1->ki_runtime ? 1 : \ 645 p2->ki_runtime < p1->ki_runtime ? -1 : 0) == 0) 646 647#define ORDERKEY_STATE \ 648 if ((result = sorted_state[(unsigned char) p2->ki_stat] - \ 649 sorted_state[(unsigned char) p1->ki_stat]) == 0) 650 651#define ORDERKEY_PRIO \ 652 if ((result = p2->ki_pri.pri_level - p1->ki_pri.pri_level) == 0) 653 654#define ORDERKEY_RSSIZE \ 655 if ((result = p2->ki_rssize - p1->ki_rssize) == 0) 656 657#define ORDERKEY_MEM \ 658 if ( (result = PROCSIZE(p2) - PROCSIZE(p1)) == 0 ) 659 660/* compare_cpu - the comparison function for sorting by cpu percentage */ 661 662int 663#ifdef ORDER 664compare_cpu(pp1, pp2) 665#else 666proc_compare(pp1, pp2) 667#endif 668 669struct proc **pp1; 670struct proc **pp2; 671 672{ 673 register struct kinfo_proc *p1; 674 register struct kinfo_proc *p2; 675 register int result; 676 register pctcpu lresult; 677 678 /* remove one level of indirection */ 679 p1 = *(struct kinfo_proc **) pp1; 680 p2 = *(struct kinfo_proc **) pp2; 681 682 ORDERKEY_PCTCPU 683 ORDERKEY_CPTICKS 684 ORDERKEY_STATE 685 ORDERKEY_PRIO 686 ORDERKEY_RSSIZE 687 ORDERKEY_MEM 688 ; 689 690 return(result); 691} 692 693#ifdef ORDER 694/* compare routines */ 695int compare_size(), compare_res(), compare_time(), compare_prio(); 696 697int (*proc_compares[])() = { 698 compare_cpu, 699 compare_size, 700 compare_res, 701 compare_time, 702 compare_prio, 703 NULL 704}; 705 706/* compare_size - the comparison function for sorting by total memory usage */ 707 708int 709compare_size(pp1, pp2) 710 711struct proc **pp1; 712struct proc **pp2; 713 714{ 715 register struct kinfo_proc *p1; 716 register struct kinfo_proc *p2; 717 register int result; 718 register pctcpu lresult; 719 720 /* remove one level of indirection */ 721 p1 = *(struct kinfo_proc **) pp1; 722 p2 = *(struct kinfo_proc **) pp2; 723 724 ORDERKEY_MEM 725 ORDERKEY_RSSIZE 726 ORDERKEY_PCTCPU 727 ORDERKEY_CPTICKS 728 ORDERKEY_STATE 729 ORDERKEY_PRIO 730 ; 731 732 return(result); 733} 734 735/* compare_res - the comparison function for sorting by resident set size */ 736 737int 738compare_res(pp1, pp2) 739 740struct proc **pp1; 741struct proc **pp2; 742 743{ 744 register struct kinfo_proc *p1; 745 register struct kinfo_proc *p2; 746 register int result; 747 register pctcpu lresult; 748 749 /* remove one level of indirection */ 750 p1 = *(struct kinfo_proc **) pp1; 751 p2 = *(struct kinfo_proc **) pp2; 752 753 ORDERKEY_RSSIZE 754 ORDERKEY_MEM 755 ORDERKEY_PCTCPU 756 ORDERKEY_CPTICKS 757 ORDERKEY_STATE 758 ORDERKEY_PRIO 759 ; 760 761 return(result); 762} 763 764/* compare_time - the comparison function for sorting by total cpu time */ 765 766int 767compare_time(pp1, pp2) 768 769struct proc **pp1; 770struct proc **pp2; 771 772{ 773 register struct kinfo_proc *p1; 774 register struct kinfo_proc *p2; 775 register int result; 776 register pctcpu lresult; 777 778 /* remove one level of indirection */ 779 p1 = *(struct kinfo_proc **) pp1; 780 p2 = *(struct kinfo_proc **) pp2; 781 782 ORDERKEY_CPTICKS 783 ORDERKEY_PCTCPU 784 ORDERKEY_STATE 785 ORDERKEY_PRIO 786 ORDERKEY_RSSIZE 787 ORDERKEY_MEM 788 ; 789 790 return(result); 791 } 792 793/* compare_prio - the comparison function for sorting by cpu percentage */ 794 795int 796compare_prio(pp1, pp2) 797 798struct proc **pp1; 799struct proc **pp2; 800 801{ 802 register struct kinfo_proc *p1; 803 register struct kinfo_proc *p2; 804 register int result; 805 register pctcpu lresult; 806 807 /* remove one level of indirection */ 808 p1 = *(struct kinfo_proc **) pp1; 809 p2 = *(struct kinfo_proc **) pp2; 810 811 ORDERKEY_PRIO 812 ORDERKEY_CPTICKS 813 ORDERKEY_PCTCPU 814 ORDERKEY_STATE 815 ORDERKEY_RSSIZE 816 ORDERKEY_MEM 817 ; 818 819 return(result); 820} 821#endif 822 823/* 824 * proc_owner(pid) - returns the uid that owns process "pid", or -1 if 825 * the process does not exist. 826 * It is EXTREMLY IMPORTANT that this function work correctly. 827 * If top runs setuid root (as in SVR4), then this function 828 * is the only thing that stands in the way of a serious 829 * security problem. It validates requests for the "kill" 830 * and "renice" commands. 831 */ 832 833int proc_owner(pid) 834 835int pid; 836 837{ 838 register int cnt; 839 register struct kinfo_proc **prefp; 840 register struct kinfo_proc *pp; 841 842 prefp = pref; 843 cnt = pref_len; 844 while (--cnt >= 0) 845 { 846 pp = *prefp++; 847 if (pp->ki_pid == (pid_t)pid) 848 { 849 return((int)pp->ki_ruid); 850 } 851 } 852 return(-1); 853} 854 855int 856swapmode(retavail, retfree) 857 int *retavail; 858 int *retfree; 859{ 860 int n; 861 int pagesize = getpagesize(); 862 struct kvm_swap swapary[1]; 863 864 *retavail = 0; 865 *retfree = 0; 866 867#define CONVERT(v) ((quad_t)(v) * pagesize / 1024) 868 869 n = kvm_getswapinfo(kd, swapary, 1, 0); 870 if (n < 0 || swapary[0].ksw_total == 0) 871 return(0); 872 873 *retavail = CONVERT(swapary[0].ksw_total); 874 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used); 875 876 n = (int)((double)swapary[0].ksw_used * 100.0 / 877 (double)swapary[0].ksw_total); 878 return(n); 879} 880 881