1 /* 2 * top - a top users display for Unix 3 * 4 * SYNOPSIS: For FreeBSD-2.x system 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, 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 * 22 * $Id: machine.c,v 1.18 1999/01/09 20:25:02 obrien Exp $ 23 */ 24 25 26 #include <sys/time.h> 27 #include <sys/types.h> 28 #include <sys/signal.h> 29 #include <sys/param.h> 30 31 #include "os.h" 32 #include <stdio.h> 33 #include <nlist.h> 34 #include <math.h> 35 #include <kvm.h> 36 #include <pwd.h> 37 #include <sys/errno.h> 38 #include <sys/sysctl.h> 39 #include <sys/dkstat.h> 40 #include <sys/file.h> 41 #include <sys/time.h> 42 #include <sys/proc.h> 43 #include <sys/user.h> 44 #include <sys/vmmeter.h> 45 #include <sys/resource.h> 46 #include <sys/rtprio.h> 47 48 /* Swap */ 49 #include <stdlib.h> 50 #include <sys/rlist.h> 51 #include <sys/conf.h> 52 53 #include <osreldate.h> /* for changes in kernel structures */ 54 55 #include "top.h" 56 #include "machine.h" 57 58 static int check_nlist __P((struct nlist *)); 59 static int getkval __P((unsigned long, int *, int, char *)); 60 extern char* printable __P((char *)); 61 int swapmode __P((int *retavail, int *retfree)); 62 static int smpmode; 63 static int namelength; 64 static int cmdlength; 65 66 67 /* get_process_info passes back a handle. This is what it looks like: */ 68 69 struct handle 70 { 71 struct kinfo_proc **next_proc; /* points to next valid proc pointer */ 72 int remaining; /* number of pointers remaining */ 73 }; 74 75 /* declarations for load_avg */ 76 #include "loadavg.h" 77 78 #define PP(pp, field) ((pp)->kp_proc . field) 79 #define EP(pp, field) ((pp)->kp_eproc . field) 80 #define VP(pp, field) ((pp)->kp_eproc.e_vm . field) 81 82 /* define what weighted cpu is. */ 83 #define weighted_cpu(pct, pp) (PP((pp), p_swtime) == 0 ? 0.0 : \ 84 ((pct) / (1.0 - exp(PP((pp), p_swtime) * logcpu)))) 85 86 /* what we consider to be process size: */ 87 #define PROCSIZE(pp) (VP((pp), vm_map.size) / 1024) 88 89 /* definitions for indices in the nlist array */ 90 91 static struct nlist nlst[] = { 92 #define X_CCPU 0 93 { "_ccpu" }, 94 #define X_CP_TIME 1 95 { "_cp_time" }, 96 #define X_AVENRUN 2 97 { "_averunnable" }, 98 99 #define X_BUFSPACE 3 100 { "_bufspace" }, /* K in buffer cache */ 101 #define X_CNT 4 102 { "_cnt" }, /* struct vmmeter cnt */ 103 104 /* Last pid */ 105 #define X_LASTPID 5 106 { "_nextpid" }, 107 { 0 } 108 }; 109 110 /* 111 * These definitions control the format of the per-process area 112 */ 113 114 static char smp_header[] = 115 " PID %-*.*s PRI NICE SIZE RES STATE C TIME WCPU CPU COMMAND"; 116 117 #define smp_Proc_format \ 118 "%5d %-*.*s %3d %3d%7s %6s %-6.6s %1x%7s %5.2f%% %5.2f%% %.*s" 119 120 static char up_header[] = 121 " PID %-*.*s PRI NICE SIZE RES STATE TIME WCPU CPU COMMAND"; 122 123 #define up_Proc_format \ 124 "%5d %-*.*s %3d %3d%7s %6s %-6.6s%.0d%7s %5.2f%% %5.2f%% %.*s" 125 126 127 128 /* process state names for the "STATE" column of the display */ 129 /* the extra nulls in the string "run" are for adding a slash and 130 the processor number when needed */ 131 132 char *state_abbrev[] = 133 { 134 "", "START", "RUN\0\0\0", "SLEEP", "STOP", "ZOMB", 135 }; 136 137 138 static kvm_t *kd; 139 140 /* values that we stash away in _init and use in later routines */ 141 142 static double logcpu; 143 144 /* these are retrieved from the kernel in _init */ 145 146 static load_avg ccpu; 147 148 /* these are offsets obtained via nlist and used in the get_ functions */ 149 150 static unsigned long cp_time_offset; 151 static unsigned long avenrun_offset; 152 static unsigned long lastpid_offset; 153 static long lastpid; 154 static unsigned long cnt_offset; 155 static unsigned long bufspace_offset; 156 static long cnt; 157 158 /* these are for calculating cpu state percentages */ 159 160 static long cp_time[CPUSTATES]; 161 static long cp_old[CPUSTATES]; 162 static long cp_diff[CPUSTATES]; 163 164 /* these are for detailing the process states */ 165 166 int process_states[6]; 167 char *procstatenames[] = { 168 "", " starting, ", " running, ", " sleeping, ", " stopped, ", 169 " zombie, ", 170 NULL 171 }; 172 173 /* these are for detailing the cpu states */ 174 175 int cpu_states[CPUSTATES]; 176 char *cpustatenames[] = { 177 "user", "nice", "system", "interrupt", "idle", NULL 178 }; 179 180 /* these are for detailing the memory statistics */ 181 182 int memory_stats[7]; 183 char *memorynames[] = { 184 "K Active, ", "K Inact, ", "K Wired, ", "K Cache, ", "K Buf, ", "K Free", 185 NULL 186 }; 187 188 int swap_stats[7]; 189 char *swapnames[] = { 190 /* 0 1 2 3 4 5 */ 191 "K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out", 192 NULL 193 }; 194 195 196 /* these are for keeping track of the proc array */ 197 198 static int nproc; 199 static int onproc = -1; 200 static int pref_len; 201 static struct kinfo_proc *pbase; 202 static struct kinfo_proc **pref; 203 204 /* these are for getting the memory statistics */ 205 206 static int pageshift; /* log base 2 of the pagesize */ 207 208 /* define pagetok in terms of pageshift */ 209 210 #define pagetok(size) ((size) << pageshift) 211 212 /* useful externals */ 213 long percentages(); 214 215 #ifdef ORDER 216 /* sorting orders. first is default */ 217 char *ordernames[] = { 218 "cpu", "size", "res", "time", "pri", NULL 219 }; 220 #endif 221 222 int 223 machine_init(statics) 224 225 struct statics *statics; 226 227 { 228 register int i = 0; 229 register int pagesize; 230 int modelen; 231 struct passwd *pw; 232 233 modelen = sizeof(smpmode); 234 if ((sysctlbyname("machdep.smp_active", &smpmode, &modelen, NULL, 0) < 0 && 235 sysctlbyname("smp.smp_active", &smpmode, &modelen, NULL, 0) < 0) || 236 modelen != sizeof(smpmode)) 237 smpmode = 0; 238 239 while ((pw = getpwent()) != NULL) { 240 if (strlen(pw->pw_name) > namelength) 241 namelength = strlen(pw->pw_name); 242 } 243 if (namelength < 8) 244 namelength = 8; 245 if (namelength > 16) 246 namelength = 16; 247 248 if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL) 249 return -1; 250 251 252 /* get the list of symbols we want to access in the kernel */ 253 (void) kvm_nlist(kd, nlst); 254 if (nlst[0].n_type == 0) 255 { 256 fprintf(stderr, "top: nlist failed\n"); 257 return(-1); 258 } 259 260 /* make sure they were all found */ 261 if (i > 0 && check_nlist(nlst) > 0) 262 { 263 return(-1); 264 } 265 266 (void) getkval(nlst[X_CCPU].n_value, (int *)(&ccpu), sizeof(ccpu), 267 nlst[X_CCPU].n_name); 268 269 /* stash away certain offsets for later use */ 270 cp_time_offset = nlst[X_CP_TIME].n_value; 271 avenrun_offset = nlst[X_AVENRUN].n_value; 272 lastpid_offset = nlst[X_LASTPID].n_value; 273 cnt_offset = nlst[X_CNT].n_value; 274 bufspace_offset = nlst[X_BUFSPACE].n_value; 275 276 /* this is used in calculating WCPU -- calculate it ahead of time */ 277 logcpu = log(loaddouble(ccpu)); 278 279 pbase = NULL; 280 pref = NULL; 281 nproc = 0; 282 onproc = -1; 283 /* get the page size with "getpagesize" and calculate pageshift from it */ 284 pagesize = getpagesize(); 285 pageshift = 0; 286 while (pagesize > 1) 287 { 288 pageshift++; 289 pagesize >>= 1; 290 } 291 292 /* we only need the amount of log(2)1024 for our conversion */ 293 pageshift -= LOG1024; 294 295 /* fill in the statics information */ 296 statics->procstate_names = procstatenames; 297 statics->cpustate_names = cpustatenames; 298 statics->memory_names = memorynames; 299 statics->swap_names = swapnames; 300 #ifdef ORDER 301 statics->order_names = ordernames; 302 #endif 303 304 /* all done! */ 305 return(0); 306 } 307 308 char *format_header(uname_field) 309 310 register char *uname_field; 311 312 { 313 register char *ptr; 314 static char Header[128]; 315 316 snprintf(Header, sizeof(Header), smpmode ? smp_header : up_header, 317 namelength, namelength, uname_field); 318 319 cmdlength = 80 - strlen(Header) + 6; 320 321 return Header; 322 } 323 324 static int swappgsin = -1; 325 static int swappgsout = -1; 326 extern struct timeval timeout; 327 328 void 329 get_system_info(si) 330 331 struct system_info *si; 332 333 { 334 long total; 335 load_avg avenrun[3]; 336 int mib[2]; 337 struct timeval boottime; 338 size_t bt_size; 339 340 /* get the cp_time array */ 341 (void) getkval(cp_time_offset, (int *)cp_time, sizeof(cp_time), 342 nlst[X_CP_TIME].n_name); 343 (void) getkval(avenrun_offset, (int *)avenrun, sizeof(avenrun), 344 nlst[X_AVENRUN].n_name); 345 346 (void) getkval(lastpid_offset, (int *)(&lastpid), sizeof(lastpid), 347 "!"); 348 349 /* convert load averages to doubles */ 350 { 351 register int i; 352 register double *infoloadp; 353 load_avg *avenrunp; 354 355 #ifdef notyet 356 struct loadavg sysload; 357 int size; 358 getkerninfo(KINFO_LOADAVG, &sysload, &size, 0); 359 #endif 360 361 infoloadp = si->load_avg; 362 avenrunp = avenrun; 363 for (i = 0; i < 3; i++) 364 { 365 #ifdef notyet 366 *infoloadp++ = ((double) sysload.ldavg[i]) / sysload.fscale; 367 #endif 368 *infoloadp++ = loaddouble(*avenrunp++); 369 } 370 } 371 372 /* convert cp_time counts to percentages */ 373 total = percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff); 374 375 /* sum memory & swap statistics */ 376 { 377 struct vmmeter sum; 378 static unsigned int swap_delay = 0; 379 static int swapavail = 0; 380 static int swapfree = 0; 381 static int bufspace = 0; 382 383 (void) getkval(cnt_offset, (int *)(&sum), sizeof(sum), 384 "_cnt"); 385 (void) getkval(bufspace_offset, (int *)(&bufspace), sizeof(bufspace), 386 "_bufspace"); 387 388 /* convert memory stats to Kbytes */ 389 memory_stats[0] = pagetok(sum.v_active_count); 390 memory_stats[1] = pagetok(sum.v_inactive_count); 391 memory_stats[2] = pagetok(sum.v_wire_count); 392 memory_stats[3] = pagetok(sum.v_cache_count); 393 memory_stats[4] = bufspace / 1024; 394 memory_stats[5] = pagetok(sum.v_free_count); 395 memory_stats[6] = -1; 396 397 /* first interval */ 398 if (swappgsin < 0) { 399 swap_stats[4] = 0; 400 swap_stats[5] = 0; 401 } 402 403 /* compute differences between old and new swap statistic */ 404 else { 405 swap_stats[4] = pagetok(((sum.v_swappgsin - swappgsin))); 406 swap_stats[5] = pagetok(((sum.v_swappgsout - swappgsout))); 407 } 408 409 swappgsin = sum.v_swappgsin; 410 swappgsout = sum.v_swappgsout; 411 412 /* call CPU heavy swapmode() only for changes */ 413 if (swap_stats[4] > 0 || swap_stats[5] > 0 || swap_delay == 0) { 414 swap_stats[3] = swapmode(&swapavail, &swapfree); 415 swap_stats[0] = swapavail; 416 swap_stats[1] = swapavail - swapfree; 417 swap_stats[2] = swapfree; 418 } 419 swap_delay = 1; 420 swap_stats[6] = -1; 421 } 422 423 /* set arrays and strings */ 424 si->cpustates = cpu_states; 425 si->memory = memory_stats; 426 si->swap = swap_stats; 427 428 429 if(lastpid > 0) { 430 si->last_pid = lastpid; 431 } else { 432 si->last_pid = -1; 433 } 434 435 /* 436 * Print how long system has been up. 437 * (Found by looking getting "boottime" from the kernel) 438 */ 439 mib[0] = CTL_KERN; 440 mib[1] = KERN_BOOTTIME; 441 bt_size = sizeof(boottime); 442 if (sysctl(mib, 2, &boottime, &bt_size, NULL, 0) != -1 && 443 boottime.tv_sec != 0) { 444 si->boottime = boottime; 445 } else { 446 si->boottime.tv_sec = -1; 447 } 448 } 449 450 static struct handle handle; 451 452 caddr_t get_process_info(si, sel, compare) 453 454 struct system_info *si; 455 struct process_select *sel; 456 int (*compare)(); 457 458 { 459 register int i; 460 register int total_procs; 461 register int active_procs; 462 register struct kinfo_proc **prefp; 463 register struct kinfo_proc *pp; 464 465 /* these are copied out of sel for speed */ 466 int show_idle; 467 int show_self; 468 int show_system; 469 int show_uid; 470 int show_command; 471 472 473 pbase = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc); 474 if (nproc > onproc) 475 pref = (struct kinfo_proc **) realloc(pref, sizeof(struct kinfo_proc *) 476 * (onproc = nproc)); 477 if (pref == NULL || pbase == NULL) { 478 (void) fprintf(stderr, "top: Out of memory.\n"); 479 quit(23); 480 } 481 /* get a pointer to the states summary array */ 482 si->procstates = process_states; 483 484 /* set up flags which define what we are going to select */ 485 show_idle = sel->idle; 486 show_self = sel->self; 487 show_system = sel->system; 488 show_uid = sel->uid != -1; 489 show_command = sel->command != NULL; 490 491 /* count up process states and get pointers to interesting procs */ 492 total_procs = 0; 493 active_procs = 0; 494 memset((char *)process_states, 0, sizeof(process_states)); 495 prefp = pref; 496 for (pp = pbase, i = 0; i < nproc; pp++, i++) 497 { 498 /* 499 * Place pointers to each valid proc structure in pref[]. 500 * Process slots that are actually in use have a non-zero 501 * status field. Processes with P_SYSTEM set are system 502 * processes---these get ignored unless show_sysprocs is set. 503 */ 504 if (PP(pp, p_stat) != 0 && 505 (show_self != PP(pp, p_pid)) && 506 (show_system || ((PP(pp, p_flag) & P_SYSTEM) == 0))) 507 { 508 total_procs++; 509 process_states[(unsigned char) PP(pp, p_stat)]++; 510 if ((PP(pp, p_stat) != SZOMB) && 511 (show_idle || (PP(pp, p_pctcpu) != 0) || 512 (PP(pp, p_stat) == SRUN)) && 513 (!show_uid || EP(pp, e_pcred.p_ruid) == (uid_t)sel->uid)) 514 { 515 *prefp++ = pp; 516 active_procs++; 517 } 518 } 519 } 520 521 /* if requested, sort the "interesting" processes */ 522 if (compare != NULL) 523 { 524 qsort((char *)pref, active_procs, sizeof(struct kinfo_proc *), compare); 525 } 526 527 /* remember active and total counts */ 528 si->p_total = total_procs; 529 si->p_active = pref_len = active_procs; 530 531 /* pass back a handle */ 532 handle.next_proc = pref; 533 handle.remaining = active_procs; 534 return((caddr_t)&handle); 535 } 536 537 char fmt[128]; /* static area where result is built */ 538 539 char *format_next_process(handle, get_userid) 540 541 caddr_t handle; 542 char *(*get_userid)(); 543 544 { 545 register struct kinfo_proc *pp; 546 register long cputime; 547 register double pct; 548 struct handle *hp; 549 char status[16]; 550 551 /* find and remember the next proc structure */ 552 hp = (struct handle *)handle; 553 pp = *(hp->next_proc++); 554 hp->remaining--; 555 556 557 /* get the process's user struct and set cputime */ 558 if ((PP(pp, p_flag) & P_INMEM) == 0) { 559 /* 560 * Print swapped processes as <pname> 561 */ 562 char *comm = PP(pp, p_comm); 563 #define COMSIZ sizeof(PP(pp, p_comm)) 564 char buf[COMSIZ]; 565 (void) strncpy(buf, comm, COMSIZ); 566 comm[0] = '<'; 567 (void) strncpy(&comm[1], buf, COMSIZ - 2); 568 comm[COMSIZ - 2] = '\0'; 569 (void) strncat(comm, ">", COMSIZ - 1); 570 comm[COMSIZ - 1] = '\0'; 571 } 572 573 #if 0 574 /* This does not produce the correct results */ 575 cputime = PP(pp, p_uticks) + PP(pp, p_sticks) + PP(pp, p_iticks); 576 #endif 577 /* This does not count interrupts */ 578 cputime = (PP(pp, p_runtime) / 1000 + 500) / 1000; 579 580 /* calculate the base for cpu percentages */ 581 pct = pctdouble(PP(pp, p_pctcpu)); 582 583 /* generate "STATE" field */ 584 switch (PP(pp, p_stat)) { 585 case SRUN: 586 if (smpmode && PP(pp, p_oncpu) >= 0) 587 sprintf(status, "CPU%d", PP(pp, p_oncpu)); 588 else 589 strcpy(status, "RUN"); 590 break; 591 case SSLEEP: 592 if (PP(pp, p_wmesg) != NULL) { 593 sprintf(status, "%.6s", EP(pp, e_wmesg)); 594 break; 595 } 596 /* fall through */ 597 default: 598 sprintf(status, "%.6s", state_abbrev[(unsigned char) PP(pp, p_stat)]); 599 break; 600 } 601 602 /* format this entry */ 603 sprintf(fmt, 604 smpmode ? smp_Proc_format : up_Proc_format, 605 PP(pp, p_pid), 606 namelength, namelength, 607 (*get_userid)(EP(pp, e_pcred.p_ruid)), 608 PP(pp, p_priority) - PZERO, 609 610 /* 611 * normal time -> nice value -20 - +20 612 * real time 0 - 31 -> nice value -52 - -21 613 * idle time 0 - 31 -> nice value +21 - +52 614 */ 615 (PP(pp, p_rtprio.type) == RTP_PRIO_NORMAL ? 616 PP(pp, p_nice) - NZERO : 617 (PP(pp, p_rtprio.type) == RTP_PRIO_REALTIME ? 618 (PRIO_MIN - 1 - RTP_PRIO_MAX + PP(pp, p_rtprio.prio)) : 619 (PRIO_MAX + 1 + PP(pp, p_rtprio.prio)))), 620 format_k2(PROCSIZE(pp)), 621 format_k2(pagetok(VP(pp, vm_rssize))), 622 status, 623 smpmode ? PP(pp, p_lastcpu) : 0, 624 format_time(cputime), 625 100.0 * weighted_cpu(pct, pp), 626 100.0 * pct, 627 cmdlength, 628 printable(PP(pp, p_comm))); 629 630 /* return the result */ 631 return(fmt); 632 } 633 634 635 /* 636 * check_nlist(nlst) - checks the nlist to see if any symbols were not 637 * found. For every symbol that was not found, a one-line 638 * message is printed to stderr. The routine returns the 639 * number of symbols NOT found. 640 */ 641 642 static int check_nlist(nlst) 643 644 register struct nlist *nlst; 645 646 { 647 register int i; 648 649 /* check to see if we got ALL the symbols we requested */ 650 /* this will write one line to stderr for every symbol not found */ 651 652 i = 0; 653 while (nlst->n_name != NULL) 654 { 655 if (nlst->n_type == 0) 656 { 657 /* this one wasn't found */ 658 (void) fprintf(stderr, "kernel: no symbol named `%s'\n", 659 nlst->n_name); 660 i = 1; 661 } 662 nlst++; 663 } 664 665 return(i); 666 } 667 668 669 /* 670 * getkval(offset, ptr, size, refstr) - get a value out of the kernel. 671 * "offset" is the byte offset into the kernel for the desired value, 672 * "ptr" points to a buffer into which the value is retrieved, 673 * "size" is the size of the buffer (and the object to retrieve), 674 * "refstr" is a reference string used when printing error meessages, 675 * if "refstr" starts with a '!', then a failure on read will not 676 * be fatal (this may seem like a silly way to do things, but I 677 * really didn't want the overhead of another argument). 678 * 679 */ 680 681 static int getkval(offset, ptr, size, refstr) 682 683 unsigned long offset; 684 int *ptr; 685 int size; 686 char *refstr; 687 688 { 689 if (kvm_read(kd, offset, (char *) ptr, size) != size) 690 { 691 if (*refstr == '!') 692 { 693 return(0); 694 } 695 else 696 { 697 fprintf(stderr, "top: kvm_read for %s: %s\n", 698 refstr, strerror(errno)); 699 quit(23); 700 } 701 } 702 return(1); 703 } 704 705 /* comparison routines for qsort */ 706 707 /* 708 * proc_compare - comparison function for "qsort" 709 * Compares the resource consumption of two processes using five 710 * distinct keys. The keys (in descending order of importance) are: 711 * percent cpu, cpu ticks, state, resident set size, total virtual 712 * memory usage. The process states are ordered as follows (from least 713 * to most important): WAIT, zombie, sleep, stop, start, run. The 714 * array declaration below maps a process state index into a number 715 * that reflects this ordering. 716 */ 717 718 static unsigned char sorted_state[] = 719 { 720 0, /* not used */ 721 3, /* sleep */ 722 1, /* ABANDONED (WAIT) */ 723 6, /* run */ 724 5, /* start */ 725 2, /* zombie */ 726 4 /* stop */ 727 }; 728 729 730 #define ORDERKEY_PCTCPU \ 731 if (lresult = (long) PP(p2, p_pctcpu) - (long) PP(p1, p_pctcpu), \ 732 (result = lresult > 0 ? 1 : lresult < 0 ? -1 : 0) == 0) 733 734 #define ORDERKEY_CPTICKS \ 735 if ((result = PP(p2, p_runtime) - PP(p1, p_runtime)) == 0) 736 737 #define ORDERKEY_STATE \ 738 if ((result = sorted_state[(unsigned char) PP(p2, p_stat)] - \ 739 sorted_state[(unsigned char) PP(p1, p_stat)]) == 0) 740 741 #define ORDERKEY_PRIO \ 742 if ((result = PP(p2, p_priority) - PP(p1, p_priority)) == 0) 743 744 #define ORDERKEY_RSSIZE \ 745 if ((result = VP(p2, vm_rssize) - VP(p1, vm_rssize)) == 0) 746 747 #define ORDERKEY_MEM \ 748 if ( (result = PROCSIZE(p2) - PROCSIZE(p1)) == 0 ) 749 750 /* compare_cpu - the comparison function for sorting by cpu percentage */ 751 752 int 753 #ifdef ORDER 754 compare_cpu(pp1, pp2) 755 #else 756 proc_compare(pp1, pp2) 757 #endif 758 759 struct proc **pp1; 760 struct proc **pp2; 761 762 { 763 register struct kinfo_proc *p1; 764 register struct kinfo_proc *p2; 765 register int result; 766 register pctcpu lresult; 767 768 /* remove one level of indirection */ 769 p1 = *(struct kinfo_proc **) pp1; 770 p2 = *(struct kinfo_proc **) pp2; 771 772 ORDERKEY_PCTCPU 773 ORDERKEY_CPTICKS 774 ORDERKEY_STATE 775 ORDERKEY_PRIO 776 ORDERKEY_RSSIZE 777 ORDERKEY_MEM 778 ; 779 780 return(result); 781 } 782 783 #ifdef ORDER 784 /* compare routines */ 785 int compare_size(), compare_res(), compare_time(), compare_prio(); 786 787 int (*proc_compares[])() = { 788 compare_cpu, 789 compare_size, 790 compare_res, 791 compare_time, 792 compare_prio, 793 NULL 794 }; 795 796 /* compare_size - the comparison function for sorting by total memory usage */ 797 798 int 799 compare_size(pp1, pp2) 800 801 struct proc **pp1; 802 struct proc **pp2; 803 804 { 805 register struct kinfo_proc *p1; 806 register struct kinfo_proc *p2; 807 register int result; 808 register pctcpu lresult; 809 810 /* remove one level of indirection */ 811 p1 = *(struct kinfo_proc **) pp1; 812 p2 = *(struct kinfo_proc **) pp2; 813 814 ORDERKEY_MEM 815 ORDERKEY_RSSIZE 816 ORDERKEY_PCTCPU 817 ORDERKEY_CPTICKS 818 ORDERKEY_STATE 819 ORDERKEY_PRIO 820 ; 821 822 return(result); 823 } 824 825 /* compare_res - the comparison function for sorting by resident set size */ 826 827 int 828 compare_res(pp1, pp2) 829 830 struct proc **pp1; 831 struct proc **pp2; 832 833 { 834 register struct kinfo_proc *p1; 835 register struct kinfo_proc *p2; 836 register int result; 837 register pctcpu lresult; 838 839 /* remove one level of indirection */ 840 p1 = *(struct kinfo_proc **) pp1; 841 p2 = *(struct kinfo_proc **) pp2; 842 843 ORDERKEY_RSSIZE 844 ORDERKEY_MEM 845 ORDERKEY_PCTCPU 846 ORDERKEY_CPTICKS 847 ORDERKEY_STATE 848 ORDERKEY_PRIO 849 ; 850 851 return(result); 852 } 853 854 /* compare_time - the comparison function for sorting by total cpu time */ 855 856 int 857 compare_time(pp1, pp2) 858 859 struct proc **pp1; 860 struct proc **pp2; 861 862 { 863 register struct kinfo_proc *p1; 864 register struct kinfo_proc *p2; 865 register int result; 866 register pctcpu lresult; 867 868 /* remove one level of indirection */ 869 p1 = *(struct kinfo_proc **) pp1; 870 p2 = *(struct kinfo_proc **) pp2; 871 872 ORDERKEY_CPTICKS 873 ORDERKEY_PCTCPU 874 ORDERKEY_STATE 875 ORDERKEY_PRIO 876 ORDERKEY_RSSIZE 877 ORDERKEY_MEM 878 ; 879 880 return(result); 881 } 882 883 /* compare_prio - the comparison function for sorting by cpu percentage */ 884 885 int 886 compare_prio(pp1, pp2) 887 888 struct proc **pp1; 889 struct proc **pp2; 890 891 { 892 register struct kinfo_proc *p1; 893 register struct kinfo_proc *p2; 894 register int result; 895 register pctcpu lresult; 896 897 /* remove one level of indirection */ 898 p1 = *(struct kinfo_proc **) pp1; 899 p2 = *(struct kinfo_proc **) pp2; 900 901 ORDERKEY_PRIO 902 ORDERKEY_CPTICKS 903 ORDERKEY_PCTCPU 904 ORDERKEY_STATE 905 ORDERKEY_RSSIZE 906 ORDERKEY_MEM 907 ; 908 909 return(result); 910 } 911 #endif 912 913 /* 914 * proc_owner(pid) - returns the uid that owns process "pid", or -1 if 915 * the process does not exist. 916 * It is EXTREMLY IMPORTANT that this function work correctly. 917 * If top runs setuid root (as in SVR4), then this function 918 * is the only thing that stands in the way of a serious 919 * security problem. It validates requests for the "kill" 920 * and "renice" commands. 921 */ 922 923 int proc_owner(pid) 924 925 int pid; 926 927 { 928 register int cnt; 929 register struct kinfo_proc **prefp; 930 register struct kinfo_proc *pp; 931 932 prefp = pref; 933 cnt = pref_len; 934 while (--cnt >= 0) 935 { 936 pp = *prefp++; 937 if (PP(pp, p_pid) == (pid_t)pid) 938 { 939 return((int)EP(pp, e_pcred.p_ruid)); 940 } 941 } 942 return(-1); 943 } 944 945 946 /* 947 * swapmode is based on a program called swapinfo written 948 * by Kevin Lahey <kml@rokkaku.atl.ga.us>. 949 */ 950 951 #define SVAR(var) __STRING(var) /* to force expansion */ 952 #define KGET(idx, var) \ 953 KGET1(idx, &var, sizeof(var), SVAR(var)) 954 #define KGET1(idx, p, s, msg) \ 955 KGET2(nlst[idx].n_value, p, s, msg) 956 #define KGET2(addr, p, s, msg) \ 957 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \ 958 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \ 959 return (0); \ 960 } 961 #define KGETRET(addr, p, s, msg) \ 962 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \ 963 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \ 964 return (0); \ 965 } 966 967 968 int 969 swapmode(retavail, retfree) 970 int *retavail; 971 int *retfree; 972 { 973 int n; 974 int pagesize = getpagesize(); 975 struct kvm_swap swapary[1]; 976 977 *retavail = 0; 978 *retfree = 0; 979 980 #define CONVERT(v) ((quad_t)(v) * pagesize / 1024) 981 982 n = kvm_getswapinfo(kd, swapary, 1, 0); 983 if (n < 0) 984 return(0); 985 986 *retavail = CONVERT(swapary[0].ksw_total); 987 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used); 988 989 n = (int)((double)swapary[0].ksw_used * 100.0 / 990 (double)swapary[0].ksw_total); 991 return(n); 992 } 993 994