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