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