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