machine.c revision 223841
1139749Simp/* 223934Sgibbs * top - a top users display for Unix 323934Sgibbs * 423934Sgibbs * SYNOPSIS: For FreeBSD-2.x and later 5102668Sgibbs * 623934Sgibbs * DESCRIPTION: 723934Sgibbs * Originally written for BSD4.4 system by Christos Zoulas. 823934Sgibbs * Ported to FreeBSD 2.x by Steven Wallace && Wolfram Schneider 923934Sgibbs * Order support hacked in from top-3.5beta6/machine/m_aix41.c 1023934Sgibbs * by Monte Mitzelfelt (for latest top see http://www.groupsys.com/topinfo/) 1123934Sgibbs * 1226997Sgibbs * This is the machine-dependent module for FreeBSD 2.2 1354211Sgibbs * Works for: 1495376Sgibbs * FreeBSD 2.2.x, 3.x, 4.x, and probably FreeBSD 2.1.x 1595376Sgibbs * 1695376Sgibbs * LIBS: -lkvm 1795376Sgibbs * 1895376Sgibbs * AUTHOR: Christos Zoulas <christos@ee.cornell.edu> 1995376Sgibbs * Steven Wallace <swallace@freebsd.org> 2095376Sgibbs * Wolfram Schneider <wosch@FreeBSD.org> 2195376Sgibbs * Thomas Moestl <tmoestl@gmx.net> 2223934Sgibbs * 2363457Sgibbs * $FreeBSD: head/usr.bin/top/machine.c 223841 2011-07-07 13:37:46Z jhb $ 2495376Sgibbs */ 2595376Sgibbs 2663457Sgibbs#include <sys/param.h> 2795376Sgibbs#include <sys/errno.h> 2895376Sgibbs#include <sys/file.h> 2995376Sgibbs#include <sys/proc.h> 3095376Sgibbs#include <sys/resource.h> 3195376Sgibbs#include <sys/rtprio.h> 3295376Sgibbs#include <sys/signal.h> 3323934Sgibbs#include <sys/sysctl.h> 3423934Sgibbs#include <sys/time.h> 3595376Sgibbs#include <sys/user.h> 3695376Sgibbs#include <sys/vmmeter.h> 3795376Sgibbs 3895376Sgibbs#include <err.h> 3923934Sgibbs#include <kvm.h> 40102668Sgibbs#include <math.h> 4165943Sgibbs#include <nlist.h> 4250477Speter#include <paths.h> 4323934Sgibbs#include <pwd.h> 4423934Sgibbs#include <stdio.h> 4523934Sgibbs#include <stdlib.h> 4623934Sgibbs#include <string.h> 4723934Sgibbs#include <strings.h> 4823934Sgibbs#include <unistd.h> 4923934Sgibbs#include <vis.h> 5023934Sgibbs 5123934Sgibbs#include "top.h" 5223934Sgibbs#include "machine.h" 5323934Sgibbs#include "screen.h" 5423934Sgibbs#include "utils.h" 5523934Sgibbs#include "layout.h" 5623934Sgibbs 5723934Sgibbs#define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var)) 5860938Sjake#define SMPUNAMELEN 13 5923934Sgibbs#define UPUNAMELEN 15 6023934Sgibbs 6123934Sgibbsextern struct process_select ps; 6223934Sgibbsextern char* printable(char *); 6323934Sgibbsstatic int smpmode; 6423934Sgibbsenum displaymodes displaymode; 6523934Sgibbs#ifdef TOP_USERNAME_LEN 6623934Sgibbsstatic int namelength = TOP_USERNAME_LEN; 6760938Sjake#else 6823934Sgibbsstatic int namelength = 8; 6923934Sgibbs#endif 7066270Sgibbsstatic int cmdlengthdelta; 7139220Sgibbs 7239220Sgibbs/* Prototypes for top internals */ 7323934Sgibbsvoid quit(int); 7423934Sgibbs 75102668Sgibbs/* get_process_info passes back a handle. This is what it looks like: */ 7623934Sgibbs 7723934Sgibbsstruct handle { 78102668Sgibbs struct kinfo_proc **next_proc; /* points to next valid proc pointer */ 7995376Sgibbs int remaining; /* number of pointers remaining */ 8079873Sgibbs}; 8195376Sgibbs 8295376Sgibbs/* declarations for load_avg */ 8395376Sgibbs#include "loadavg.h" 8423934Sgibbs 8539220Sgibbs/* define what weighted cpu is. */ 8639220Sgibbs#define weighted_cpu(pct, pp) ((pp)->ki_swtime == 0 ? 0.0 : \ 8795376Sgibbs ((pct) / (1.0 - exp((pp)->ki_swtime * logcpu)))) 8839220Sgibbs 8966270Sgibbs/* what we consider to be process size: */ 9039220Sgibbs#define PROCSIZE(pp) ((pp)->ki_size / 1024) 9139220Sgibbs 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 const char *format_nice(const struct kinfo_proc *pp); 239static void getsysctl(const char *name, void *ptr, size_t len); 240static int swapmode(int *retavail, int *retfree); 241 242int 243machine_init(struct statics *statics, char do_unames) 244{ 245 int pagesize; 246 size_t modelen; 247 struct passwd *pw; 248 249 modelen = sizeof(smpmode); 250 if ((sysctlbyname("machdep.smp_active", &smpmode, &modelen, 251 NULL, 0) != 0 && 252 sysctlbyname("kern.smp.active", &smpmode, &modelen, 253 NULL, 0) != 0) || 254 modelen != sizeof(smpmode)) 255 smpmode = 0; 256 257 if (do_unames) { 258 while ((pw = getpwent()) != NULL) { 259 if (strlen(pw->pw_name) > namelength) 260 namelength = strlen(pw->pw_name); 261 } 262 } 263 if (smpmode && namelength > SMPUNAMELEN) 264 namelength = SMPUNAMELEN; 265 else if (namelength > UPUNAMELEN) 266 namelength = UPUNAMELEN; 267 268 kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open"); 269 if (kd == NULL) 270 return (-1); 271 272 GETSYSCTL("kern.ccpu", ccpu); 273 274 /* this is used in calculating WCPU -- calculate it ahead of time */ 275 logcpu = log(loaddouble(ccpu)); 276 277 pbase = NULL; 278 pref = NULL; 279 nproc = 0; 280 onproc = -1; 281 282 /* get the page size and calculate pageshift from it */ 283 pagesize = getpagesize(); 284 pageshift = 0; 285 while (pagesize > 1) { 286 pageshift++; 287 pagesize >>= 1; 288 } 289 290 /* we only need the amount of log(2)1024 for our conversion */ 291 pageshift -= LOG1024; 292 293 /* fill in the statics information */ 294 statics->procstate_names = procstatenames; 295 statics->cpustate_names = cpustatenames; 296 statics->memory_names = memorynames; 297 statics->swap_names = swapnames; 298#ifdef ORDER 299 statics->order_names = ordernames; 300#endif 301 302 /* Adjust display based on ncpus */ 303 if (pcpu_stats) { 304 int i, j, empty; 305 size_t size; 306 307 cpumask = 0; 308 ncpus = 0; 309 GETSYSCTL("kern.smp.maxcpus", maxcpu); 310 size = sizeof(long) * maxcpu * CPUSTATES; 311 times = malloc(size); 312 if (times == NULL) 313 err(1, "malloc %zd bytes", size); 314 if (sysctlbyname("kern.cp_times", times, &size, NULL, 0) == -1) 315 err(1, "sysctlbyname kern.cp_times"); 316 pcpu_cp_time = calloc(1, size); 317 maxid = (size / CPUSTATES / sizeof(long)) - 1; 318 for (i = 0; i <= maxid; i++) { 319 empty = 1; 320 for (j = 0; empty && j < CPUSTATES; j++) { 321 if (times[i * CPUSTATES + j] != 0) 322 empty = 0; 323 } 324 if (!empty) { 325 cpumask |= (1ul << i); 326 ncpus++; 327 } 328 } 329 330 if (ncpus > 1) { 331 y_mem += ncpus - 1; /* 3 */ 332 y_swap += ncpus - 1; /* 4 */ 333 y_idlecursor += ncpus - 1; /* 5 */ 334 y_message += ncpus - 1; /* 5 */ 335 y_header += ncpus - 1; /* 6 */ 336 y_procs += ncpus - 1; /* 7 */ 337 Header_lines += ncpus - 1; /* 7 */ 338 } 339 size = sizeof(long) * ncpus * CPUSTATES; 340 pcpu_cp_old = calloc(1, size); 341 pcpu_cp_diff = calloc(1, size); 342 pcpu_cpu_states = calloc(1, size); 343 statics->ncpus = ncpus; 344 } else { 345 statics->ncpus = 1; 346 } 347 348 /* all done! */ 349 return (0); 350} 351 352char * 353format_header(char *uname_field) 354{ 355 static char Header[128]; 356 const char *prehead; 357 358 switch (displaymode) { 359 case DISP_CPU: 360 /* 361 * The logic of picking the right header format seems reverse 362 * here because we only want to display a THR column when 363 * "thread mode" is off (and threads are not listed as 364 * separate lines). 365 */ 366 prehead = smpmode ? 367 (ps.thread ? smp_header : smp_header_thr) : 368 (ps.thread ? up_header : up_header_thr); 369 snprintf(Header, sizeof(Header), prehead, 370 ps.jail ? " JID" : "", 371 namelength, namelength, uname_field, 372 ps.wcpu ? "WCPU" : "CPU"); 373 break; 374 case DISP_IO: 375 prehead = io_header; 376 snprintf(Header, sizeof(Header), prehead, 377 ps.jail ? " JID" : "", 378 namelength, namelength, uname_field); 379 break; 380 } 381 cmdlengthdelta = strlen(Header) - 7; 382 return (Header); 383} 384 385static int swappgsin = -1; 386static int swappgsout = -1; 387extern struct timeval timeout; 388 389 390void 391get_system_info(struct system_info *si) 392{ 393 long total; 394 struct loadavg sysload; 395 int mib[2]; 396 struct timeval boottime; 397 size_t bt_size; 398 int i, j; 399 size_t size; 400 401 /* get the cp_time array */ 402 if (pcpu_stats) { 403 size = (maxid + 1) * CPUSTATES * sizeof(long); 404 if (sysctlbyname("kern.cp_times", pcpu_cp_time, &size, NULL, 0) == -1) 405 err(1, "sysctlbyname kern.cp_times"); 406 } else { 407 GETSYSCTL("kern.cp_time", cp_time); 408 } 409 GETSYSCTL("vm.loadavg", sysload); 410 GETSYSCTL("kern.lastpid", lastpid); 411 412 /* convert load averages to doubles */ 413 for (i = 0; i < 3; i++) 414 si->load_avg[i] = (double)sysload.ldavg[i] / sysload.fscale; 415 416 if (pcpu_stats) { 417 for (i = j = 0; i <= maxid; i++) { 418 if ((cpumask & (1ul << i)) == 0) 419 continue; 420 /* convert cp_time counts to percentages */ 421 percentages(CPUSTATES, &pcpu_cpu_states[j * CPUSTATES], 422 &pcpu_cp_time[j * CPUSTATES], 423 &pcpu_cp_old[j * CPUSTATES], 424 &pcpu_cp_diff[j * CPUSTATES]); 425 j++; 426 } 427 } else { 428 /* convert cp_time counts to percentages */ 429 percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff); 430 } 431 432 /* sum memory & swap statistics */ 433 { 434 static unsigned int swap_delay = 0; 435 static int swapavail = 0; 436 static int swapfree = 0; 437 static long bufspace = 0; 438 static int nspgsin, nspgsout; 439 440 GETSYSCTL("vfs.bufspace", bufspace); 441 GETSYSCTL("vm.stats.vm.v_active_count", memory_stats[0]); 442 GETSYSCTL("vm.stats.vm.v_inactive_count", memory_stats[1]); 443 GETSYSCTL("vm.stats.vm.v_wire_count", memory_stats[2]); 444 GETSYSCTL("vm.stats.vm.v_cache_count", memory_stats[3]); 445 GETSYSCTL("vm.stats.vm.v_free_count", memory_stats[5]); 446 GETSYSCTL("vm.stats.vm.v_swappgsin", nspgsin); 447 GETSYSCTL("vm.stats.vm.v_swappgsout", nspgsout); 448 /* convert memory stats to Kbytes */ 449 memory_stats[0] = pagetok(memory_stats[0]); 450 memory_stats[1] = pagetok(memory_stats[1]); 451 memory_stats[2] = pagetok(memory_stats[2]); 452 memory_stats[3] = pagetok(memory_stats[3]); 453 memory_stats[4] = bufspace / 1024; 454 memory_stats[5] = pagetok(memory_stats[5]); 455 memory_stats[6] = -1; 456 457 /* first interval */ 458 if (swappgsin < 0) { 459 swap_stats[4] = 0; 460 swap_stats[5] = 0; 461 } 462 463 /* compute differences between old and new swap statistic */ 464 else { 465 swap_stats[4] = pagetok(((nspgsin - swappgsin))); 466 swap_stats[5] = pagetok(((nspgsout - swappgsout))); 467 } 468 469 swappgsin = nspgsin; 470 swappgsout = nspgsout; 471 472 /* call CPU heavy swapmode() only for changes */ 473 if (swap_stats[4] > 0 || swap_stats[5] > 0 || swap_delay == 0) { 474 swap_stats[3] = swapmode(&swapavail, &swapfree); 475 swap_stats[0] = swapavail; 476 swap_stats[1] = swapavail - swapfree; 477 swap_stats[2] = swapfree; 478 } 479 swap_delay = 1; 480 swap_stats[6] = -1; 481 } 482 483 /* set arrays and strings */ 484 if (pcpu_stats) { 485 si->cpustates = pcpu_cpu_states; 486 si->ncpus = ncpus; 487 } else { 488 si->cpustates = cpu_states; 489 si->ncpus = 1; 490 } 491 si->memory = memory_stats; 492 si->swap = swap_stats; 493 494 495 if (lastpid > 0) { 496 si->last_pid = lastpid; 497 } else { 498 si->last_pid = -1; 499 } 500 501 /* 502 * Print how long system has been up. 503 * (Found by looking getting "boottime" from the kernel) 504 */ 505 mib[0] = CTL_KERN; 506 mib[1] = KERN_BOOTTIME; 507 bt_size = sizeof(boottime); 508 if (sysctl(mib, 2, &boottime, &bt_size, NULL, 0) != -1 && 509 boottime.tv_sec != 0) { 510 si->boottime = boottime; 511 } else { 512 si->boottime.tv_sec = -1; 513 } 514} 515 516#define NOPROC ((void *)-1) 517 518/* 519 * We need to compare data from the old process entry with the new 520 * process entry. 521 * To facilitate doing this quickly we stash a pointer in the kinfo_proc 522 * structure to cache the mapping. We also use a negative cache pointer 523 * of NOPROC to avoid duplicate lookups. 524 * XXX: this could be done when the actual processes are fetched, we do 525 * it here out of laziness. 526 */ 527const struct kinfo_proc * 528get_old_proc(struct kinfo_proc *pp) 529{ 530 struct kinfo_proc **oldpp, *oldp; 531 532 /* 533 * If this is the first fetch of the kinfo_procs then we don't have 534 * any previous entries. 535 */ 536 if (previous_proc_count == 0) 537 return (NULL); 538 /* negative cache? */ 539 if (pp->ki_udata == NOPROC) 540 return (NULL); 541 /* cached? */ 542 if (pp->ki_udata != NULL) 543 return (pp->ki_udata); 544 /* 545 * Not cached, 546 * 1) look up based on pid. 547 * 2) compare process start. 548 * If we fail here, then setup a negative cache entry, otherwise 549 * cache it. 550 */ 551 oldpp = bsearch(&pp, previous_pref, previous_proc_count, 552 sizeof(*previous_pref), compare_pid); 553 if (oldpp == NULL) { 554 pp->ki_udata = NOPROC; 555 return (NULL); 556 } 557 oldp = *oldpp; 558 if (bcmp(&oldp->ki_start, &pp->ki_start, sizeof(pp->ki_start)) != 0) { 559 pp->ki_udata = NOPROC; 560 return (NULL); 561 } 562 pp->ki_udata = oldp; 563 return (oldp); 564} 565 566/* 567 * Return the total amount of IO done in blocks in/out and faults. 568 * store the values individually in the pointers passed in. 569 */ 570long 571get_io_stats(struct kinfo_proc *pp, long *inp, long *oup, long *flp, 572 long *vcsw, long *ivcsw) 573{ 574 const struct kinfo_proc *oldp; 575 static struct kinfo_proc dummy; 576 long ret; 577 578 oldp = get_old_proc(pp); 579 if (oldp == NULL) { 580 bzero(&dummy, sizeof(dummy)); 581 oldp = &dummy; 582 } 583 *inp = RU(pp)->ru_inblock - RU(oldp)->ru_inblock; 584 *oup = RU(pp)->ru_oublock - RU(oldp)->ru_oublock; 585 *flp = RU(pp)->ru_majflt - RU(oldp)->ru_majflt; 586 *vcsw = RU(pp)->ru_nvcsw - RU(oldp)->ru_nvcsw; 587 *ivcsw = RU(pp)->ru_nivcsw - RU(oldp)->ru_nivcsw; 588 ret = 589 (RU(pp)->ru_inblock - RU(oldp)->ru_inblock) + 590 (RU(pp)->ru_oublock - RU(oldp)->ru_oublock) + 591 (RU(pp)->ru_majflt - RU(oldp)->ru_majflt); 592 return (ret); 593} 594 595/* 596 * Return the total number of block in/out and faults by a process. 597 */ 598long 599get_io_total(struct kinfo_proc *pp) 600{ 601 long dummy; 602 603 return (get_io_stats(pp, &dummy, &dummy, &dummy, &dummy, &dummy)); 604} 605 606static struct handle handle; 607 608caddr_t 609get_process_info(struct system_info *si, struct process_select *sel, 610 int (*compare)(const void *, const void *)) 611{ 612 int i; 613 int total_procs; 614 long p_io; 615 long p_inblock, p_oublock, p_majflt, p_vcsw, p_ivcsw; 616 int active_procs; 617 struct kinfo_proc **prefp; 618 struct kinfo_proc *pp; 619 struct kinfo_proc *prev_pp = NULL; 620 621 /* these are copied out of sel for speed */ 622 int show_idle; 623 int show_self; 624 int show_system; 625 int show_uid; 626 int show_command; 627 int show_kidle; 628 629 /* 630 * Save the previous process info. 631 */ 632 if (previous_proc_count_max < nproc) { 633 free(previous_procs); 634 previous_procs = malloc(nproc * sizeof(*previous_procs)); 635 free(previous_pref); 636 previous_pref = malloc(nproc * sizeof(*previous_pref)); 637 if (previous_procs == NULL || previous_pref == NULL) { 638 (void) fprintf(stderr, "top: Out of memory.\n"); 639 quit(23); 640 } 641 previous_proc_count_max = nproc; 642 } 643 if (nproc) { 644 for (i = 0; i < nproc; i++) 645 previous_pref[i] = &previous_procs[i]; 646 bcopy(pbase, previous_procs, nproc * sizeof(*previous_procs)); 647 qsort(previous_pref, nproc, sizeof(*previous_pref), 648 compare_pid); 649 } 650 previous_proc_count = nproc; 651 652 pbase = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc); 653 if (nproc > onproc) 654 pref = realloc(pref, sizeof(*pref) * (onproc = nproc)); 655 if (pref == NULL || pbase == NULL) { 656 (void) fprintf(stderr, "top: Out of memory.\n"); 657 quit(23); 658 } 659 /* get a pointer to the states summary array */ 660 si->procstates = process_states; 661 662 /* set up flags which define what we are going to select */ 663 show_idle = sel->idle; 664 show_self = sel->self == -1; 665 show_system = sel->system; 666 show_uid = sel->uid != -1; 667 show_command = sel->command != NULL; 668 show_kidle = sel->kidle; 669 670 /* count up process states and get pointers to interesting procs */ 671 total_procs = 0; 672 active_procs = 0; 673 total_inblock = 0; 674 total_oublock = 0; 675 total_majflt = 0; 676 memset((char *)process_states, 0, sizeof(process_states)); 677 prefp = pref; 678 for (pp = pbase, i = 0; i < nproc; pp++, i++) { 679 680 if (pp->ki_stat == 0) 681 /* not in use */ 682 continue; 683 684 if (!show_self && pp->ki_pid == sel->self) 685 /* skip self */ 686 continue; 687 688 if (!show_system && (pp->ki_flag & P_SYSTEM)) 689 /* skip system process */ 690 continue; 691 692 p_io = get_io_stats(pp, &p_inblock, &p_oublock, &p_majflt, 693 &p_vcsw, &p_ivcsw); 694 total_inblock += p_inblock; 695 total_oublock += p_oublock; 696 total_majflt += p_majflt; 697 total_procs++; 698 process_states[pp->ki_stat]++; 699 700 if (pp->ki_stat == SZOMB) 701 /* skip zombies */ 702 continue; 703 704 if (!show_kidle && pp->ki_tdflags & TDF_IDLETD) 705 /* skip kernel idle process */ 706 continue; 707 708 if (displaymode == DISP_CPU && !show_idle && 709 (pp->ki_pctcpu == 0 || 710 pp->ki_stat == SSTOP || pp->ki_stat == SIDL)) 711 /* skip idle or non-running processes */ 712 continue; 713 714 if (displaymode == DISP_IO && !show_idle && p_io == 0) 715 /* skip processes that aren't doing I/O */ 716 continue; 717 718 if (show_uid && pp->ki_ruid != (uid_t)sel->uid) 719 /* skip proc. that don't belong to the selected UID */ 720 continue; 721 722 /* 723 * When not showing threads, take the first thread 724 * for output and add the fields that we can from 725 * the rest of the process's threads rather than 726 * using the system's mostly-broken KERN_PROC_PROC. 727 */ 728 if (sel->thread || prev_pp == NULL || 729 prev_pp->ki_pid != pp->ki_pid) { 730 *prefp++ = pp; 731 active_procs++; 732 prev_pp = pp; 733 } else { 734 prev_pp->ki_pctcpu += pp->ki_pctcpu; 735 prev_pp->ki_runtime += pp->ki_runtime; 736 } 737 } 738 739 /* if requested, sort the "interesting" processes */ 740 if (compare != NULL) 741 qsort(pref, active_procs, sizeof(*pref), compare); 742 743 /* remember active and total counts */ 744 si->p_total = total_procs; 745 si->p_active = pref_len = active_procs; 746 747 /* pass back a handle */ 748 handle.next_proc = pref; 749 handle.remaining = active_procs; 750 return ((caddr_t)&handle); 751} 752 753static char fmt[128]; /* static area where result is built */ 754 755char * 756format_next_process(caddr_t handle, char *(*get_userid)(int), int flags) 757{ 758 struct kinfo_proc *pp; 759 const struct kinfo_proc *oldp; 760 long cputime; 761 double pct; 762 struct handle *hp; 763 char status[16]; 764 int state; 765 struct rusage ru, *rup; 766 long p_tot, s_tot; 767 char *proc_fmt, thr_buf[6], jid_buf[6]; 768 char *cmdbuf = NULL; 769 char **args; 770 771 /* find and remember the next proc structure */ 772 hp = (struct handle *)handle; 773 pp = *(hp->next_proc++); 774 hp->remaining--; 775 776 /* get the process's command name */ 777 if ((pp->ki_flag & P_INMEM) == 0) { 778 /* 779 * Print swapped processes as <pname> 780 */ 781 size_t len; 782 783 len = strlen(pp->ki_comm); 784 if (len > sizeof(pp->ki_comm) - 3) 785 len = sizeof(pp->ki_comm) - 3; 786 memmove(pp->ki_comm + 1, pp->ki_comm, len); 787 pp->ki_comm[0] = '<'; 788 pp->ki_comm[len + 1] = '>'; 789 pp->ki_comm[len + 2] = '\0'; 790 } 791 792 /* 793 * Convert the process's runtime from microseconds to seconds. This 794 * time includes the interrupt time although that is not wanted here. 795 * ps(1) is similarly sloppy. 796 */ 797 cputime = (pp->ki_runtime + 500000) / 1000000; 798 799 /* calculate the base for cpu percentages */ 800 pct = pctdouble(pp->ki_pctcpu); 801 802 /* generate "STATE" field */ 803 switch (state = pp->ki_stat) { 804 case SRUN: 805 if (smpmode && pp->ki_oncpu != 0xff) 806 sprintf(status, "CPU%d", pp->ki_oncpu); 807 else 808 strcpy(status, "RUN"); 809 break; 810 case SLOCK: 811 if (pp->ki_kiflag & KI_LOCKBLOCK) { 812 sprintf(status, "*%.6s", pp->ki_lockname); 813 break; 814 } 815 /* fall through */ 816 case SSLEEP: 817 if (pp->ki_wmesg != NULL) { 818 sprintf(status, "%.6s", pp->ki_wmesg); 819 break; 820 } 821 /* FALLTHROUGH */ 822 default: 823 824 if (state >= 0 && 825 state < sizeof(state_abbrev) / sizeof(*state_abbrev)) 826 sprintf(status, "%.6s", state_abbrev[state]); 827 else 828 sprintf(status, "?%5d", state); 829 break; 830 } 831 832 cmdbuf = (char *)malloc(cmdlengthdelta + 1); 833 if (cmdbuf == NULL) { 834 warn("malloc(%d)", cmdlengthdelta + 1); 835 return NULL; 836 } 837 838 if (!(flags & FMT_SHOWARGS)) { 839 if (ps.thread && pp->ki_flag & P_HADTHREADS && 840 pp->ki_ocomm[0]) { 841 snprintf(cmdbuf, cmdlengthdelta, "{%s}", pp->ki_ocomm); 842 } else { 843 snprintf(cmdbuf, cmdlengthdelta, "%s", pp->ki_comm); 844 } 845 } else { 846 if (pp->ki_flag & P_SYSTEM || 847 pp->ki_args == NULL || 848 (args = kvm_getargv(kd, pp, cmdlengthdelta)) == NULL || 849 !(*args)) { 850 if (ps.thread && pp->ki_flag & P_HADTHREADS && 851 pp->ki_ocomm[0]) { 852 snprintf(cmdbuf, cmdlengthdelta, 853 "{%s}", pp->ki_ocomm); 854 } else { 855 snprintf(cmdbuf, cmdlengthdelta, 856 "[%s]", pp->ki_comm); 857 } 858 } else { 859 char *src, *dst, *argbuf; 860 char *cmd; 861 size_t argbuflen; 862 size_t len; 863 864 argbuflen = cmdlengthdelta * 4; 865 argbuf = (char *)malloc(argbuflen + 1); 866 if (argbuf == NULL) { 867 warn("malloc(%d)", argbuflen + 1); 868 free(cmdbuf); 869 return NULL; 870 } 871 872 dst = argbuf; 873 874 /* Extract cmd name from argv */ 875 cmd = strrchr(*args, '/'); 876 if (cmd == NULL) 877 cmd = *args; 878 else 879 cmd++; 880 881 for (; (src = *args++) != NULL; ) { 882 if (*src == '\0') 883 continue; 884 len = (argbuflen - (dst - argbuf) - 1) / 4; 885 strvisx(dst, src, 886 strlen(src) < len ? strlen(src) : len, 887 VIS_NL | VIS_CSTYLE); 888 while (*dst != '\0') 889 dst++; 890 if ((argbuflen - (dst - argbuf) - 1) / 4 > 0) 891 *dst++ = ' '; /* add delimiting space */ 892 } 893 if (dst != argbuf && dst[-1] == ' ') 894 dst--; 895 *dst = '\0'; 896 897 if (strcmp(cmd, pp->ki_comm) != 0 ) 898 snprintf(cmdbuf, cmdlengthdelta, 899 "%s (%s)",argbuf, pp->ki_comm); 900 else 901 strlcpy(cmdbuf, argbuf, cmdlengthdelta); 902 903 free(argbuf); 904 } 905 } 906 907 if (ps.jail == 0) 908 jid_buf[0] = '\0'; 909 else 910 snprintf(jid_buf, sizeof(jid_buf), " %*d", 911 sizeof(jid_buf) - 3, pp->ki_jid); 912 913 if (displaymode == DISP_IO) { 914 oldp = get_old_proc(pp); 915 if (oldp != NULL) { 916 ru.ru_inblock = RU(pp)->ru_inblock - 917 RU(oldp)->ru_inblock; 918 ru.ru_oublock = RU(pp)->ru_oublock - 919 RU(oldp)->ru_oublock; 920 ru.ru_majflt = RU(pp)->ru_majflt - RU(oldp)->ru_majflt; 921 ru.ru_nvcsw = RU(pp)->ru_nvcsw - RU(oldp)->ru_nvcsw; 922 ru.ru_nivcsw = RU(pp)->ru_nivcsw - RU(oldp)->ru_nivcsw; 923 rup = &ru; 924 } else { 925 rup = RU(pp); 926 } 927 p_tot = rup->ru_inblock + rup->ru_oublock + rup->ru_majflt; 928 s_tot = total_inblock + total_oublock + total_majflt; 929 930 sprintf(fmt, io_Proc_format, 931 pp->ki_pid, 932 jid_buf, 933 namelength, namelength, (*get_userid)(pp->ki_ruid), 934 rup->ru_nvcsw, 935 rup->ru_nivcsw, 936 rup->ru_inblock, 937 rup->ru_oublock, 938 rup->ru_majflt, 939 p_tot, 940 s_tot == 0 ? 0.0 : (p_tot * 100.0 / s_tot), 941 screen_width > cmdlengthdelta ? 942 screen_width - cmdlengthdelta : 0, 943 printable(cmdbuf)); 944 945 free(cmdbuf); 946 947 return (fmt); 948 } 949 950 /* format this entry */ 951 proc_fmt = smpmode ? smp_Proc_format : up_Proc_format; 952 if (ps.thread != 0) 953 thr_buf[0] = '\0'; 954 else 955 snprintf(thr_buf, sizeof(thr_buf), "%*d ", 956 sizeof(thr_buf) - 2, pp->ki_numthreads); 957 958 sprintf(fmt, proc_fmt, 959 pp->ki_pid, 960 jid_buf, 961 namelength, namelength, (*get_userid)(pp->ki_ruid), 962 thr_buf, 963 pp->ki_pri.pri_level - PZERO, 964 format_nice(pp), 965 format_k2(PROCSIZE(pp)), 966 format_k2(pagetok(pp->ki_rssize)), 967 status, 968 smpmode ? pp->ki_lastcpu : 0, 969 format_time(cputime), 970 ps.wcpu ? 100.0 * weighted_cpu(pct, pp) : 100.0 * pct, 971 screen_width > cmdlengthdelta ? screen_width - cmdlengthdelta : 0, 972 printable(cmdbuf)); 973 974 free(cmdbuf); 975 976 /* return the result */ 977 return (fmt); 978} 979 980static void 981getsysctl(const char *name, void *ptr, size_t len) 982{ 983 size_t nlen = len; 984 985 if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) { 986 fprintf(stderr, "top: sysctl(%s...) failed: %s\n", name, 987 strerror(errno)); 988 quit(23); 989 } 990 if (nlen != len) { 991 fprintf(stderr, "top: sysctl(%s...) expected %lu, got %lu\n", 992 name, (unsigned long)len, (unsigned long)nlen); 993 quit(23); 994 } 995} 996 997static const char * 998format_nice(const struct kinfo_proc *pp) 999{ 1000 const char *fifo, *kthread; 1001 int rtpri; 1002 static char nicebuf[4 + 1]; 1003 1004 fifo = PRI_NEED_RR(pp->ki_pri.pri_class) ? "" : "F"; 1005 kthread = (pp->ki_flag & P_KTHREAD) ? "k" : ""; 1006 switch (PRI_BASE(pp->ki_pri.pri_class)) { 1007 case PRI_ITHD: 1008 return ("-"); 1009 case PRI_REALTIME: 1010 /* 1011 * XXX: the kernel doesn't tell us the original rtprio and 1012 * doesn't really know what it was, so to recover it we 1013 * must be more chummy with the implementation than the 1014 * implementation is with itself. pri_user gives a 1015 * constant "base" priority, but is only initialized 1016 * properly for user threads. pri_native gives what the 1017 * kernel calls the "base" priority, but it isn't constant 1018 * since it is changed by priority propagation. pri_native 1019 * also isn't properly initialized for all threads, but it 1020 * is properly initialized for kernel realtime and idletime 1021 * threads. Thus we use pri_user for the base priority of 1022 * user threads (it is always correct) and pri_native for 1023 * the base priority of kernel realtime and idletime threads 1024 * (there is nothing better, and it is usually correct). 1025 * 1026 * The field width and thus the buffer are too small for 1027 * values like "kr31F", but such values shouldn't occur, 1028 * and if they do then the tailing "F" is not displayed. 1029 */ 1030 rtpri = ((pp->ki_flag & P_KTHREAD) ? pp->ki_pri.pri_native : 1031 pp->ki_pri.pri_user) - PRI_MIN_REALTIME; 1032 snprintf(nicebuf, sizeof(nicebuf), "%sr%d%s", 1033 kthread, rtpri, fifo); 1034 break; 1035 case PRI_TIMESHARE: 1036 if (pp->ki_flag & P_KTHREAD) 1037 return ("-"); 1038 snprintf(nicebuf, sizeof(nicebuf), "%d", pp->ki_nice - NZERO); 1039 break; 1040 case PRI_IDLE: 1041 /* XXX: as above. */ 1042 rtpri = ((pp->ki_flag & P_KTHREAD) ? pp->ki_pri.pri_native : 1043 pp->ki_pri.pri_user) - PRI_MIN_IDLE; 1044 snprintf(nicebuf, sizeof(nicebuf), "%si%d%s", 1045 kthread, rtpri, fifo); 1046 break; 1047 default: 1048 return ("?"); 1049 } 1050 return (nicebuf); 1051} 1052 1053/* comparison routines for qsort */ 1054 1055static int 1056compare_pid(const void *p1, const void *p2) 1057{ 1058 const struct kinfo_proc * const *pp1 = p1; 1059 const struct kinfo_proc * const *pp2 = p2; 1060 1061 if ((*pp2)->ki_pid < 0 || (*pp1)->ki_pid < 0) 1062 abort(); 1063 1064 return ((*pp1)->ki_pid - (*pp2)->ki_pid); 1065} 1066 1067/* 1068 * proc_compare - comparison function for "qsort" 1069 * Compares the resource consumption of two processes using five 1070 * distinct keys. The keys (in descending order of importance) are: 1071 * percent cpu, cpu ticks, state, resident set size, total virtual 1072 * memory usage. The process states are ordered as follows (from least 1073 * to most important): WAIT, zombie, sleep, stop, start, run. The 1074 * array declaration below maps a process state index into a number 1075 * that reflects this ordering. 1076 */ 1077 1078static int sorted_state[] = { 1079 0, /* not used */ 1080 3, /* sleep */ 1081 1, /* ABANDONED (WAIT) */ 1082 6, /* run */ 1083 5, /* start */ 1084 2, /* zombie */ 1085 4 /* stop */ 1086}; 1087 1088 1089#define ORDERKEY_PCTCPU(a, b) do { \ 1090 long diff; \ 1091 if (ps.wcpu) \ 1092 diff = floor(1.0E6 * weighted_cpu(pctdouble((b)->ki_pctcpu), \ 1093 (b))) - \ 1094 floor(1.0E6 * weighted_cpu(pctdouble((a)->ki_pctcpu), \ 1095 (a))); \ 1096 else \ 1097 diff = (long)(b)->ki_pctcpu - (long)(a)->ki_pctcpu; \ 1098 if (diff != 0) \ 1099 return (diff > 0 ? 1 : -1); \ 1100} while (0) 1101 1102#define ORDERKEY_CPTICKS(a, b) do { \ 1103 int64_t diff = (int64_t)(b)->ki_runtime - (int64_t)(a)->ki_runtime; \ 1104 if (diff != 0) \ 1105 return (diff > 0 ? 1 : -1); \ 1106} while (0) 1107 1108#define ORDERKEY_STATE(a, b) do { \ 1109 int diff = sorted_state[(b)->ki_stat] - sorted_state[(a)->ki_stat]; \ 1110 if (diff != 0) \ 1111 return (diff > 0 ? 1 : -1); \ 1112} while (0) 1113 1114#define ORDERKEY_PRIO(a, b) do { \ 1115 int diff = (int)(b)->ki_pri.pri_level - (int)(a)->ki_pri.pri_level; \ 1116 if (diff != 0) \ 1117 return (diff > 0 ? 1 : -1); \ 1118} while (0) 1119 1120#define ORDERKEY_THREADS(a, b) do { \ 1121 int diff = (int)(b)->ki_numthreads - (int)(a)->ki_numthreads; \ 1122 if (diff != 0) \ 1123 return (diff > 0 ? 1 : -1); \ 1124} while (0) 1125 1126#define ORDERKEY_RSSIZE(a, b) do { \ 1127 long diff = (long)(b)->ki_rssize - (long)(a)->ki_rssize; \ 1128 if (diff != 0) \ 1129 return (diff > 0 ? 1 : -1); \ 1130} while (0) 1131 1132#define ORDERKEY_MEM(a, b) do { \ 1133 long diff = (long)PROCSIZE((b)) - (long)PROCSIZE((a)); \ 1134 if (diff != 0) \ 1135 return (diff > 0 ? 1 : -1); \ 1136} while (0) 1137 1138#define ORDERKEY_JID(a, b) do { \ 1139 int diff = (int)(b)->ki_jid - (int)(a)->ki_jid; \ 1140 if (diff != 0) \ 1141 return (diff > 0 ? 1 : -1); \ 1142} while (0) 1143 1144/* compare_cpu - the comparison function for sorting by cpu percentage */ 1145 1146int 1147#ifdef ORDER 1148compare_cpu(void *arg1, void *arg2) 1149#else 1150proc_compare(void *arg1, void *arg2) 1151#endif 1152{ 1153 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1154 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1155 1156 ORDERKEY_PCTCPU(p1, p2); 1157 ORDERKEY_CPTICKS(p1, p2); 1158 ORDERKEY_STATE(p1, p2); 1159 ORDERKEY_PRIO(p1, p2); 1160 ORDERKEY_RSSIZE(p1, p2); 1161 ORDERKEY_MEM(p1, p2); 1162 1163 return (0); 1164} 1165 1166#ifdef ORDER 1167/* "cpu" compare routines */ 1168int compare_size(), compare_res(), compare_time(), compare_prio(), 1169 compare_threads(); 1170 1171/* 1172 * "io" compare routines. Context switches aren't i/o, but are displayed 1173 * on the "io" display. 1174 */ 1175int compare_iototal(), compare_ioread(), compare_iowrite(), compare_iofault(), 1176 compare_vcsw(), compare_ivcsw(); 1177 1178int (*compares[])() = { 1179 compare_cpu, 1180 compare_size, 1181 compare_res, 1182 compare_time, 1183 compare_prio, 1184 compare_threads, 1185 compare_iototal, 1186 compare_ioread, 1187 compare_iowrite, 1188 compare_iofault, 1189 compare_vcsw, 1190 compare_ivcsw, 1191 compare_jid, 1192 NULL 1193}; 1194 1195/* compare_size - the comparison function for sorting by total memory usage */ 1196 1197int 1198compare_size(void *arg1, void *arg2) 1199{ 1200 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1201 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1202 1203 ORDERKEY_MEM(p1, p2); 1204 ORDERKEY_RSSIZE(p1, p2); 1205 ORDERKEY_PCTCPU(p1, p2); 1206 ORDERKEY_CPTICKS(p1, p2); 1207 ORDERKEY_STATE(p1, p2); 1208 ORDERKEY_PRIO(p1, p2); 1209 1210 return (0); 1211} 1212 1213/* compare_res - the comparison function for sorting by resident set size */ 1214 1215int 1216compare_res(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_RSSIZE(p1, p2); 1222 ORDERKEY_MEM(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_time - the comparison function for sorting by total cpu time */ 1232 1233int 1234compare_time(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_CPTICKS(p1, p2); 1240 ORDERKEY_PCTCPU(p1, p2); 1241 ORDERKEY_STATE(p1, p2); 1242 ORDERKEY_PRIO(p1, p2); 1243 ORDERKEY_RSSIZE(p1, p2); 1244 ORDERKEY_MEM(p1, p2); 1245 1246 return (0); 1247} 1248 1249/* compare_prio - the comparison function for sorting by priority */ 1250 1251int 1252compare_prio(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_PRIO(p1, p2); 1258 ORDERKEY_CPTICKS(p1, p2); 1259 ORDERKEY_PCTCPU(p1, p2); 1260 ORDERKEY_STATE(p1, p2); 1261 ORDERKEY_RSSIZE(p1, p2); 1262 ORDERKEY_MEM(p1, p2); 1263 1264 return (0); 1265} 1266 1267/* compare_threads - the comparison function for sorting by threads */ 1268int 1269compare_threads(void *arg1, void *arg2) 1270{ 1271 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1272 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1273 1274 ORDERKEY_THREADS(p1, p2); 1275 ORDERKEY_PCTCPU(p1, p2); 1276 ORDERKEY_CPTICKS(p1, p2); 1277 ORDERKEY_STATE(p1, p2); 1278 ORDERKEY_PRIO(p1, p2); 1279 ORDERKEY_RSSIZE(p1, p2); 1280 ORDERKEY_MEM(p1, p2); 1281 1282 return (0); 1283} 1284 1285/* compare_jid - the comparison function for sorting by jid */ 1286static int 1287compare_jid(const void *arg1, const void *arg2) 1288{ 1289 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1290 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1291 1292 ORDERKEY_JID(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#endif /* ORDER */ 1303 1304/* assorted comparison functions for sorting by i/o */ 1305 1306int 1307#ifdef ORDER 1308compare_iototal(void *arg1, void *arg2) 1309#else 1310io_compare(void *arg1, void *arg2) 1311#endif 1312{ 1313 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1314 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1315 1316 return (get_io_total(p2) - get_io_total(p1)); 1317} 1318 1319#ifdef ORDER 1320int 1321compare_ioread(void *arg1, void *arg2) 1322{ 1323 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1324 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1325 long dummy, inp1, inp2; 1326 1327 (void) get_io_stats(p1, &inp1, &dummy, &dummy, &dummy, &dummy); 1328 (void) get_io_stats(p2, &inp2, &dummy, &dummy, &dummy, &dummy); 1329 1330 return (inp2 - inp1); 1331} 1332 1333int 1334compare_iowrite(void *arg1, void *arg2) 1335{ 1336 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1337 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1338 long dummy, oup1, oup2; 1339 1340 (void) get_io_stats(p1, &dummy, &oup1, &dummy, &dummy, &dummy); 1341 (void) get_io_stats(p2, &dummy, &oup2, &dummy, &dummy, &dummy); 1342 1343 return (oup2 - oup1); 1344} 1345 1346int 1347compare_iofault(void *arg1, void *arg2) 1348{ 1349 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1350 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1351 long dummy, flp1, flp2; 1352 1353 (void) get_io_stats(p1, &dummy, &dummy, &flp1, &dummy, &dummy); 1354 (void) get_io_stats(p2, &dummy, &dummy, &flp2, &dummy, &dummy); 1355 1356 return (flp2 - flp1); 1357} 1358 1359int 1360compare_vcsw(void *arg1, void *arg2) 1361{ 1362 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1363 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1364 long dummy, flp1, flp2; 1365 1366 (void) get_io_stats(p1, &dummy, &dummy, &dummy, &flp1, &dummy); 1367 (void) get_io_stats(p2, &dummy, &dummy, &dummy, &flp2, &dummy); 1368 1369 return (flp2 - flp1); 1370} 1371 1372int 1373compare_ivcsw(void *arg1, void *arg2) 1374{ 1375 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1376 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1377 long dummy, flp1, flp2; 1378 1379 (void) get_io_stats(p1, &dummy, &dummy, &dummy, &dummy, &flp1); 1380 (void) get_io_stats(p2, &dummy, &dummy, &dummy, &dummy, &flp2); 1381 1382 return (flp2 - flp1); 1383} 1384#endif /* ORDER */ 1385 1386/* 1387 * proc_owner(pid) - returns the uid that owns process "pid", or -1 if 1388 * the process does not exist. 1389 * It is EXTREMLY IMPORTANT that this function work correctly. 1390 * If top runs setuid root (as in SVR4), then this function 1391 * is the only thing that stands in the way of a serious 1392 * security problem. It validates requests for the "kill" 1393 * and "renice" commands. 1394 */ 1395 1396int 1397proc_owner(int pid) 1398{ 1399 int cnt; 1400 struct kinfo_proc **prefp; 1401 struct kinfo_proc *pp; 1402 1403 prefp = pref; 1404 cnt = pref_len; 1405 while (--cnt >= 0) { 1406 pp = *prefp++; 1407 if (pp->ki_pid == (pid_t)pid) 1408 return ((int)pp->ki_ruid); 1409 } 1410 return (-1); 1411} 1412 1413static int 1414swapmode(int *retavail, int *retfree) 1415{ 1416 int n; 1417 int pagesize = getpagesize(); 1418 struct kvm_swap swapary[1]; 1419 1420 *retavail = 0; 1421 *retfree = 0; 1422 1423#define CONVERT(v) ((quad_t)(v) * pagesize / 1024) 1424 1425 n = kvm_getswapinfo(kd, swapary, 1, 0); 1426 if (n < 0 || swapary[0].ksw_total == 0) 1427 return (0); 1428 1429 *retavail = CONVERT(swapary[0].ksw_total); 1430 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used); 1431 1432 n = (int)(swapary[0].ksw_used * 100.0 / swapary[0].ksw_total); 1433 return (n); 1434} 1435