machine.c revision 224205
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 224205 2011-07-18 21:15:47Z jhb $ 24 */ 25 26#include <sys/param.h> 27#include <sys/errno.h> 28#include <sys/file.h> 29#include <sys/proc.h> 30#include <sys/resource.h> 31#include <sys/rtprio.h> 32#include <sys/signal.h> 33#include <sys/sysctl.h> 34#include <sys/time.h> 35#include <sys/user.h> 36#include <sys/vmmeter.h> 37 38#include <err.h> 39#include <kvm.h> 40#include <math.h> 41#include <nlist.h> 42#include <paths.h> 43#include <pwd.h> 44#include <stdio.h> 45#include <stdlib.h> 46#include <string.h> 47#include <strings.h> 48#include <unistd.h> 49#include <vis.h> 50 51#include "top.h" 52#include "machine.h" 53#include "screen.h" 54#include "utils.h" 55#include "layout.h" 56 57#define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var)) 58#define SMPUNAMELEN 13 59#define UPUNAMELEN 15 60 61extern struct process_select ps; 62extern char* printable(char *); 63static int smpmode; 64enum displaymodes displaymode; 65#ifdef TOP_USERNAME_LEN 66static int namelength = TOP_USERNAME_LEN; 67#else 68static int namelength = 8; 69#endif 70static int cmdlengthdelta; 71 72/* Prototypes for top internals */ 73void quit(int); 74 75/* get_process_info passes back a handle. This is what it looks like: */ 76 77struct handle { 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#define RU(pp) (&(pp)->ki_rusage) 93#define RUTOT(pp) \ 94 (RU(pp)->ru_inblock + RU(pp)->ru_oublock + RU(pp)->ru_majflt) 95 96 97/* definitions for indices in the nlist array */ 98 99/* 100 * These definitions control the format of the per-process area 101 */ 102 103static char io_header[] = 104 " PID%s %-*.*s VCSW IVCSW READ WRITE FAULT TOTAL PERCENT COMMAND"; 105 106#define io_Proc_format \ 107 "%5d%s %-*.*s %6ld %6ld %6ld %6ld %6ld %6ld %6.2f%% %.*s" 108 109static char smp_header_thr[] = 110 " PID%s %-*.*s THR PRI NICE SIZE RES STATE C TIME %6s COMMAND"; 111static char smp_header[] = 112 " PID%s %-*.*s " "PRI NICE SIZE RES STATE C TIME %6s COMMAND"; 113 114#define smp_Proc_format \ 115 "%5d%s %-*.*s %s%3d %4s%7s %6s %-6.6s %2d%7s %5.2f%% %.*s" 116 117static char up_header_thr[] = 118 " PID%s %-*.*s THR PRI NICE SIZE RES STATE TIME %6s COMMAND"; 119static char up_header[] = 120 " PID%s %-*.*s " "PRI NICE SIZE RES STATE TIME %6s COMMAND"; 121 122#define up_Proc_format \ 123 "%5d%s %-*.*s %s%3d %4s%7s %6s %-6.6s%.0d%7s %5.2f%% %.*s" 124 125 126/* process state names for the "STATE" column of the display */ 127/* the extra nulls in the string "run" are for adding a slash and 128 the processor number when needed */ 129 130char *state_abbrev[] = { 131 "", "START", "RUN\0\0\0", "SLEEP", "STOP", "ZOMB", "WAIT", "LOCK" 132}; 133 134 135static kvm_t *kd; 136 137/* values that we stash away in _init and use in later routines */ 138 139static double logcpu; 140 141/* these are retrieved from the kernel in _init */ 142 143static load_avg ccpu; 144 145/* these are used in the get_ functions */ 146 147static int lastpid; 148 149/* these are for calculating cpu state percentages */ 150 151static long cp_time[CPUSTATES]; 152static long cp_old[CPUSTATES]; 153static long cp_diff[CPUSTATES]; 154 155/* these are for detailing the process states */ 156 157int process_states[8]; 158char *procstatenames[] = { 159 "", " starting, ", " running, ", " sleeping, ", " stopped, ", 160 " zombie, ", " waiting, ", " lock, ", 161 NULL 162}; 163 164/* these are for detailing the cpu states */ 165 166int cpu_states[CPUSTATES]; 167char *cpustatenames[] = { 168 "user", "nice", "system", "interrupt", "idle", NULL 169}; 170 171/* these are for detailing the memory statistics */ 172 173int memory_stats[7]; 174char *memorynames[] = { 175 "K Active, ", "K Inact, ", "K Wired, ", "K Cache, ", "K Buf, ", 176 "K Free", NULL 177}; 178 179int swap_stats[7]; 180char *swapnames[] = { 181 "K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out", 182 NULL 183}; 184 185 186/* these are for keeping track of the proc array */ 187 188static int nproc; 189static int onproc = -1; 190static int pref_len; 191static struct kinfo_proc *pbase; 192static struct kinfo_proc **pref; 193static struct kinfo_proc *previous_procs; 194static struct kinfo_proc **previous_pref; 195static int previous_proc_count = 0; 196static int previous_proc_count_max = 0; 197 198/* total number of io operations */ 199static long total_inblock; 200static long total_oublock; 201static long total_majflt; 202 203/* these are for getting the memory statistics */ 204 205static int pageshift; /* log base 2 of the pagesize */ 206 207/* define pagetok in terms of pageshift */ 208 209#define pagetok(size) ((size) << pageshift) 210 211/* useful externals */ 212long percentages(); 213 214#ifdef ORDER 215/* 216 * Sorting orders. The first element is the default. 217 */ 218char *ordernames[] = { 219 "cpu", "size", "res", "time", "pri", "threads", 220 "total", "read", "write", "fault", "vcsw", "ivcsw", 221 "jid", NULL 222}; 223#endif 224 225/* Per-cpu time states */ 226static int maxcpu; 227static int maxid; 228static int ncpus; 229static u_long cpumask; 230static long *times; 231static long *pcpu_cp_time; 232static long *pcpu_cp_old; 233static long *pcpu_cp_diff; 234static int *pcpu_cpu_states; 235 236static int compare_jid(const void *a, const void *b); 237static int compare_pid(const void *a, const void *b); 238static int compare_tid(const void *a, const void *b); 239static const char *format_nice(const struct kinfo_proc *pp); 240static void getsysctl(const char *name, void *ptr, size_t len); 241static int swapmode(int *retavail, int *retfree); 242 243void 244toggle_pcpustats(void) 245{ 246 247 if (ncpus == 1) 248 return; 249 250 /* Adjust display based on ncpus */ 251 if (pcpu_stats) { 252 y_mem += ncpus - 1; /* 3 */ 253 y_swap += ncpus - 1; /* 4 */ 254 y_idlecursor += ncpus - 1; /* 5 */ 255 y_message += ncpus - 1; /* 5 */ 256 y_header += ncpus - 1; /* 6 */ 257 y_procs += ncpus - 1; /* 7 */ 258 Header_lines += ncpus - 1; /* 7 */ 259 } else { 260 y_mem = 3; 261 y_swap = 4; 262 y_idlecursor = 5; 263 y_message = 5; 264 y_header = 6; 265 y_procs = 7; 266 Header_lines = 7; 267 } 268} 269 270int 271machine_init(struct statics *statics, char do_unames) 272{ 273 int i, j, empty, pagesize; 274 size_t size; 275 struct passwd *pw; 276 277 size = sizeof(smpmode); 278 if ((sysctlbyname("machdep.smp_active", &smpmode, &size, 279 NULL, 0) != 0 && 280 sysctlbyname("kern.smp.active", &smpmode, &size, 281 NULL, 0) != 0) || 282 size != sizeof(smpmode)) 283 smpmode = 0; 284 285 if (do_unames) { 286 while ((pw = getpwent()) != NULL) { 287 if (strlen(pw->pw_name) > namelength) 288 namelength = strlen(pw->pw_name); 289 } 290 } 291 if (smpmode && namelength > SMPUNAMELEN) 292 namelength = SMPUNAMELEN; 293 else if (namelength > UPUNAMELEN) 294 namelength = UPUNAMELEN; 295 296 kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open"); 297 if (kd == NULL) 298 return (-1); 299 300 GETSYSCTL("kern.ccpu", ccpu); 301 302 /* this is used in calculating WCPU -- calculate it ahead of time */ 303 logcpu = log(loaddouble(ccpu)); 304 305 pbase = NULL; 306 pref = NULL; 307 nproc = 0; 308 onproc = -1; 309 310 /* get the page size and calculate pageshift from it */ 311 pagesize = getpagesize(); 312 pageshift = 0; 313 while (pagesize > 1) { 314 pageshift++; 315 pagesize >>= 1; 316 } 317 318 /* we only need the amount of log(2)1024 for our conversion */ 319 pageshift -= LOG1024; 320 321 /* fill in the statics information */ 322 statics->procstate_names = procstatenames; 323 statics->cpustate_names = cpustatenames; 324 statics->memory_names = memorynames; 325 statics->swap_names = swapnames; 326#ifdef ORDER 327 statics->order_names = ordernames; 328#endif 329 330 /* Allocate state for per-CPU stats. */ 331 cpumask = 0; 332 ncpus = 0; 333 GETSYSCTL("kern.smp.maxcpus", maxcpu); 334 size = sizeof(long) * maxcpu * CPUSTATES; 335 times = malloc(size); 336 if (times == NULL) 337 err(1, "malloc %zd bytes", size); 338 if (sysctlbyname("kern.cp_times", times, &size, NULL, 0) == -1) 339 err(1, "sysctlbyname kern.cp_times"); 340 pcpu_cp_time = calloc(1, size); 341 maxid = (size / CPUSTATES / sizeof(long)) - 1; 342 for (i = 0; i <= maxid; i++) { 343 empty = 1; 344 for (j = 0; empty && j < CPUSTATES; j++) { 345 if (times[i * CPUSTATES + j] != 0) 346 empty = 0; 347 } 348 if (!empty) { 349 cpumask |= (1ul << i); 350 ncpus++; 351 } 352 } 353 size = sizeof(long) * ncpus * CPUSTATES; 354 pcpu_cp_old = calloc(1, size); 355 pcpu_cp_diff = calloc(1, size); 356 pcpu_cpu_states = calloc(1, size); 357 statics->ncpus = ncpus; 358 359 if (pcpu_stats) 360 toggle_pcpustats(); 361 362 /* all done! */ 363 return (0); 364} 365 366char * 367format_header(char *uname_field) 368{ 369 static char Header[128]; 370 const char *prehead; 371 372 switch (displaymode) { 373 case DISP_CPU: 374 /* 375 * The logic of picking the right header format seems reverse 376 * here because we only want to display a THR column when 377 * "thread mode" is off (and threads are not listed as 378 * separate lines). 379 */ 380 prehead = smpmode ? 381 (ps.thread ? smp_header : smp_header_thr) : 382 (ps.thread ? up_header : up_header_thr); 383 snprintf(Header, sizeof(Header), prehead, 384 ps.jail ? " JID" : "", 385 namelength, namelength, uname_field, 386 ps.wcpu ? "WCPU" : "CPU"); 387 break; 388 case DISP_IO: 389 prehead = io_header; 390 snprintf(Header, sizeof(Header), prehead, 391 ps.jail ? " JID" : "", 392 namelength, namelength, uname_field); 393 break; 394 } 395 cmdlengthdelta = strlen(Header) - 7; 396 return (Header); 397} 398 399static int swappgsin = -1; 400static int swappgsout = -1; 401extern struct timeval timeout; 402 403 404void 405get_system_info(struct system_info *si) 406{ 407 long total; 408 struct loadavg sysload; 409 int mib[2]; 410 struct timeval boottime; 411 size_t bt_size; 412 int i, j; 413 size_t size; 414 415 /* get the CPU stats */ 416 size = (maxid + 1) * CPUSTATES * sizeof(long); 417 if (sysctlbyname("kern.cp_times", pcpu_cp_time, &size, NULL, 0) == -1) 418 err(1, "sysctlbyname kern.cp_times"); 419 GETSYSCTL("kern.cp_time", cp_time); 420 GETSYSCTL("vm.loadavg", sysload); 421 GETSYSCTL("kern.lastpid", lastpid); 422 423 /* convert load averages to doubles */ 424 for (i = 0; i < 3; i++) 425 si->load_avg[i] = (double)sysload.ldavg[i] / sysload.fscale; 426 427 /* convert cp_time counts to percentages */ 428 for (i = j = 0; i <= maxid; i++) { 429 if ((cpumask & (1ul << i)) == 0) 430 continue; 431 percentages(CPUSTATES, &pcpu_cpu_states[j * CPUSTATES], 432 &pcpu_cp_time[j * CPUSTATES], 433 &pcpu_cp_old[j * CPUSTATES], 434 &pcpu_cp_diff[j * CPUSTATES]); 435 j++; 436 } 437 percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff); 438 439 /* sum memory & swap statistics */ 440 { 441 static unsigned int swap_delay = 0; 442 static int swapavail = 0; 443 static int swapfree = 0; 444 static long bufspace = 0; 445 static int nspgsin, nspgsout; 446 447 GETSYSCTL("vfs.bufspace", bufspace); 448 GETSYSCTL("vm.stats.vm.v_active_count", memory_stats[0]); 449 GETSYSCTL("vm.stats.vm.v_inactive_count", memory_stats[1]); 450 GETSYSCTL("vm.stats.vm.v_wire_count", memory_stats[2]); 451 GETSYSCTL("vm.stats.vm.v_cache_count", memory_stats[3]); 452 GETSYSCTL("vm.stats.vm.v_free_count", memory_stats[5]); 453 GETSYSCTL("vm.stats.vm.v_swappgsin", nspgsin); 454 GETSYSCTL("vm.stats.vm.v_swappgsout", nspgsout); 455 /* convert memory stats to Kbytes */ 456 memory_stats[0] = pagetok(memory_stats[0]); 457 memory_stats[1] = pagetok(memory_stats[1]); 458 memory_stats[2] = pagetok(memory_stats[2]); 459 memory_stats[3] = pagetok(memory_stats[3]); 460 memory_stats[4] = bufspace / 1024; 461 memory_stats[5] = pagetok(memory_stats[5]); 462 memory_stats[6] = -1; 463 464 /* first interval */ 465 if (swappgsin < 0) { 466 swap_stats[4] = 0; 467 swap_stats[5] = 0; 468 } 469 470 /* compute differences between old and new swap statistic */ 471 else { 472 swap_stats[4] = pagetok(((nspgsin - swappgsin))); 473 swap_stats[5] = pagetok(((nspgsout - swappgsout))); 474 } 475 476 swappgsin = nspgsin; 477 swappgsout = nspgsout; 478 479 /* call CPU heavy swapmode() only for changes */ 480 if (swap_stats[4] > 0 || swap_stats[5] > 0 || swap_delay == 0) { 481 swap_stats[3] = swapmode(&swapavail, &swapfree); 482 swap_stats[0] = swapavail; 483 swap_stats[1] = swapavail - swapfree; 484 swap_stats[2] = swapfree; 485 } 486 swap_delay = 1; 487 swap_stats[6] = -1; 488 } 489 490 /* set arrays and strings */ 491 if (pcpu_stats) { 492 si->cpustates = pcpu_cpu_states; 493 si->ncpus = ncpus; 494 } else { 495 si->cpustates = cpu_states; 496 si->ncpus = 1; 497 } 498 si->memory = memory_stats; 499 si->swap = swap_stats; 500 501 502 if (lastpid > 0) { 503 si->last_pid = lastpid; 504 } else { 505 si->last_pid = -1; 506 } 507 508 /* 509 * Print how long system has been up. 510 * (Found by looking getting "boottime" from the kernel) 511 */ 512 mib[0] = CTL_KERN; 513 mib[1] = KERN_BOOTTIME; 514 bt_size = sizeof(boottime); 515 if (sysctl(mib, 2, &boottime, &bt_size, NULL, 0) != -1 && 516 boottime.tv_sec != 0) { 517 si->boottime = boottime; 518 } else { 519 si->boottime.tv_sec = -1; 520 } 521} 522 523#define NOPROC ((void *)-1) 524 525/* 526 * We need to compare data from the old process entry with the new 527 * process entry. 528 * To facilitate doing this quickly we stash a pointer in the kinfo_proc 529 * structure to cache the mapping. We also use a negative cache pointer 530 * of NOPROC to avoid duplicate lookups. 531 * XXX: this could be done when the actual processes are fetched, we do 532 * it here out of laziness. 533 */ 534const struct kinfo_proc * 535get_old_proc(struct kinfo_proc *pp) 536{ 537 struct kinfo_proc **oldpp, *oldp; 538 539 /* 540 * If this is the first fetch of the kinfo_procs then we don't have 541 * any previous entries. 542 */ 543 if (previous_proc_count == 0) 544 return (NULL); 545 /* negative cache? */ 546 if (pp->ki_udata == NOPROC) 547 return (NULL); 548 /* cached? */ 549 if (pp->ki_udata != NULL) 550 return (pp->ki_udata); 551 /* 552 * Not cached, 553 * 1) look up based on pid. 554 * 2) compare process start. 555 * If we fail here, then setup a negative cache entry, otherwise 556 * cache it. 557 */ 558 oldpp = bsearch(&pp, previous_pref, previous_proc_count, 559 sizeof(*previous_pref), ps.thread ? compare_tid : compare_pid); 560 if (oldpp == NULL) { 561 pp->ki_udata = NOPROC; 562 return (NULL); 563 } 564 oldp = *oldpp; 565 if (bcmp(&oldp->ki_start, &pp->ki_start, sizeof(pp->ki_start)) != 0) { 566 pp->ki_udata = NOPROC; 567 return (NULL); 568 } 569 pp->ki_udata = oldp; 570 return (oldp); 571} 572 573/* 574 * Return the total amount of IO done in blocks in/out and faults. 575 * store the values individually in the pointers passed in. 576 */ 577long 578get_io_stats(struct kinfo_proc *pp, long *inp, long *oup, long *flp, 579 long *vcsw, long *ivcsw) 580{ 581 const struct kinfo_proc *oldp; 582 static struct kinfo_proc dummy; 583 long ret; 584 585 oldp = get_old_proc(pp); 586 if (oldp == NULL) { 587 bzero(&dummy, sizeof(dummy)); 588 oldp = &dummy; 589 } 590 *inp = RU(pp)->ru_inblock - RU(oldp)->ru_inblock; 591 *oup = RU(pp)->ru_oublock - RU(oldp)->ru_oublock; 592 *flp = RU(pp)->ru_majflt - RU(oldp)->ru_majflt; 593 *vcsw = RU(pp)->ru_nvcsw - RU(oldp)->ru_nvcsw; 594 *ivcsw = RU(pp)->ru_nivcsw - RU(oldp)->ru_nivcsw; 595 ret = 596 (RU(pp)->ru_inblock - RU(oldp)->ru_inblock) + 597 (RU(pp)->ru_oublock - RU(oldp)->ru_oublock) + 598 (RU(pp)->ru_majflt - RU(oldp)->ru_majflt); 599 return (ret); 600} 601 602/* 603 * Return the total number of block in/out and faults by a process. 604 */ 605long 606get_io_total(struct kinfo_proc *pp) 607{ 608 long dummy; 609 610 return (get_io_stats(pp, &dummy, &dummy, &dummy, &dummy, &dummy)); 611} 612 613static struct handle handle; 614 615caddr_t 616get_process_info(struct system_info *si, struct process_select *sel, 617 int (*compare)(const void *, const void *)) 618{ 619 int i; 620 int total_procs; 621 long p_io; 622 long p_inblock, p_oublock, p_majflt, p_vcsw, p_ivcsw; 623 int active_procs; 624 struct kinfo_proc **prefp; 625 struct kinfo_proc *pp; 626 627 /* these are copied out of sel for speed */ 628 int show_idle; 629 int show_self; 630 int show_system; 631 int show_uid; 632 int show_command; 633 int show_kidle; 634 635 /* 636 * Save the previous process info. 637 */ 638 if (previous_proc_count_max < nproc) { 639 free(previous_procs); 640 previous_procs = malloc(nproc * sizeof(*previous_procs)); 641 free(previous_pref); 642 previous_pref = malloc(nproc * sizeof(*previous_pref)); 643 if (previous_procs == NULL || previous_pref == NULL) { 644 (void) fprintf(stderr, "top: Out of memory.\n"); 645 quit(23); 646 } 647 previous_proc_count_max = nproc; 648 } 649 if (nproc) { 650 for (i = 0; i < nproc; i++) 651 previous_pref[i] = &previous_procs[i]; 652 bcopy(pbase, previous_procs, nproc * sizeof(*previous_procs)); 653 qsort(previous_pref, nproc, sizeof(*previous_pref), 654 ps.thread ? compare_tid : compare_pid); 655 } 656 previous_proc_count = nproc; 657 658 pbase = kvm_getprocs(kd, sel->thread ? KERN_PROC_ALL : KERN_PROC_PROC, 659 0, &nproc); 660 if (nproc > onproc) 661 pref = realloc(pref, sizeof(*pref) * (onproc = nproc)); 662 if (pref == NULL || pbase == NULL) { 663 (void) fprintf(stderr, "top: Out of memory.\n"); 664 quit(23); 665 } 666 /* get a pointer to the states summary array */ 667 si->procstates = process_states; 668 669 /* set up flags which define what we are going to select */ 670 show_idle = sel->idle; 671 show_self = sel->self == -1; 672 show_system = sel->system; 673 show_uid = sel->uid != -1; 674 show_command = sel->command != NULL; 675 show_kidle = sel->kidle; 676 677 /* count up process states and get pointers to interesting procs */ 678 total_procs = 0; 679 active_procs = 0; 680 total_inblock = 0; 681 total_oublock = 0; 682 total_majflt = 0; 683 memset((char *)process_states, 0, sizeof(process_states)); 684 prefp = pref; 685 for (pp = pbase, i = 0; i < nproc; pp++, i++) { 686 687 if (pp->ki_stat == 0) 688 /* not in use */ 689 continue; 690 691 if (!show_self && pp->ki_pid == sel->self) 692 /* skip self */ 693 continue; 694 695 if (!show_system && (pp->ki_flag & P_SYSTEM)) 696 /* skip system process */ 697 continue; 698 699 p_io = get_io_stats(pp, &p_inblock, &p_oublock, &p_majflt, 700 &p_vcsw, &p_ivcsw); 701 total_inblock += p_inblock; 702 total_oublock += p_oublock; 703 total_majflt += p_majflt; 704 total_procs++; 705 process_states[pp->ki_stat]++; 706 707 if (pp->ki_stat == SZOMB) 708 /* skip zombies */ 709 continue; 710 711 if (!show_kidle && pp->ki_tdflags & TDF_IDLETD) 712 /* skip kernel idle process */ 713 continue; 714 715 if (displaymode == DISP_CPU && !show_idle && 716 (pp->ki_pctcpu == 0 || 717 pp->ki_stat == SSTOP || pp->ki_stat == SIDL)) 718 /* skip idle or non-running processes */ 719 continue; 720 721 if (displaymode == DISP_IO && !show_idle && p_io == 0) 722 /* skip processes that aren't doing I/O */ 723 continue; 724 725 if (show_uid && pp->ki_ruid != (uid_t)sel->uid) 726 /* skip proc. that don't belong to the selected UID */ 727 continue; 728 729 *prefp++ = pp; 730 active_procs++; 731 } 732 733 /* if requested, sort the "interesting" processes */ 734 if (compare != NULL) 735 qsort(pref, active_procs, sizeof(*pref), compare); 736 737 /* remember active and total counts */ 738 si->p_total = total_procs; 739 si->p_active = pref_len = active_procs; 740 741 /* pass back a handle */ 742 handle.next_proc = pref; 743 handle.remaining = active_procs; 744 return ((caddr_t)&handle); 745} 746 747static char fmt[128]; /* static area where result is built */ 748 749char * 750format_next_process(caddr_t handle, char *(*get_userid)(int), int flags) 751{ 752 struct kinfo_proc *pp; 753 const struct kinfo_proc *oldp; 754 long cputime; 755 double pct; 756 struct handle *hp; 757 char status[16]; 758 int state; 759 struct rusage ru, *rup; 760 long p_tot, s_tot; 761 char *proc_fmt, thr_buf[6], jid_buf[6]; 762 char *cmdbuf = NULL; 763 char **args; 764 765 /* find and remember the next proc structure */ 766 hp = (struct handle *)handle; 767 pp = *(hp->next_proc++); 768 hp->remaining--; 769 770 /* get the process's command name */ 771 if ((pp->ki_flag & P_INMEM) == 0) { 772 /* 773 * Print swapped processes as <pname> 774 */ 775 size_t len; 776 777 len = strlen(pp->ki_comm); 778 if (len > sizeof(pp->ki_comm) - 3) 779 len = sizeof(pp->ki_comm) - 3; 780 memmove(pp->ki_comm + 1, pp->ki_comm, len); 781 pp->ki_comm[0] = '<'; 782 pp->ki_comm[len + 1] = '>'; 783 pp->ki_comm[len + 2] = '\0'; 784 } 785 786 /* 787 * Convert the process's runtime from microseconds to seconds. This 788 * time includes the interrupt time although that is not wanted here. 789 * ps(1) is similarly sloppy. 790 */ 791 cputime = (pp->ki_runtime + 500000) / 1000000; 792 793 /* calculate the base for cpu percentages */ 794 pct = pctdouble(pp->ki_pctcpu); 795 796 /* generate "STATE" field */ 797 switch (state = pp->ki_stat) { 798 case SRUN: 799 if (smpmode && pp->ki_oncpu != 0xff) 800 sprintf(status, "CPU%d", pp->ki_oncpu); 801 else 802 strcpy(status, "RUN"); 803 break; 804 case SLOCK: 805 if (pp->ki_kiflag & KI_LOCKBLOCK) { 806 sprintf(status, "*%.6s", pp->ki_lockname); 807 break; 808 } 809 /* fall through */ 810 case SSLEEP: 811 if (pp->ki_wmesg != NULL) { 812 sprintf(status, "%.6s", pp->ki_wmesg); 813 break; 814 } 815 /* FALLTHROUGH */ 816 default: 817 818 if (state >= 0 && 819 state < sizeof(state_abbrev) / sizeof(*state_abbrev)) 820 sprintf(status, "%.6s", state_abbrev[state]); 821 else 822 sprintf(status, "?%5d", state); 823 break; 824 } 825 826 cmdbuf = (char *)malloc(cmdlengthdelta + 1); 827 if (cmdbuf == NULL) { 828 warn("malloc(%d)", cmdlengthdelta + 1); 829 return NULL; 830 } 831 832 if (!(flags & FMT_SHOWARGS)) { 833 if (ps.thread && pp->ki_flag & P_HADTHREADS && 834 pp->ki_tdname[0]) { 835 snprintf(cmdbuf, cmdlengthdelta, "%s{%s}", pp->ki_comm, 836 pp->ki_tdname); 837 } else { 838 snprintf(cmdbuf, cmdlengthdelta, "%s", pp->ki_comm); 839 } 840 } else { 841 if (pp->ki_flag & P_SYSTEM || 842 pp->ki_args == NULL || 843 (args = kvm_getargv(kd, pp, cmdlengthdelta)) == NULL || 844 !(*args)) { 845 if (ps.thread && pp->ki_flag & P_HADTHREADS && 846 pp->ki_tdname[0]) { 847 snprintf(cmdbuf, cmdlengthdelta, 848 "[%s{%s}]", pp->ki_comm, pp->ki_tdname); 849 } else { 850 snprintf(cmdbuf, cmdlengthdelta, 851 "[%s]", pp->ki_comm); 852 } 853 } else { 854 char *src, *dst, *argbuf; 855 char *cmd; 856 size_t argbuflen; 857 size_t len; 858 859 argbuflen = cmdlengthdelta * 4; 860 argbuf = (char *)malloc(argbuflen + 1); 861 if (argbuf == NULL) { 862 warn("malloc(%d)", argbuflen + 1); 863 free(cmdbuf); 864 return NULL; 865 } 866 867 dst = argbuf; 868 869 /* Extract cmd name from argv */ 870 cmd = strrchr(*args, '/'); 871 if (cmd == NULL) 872 cmd = *args; 873 else 874 cmd++; 875 876 for (; (src = *args++) != NULL; ) { 877 if (*src == '\0') 878 continue; 879 len = (argbuflen - (dst - argbuf) - 1) / 4; 880 strvisx(dst, src, 881 strlen(src) < len ? strlen(src) : len, 882 VIS_NL | VIS_CSTYLE); 883 while (*dst != '\0') 884 dst++; 885 if ((argbuflen - (dst - argbuf) - 1) / 4 > 0) 886 *dst++ = ' '; /* add delimiting space */ 887 } 888 if (dst != argbuf && dst[-1] == ' ') 889 dst--; 890 *dst = '\0'; 891 892 if (strcmp(cmd, pp->ki_comm) != 0 ) { 893 if (ps.thread && pp->ki_flag & P_HADTHREADS && 894 pp->ki_tdname[0]) 895 snprintf(cmdbuf, cmdlengthdelta, 896 "%s (%s){%s}", argbuf, pp->ki_comm, 897 pp->ki_tdname); 898 else 899 snprintf(cmdbuf, cmdlengthdelta, 900 "%s (%s)", argbuf, pp->ki_comm); 901 } else { 902 if (ps.thread && pp->ki_flag & P_HADTHREADS && 903 pp->ki_tdname[0]) 904 snprintf(cmdbuf, cmdlengthdelta, 905 "%s{%s}", argbuf, pp->ki_tdname); 906 else 907 strlcpy(cmdbuf, argbuf, cmdlengthdelta); 908 } 909 free(argbuf); 910 } 911 } 912 913 if (ps.jail == 0) 914 jid_buf[0] = '\0'; 915 else 916 snprintf(jid_buf, sizeof(jid_buf), " %*d", 917 sizeof(jid_buf) - 3, pp->ki_jid); 918 919 if (displaymode == DISP_IO) { 920 oldp = get_old_proc(pp); 921 if (oldp != NULL) { 922 ru.ru_inblock = RU(pp)->ru_inblock - 923 RU(oldp)->ru_inblock; 924 ru.ru_oublock = RU(pp)->ru_oublock - 925 RU(oldp)->ru_oublock; 926 ru.ru_majflt = RU(pp)->ru_majflt - RU(oldp)->ru_majflt; 927 ru.ru_nvcsw = RU(pp)->ru_nvcsw - RU(oldp)->ru_nvcsw; 928 ru.ru_nivcsw = RU(pp)->ru_nivcsw - RU(oldp)->ru_nivcsw; 929 rup = &ru; 930 } else { 931 rup = RU(pp); 932 } 933 p_tot = rup->ru_inblock + rup->ru_oublock + rup->ru_majflt; 934 s_tot = total_inblock + total_oublock + total_majflt; 935 936 sprintf(fmt, io_Proc_format, 937 pp->ki_pid, 938 jid_buf, 939 namelength, namelength, (*get_userid)(pp->ki_ruid), 940 rup->ru_nvcsw, 941 rup->ru_nivcsw, 942 rup->ru_inblock, 943 rup->ru_oublock, 944 rup->ru_majflt, 945 p_tot, 946 s_tot == 0 ? 0.0 : (p_tot * 100.0 / s_tot), 947 screen_width > cmdlengthdelta ? 948 screen_width - cmdlengthdelta : 0, 949 printable(cmdbuf)); 950 951 free(cmdbuf); 952 953 return (fmt); 954 } 955 956 /* format this entry */ 957 proc_fmt = smpmode ? smp_Proc_format : up_Proc_format; 958 if (ps.thread != 0) 959 thr_buf[0] = '\0'; 960 else 961 snprintf(thr_buf, sizeof(thr_buf), "%*d ", 962 sizeof(thr_buf) - 2, pp->ki_numthreads); 963 964 sprintf(fmt, proc_fmt, 965 pp->ki_pid, 966 jid_buf, 967 namelength, namelength, (*get_userid)(pp->ki_ruid), 968 thr_buf, 969 pp->ki_pri.pri_level - PZERO, 970 format_nice(pp), 971 format_k2(PROCSIZE(pp)), 972 format_k2(pagetok(pp->ki_rssize)), 973 status, 974 smpmode ? pp->ki_lastcpu : 0, 975 format_time(cputime), 976 ps.wcpu ? 100.0 * weighted_cpu(pct, pp) : 100.0 * pct, 977 screen_width > cmdlengthdelta ? screen_width - cmdlengthdelta : 0, 978 printable(cmdbuf)); 979 980 free(cmdbuf); 981 982 /* return the result */ 983 return (fmt); 984} 985 986static void 987getsysctl(const char *name, void *ptr, size_t len) 988{ 989 size_t nlen = len; 990 991 if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) { 992 fprintf(stderr, "top: sysctl(%s...) failed: %s\n", name, 993 strerror(errno)); 994 quit(23); 995 } 996 if (nlen != len) { 997 fprintf(stderr, "top: sysctl(%s...) expected %lu, got %lu\n", 998 name, (unsigned long)len, (unsigned long)nlen); 999 quit(23); 1000 } 1001} 1002 1003static const char * 1004format_nice(const struct kinfo_proc *pp) 1005{ 1006 const char *fifo, *kthread; 1007 int rtpri; 1008 static char nicebuf[4 + 1]; 1009 1010 fifo = PRI_NEED_RR(pp->ki_pri.pri_class) ? "" : "F"; 1011 kthread = (pp->ki_flag & P_KTHREAD) ? "k" : ""; 1012 switch (PRI_BASE(pp->ki_pri.pri_class)) { 1013 case PRI_ITHD: 1014 return ("-"); 1015 case PRI_REALTIME: 1016 /* 1017 * XXX: the kernel doesn't tell us the original rtprio and 1018 * doesn't really know what it was, so to recover it we 1019 * must be more chummy with the implementation than the 1020 * implementation is with itself. pri_user gives a 1021 * constant "base" priority, but is only initialized 1022 * properly for user threads. pri_native gives what the 1023 * kernel calls the "base" priority, but it isn't constant 1024 * since it is changed by priority propagation. pri_native 1025 * also isn't properly initialized for all threads, but it 1026 * is properly initialized for kernel realtime and idletime 1027 * threads. Thus we use pri_user for the base priority of 1028 * user threads (it is always correct) and pri_native for 1029 * the base priority of kernel realtime and idletime threads 1030 * (there is nothing better, and it is usually correct). 1031 * 1032 * The field width and thus the buffer are too small for 1033 * values like "kr31F", but such values shouldn't occur, 1034 * and if they do then the tailing "F" is not displayed. 1035 */ 1036 rtpri = ((pp->ki_flag & P_KTHREAD) ? pp->ki_pri.pri_native : 1037 pp->ki_pri.pri_user) - PRI_MIN_REALTIME; 1038 snprintf(nicebuf, sizeof(nicebuf), "%sr%d%s", 1039 kthread, rtpri, fifo); 1040 break; 1041 case PRI_TIMESHARE: 1042 if (pp->ki_flag & P_KTHREAD) 1043 return ("-"); 1044 snprintf(nicebuf, sizeof(nicebuf), "%d", pp->ki_nice - NZERO); 1045 break; 1046 case PRI_IDLE: 1047 /* XXX: as above. */ 1048 rtpri = ((pp->ki_flag & P_KTHREAD) ? pp->ki_pri.pri_native : 1049 pp->ki_pri.pri_user) - PRI_MIN_IDLE; 1050 snprintf(nicebuf, sizeof(nicebuf), "%si%d%s", 1051 kthread, rtpri, fifo); 1052 break; 1053 default: 1054 return ("?"); 1055 } 1056 return (nicebuf); 1057} 1058 1059/* comparison routines for qsort */ 1060 1061static int 1062compare_pid(const void *p1, const void *p2) 1063{ 1064 const struct kinfo_proc * const *pp1 = p1; 1065 const struct kinfo_proc * const *pp2 = p2; 1066 1067 if ((*pp2)->ki_pid < 0 || (*pp1)->ki_pid < 0) 1068 abort(); 1069 1070 return ((*pp1)->ki_pid - (*pp2)->ki_pid); 1071} 1072 1073static int 1074compare_tid(const void *p1, const void *p2) 1075{ 1076 const struct kinfo_proc * const *pp1 = p1; 1077 const struct kinfo_proc * const *pp2 = p2; 1078 1079 if ((*pp2)->ki_tid < 0 || (*pp1)->ki_tid < 0) 1080 abort(); 1081 1082 return ((*pp1)->ki_tid - (*pp2)->ki_tid); 1083} 1084 1085/* 1086 * proc_compare - comparison function for "qsort" 1087 * Compares the resource consumption of two processes using five 1088 * distinct keys. The keys (in descending order of importance) are: 1089 * percent cpu, cpu ticks, state, resident set size, total virtual 1090 * memory usage. The process states are ordered as follows (from least 1091 * to most important): WAIT, zombie, sleep, stop, start, run. The 1092 * array declaration below maps a process state index into a number 1093 * that reflects this ordering. 1094 */ 1095 1096static int sorted_state[] = { 1097 0, /* not used */ 1098 3, /* sleep */ 1099 1, /* ABANDONED (WAIT) */ 1100 6, /* run */ 1101 5, /* start */ 1102 2, /* zombie */ 1103 4 /* stop */ 1104}; 1105 1106 1107#define ORDERKEY_PCTCPU(a, b) do { \ 1108 long diff; \ 1109 if (ps.wcpu) \ 1110 diff = floor(1.0E6 * weighted_cpu(pctdouble((b)->ki_pctcpu), \ 1111 (b))) - \ 1112 floor(1.0E6 * weighted_cpu(pctdouble((a)->ki_pctcpu), \ 1113 (a))); \ 1114 else \ 1115 diff = (long)(b)->ki_pctcpu - (long)(a)->ki_pctcpu; \ 1116 if (diff != 0) \ 1117 return (diff > 0 ? 1 : -1); \ 1118} while (0) 1119 1120#define ORDERKEY_CPTICKS(a, b) do { \ 1121 int64_t diff = (int64_t)(b)->ki_runtime - (int64_t)(a)->ki_runtime; \ 1122 if (diff != 0) \ 1123 return (diff > 0 ? 1 : -1); \ 1124} while (0) 1125 1126#define ORDERKEY_STATE(a, b) do { \ 1127 int diff = sorted_state[(b)->ki_stat] - sorted_state[(a)->ki_stat]; \ 1128 if (diff != 0) \ 1129 return (diff > 0 ? 1 : -1); \ 1130} while (0) 1131 1132#define ORDERKEY_PRIO(a, b) do { \ 1133 int diff = (int)(b)->ki_pri.pri_level - (int)(a)->ki_pri.pri_level; \ 1134 if (diff != 0) \ 1135 return (diff > 0 ? 1 : -1); \ 1136} while (0) 1137 1138#define ORDERKEY_THREADS(a, b) do { \ 1139 int diff = (int)(b)->ki_numthreads - (int)(a)->ki_numthreads; \ 1140 if (diff != 0) \ 1141 return (diff > 0 ? 1 : -1); \ 1142} while (0) 1143 1144#define ORDERKEY_RSSIZE(a, b) do { \ 1145 long diff = (long)(b)->ki_rssize - (long)(a)->ki_rssize; \ 1146 if (diff != 0) \ 1147 return (diff > 0 ? 1 : -1); \ 1148} while (0) 1149 1150#define ORDERKEY_MEM(a, b) do { \ 1151 long diff = (long)PROCSIZE((b)) - (long)PROCSIZE((a)); \ 1152 if (diff != 0) \ 1153 return (diff > 0 ? 1 : -1); \ 1154} while (0) 1155 1156#define ORDERKEY_JID(a, b) do { \ 1157 int diff = (int)(b)->ki_jid - (int)(a)->ki_jid; \ 1158 if (diff != 0) \ 1159 return (diff > 0 ? 1 : -1); \ 1160} while (0) 1161 1162/* compare_cpu - the comparison function for sorting by cpu percentage */ 1163 1164int 1165#ifdef ORDER 1166compare_cpu(void *arg1, void *arg2) 1167#else 1168proc_compare(void *arg1, void *arg2) 1169#endif 1170{ 1171 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1172 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1173 1174 ORDERKEY_PCTCPU(p1, p2); 1175 ORDERKEY_CPTICKS(p1, p2); 1176 ORDERKEY_STATE(p1, p2); 1177 ORDERKEY_PRIO(p1, p2); 1178 ORDERKEY_RSSIZE(p1, p2); 1179 ORDERKEY_MEM(p1, p2); 1180 1181 return (0); 1182} 1183 1184#ifdef ORDER 1185/* "cpu" compare routines */ 1186int compare_size(), compare_res(), compare_time(), compare_prio(), 1187 compare_threads(); 1188 1189/* 1190 * "io" compare routines. Context switches aren't i/o, but are displayed 1191 * on the "io" display. 1192 */ 1193int compare_iototal(), compare_ioread(), compare_iowrite(), compare_iofault(), 1194 compare_vcsw(), compare_ivcsw(); 1195 1196int (*compares[])() = { 1197 compare_cpu, 1198 compare_size, 1199 compare_res, 1200 compare_time, 1201 compare_prio, 1202 compare_threads, 1203 compare_iototal, 1204 compare_ioread, 1205 compare_iowrite, 1206 compare_iofault, 1207 compare_vcsw, 1208 compare_ivcsw, 1209 compare_jid, 1210 NULL 1211}; 1212 1213/* compare_size - the comparison function for sorting by total memory usage */ 1214 1215int 1216compare_size(void *arg1, void *arg2) 1217{ 1218 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1219 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1220 1221 ORDERKEY_MEM(p1, p2); 1222 ORDERKEY_RSSIZE(p1, p2); 1223 ORDERKEY_PCTCPU(p1, p2); 1224 ORDERKEY_CPTICKS(p1, p2); 1225 ORDERKEY_STATE(p1, p2); 1226 ORDERKEY_PRIO(p1, p2); 1227 1228 return (0); 1229} 1230 1231/* compare_res - the comparison function for sorting by resident set size */ 1232 1233int 1234compare_res(void *arg1, void *arg2) 1235{ 1236 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1237 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1238 1239 ORDERKEY_RSSIZE(p1, p2); 1240 ORDERKEY_MEM(p1, p2); 1241 ORDERKEY_PCTCPU(p1, p2); 1242 ORDERKEY_CPTICKS(p1, p2); 1243 ORDERKEY_STATE(p1, p2); 1244 ORDERKEY_PRIO(p1, p2); 1245 1246 return (0); 1247} 1248 1249/* compare_time - the comparison function for sorting by total cpu time */ 1250 1251int 1252compare_time(void *arg1, void *arg2) 1253{ 1254 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1255 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1256 1257 ORDERKEY_CPTICKS(p1, p2); 1258 ORDERKEY_PCTCPU(p1, p2); 1259 ORDERKEY_STATE(p1, p2); 1260 ORDERKEY_PRIO(p1, p2); 1261 ORDERKEY_RSSIZE(p1, p2); 1262 ORDERKEY_MEM(p1, p2); 1263 1264 return (0); 1265} 1266 1267/* compare_prio - the comparison function for sorting by priority */ 1268 1269int 1270compare_prio(void *arg1, void *arg2) 1271{ 1272 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1273 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1274 1275 ORDERKEY_PRIO(p1, p2); 1276 ORDERKEY_CPTICKS(p1, p2); 1277 ORDERKEY_PCTCPU(p1, p2); 1278 ORDERKEY_STATE(p1, p2); 1279 ORDERKEY_RSSIZE(p1, p2); 1280 ORDERKEY_MEM(p1, p2); 1281 1282 return (0); 1283} 1284 1285/* compare_threads - the comparison function for sorting by threads */ 1286int 1287compare_threads(void *arg1, void *arg2) 1288{ 1289 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1290 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1291 1292 ORDERKEY_THREADS(p1, p2); 1293 ORDERKEY_PCTCPU(p1, p2); 1294 ORDERKEY_CPTICKS(p1, p2); 1295 ORDERKEY_STATE(p1, p2); 1296 ORDERKEY_PRIO(p1, p2); 1297 ORDERKEY_RSSIZE(p1, p2); 1298 ORDERKEY_MEM(p1, p2); 1299 1300 return (0); 1301} 1302 1303/* compare_jid - the comparison function for sorting by jid */ 1304static int 1305compare_jid(const void *arg1, const void *arg2) 1306{ 1307 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1308 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1309 1310 ORDERKEY_JID(p1, p2); 1311 ORDERKEY_PCTCPU(p1, p2); 1312 ORDERKEY_CPTICKS(p1, p2); 1313 ORDERKEY_STATE(p1, p2); 1314 ORDERKEY_PRIO(p1, p2); 1315 ORDERKEY_RSSIZE(p1, p2); 1316 ORDERKEY_MEM(p1, p2); 1317 1318 return (0); 1319} 1320#endif /* ORDER */ 1321 1322/* assorted comparison functions for sorting by i/o */ 1323 1324int 1325#ifdef ORDER 1326compare_iototal(void *arg1, void *arg2) 1327#else 1328io_compare(void *arg1, void *arg2) 1329#endif 1330{ 1331 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1332 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1333 1334 return (get_io_total(p2) - get_io_total(p1)); 1335} 1336 1337#ifdef ORDER 1338int 1339compare_ioread(void *arg1, void *arg2) 1340{ 1341 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1342 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1343 long dummy, inp1, inp2; 1344 1345 (void) get_io_stats(p1, &inp1, &dummy, &dummy, &dummy, &dummy); 1346 (void) get_io_stats(p2, &inp2, &dummy, &dummy, &dummy, &dummy); 1347 1348 return (inp2 - inp1); 1349} 1350 1351int 1352compare_iowrite(void *arg1, void *arg2) 1353{ 1354 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1355 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1356 long dummy, oup1, oup2; 1357 1358 (void) get_io_stats(p1, &dummy, &oup1, &dummy, &dummy, &dummy); 1359 (void) get_io_stats(p2, &dummy, &oup2, &dummy, &dummy, &dummy); 1360 1361 return (oup2 - oup1); 1362} 1363 1364int 1365compare_iofault(void *arg1, void *arg2) 1366{ 1367 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1368 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1369 long dummy, flp1, flp2; 1370 1371 (void) get_io_stats(p1, &dummy, &dummy, &flp1, &dummy, &dummy); 1372 (void) get_io_stats(p2, &dummy, &dummy, &flp2, &dummy, &dummy); 1373 1374 return (flp2 - flp1); 1375} 1376 1377int 1378compare_vcsw(void *arg1, void *arg2) 1379{ 1380 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1381 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1382 long dummy, flp1, flp2; 1383 1384 (void) get_io_stats(p1, &dummy, &dummy, &dummy, &flp1, &dummy); 1385 (void) get_io_stats(p2, &dummy, &dummy, &dummy, &flp2, &dummy); 1386 1387 return (flp2 - flp1); 1388} 1389 1390int 1391compare_ivcsw(void *arg1, void *arg2) 1392{ 1393 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1394 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1395 long dummy, flp1, flp2; 1396 1397 (void) get_io_stats(p1, &dummy, &dummy, &dummy, &dummy, &flp1); 1398 (void) get_io_stats(p2, &dummy, &dummy, &dummy, &dummy, &flp2); 1399 1400 return (flp2 - flp1); 1401} 1402#endif /* ORDER */ 1403 1404/* 1405 * proc_owner(pid) - returns the uid that owns process "pid", or -1 if 1406 * the process does not exist. 1407 * It is EXTREMLY IMPORTANT that this function work correctly. 1408 * If top runs setuid root (as in SVR4), then this function 1409 * is the only thing that stands in the way of a serious 1410 * security problem. It validates requests for the "kill" 1411 * and "renice" commands. 1412 */ 1413 1414int 1415proc_owner(int pid) 1416{ 1417 int cnt; 1418 struct kinfo_proc **prefp; 1419 struct kinfo_proc *pp; 1420 1421 prefp = pref; 1422 cnt = pref_len; 1423 while (--cnt >= 0) { 1424 pp = *prefp++; 1425 if (pp->ki_pid == (pid_t)pid) 1426 return ((int)pp->ki_ruid); 1427 } 1428 return (-1); 1429} 1430 1431static int 1432swapmode(int *retavail, int *retfree) 1433{ 1434 int n; 1435 int pagesize = getpagesize(); 1436 struct kvm_swap swapary[1]; 1437 1438 *retavail = 0; 1439 *retfree = 0; 1440 1441#define CONVERT(v) ((quad_t)(v) * pagesize / 1024) 1442 1443 n = kvm_getswapinfo(kd, swapary, 1, 0); 1444 if (n < 0 || swapary[0].ksw_total == 0) 1445 return (0); 1446 1447 *retavail = CONVERT(swapary[0].ksw_total); 1448 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used); 1449 1450 n = (int)(swapary[0].ksw_used * 100.0 / swapary[0].ksw_total); 1451 return (n); 1452} 1453