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