1 /* 2 * top - a top users display for Unix 3 * 4 * SYNOPSIS: For DragonFly 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 DragonFly 2.5.1 13 * Should work for: 14 * DragonFly 2.x and above 15 * 16 * LIBS: -lkvm 17 * 18 * AUTHOR: Jan Lentfer <Jan.Lentfer@web.de> 19 * This module has been put together from different sources and is based on the 20 * work of many other people, e.g. Matthew Dillon, Simon Schubert, Jordan Gordeev. 21 * 22 * $FreeBSD: src/usr.bin/top/machine.c,v 1.29.2.2 2001/07/31 20:27:05 tmm Exp $ 23 */ 24 25 #include <sys/user.h> 26 #include <sys/types.h> 27 #include <sys/time.h> 28 #include <sys/signal.h> 29 #include <sys/param.h> 30 31 #include "os.h" 32 #include <err.h> 33 #include <kvm.h> 34 #include <stdio.h> 35 #include <unistd.h> 36 #include <math.h> 37 #include <pwd.h> 38 #include <sys/errno.h> 39 #include <sys/sysctl.h> 40 #include <sys/file.h> 41 #include <sys/vmmeter.h> 42 #include <sys/resource.h> 43 #include <sys/rtprio.h> 44 45 /* Swap */ 46 #include <stdlib.h> 47 #include <sys/conf.h> 48 49 #include <osreldate.h> /* for changes in kernel structures */ 50 51 #include <sys/kinfo.h> 52 #include <kinfo.h> 53 #include "top.h" 54 #include "display.h" 55 #include "machine.h" 56 #include "screen.h" 57 #include "utils.h" 58 59 int swapmode(int *retavail, int *retfree); 60 static int namelength; 61 static int cmdlength; 62 static int show_fullcmd; 63 64 int n_cpus = 0; 65 66 /* get_process_info passes back a handle. This is what it looks like: */ 67 68 struct handle { 69 struct kinfo_proc **next_proc; /* points to next valid proc pointer */ 70 int remaining; /* number of pointers remaining */ 71 }; 72 73 /* declarations for load_avg */ 74 #include "loadavg.h" 75 76 #define PP(pp, field) ((pp)->kp_ ## field) 77 #define LP(pp, field) ((pp)->kp_lwp.kl_ ## field) 78 #define VP(pp, field) ((pp)->kp_vm_ ## field) 79 80 /* what we consider to be process size: */ 81 #define PROCSIZE(pp) (VP((pp), map_size) / 1024) 82 83 /* 84 * These definitions control the format of the per-process area 85 */ 86 87 static char smp_header[] = 88 " PID %-*.*s NICE SIZE RES STATE CPU TIME CTIME CPU COMMAND"; 89 90 #define smp_Proc_format \ 91 "%6d %-*.*s %3d%7s %6s %8.8s %2d %6s %7s %5.2f%% %.*s" 92 93 /* process state names for the "STATE" column of the display */ 94 /* 95 * the extra nulls in the string "run" are for adding a slash and the 96 * processor number when needed 97 */ 98 99 const char *state_abbrev[] = { 100 "", "RUN\0\0\0", "STOP", "SLEEP", 101 }; 102 103 104 static kvm_t *kd; 105 106 /* values that we stash away in _init and use in later routines */ 107 108 static long lastpid; 109 110 /* these are for calculating cpu state percentages */ 111 112 static struct kinfo_cputime *cp_time, *cp_old; 113 114 /* these are for detailing the process states */ 115 116 #define MAXPSTATES 6 117 118 int process_states[MAXPSTATES]; 119 120 char *procstatenames[] = { 121 " running, ", " idle, ", " active, ", " stopped, ", " zombie, ", 122 NULL 123 }; 124 125 /* these are for detailing the cpu states */ 126 #define CPU_STATES 5 127 int *cpu_states; 128 int* cpu_averages; 129 char *cpustatenames[CPU_STATES + 1] = { 130 "user", "nice", "system", "interrupt", "idle", NULL 131 }; 132 133 /* these are for detailing the memory statistics */ 134 135 long memory_stats[7]; 136 char *memorynames[] = { 137 "K Active, ", "K Inact, ", "K Wired, ", "K Cache, ", "K Buf, ", "K Free", 138 NULL 139 }; 140 141 long swap_stats[7]; 142 char *swapnames[] = { 143 /* 0 1 2 3 4 5 */ 144 "K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out", 145 NULL 146 }; 147 148 149 /* these are for keeping track of the proc array */ 150 151 static int nproc; 152 static int onproc = -1; 153 static int pref_len; 154 static struct kinfo_proc *pbase; 155 static struct kinfo_proc **pref; 156 157 /* these are for getting the memory statistics */ 158 159 static int pageshift; /* log base 2 of the pagesize */ 160 161 /* define pagetok in terms of pageshift */ 162 163 #define pagetok(size) ((size) << pageshift) 164 165 /* sorting orders. first is default */ 166 char *ordernames[] = { 167 "cpu", "size", "res", "time", "pri", "thr", "pid", "ctime", "pres", NULL 168 }; 169 170 /* compare routines */ 171 int proc_compare (struct kinfo_proc **, struct kinfo_proc **); 172 int compare_size (struct kinfo_proc **, struct kinfo_proc **); 173 int compare_res (struct kinfo_proc **, struct kinfo_proc **); 174 int compare_time (struct kinfo_proc **, struct kinfo_proc **); 175 int compare_ctime (struct kinfo_proc **, struct kinfo_proc **); 176 int compare_prio(struct kinfo_proc **, struct kinfo_proc **); 177 int compare_thr (struct kinfo_proc **, struct kinfo_proc **); 178 int compare_pid (struct kinfo_proc **, struct kinfo_proc **); 179 int compare_pres(struct kinfo_proc **, struct kinfo_proc **); 180 181 int (*proc_compares[]) (struct kinfo_proc **,struct kinfo_proc **) = { 182 proc_compare, 183 compare_size, 184 compare_res, 185 compare_time, 186 compare_prio, 187 compare_thr, 188 compare_pid, 189 compare_ctime, 190 compare_pres, 191 NULL 192 }; 193 194 static void 195 cputime_percentages(int out[CPU_STATES], struct kinfo_cputime *new, 196 struct kinfo_cputime *old) 197 { 198 struct kinfo_cputime diffs; 199 uint64_t total_change, half_total; 200 201 /* initialization */ 202 total_change = 0; 203 204 diffs.cp_user = new->cp_user - old->cp_user; 205 diffs.cp_nice = new->cp_nice - old->cp_nice; 206 diffs.cp_sys = new->cp_sys - old->cp_sys; 207 diffs.cp_intr = new->cp_intr - old->cp_intr; 208 diffs.cp_idle = new->cp_idle - old->cp_idle; 209 total_change = diffs.cp_user + diffs.cp_nice + diffs.cp_sys + 210 diffs.cp_intr + diffs.cp_idle; 211 old->cp_user = new->cp_user; 212 old->cp_nice = new->cp_nice; 213 old->cp_sys = new->cp_sys; 214 old->cp_intr = new->cp_intr; 215 old->cp_idle = new->cp_idle; 216 217 /* avoid divide by zero potential */ 218 if (total_change == 0) 219 total_change = 1; 220 221 /* calculate percentages based on overall change, rounding up */ 222 half_total = total_change >> 1; 223 224 out[0] = ((diffs.cp_user * 1000LL + half_total) / total_change); 225 out[1] = ((diffs.cp_nice * 1000LL + half_total) / total_change); 226 out[2] = ((diffs.cp_sys * 1000LL + half_total) / total_change); 227 out[3] = ((diffs.cp_intr * 1000LL + half_total) / total_change); 228 out[4] = ((diffs.cp_idle * 1000LL + half_total) / total_change); 229 } 230 231 int 232 machine_init(struct statics *statics) 233 { 234 int pagesize; 235 size_t modelen; 236 struct passwd *pw; 237 struct timeval boottime; 238 239 if (n_cpus < 1) { 240 if (kinfo_get_cpus(&n_cpus)) 241 err(1, "kinfo_get_cpus failed"); 242 } 243 /* get boot time */ 244 modelen = sizeof(boottime); 245 if (sysctlbyname("kern.boottime", &boottime, &modelen, NULL, 0) == -1) { 246 /* we have no boottime to report */ 247 boottime.tv_sec = -1; 248 } 249 250 while ((pw = getpwent()) != NULL) { 251 if ((int)strlen(pw->pw_name) > namelength) 252 namelength = strlen(pw->pw_name); 253 } 254 if (namelength < 8) 255 namelength = 8; 256 if (namelength > 13) 257 namelength = 13; 258 259 if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL)) == NULL) 260 return -1; 261 262 pbase = NULL; 263 pref = NULL; 264 nproc = 0; 265 onproc = -1; 266 /* 267 * get the page size with "getpagesize" and calculate pageshift from 268 * it 269 */ 270 pagesize = getpagesize(); 271 pageshift = 0; 272 while (pagesize > 1) { 273 pageshift++; 274 pagesize >>= 1; 275 } 276 277 /* we only need the amount of log(2)1024 for our conversion */ 278 pageshift -= LOG1024; 279 280 /* fill in the statics information */ 281 statics->procstate_names = procstatenames; 282 statics->cpustate_names = cpustatenames; 283 statics->memory_names = memorynames; 284 statics->boottime = boottime.tv_sec; 285 statics->swap_names = swapnames; 286 statics->order_names = ordernames; 287 /* we need kvm descriptor in order to show full commands */ 288 statics->flags.fullcmds = kd != NULL; 289 290 /* all done! */ 291 return (0); 292 } 293 294 char * 295 format_header(char *uname_field) 296 { 297 static char Header[128]; 298 299 snprintf(Header, sizeof(Header), smp_header, 300 namelength, namelength, uname_field); 301 302 if (screen_width <= 79) 303 cmdlength = 80; 304 else 305 cmdlength = screen_width; 306 307 cmdlength = cmdlength - strlen(Header) + 6; 308 309 return Header; 310 } 311 312 static int swappgsin = -1; 313 static int swappgsout = -1; 314 extern struct timeval timeout; 315 316 void 317 get_system_info(struct system_info *si) 318 { 319 size_t len; 320 int cpu; 321 322 if (cpu_states == NULL) { 323 cpu_states = malloc(sizeof(*cpu_states) * CPU_STATES * n_cpus); 324 if (cpu_states == NULL) 325 err(1, "malloc"); 326 bzero(cpu_states, sizeof(*cpu_states) * CPU_STATES * n_cpus); 327 } 328 if (cp_time == NULL) { 329 cp_time = malloc(2 * n_cpus * sizeof(cp_time[0])); 330 if (cp_time == NULL) 331 err(1, "cp_time"); 332 cp_old = cp_time + n_cpus; 333 len = n_cpus * sizeof(cp_old[0]); 334 bzero(cp_time, len); 335 if (sysctlbyname("kern.cputime", cp_old, &len, NULL, 0)) 336 err(1, "kern.cputime"); 337 } 338 len = n_cpus * sizeof(cp_time[0]); 339 bzero(cp_time, len); 340 if (sysctlbyname("kern.cputime", cp_time, &len, NULL, 0)) 341 err(1, "kern.cputime"); 342 343 getloadavg(si->load_avg, 3); 344 345 lastpid = 0; 346 347 /* convert cp_time counts to percentages */ 348 int combine_cpus = (enable_ncpus == 0 && n_cpus > 1); 349 for (cpu = 0; cpu < n_cpus; ++cpu) { 350 cputime_percentages(cpu_states + cpu * CPU_STATES, 351 &cp_time[cpu], &cp_old[cpu]); 352 } 353 if (combine_cpus) { 354 if (cpu_averages == NULL) { 355 cpu_averages = malloc(sizeof(*cpu_averages) * CPU_STATES); 356 if (cpu_averages == NULL) 357 err(1, "cpu_averages"); 358 } 359 bzero(cpu_averages, sizeof(*cpu_averages) * CPU_STATES); 360 for (cpu = 0; cpu < n_cpus; ++cpu) { 361 int j = 0; 362 cpu_averages[0] += *(cpu_states + ((cpu * CPU_STATES) + j++) ); 363 cpu_averages[1] += *(cpu_states + ((cpu * CPU_STATES) + j++) ); 364 cpu_averages[2] += *(cpu_states + ((cpu * CPU_STATES) + j++) ); 365 cpu_averages[3] += *(cpu_states + ((cpu * CPU_STATES) + j++) ); 366 cpu_averages[4] += *(cpu_states + ((cpu * CPU_STATES) + j++) ); 367 } 368 for (int i = 0; i < CPU_STATES; ++i) 369 cpu_averages[i] /= n_cpus; 370 } 371 372 /* sum memory & swap statistics */ 373 { 374 struct vmmeter vmm; 375 struct vmstats vms; 376 size_t vms_size = sizeof(vms); 377 size_t vmm_size = sizeof(vmm); 378 static unsigned int swap_delay = 0; 379 static int swapavail = 0; 380 static int swapfree = 0; 381 static long bufspace = 0; 382 383 if (sysctlbyname("vm.vmstats", &vms, &vms_size, NULL, 0)) 384 err(1, "sysctlbyname: vm.vmstats"); 385 386 if (sysctlbyname("vm.vmmeter", &vmm, &vmm_size, NULL, 0)) 387 err(1, "sysctlbyname: vm.vmmeter"); 388 389 if (kinfo_get_vfs_bufspace(&bufspace)) 390 err(1, "kinfo_get_vfs_bufspace"); 391 392 /* convert memory stats to Kbytes */ 393 memory_stats[0] = pagetok(vms.v_active_count); 394 memory_stats[1] = pagetok(vms.v_inactive_count); 395 memory_stats[2] = pagetok(vms.v_wire_count); 396 memory_stats[3] = pagetok(vms.v_cache_count); 397 memory_stats[4] = bufspace / 1024; 398 memory_stats[5] = pagetok(vms.v_free_count); 399 memory_stats[6] = -1; 400 401 /* first interval */ 402 if (swappgsin < 0) { 403 swap_stats[4] = 0; 404 swap_stats[5] = 0; 405 } 406 /* compute differences between old and new swap statistic */ 407 else { 408 swap_stats[4] = pagetok(((vmm.v_swappgsin - swappgsin))); 409 swap_stats[5] = pagetok(((vmm.v_swappgsout - swappgsout))); 410 } 411 412 swappgsin = vmm.v_swappgsin; 413 swappgsout = vmm.v_swappgsout; 414 415 /* call CPU heavy swapmode() only for changes */ 416 if (swap_stats[4] > 0 || swap_stats[5] > 0 || swap_delay == 0) { 417 swap_stats[3] = swapmode(&swapavail, &swapfree); 418 swap_stats[0] = swapavail; 419 swap_stats[1] = swapavail - swapfree; 420 swap_stats[2] = swapfree; 421 } 422 swap_delay = 1; 423 swap_stats[6] = -1; 424 } 425 426 /* set arrays and strings */ 427 si->cpustates = combine_cpus == 1 ? 428 cpu_averages : cpu_states; 429 si->memory = memory_stats; 430 si->swap = swap_stats; 431 432 433 if (lastpid > 0) { 434 si->last_pid = lastpid; 435 } else { 436 si->last_pid = -1; 437 } 438 } 439 440 441 static struct handle handle; 442 443 caddr_t 444 get_process_info(struct system_info *si, struct process_select *sel, 445 int compare_index) 446 { 447 int i; 448 int total_procs; 449 int active_procs; 450 struct kinfo_proc **prefp; 451 struct kinfo_proc *pp; 452 453 /* these are copied out of sel for speed */ 454 int show_idle; 455 int show_system; 456 int show_uid; 457 int show_threads; 458 459 show_threads = sel->threads; 460 461 462 pbase = kvm_getprocs(kd, 463 KERN_PROC_ALL | (show_threads ? KERN_PROC_FLAG_LWP : 0), 0, &nproc); 464 if (nproc > onproc) 465 pref = (struct kinfo_proc **)realloc(pref, sizeof(struct kinfo_proc *) 466 * (onproc = nproc)); 467 if (pref == NULL || pbase == NULL) { 468 (void)fprintf(stderr, "top: Out of memory.\n"); 469 quit(23); 470 } 471 /* get a pointer to the states summary array */ 472 si->procstates = process_states; 473 474 /* set up flags which define what we are going to select */ 475 show_idle = sel->idle; 476 show_system = sel->system; 477 show_uid = sel->uid != -1; 478 show_fullcmd = sel->fullcmd; 479 480 /* count up process states and get pointers to interesting procs */ 481 total_procs = 0; 482 active_procs = 0; 483 memset((char *)process_states, 0, sizeof(process_states)); 484 prefp = pref; 485 for (pp = pbase, i = 0; i < nproc; pp++, i++) { 486 /* 487 * Place pointers to each valid proc structure in pref[]. 488 * Process slots that are actually in use have a non-zero 489 * status field. Processes with P_SYSTEM set are system 490 * processes---these get ignored unless show_sysprocs is set. 491 */ 492 if ((show_system && (LP(pp, pid) == -1)) || 493 (show_system || ((PP(pp, flags) & P_SYSTEM) == 0))) { 494 int lpstate = LP(pp, stat); 495 int pstate = PP(pp, stat); 496 497 total_procs++; 498 if (lpstate == LSRUN) 499 process_states[0]++; 500 if (pstate >= 0 && pstate < MAXPSTATES - 1) 501 process_states[pstate]++; 502 if ((show_system && (LP(pp, pid) == -1)) || 503 (show_idle || (LP(pp, pctcpu) != 0) || 504 (lpstate == LSRUN)) && 505 (!show_uid || PP(pp, ruid) == (uid_t) sel->uid)) { 506 *prefp++ = pp; 507 active_procs++; 508 } 509 } 510 } 511 512 qsort((char *)pref, active_procs, sizeof(struct kinfo_proc *), 513 (int (*)(const void *, const void *))proc_compares[compare_index]); 514 515 /* remember active and total counts */ 516 si->p_total = total_procs; 517 si->p_active = pref_len = active_procs; 518 519 /* pass back a handle */ 520 handle.next_proc = pref; 521 handle.remaining = active_procs; 522 return ((caddr_t) & handle); 523 } 524 525 char fmt[MAX_COLS]; /* static area where result is built */ 526 527 char * 528 format_next_process(caddr_t xhandle, char *(*get_userid) (int)) 529 { 530 struct kinfo_proc *pp; 531 long cputime; 532 long ccputime; 533 double pct; 534 struct handle *hp; 535 char status[16]; 536 int state; 537 int xnice; 538 int prefer_fullcmd; 539 char **comm_full; 540 char *comm; 541 char cputime_fmt[10], ccputime_fmt[10]; 542 543 /* find and remember the next proc structure */ 544 hp = (struct handle *)xhandle; 545 pp = *(hp->next_proc++); 546 hp->remaining--; 547 548 /* get the process's command name */ 549 prefer_fullcmd = show_fullcmd; 550 if (show_fullcmd) { 551 if ((comm_full = kvm_getargv(kd, pp, 0)) == NULL) { 552 prefer_fullcmd = 0; 553 comm = PP(pp, comm); 554 } 555 } 556 else { 557 comm = PP(pp, comm); 558 } 559 560 /* 561 * Convert the process's runtime from microseconds to seconds. This 562 * time includes the interrupt time to be in compliance with ps output. 563 */ 564 cputime = (LP(pp, uticks) + LP(pp, sticks) + LP(pp, iticks)) / 1000000; 565 ccputime = cputime + PP(pp, cru).ru_stime.tv_sec + PP(pp, cru).ru_utime.tv_sec; 566 format_time(cputime, cputime_fmt, sizeof(cputime_fmt)); 567 format_time(ccputime, ccputime_fmt, sizeof(ccputime_fmt)); 568 569 /* calculate the base for cpu percentages */ 570 pct = pctdouble(LP(pp, pctcpu)); 571 572 /* generate "STATE" field */ 573 switch (state = LP(pp, stat)) { 574 case LSRUN: 575 if (LP(pp, tdflags) & TDF_RUNNING) 576 sprintf(status, "CPU%d", LP(pp, cpuid)); 577 else 578 strcpy(status, "RUN"); 579 break; 580 case LSSLEEP: 581 if (LP(pp, wmesg) != NULL) { 582 sprintf(status, "%.8s", LP(pp, wmesg)); /* WMESGLEN */ 583 break; 584 } 585 /* fall through */ 586 default: 587 588 if (state >= 0 && 589 (unsigned)state < sizeof(state_abbrev) / sizeof(*state_abbrev)) 590 sprintf(status, "%.6s", state_abbrev[(unsigned char)state]); 591 else 592 sprintf(status, "?%5d", state); 593 break; 594 } 595 596 if (PP(pp, stat) == SZOMB) 597 strcpy(status, "ZOMB"); 598 599 /* 600 * idle time 0 - 31 -> nice value +21 - +52 normal time -> nice 601 * value -20 - +20 real time 0 - 31 -> nice value -52 - -21 thread 602 * 0 - 31 -> nice value -53 - 603 */ 604 switch (LP(pp, rtprio.type)) { 605 case RTP_PRIO_REALTIME: 606 xnice = PRIO_MIN - 1 - RTP_PRIO_MAX + LP(pp, rtprio.prio); 607 break; 608 case RTP_PRIO_IDLE: 609 xnice = PRIO_MAX + 1 + LP(pp, rtprio.prio); 610 break; 611 case RTP_PRIO_THREAD: 612 xnice = PRIO_MIN - 1 - RTP_PRIO_MAX - LP(pp, rtprio.prio); 613 break; 614 default: 615 xnice = PP(pp, nice); 616 break; 617 } 618 619 /* format this entry */ 620 snprintf(fmt, sizeof(fmt), 621 smp_Proc_format, 622 (int)PP(pp, pid), 623 namelength, namelength, 624 get_userid(PP(pp, ruid)), 625 (int)xnice, 626 format_k(PROCSIZE(pp)), 627 format_k(pagetok(VP(pp, rssize))), 628 status, 629 LP(pp, cpuid), 630 cputime_fmt, 631 ccputime_fmt, 632 100.0 * pct, 633 cmdlength, 634 prefer_fullcmd ? *comm_full : comm); 635 636 /* return the result */ 637 return (fmt); 638 } 639 640 /* comparison routines for qsort */ 641 642 /* 643 * proc_compare - comparison function for "qsort" 644 * Compares the resource consumption of two processes using five 645 * distinct keys. The keys (in descending order of importance) are: 646 * percent cpu, cpu ticks, state, resident set size, total virtual 647 * memory usage. The process states are ordered as follows (from least 648 * to most important): WAIT, zombie, sleep, stop, start, run. The 649 * array declaration below maps a process state index into a number 650 * that reflects this ordering. 651 */ 652 653 static unsigned char sorted_state[] = 654 { 655 0, /* not used */ 656 3, /* sleep */ 657 1, /* ABANDONED (WAIT) */ 658 6, /* run */ 659 5, /* start */ 660 2, /* zombie */ 661 4 /* stop */ 662 }; 663 664 665 #define ORDERKEY_PCTCPU \ 666 if (lresult = (long) LP(p2, pctcpu) - (long) LP(p1, pctcpu), \ 667 (result = lresult > 0 ? 1 : lresult < 0 ? -1 : 0) == 0) 668 669 #define CPTICKS(p) (LP(p, uticks) + LP(p, sticks) + LP(p, iticks)) 670 671 #define ORDERKEY_CPTICKS \ 672 if ((result = CPTICKS(p2) > CPTICKS(p1) ? 1 : \ 673 CPTICKS(p2) < CPTICKS(p1) ? -1 : 0) == 0) 674 675 #define CTIME(p) (((LP(p, uticks) + LP(p, sticks) + LP(p, iticks))/1000000) + \ 676 PP(p, cru).ru_stime.tv_sec + PP(p, cru).ru_utime.tv_sec) 677 678 #define ORDERKEY_CTIME \ 679 if ((result = CTIME(p2) > CTIME(p1) ? 1 : \ 680 CTIME(p2) < CTIME(p1) ? -1 : 0) == 0) 681 682 #define ORDERKEY_STATE \ 683 if ((result = sorted_state[(unsigned char) PP(p2, stat)] - \ 684 sorted_state[(unsigned char) PP(p1, stat)]) == 0) 685 686 #define ORDERKEY_PRIO \ 687 if ((result = LP(p2, prio) - LP(p1, prio)) == 0) 688 689 #define ORDERKEY_KTHREADS \ 690 if ((result = (LP(p1, pid) == 0) - (LP(p2, pid) == 0)) == 0) 691 692 #define ORDERKEY_KTHREADS_PRIO \ 693 if ((result = LP(p2, tdprio) - LP(p1, tdprio)) == 0) 694 695 #define ORDERKEY_RSSIZE \ 696 if ((result = VP(p2, rssize) - VP(p1, rssize)) == 0) 697 698 #define ORDERKEY_MEM \ 699 if ( (result = PROCSIZE(p2) - PROCSIZE(p1)) == 0 ) 700 701 #define ORDERKEY_PID \ 702 if ( (result = PP(p1, pid) - PP(p2, pid)) == 0) 703 704 #define ORDERKEY_PRSSIZE \ 705 if((result = VP(p2, prssize) - VP(p1, prssize)) == 0) 706 707 /* compare_cpu - the comparison function for sorting by cpu percentage */ 708 709 int 710 proc_compare(struct kinfo_proc **pp1, struct kinfo_proc **pp2) 711 { 712 struct kinfo_proc *p1; 713 struct kinfo_proc *p2; 714 int result; 715 pctcpu lresult; 716 717 /* remove one level of indirection */ 718 p1 = *(struct kinfo_proc **) pp1; 719 p2 = *(struct kinfo_proc **) pp2; 720 721 ORDERKEY_PCTCPU 722 ORDERKEY_CPTICKS 723 ORDERKEY_STATE 724 ORDERKEY_PRIO 725 ORDERKEY_RSSIZE 726 ORDERKEY_MEM 727 {} 728 729 return (result); 730 } 731 732 /* compare_size - the comparison function for sorting by total memory usage */ 733 734 int 735 compare_size(struct kinfo_proc **pp1, struct kinfo_proc **pp2) 736 { 737 struct kinfo_proc *p1; 738 struct kinfo_proc *p2; 739 int result; 740 pctcpu lresult; 741 742 /* remove one level of indirection */ 743 p1 = *(struct kinfo_proc **) pp1; 744 p2 = *(struct kinfo_proc **) pp2; 745 746 ORDERKEY_MEM 747 ORDERKEY_RSSIZE 748 ORDERKEY_PCTCPU 749 ORDERKEY_CPTICKS 750 ORDERKEY_STATE 751 ORDERKEY_PRIO 752 {} 753 754 return (result); 755 } 756 757 /* compare_res - the comparison function for sorting by resident set size */ 758 759 int 760 compare_res(struct kinfo_proc **pp1, struct kinfo_proc **pp2) 761 { 762 struct kinfo_proc *p1; 763 struct kinfo_proc *p2; 764 int result; 765 pctcpu lresult; 766 767 /* remove one level of indirection */ 768 p1 = *(struct kinfo_proc **) pp1; 769 p2 = *(struct kinfo_proc **) pp2; 770 771 ORDERKEY_RSSIZE 772 ORDERKEY_MEM 773 ORDERKEY_PCTCPU 774 ORDERKEY_CPTICKS 775 ORDERKEY_STATE 776 ORDERKEY_PRIO 777 {} 778 779 return (result); 780 } 781 782 /* compare_pres - the comparison function for sorting by proportional resident set size */ 783 784 int 785 compare_pres(struct kinfo_proc **pp1, struct kinfo_proc **pp2) 786 { 787 struct kinfo_proc *p1; 788 struct kinfo_proc *p2; 789 int result; 790 pctcpu lresult; 791 792 /* remove one level of indirection */ 793 p1 = *(struct kinfo_proc **) pp1; 794 p2 = *(struct kinfo_proc **) pp2; 795 796 ORDERKEY_PRSSIZE 797 ORDERKEY_RSSIZE 798 ORDERKEY_MEM 799 ORDERKEY_PCTCPU 800 ORDERKEY_CPTICKS 801 ORDERKEY_STATE 802 ORDERKEY_PRIO 803 {} 804 805 return (result); 806 } 807 808 /* compare_time - the comparison function for sorting by total cpu time */ 809 810 int 811 compare_time(struct kinfo_proc **pp1, struct kinfo_proc **pp2) 812 { 813 struct kinfo_proc *p1; 814 struct kinfo_proc *p2; 815 int result; 816 pctcpu lresult; 817 818 /* remove one level of indirection */ 819 p1 = *(struct kinfo_proc **) pp1; 820 p2 = *(struct kinfo_proc **) pp2; 821 822 ORDERKEY_CPTICKS 823 ORDERKEY_PCTCPU 824 ORDERKEY_KTHREADS 825 ORDERKEY_KTHREADS_PRIO 826 ORDERKEY_STATE 827 ORDERKEY_PRIO 828 ORDERKEY_RSSIZE 829 ORDERKEY_MEM 830 {} 831 832 return (result); 833 } 834 835 int 836 compare_ctime(struct kinfo_proc **pp1, struct kinfo_proc **pp2) 837 { 838 struct kinfo_proc *p1; 839 struct kinfo_proc *p2; 840 int result; 841 pctcpu lresult; 842 843 /* remove one level of indirection */ 844 p1 = *(struct kinfo_proc **) pp1; 845 p2 = *(struct kinfo_proc **) pp2; 846 847 ORDERKEY_CTIME 848 ORDERKEY_PCTCPU 849 ORDERKEY_KTHREADS 850 ORDERKEY_KTHREADS_PRIO 851 ORDERKEY_STATE 852 ORDERKEY_PRIO 853 ORDERKEY_RSSIZE 854 ORDERKEY_MEM 855 {} 856 857 return (result); 858 } 859 860 /* compare_prio - the comparison function for sorting by cpu percentage */ 861 862 int 863 compare_prio(struct kinfo_proc **pp1, struct kinfo_proc **pp2) 864 { 865 struct kinfo_proc *p1; 866 struct kinfo_proc *p2; 867 int result; 868 pctcpu lresult; 869 870 /* remove one level of indirection */ 871 p1 = *(struct kinfo_proc **) pp1; 872 p2 = *(struct kinfo_proc **) pp2; 873 874 ORDERKEY_KTHREADS 875 ORDERKEY_KTHREADS_PRIO 876 ORDERKEY_PRIO 877 ORDERKEY_CPTICKS 878 ORDERKEY_PCTCPU 879 ORDERKEY_STATE 880 ORDERKEY_RSSIZE 881 ORDERKEY_MEM 882 {} 883 884 return (result); 885 } 886 887 int 888 compare_thr(struct kinfo_proc **pp1, struct kinfo_proc **pp2) 889 { 890 struct kinfo_proc *p1; 891 struct kinfo_proc *p2; 892 int result; 893 pctcpu lresult; 894 895 /* remove one level of indirection */ 896 p1 = *(struct kinfo_proc **)pp1; 897 p2 = *(struct kinfo_proc **)pp2; 898 899 ORDERKEY_KTHREADS 900 ORDERKEY_KTHREADS_PRIO 901 ORDERKEY_CPTICKS 902 ORDERKEY_PCTCPU 903 ORDERKEY_STATE 904 ORDERKEY_RSSIZE 905 ORDERKEY_MEM 906 {} 907 908 return (result); 909 } 910 911 /* compare_pid - the comparison function for sorting by process id */ 912 913 int 914 compare_pid(struct kinfo_proc **pp1, struct kinfo_proc **pp2) 915 { 916 struct kinfo_proc *p1; 917 struct kinfo_proc *p2; 918 int result; 919 920 /* remove one level of indirection */ 921 p1 = *(struct kinfo_proc **) pp1; 922 p2 = *(struct kinfo_proc **) pp2; 923 924 ORDERKEY_PID 925 ; 926 927 return(result); 928 } 929 930 /* 931 * proc_owner(pid) - returns the uid that owns process "pid", or -1 if 932 * the process does not exist. 933 * It is EXTREMLY IMPORTANT that this function work correctly. 934 * If top runs setuid root (as in SVR4), then this function 935 * is the only thing that stands in the way of a serious 936 * security problem. It validates requests for the "kill" 937 * and "renice" commands. 938 */ 939 940 int 941 proc_owner(int pid) 942 { 943 int xcnt; 944 struct kinfo_proc **prefp; 945 struct kinfo_proc *pp; 946 947 prefp = pref; 948 xcnt = pref_len; 949 while (--xcnt >= 0) { 950 pp = *prefp++; 951 if (PP(pp, pid) == (pid_t) pid) { 952 return ((int)PP(pp, ruid)); 953 } 954 } 955 return (-1); 956 } 957 958 959 /* 960 * swapmode is based on a program called swapinfo written 961 * by Kevin Lahey <kml@rokkaku.atl.ga.us>. 962 */ 963 int 964 swapmode(int *retavail, int *retfree) 965 { 966 int n; 967 int pagesize = getpagesize(); 968 struct kvm_swap swapary[1]; 969 970 *retavail = 0; 971 *retfree = 0; 972 973 #define CONVERT(v) ((quad_t)(v) * pagesize / 1024) 974 975 n = kvm_getswapinfo(kd, swapary, 1, 0); 976 if (n < 0 || swapary[0].ksw_total == 0) 977 return (0); 978 979 *retavail = CONVERT(swapary[0].ksw_total); 980 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used); 981 982 n = (int)((double)swapary[0].ksw_used * 100.0 / 983 (double)swapary[0].ksw_total); 984 return (n); 985 } 986