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