machine.c revision 50477
1132720Skan/* 2132720Skan * top - a top users display for Unix 3169691Skan * 4132720Skan * SYNOPSIS: For FreeBSD-2.x and later 5132720Skan * 6132720Skan * DESCRIPTION: 7132720Skan * Originally written for BSD4.4 system by Christos Zoulas. 8132720Skan * Ported to FreeBSD 2.x by Steven Wallace && Wolfram Schneider 9132720Skan * Order support hacked in from top-3.5beta6/machine/m_aix41.c 10132720Skan * by Monte Mitzelfelt (for latest top see http://www.groupsys.com/topinfo/) 11132720Skan * 12132720Skan * This is the machine-dependent module for FreeBSD 2.2 13132720Skan * Works for: 14132720Skan * FreeBSD 2.2.x, 3.x, 4.x, and probably FreeBSD 2.1.x 15132720Skan * 16132720Skan * LIBS: -lkvm 17132720Skan * 18132720Skan * AUTHOR: Christos Zoulas <christos@ee.cornell.edu> 19169691Skan * Steven Wallace <swallace@freebsd.org> 20132720Skan * Wolfram Schneider <wosch@FreeBSD.org> 21132720Skan * 22132720Skan * $FreeBSD: head/usr.bin/top/machine.c 50477 1999-08-28 01:08:13Z peter $ 23132720Skan */ 24132720Skan 25132720Skan 26132720Skan#include <sys/time.h> 27132720Skan#include <sys/types.h> 28132720Skan#include <sys/signal.h> 29132720Skan#include <sys/param.h> 30132720Skan 31132720Skan#include "os.h" 32132720Skan#include <stdio.h> 33132720Skan#include <nlist.h> 34132720Skan#include <math.h> 35132720Skan#include <kvm.h> 36132720Skan#include <pwd.h> 37132720Skan#include <sys/errno.h> 38132720Skan#include <sys/sysctl.h> 39132720Skan#include <sys/dkstat.h> 40132720Skan#include <sys/file.h> 41169691Skan#include <sys/time.h> 42146897Skan#include <sys/proc.h> 43169691Skan#include <sys/user.h> 44169691Skan#include <sys/vmmeter.h> 45146897Skan#include <sys/resource.h> 46132720Skan#include <sys/rtprio.h> 47132720Skan 48132720Skan/* Swap */ 49132720Skan#include <stdlib.h> 50132720Skan#include <sys/conf.h> 51132720Skan 52132720Skan#include <osreldate.h> /* for changes in kernel structures */ 53132720Skan 54132720Skan#include "top.h" 55132720Skan#include "machine.h" 56132720Skan 57132720Skanstatic int check_nlist __P((struct nlist *)); 58132720Skanstatic int getkval __P((unsigned long, int *, int, char *)); 59132720Skanextern char* printable __P((char *)); 60132720Skanint swapmode __P((int *retavail, int *retfree)); 61132720Skanstatic int smpmode; 62132720Skanstatic int namelength; 63132720Skanstatic int cmdlength; 64132720Skan 65132720Skan 66132720Skan/* get_process_info passes back a handle. This is what it looks like: */ 67132720Skan 68132720Skanstruct handle 69132720Skan{ 70132720Skan struct kinfo_proc **next_proc; /* points to next valid proc pointer */ 71132720Skan int remaining; /* number of pointers remaining */ 72132720Skan}; 73132720Skan 74132720Skan/* declarations for load_avg */ 75132720Skan#include "loadavg.h" 76132720Skan 77132720Skan#define PP(pp, field) ((pp)->kp_proc . field) 78132720Skan#define EP(pp, field) ((pp)->kp_eproc . field) 79132720Skan#define VP(pp, field) ((pp)->kp_eproc.e_vm . field) 80132720Skan 81132720Skan/* define what weighted cpu is. */ 82132720Skan#define weighted_cpu(pct, pp) (PP((pp), p_swtime) == 0 ? 0.0 : \ 83132720Skan ((pct) / (1.0 - exp(PP((pp), p_swtime) * logcpu)))) 84132720Skan 85132720Skan/* what we consider to be process size: */ 86132720Skan#define PROCSIZE(pp) (VP((pp), vm_map.size) / 1024) 87132720Skan 88132720Skan/* definitions for indices in the nlist array */ 89132720Skan 90132720Skanstatic struct nlist nlst[] = { 91132720Skan#define X_CCPU 0 92132720Skan { "_ccpu" }, 93132720Skan#define X_CP_TIME 1 94132720Skan { "_cp_time" }, 95132720Skan#define X_AVENRUN 2 96132720Skan { "_averunnable" }, 97132720Skan 98132720Skan#define X_BUFSPACE 3 99132720Skan { "_bufspace" }, /* K in buffer cache */ 100132720Skan#define X_CNT 4 101132720Skan { "_cnt" }, /* struct vmmeter cnt */ 102132720Skan 103132720Skan/* Last pid */ 104132720Skan#define X_LASTPID 5 105132720Skan { "_nextpid" }, 106132720Skan { 0 } 107132720Skan}; 108132720Skan 109169691Skan/* 110132720Skan * These definitions control the format of the per-process area 111132720Skan */ 112132720Skan 113169691Skanstatic char smp_header[] = 114169691Skan " PID %-*.*s PRI NICE SIZE RES STATE C TIME WCPU CPU COMMAND"; 115132720Skan 116132720Skan#define smp_Proc_format \ 117132720Skan "%5d %-*.*s %3d %3d%7s %6s %-6.6s %1x%7s %5.2f%% %5.2f%% %.*s" 118169691Skan 119132720Skanstatic char up_header[] = 120132720Skan " PID %-*.*s PRI NICE SIZE RES STATE TIME WCPU CPU COMMAND"; 121169691Skan 122132720Skan#define up_Proc_format \ 123132720Skan "%5d %-*.*s %3d %3d%7s %6s %-6.6s%.0d%7s %5.2f%% %5.2f%% %.*s" 124132720Skan 125169691Skan 126132720Skan 127132720Skan/* process state names for the "STATE" column of the display */ 128132720Skan/* the extra nulls in the string "run" are for adding a slash and 129169691Skan the processor number when needed */ 130132720Skan 131132720Skanchar *state_abbrev[] = 132132720Skan{ 133169691Skan "", "START", "RUN\0\0\0", "SLEEP", "STOP", "ZOMB", 134169691Skan}; 135132720Skan 136132720Skan 137132720Skanstatic kvm_t *kd; 138132720Skan 139169691Skan/* values that we stash away in _init and use in later routines */ 140132720Skan 141132720Skanstatic double logcpu; 142169691Skan 143132720Skan/* these are retrieved from the kernel in _init */ 144132720Skan 145132720Skanstatic load_avg ccpu; 146132720Skan 147169691Skan/* these are offsets obtained via nlist and used in the get_ functions */ 148132720Skan 149132720Skanstatic unsigned long cp_time_offset; 150169691Skanstatic unsigned long avenrun_offset; 151169691Skanstatic unsigned long lastpid_offset; 152132720Skanstatic long lastpid; 153132720Skanstatic unsigned long cnt_offset; 154132720Skanstatic unsigned long bufspace_offset; 155169691Skanstatic long cnt; 156169691Skan 157169691Skan/* these are for calculating cpu state percentages */ 158169691Skan 159169691Skanstatic long cp_time[CPUSTATES]; 160169691Skanstatic long cp_old[CPUSTATES]; 161169691Skanstatic long cp_diff[CPUSTATES]; 162169691Skan 163132720Skan/* these are for detailing the process states */ 164132720Skan 165169691Skanint process_states[6]; 166132720Skanchar *procstatenames[] = { 167132720Skan "", " starting, ", " running, ", " sleeping, ", " stopped, ", 168132720Skan " zombie, ", 169169691Skan NULL 170132720Skan}; 171132720Skan 172132720Skan/* these are for detailing the cpu states */ 173132720Skan 174132720Skanint cpu_states[CPUSTATES]; 175132720Skanchar *cpustatenames[] = { 176132720Skan "user", "nice", "system", "interrupt", "idle", NULL 177132720Skan}; 178132720Skan 179132720Skan/* these are for detailing the memory statistics */ 180132720Skan 181132720Skanint memory_stats[7]; 182132720Skanchar *memorynames[] = { 183169691Skan "K Active, ", "K Inact, ", "K Wired, ", "K Cache, ", "K Buf, ", "K Free", 184169691Skan NULL 185169691Skan}; 186169691Skan 187169691Skanint swap_stats[7]; 188169691Skanchar *swapnames[] = { 189169691Skan/* 0 1 2 3 4 5 */ 190132720Skan "K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out", 191132720Skan NULL 192132720Skan}; 193169691Skan 194169691Skan 195169691Skan/* these are for keeping track of the proc array */ 196169691Skan 197169691Skanstatic int nproc; 198169691Skanstatic int onproc = -1; 199169691Skanstatic int pref_len; 200169691Skanstatic struct kinfo_proc *pbase; 201169691Skanstatic struct kinfo_proc **pref; 202132720Skan 203132720Skan/* these are for getting the memory statistics */ 204132720Skan 205132720Skanstatic int pageshift; /* log base 2 of the pagesize */ 206132720Skan 207132720Skan/* define pagetok in terms of pageshift */ 208132720Skan 209132720Skan#define pagetok(size) ((size) << pageshift) 210132720Skan 211132720Skan/* useful externals */ 212132720Skanlong percentages(); 213132720Skan 214132720Skan#ifdef ORDER 215132720Skan/* sorting orders. first is default */ 216132720Skanchar *ordernames[] = { 217132720Skan "cpu", "size", "res", "time", "pri", NULL 218132720Skan}; 219132720Skan#endif 220132720Skan 221132720Skanint 222132720Skanmachine_init(statics) 223132720Skan 224132720Skanstruct statics *statics; 225132720Skan 226169691Skan{ 227132720Skan register int i = 0; 228132720Skan register int pagesize; 229132720Skan int modelen; 230169691Skan struct passwd *pw; 231169691Skan 232169691Skan modelen = sizeof(smpmode); 233169691Skan if ((sysctlbyname("machdep.smp_active", &smpmode, &modelen, NULL, 0) < 0 && 234169691Skan sysctlbyname("smp.smp_active", &smpmode, &modelen, NULL, 0) < 0) || 235169691Skan modelen != sizeof(smpmode)) 236169691Skan smpmode = 0; 237169691Skan 238132720Skan while ((pw = getpwent()) != NULL) { 239132720Skan if (strlen(pw->pw_name) > namelength) 240132720Skan namelength = strlen(pw->pw_name); 241132720Skan } 242132720Skan if (namelength < 8) 243132720Skan namelength = 8; 244132720Skan if (namelength > 16) 245132720Skan namelength = 16; 246132720Skan 247132720Skan if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL) 248132720Skan return -1; 249132720Skan 250132720Skan 251132720Skan /* get the list of symbols we want to access in the kernel */ 252132720Skan (void) kvm_nlist(kd, nlst); 253132720Skan if (nlst[0].n_type == 0) 254132720Skan { 255132720Skan fprintf(stderr, "top: nlist failed\n"); 256132720Skan return(-1); 257169691Skan } 258132720Skan 259132720Skan /* make sure they were all found */ 260132720Skan if (i > 0 && check_nlist(nlst) > 0) 261132720Skan { 262132720Skan return(-1); 263132720Skan } 264132720Skan 265132720Skan (void) getkval(nlst[X_CCPU].n_value, (int *)(&ccpu), sizeof(ccpu), 266132720Skan nlst[X_CCPU].n_name); 267169691Skan 268169691Skan /* stash away certain offsets for later use */ 269132720Skan cp_time_offset = nlst[X_CP_TIME].n_value; 270132720Skan avenrun_offset = nlst[X_AVENRUN].n_value; 271169691Skan lastpid_offset = nlst[X_LASTPID].n_value; 272169691Skan cnt_offset = nlst[X_CNT].n_value; 273169691Skan bufspace_offset = nlst[X_BUFSPACE].n_value; 274169691Skan 275169691Skan /* this is used in calculating WCPU -- calculate it ahead of time */ 276132720Skan logcpu = log(loaddouble(ccpu)); 277132720Skan 278132720Skan pbase = NULL; 279132720Skan pref = NULL; 280132720Skan nproc = 0; 281132720Skan onproc = -1; 282132720Skan /* get the page size with "getpagesize" and calculate pageshift from it */ 283132720Skan pagesize = getpagesize(); 284132720Skan pageshift = 0; 285132720Skan while (pagesize > 1) 286132720Skan { 287132720Skan pageshift++; 288132720Skan pagesize >>= 1; 289132720Skan } 290132720Skan 291132720Skan /* we only need the amount of log(2)1024 for our conversion */ 292132720Skan pageshift -= LOG1024; 293132720Skan 294132720Skan /* fill in the statics information */ 295132720Skan statics->procstate_names = procstatenames; 296132720Skan statics->cpustate_names = cpustatenames; 297132720Skan statics->memory_names = memorynames; 298132720Skan statics->swap_names = swapnames; 299132720Skan#ifdef ORDER 300132720Skan statics->order_names = ordernames; 301132720Skan#endif 302132720Skan 303132720Skan /* all done! */ 304132720Skan return(0); 305132720Skan} 306132720Skan 307132720Skanchar *format_header(uname_field) 308132720Skan 309132720Skanregister char *uname_field; 310132720Skan 311132720Skan{ 312132720Skan register char *ptr; 313132720Skan static char Header[128]; 314132720Skan 315132720Skan snprintf(Header, sizeof(Header), smpmode ? smp_header : up_header, 316132720Skan namelength, namelength, uname_field); 317132720Skan 318132720Skan cmdlength = 80 - strlen(Header) + 6; 319132720Skan 320132720Skan return Header; 321132720Skan} 322132720Skan 323132720Skanstatic int swappgsin = -1; 324132720Skanstatic int swappgsout = -1; 325132720Skanextern struct timeval timeout; 326132720Skan 327132720Skanvoid 328132720Skanget_system_info(si) 329132720Skan 330132720Skanstruct system_info *si; 331132720Skan 332132720Skan{ 333132720Skan long total; 334132720Skan load_avg avenrun[3]; 335132720Skan int mib[2]; 336132720Skan struct timeval boottime; 337132720Skan size_t bt_size; 338132720Skan 339132720Skan /* get the cp_time array */ 340132720Skan (void) getkval(cp_time_offset, (int *)cp_time, sizeof(cp_time), 341132720Skan nlst[X_CP_TIME].n_name); 342132720Skan (void) getkval(avenrun_offset, (int *)avenrun, sizeof(avenrun), 343132720Skan nlst[X_AVENRUN].n_name); 344132720Skan 345132720Skan (void) getkval(lastpid_offset, (int *)(&lastpid), sizeof(lastpid), 346132720Skan "!"); 347132720Skan 348132720Skan /* convert load averages to doubles */ 349132720Skan { 350132720Skan register int i; 351132720Skan register double *infoloadp; 352132720Skan load_avg *avenrunp; 353132720Skan 354132720Skan#ifdef notyet 355132720Skan struct loadavg sysload; 356132720Skan int size; 357132720Skan getkerninfo(KINFO_LOADAVG, &sysload, &size, 0); 358132720Skan#endif 359132720Skan 360132720Skan infoloadp = si->load_avg; 361132720Skan avenrunp = avenrun; 362132720Skan for (i = 0; i < 3; i++) 363132720Skan { 364132720Skan#ifdef notyet 365132720Skan *infoloadp++ = ((double) sysload.ldavg[i]) / sysload.fscale; 366132720Skan#endif 367132720Skan *infoloadp++ = loaddouble(*avenrunp++); 368132720Skan } 369132720Skan } 370132720Skan 371132720Skan /* convert cp_time counts to percentages */ 372132720Skan total = percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff); 373132720Skan 374132720Skan /* sum memory & swap statistics */ 375132720Skan { 376132720Skan struct vmmeter sum; 377132720Skan static unsigned int swap_delay = 0; 378132720Skan static int swapavail = 0; 379132720Skan static int swapfree = 0; 380132720Skan static int bufspace = 0; 381132720Skan 382132720Skan (void) getkval(cnt_offset, (int *)(&sum), sizeof(sum), 383132720Skan "_cnt"); 384132720Skan (void) getkval(bufspace_offset, (int *)(&bufspace), sizeof(bufspace), 385132720Skan "_bufspace"); 386132720Skan 387132720Skan /* convert memory stats to Kbytes */ 388132720Skan memory_stats[0] = pagetok(sum.v_active_count); 389132720Skan memory_stats[1] = pagetok(sum.v_inactive_count); 390132720Skan memory_stats[2] = pagetok(sum.v_wire_count); 391132720Skan memory_stats[3] = pagetok(sum.v_cache_count); 392132720Skan memory_stats[4] = bufspace / 1024; 393132720Skan memory_stats[5] = pagetok(sum.v_free_count); 394132720Skan memory_stats[6] = -1; 395132720Skan 396132720Skan /* first interval */ 397132720Skan if (swappgsin < 0) { 398132720Skan swap_stats[4] = 0; 399132720Skan swap_stats[5] = 0; 400132720Skan } 401132720Skan 402132720Skan /* compute differences between old and new swap statistic */ 403132720Skan else { 404132720Skan swap_stats[4] = pagetok(((sum.v_swappgsin - swappgsin))); 405132720Skan swap_stats[5] = pagetok(((sum.v_swappgsout - swappgsout))); 406132720Skan } 407132720Skan 408132720Skan swappgsin = sum.v_swappgsin; 409132720Skan swappgsout = sum.v_swappgsout; 410132720Skan 411132720Skan /* call CPU heavy swapmode() only for changes */ 412132720Skan if (swap_stats[4] > 0 || swap_stats[5] > 0 || swap_delay == 0) { 413132720Skan swap_stats[3] = swapmode(&swapavail, &swapfree); 414132720Skan swap_stats[0] = swapavail; 415132720Skan swap_stats[1] = swapavail - swapfree; 416132720Skan swap_stats[2] = swapfree; 417132720Skan } 418132720Skan swap_delay = 1; 419132720Skan swap_stats[6] = -1; 420132720Skan } 421132720Skan 422132720Skan /* set arrays and strings */ 423132720Skan si->cpustates = cpu_states; 424132720Skan si->memory = memory_stats; 425132720Skan si->swap = swap_stats; 426132720Skan 427132720Skan 428132720Skan if(lastpid > 0) { 429132720Skan si->last_pid = lastpid; 430132720Skan } else { 431132720Skan si->last_pid = -1; 432132720Skan } 433132720Skan 434132720Skan /* 435132720Skan * Print how long system has been up. 436132720Skan * (Found by looking getting "boottime" from the kernel) 437132720Skan */ 438132720Skan mib[0] = CTL_KERN; 439132720Skan mib[1] = KERN_BOOTTIME; 440132720Skan bt_size = sizeof(boottime); 441132720Skan if (sysctl(mib, 2, &boottime, &bt_size, NULL, 0) != -1 && 442132720Skan boottime.tv_sec != 0) { 443132720Skan si->boottime = boottime; 444132720Skan } else { 445132720Skan si->boottime.tv_sec = -1; 446132720Skan } 447132720Skan} 448132720Skan 449132720Skanstatic struct handle handle; 450132720Skan 451132720Skancaddr_t get_process_info(si, sel, compare) 452132720Skan 453132720Skanstruct system_info *si; 454132720Skanstruct process_select *sel; 455132720Skanint (*compare)(); 456132720Skan 457132720Skan{ 458132720Skan register int i; 459132720Skan register int total_procs; 460132720Skan register int active_procs; 461132720Skan register struct kinfo_proc **prefp; 462132720Skan register struct kinfo_proc *pp; 463132720Skan 464132720Skan /* these are copied out of sel for speed */ 465132720Skan int show_idle; 466132720Skan int show_self; 467132720Skan int show_system; 468132720Skan int show_uid; 469132720Skan int show_command; 470132720Skan 471132720Skan 472132720Skan pbase = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc); 473132720Skan if (nproc > onproc) 474132720Skan pref = (struct kinfo_proc **) realloc(pref, sizeof(struct kinfo_proc *) 475132720Skan * (onproc = nproc)); 476132720Skan if (pref == NULL || pbase == NULL) { 477132720Skan (void) fprintf(stderr, "top: Out of memory.\n"); 478132720Skan quit(23); 479132720Skan } 480132720Skan /* get a pointer to the states summary array */ 481132720Skan si->procstates = process_states; 482132720Skan 483132720Skan /* set up flags which define what we are going to select */ 484132720Skan show_idle = sel->idle; 485132720Skan show_self = sel->self; 486132720Skan show_system = sel->system; 487132720Skan show_uid = sel->uid != -1; 488132720Skan show_command = sel->command != NULL; 489132720Skan 490132720Skan /* count up process states and get pointers to interesting procs */ 491132720Skan total_procs = 0; 492132720Skan active_procs = 0; 493132720Skan memset((char *)process_states, 0, sizeof(process_states)); 494132720Skan prefp = pref; 495132720Skan for (pp = pbase, i = 0; i < nproc; pp++, i++) 496132720Skan { 497132720Skan /* 498132720Skan * Place pointers to each valid proc structure in pref[]. 499132720Skan * Process slots that are actually in use have a non-zero 500132720Skan * status field. Processes with P_SYSTEM set are system 501132720Skan * processes---these get ignored unless show_sysprocs is set. 502132720Skan */ 503132720Skan if (PP(pp, p_stat) != 0 && 504132720Skan (show_self != PP(pp, p_pid)) && 505132720Skan (show_system || ((PP(pp, p_flag) & P_SYSTEM) == 0))) 506132720Skan { 507132720Skan total_procs++; 508132720Skan process_states[(unsigned char) PP(pp, p_stat)]++; 509132720Skan if ((PP(pp, p_stat) != SZOMB) && 510132720Skan (show_idle || (PP(pp, p_pctcpu) != 0) || 511132720Skan (PP(pp, p_stat) == SRUN)) && 512132720Skan (!show_uid || EP(pp, e_pcred.p_ruid) == (uid_t)sel->uid)) 513132720Skan { 514132720Skan *prefp++ = pp; 515132720Skan active_procs++; 516132720Skan } 517132720Skan } 518132720Skan } 519132720Skan 520132720Skan /* if requested, sort the "interesting" processes */ 521132720Skan if (compare != NULL) 522132720Skan { 523132720Skan qsort((char *)pref, active_procs, sizeof(struct kinfo_proc *), compare); 524132720Skan } 525132720Skan 526132720Skan /* remember active and total counts */ 527132720Skan si->p_total = total_procs; 528132720Skan si->p_active = pref_len = active_procs; 529132720Skan 530169691Skan /* pass back a handle */ 531132720Skan handle.next_proc = pref; 532132720Skan handle.remaining = active_procs; 533132720Skan return((caddr_t)&handle); 534132720Skan} 535132720Skan 536132720Skanchar fmt[128]; /* static area where result is built */ 537132720Skan 538132720Skanchar *format_next_process(handle, get_userid) 539132720Skan 540132720Skancaddr_t handle; 541132720Skanchar *(*get_userid)(); 542132720Skan 543132720Skan{ 544132720Skan register struct kinfo_proc *pp; 545132720Skan register long cputime; 546132720Skan register double pct; 547132720Skan struct handle *hp; 548132720Skan char status[16]; 549132720Skan int state; 550132720Skan 551132720Skan /* find and remember the next proc structure */ 552132720Skan hp = (struct handle *)handle; 553132720Skan pp = *(hp->next_proc++); 554132720Skan hp->remaining--; 555132720Skan 556132720Skan 557132720Skan /* get the process's user struct and set cputime */ 558132720Skan if ((PP(pp, p_flag) & P_INMEM) == 0) { 559132720Skan /* 560132720Skan * Print swapped processes as <pname> 561132720Skan */ 562132720Skan char *comm = PP(pp, p_comm); 563132720Skan#define COMSIZ sizeof(PP(pp, p_comm)) 564132720Skan char buf[COMSIZ]; 565132720Skan (void) strncpy(buf, comm, COMSIZ); 566132720Skan comm[0] = '<'; 567132720Skan (void) strncpy(&comm[1], buf, COMSIZ - 2); 568132720Skan comm[COMSIZ - 2] = '\0'; 569132720Skan (void) strncat(comm, ">", COMSIZ - 1); 570132720Skan comm[COMSIZ - 1] = '\0'; 571132720Skan } 572132720Skan 573132720Skan#if 0 574132720Skan /* This does not produce the correct results */ 575132720Skan cputime = PP(pp, p_uticks) + PP(pp, p_sticks) + PP(pp, p_iticks); 576132720Skan#endif 577132720Skan /* This does not count interrupts */ 578132720Skan cputime = (PP(pp, p_runtime) / 1000 + 500) / 1000; 579132720Skan 580132720Skan /* calculate the base for cpu percentages */ 581132720Skan pct = pctdouble(PP(pp, p_pctcpu)); 582132720Skan 583132720Skan /* generate "STATE" field */ 584132720Skan switch (state = PP(pp, p_stat)) { 585132720Skan case SRUN: 586132720Skan if (smpmode && PP(pp, p_oncpu) != 0xff) 587132720Skan sprintf(status, "CPU%d", PP(pp, p_oncpu)); 588132720Skan else 589132720Skan strcpy(status, "RUN"); 590169691Skan break; 591132720Skan case SSLEEP: 592132720Skan if (PP(pp, p_wmesg) != NULL) { 593132720Skan sprintf(status, "%.6s", EP(pp, e_wmesg)); 594132720Skan break; 595132720Skan } 596132720Skan /* fall through */ 597132720Skan default: 598169691Skan 599169691Skan if (state >= 0 && 600169691Skan state < sizeof(state_abbrev) / sizeof(*state_abbrev)) 601169691Skan sprintf(status, "%.6s", state_abbrev[(unsigned char) state]); 602169691Skan else 603169691Skan sprintf(status, "?%5d", state); 604169691Skan break; 605169691Skan } 606132720Skan 607169691Skan /* format this entry */ 608146897Skan sprintf(fmt, 609146897Skan smpmode ? smp_Proc_format : up_Proc_format, 610146897Skan PP(pp, p_pid), 611132720Skan namelength, namelength, 612169691Skan (*get_userid)(EP(pp, e_pcred.p_ruid)), 613132720Skan PP(pp, p_priority) - PZERO, 614132720Skan 615132720Skan /* 616132720Skan * normal time -> nice value -20 - +20 617132720Skan * real time 0 - 31 -> nice value -52 - -21 618132720Skan * idle time 0 - 31 -> nice value +21 - +52 619132720Skan */ 620132720Skan (PP(pp, p_rtprio.type) == RTP_PRIO_NORMAL ? 621132720Skan PP(pp, p_nice) - NZERO : 622132720Skan (RTP_PRIO_IS_REALTIME(PP(pp, p_rtprio.type)) ? 623132720Skan (PRIO_MIN - 1 - RTP_PRIO_MAX + PP(pp, p_rtprio.prio)) : 624132720Skan (PRIO_MAX + 1 + PP(pp, p_rtprio.prio)))), 625132720Skan format_k2(PROCSIZE(pp)), 626132720Skan format_k2(pagetok(VP(pp, vm_rssize))), 627132720Skan status, 628132720Skan smpmode ? PP(pp, p_lastcpu) : 0, 629132720Skan format_time(cputime), 630132720Skan 100.0 * weighted_cpu(pct, pp), 631132720Skan 100.0 * pct, 632132720Skan cmdlength, 633132720Skan printable(PP(pp, p_comm))); 634132720Skan 635132720Skan /* return the result */ 636132720Skan return(fmt); 637132720Skan} 638132720Skan 639132720Skan 640132720Skan/* 641132720Skan * check_nlist(nlst) - checks the nlist to see if any symbols were not 642132720Skan * found. For every symbol that was not found, a one-line 643132720Skan * message is printed to stderr. The routine returns the 644132720Skan * number of symbols NOT found. 645132720Skan */ 646132720Skan 647132720Skanstatic int check_nlist(nlst) 648132720Skan 649132720Skanregister struct nlist *nlst; 650132720Skan 651132720Skan{ 652132720Skan register int i; 653132720Skan 654132720Skan /* check to see if we got ALL the symbols we requested */ 655132720Skan /* this will write one line to stderr for every symbol not found */ 656132720Skan 657132720Skan i = 0; 658132720Skan while (nlst->n_name != NULL) 659132720Skan { 660132720Skan if (nlst->n_type == 0) 661132720Skan { 662132720Skan /* this one wasn't found */ 663132720Skan (void) fprintf(stderr, "kernel: no symbol named `%s'\n", 664132720Skan nlst->n_name); 665132720Skan i = 1; 666132720Skan } 667132720Skan nlst++; 668132720Skan } 669132720Skan 670132720Skan return(i); 671132720Skan} 672132720Skan 673132720Skan 674132720Skan/* 675132720Skan * getkval(offset, ptr, size, refstr) - get a value out of the kernel. 676169691Skan * "offset" is the byte offset into the kernel for the desired value, 677169691Skan * "ptr" points to a buffer into which the value is retrieved, 678132720Skan * "size" is the size of the buffer (and the object to retrieve), 679132720Skan * "refstr" is a reference string used when printing error meessages, 680132720Skan * if "refstr" starts with a '!', then a failure on read will not 681169691Skan * be fatal (this may seem like a silly way to do things, but I 682132720Skan * really didn't want the overhead of another argument). 683132720Skan * 684132720Skan */ 685169691Skan 686169691Skanstatic int getkval(offset, ptr, size, refstr) 687132720Skan 688132720Skanunsigned long offset; 689132720Skanint *ptr; 690169691Skanint size; 691169691Skanchar *refstr; 692132720Skan 693{ 694 if (kvm_read(kd, offset, (char *) ptr, size) != size) 695 { 696 if (*refstr == '!') 697 { 698 return(0); 699 } 700 else 701 { 702 fprintf(stderr, "top: kvm_read for %s: %s\n", 703 refstr, strerror(errno)); 704 quit(23); 705 } 706 } 707 return(1); 708} 709 710/* comparison routines for qsort */ 711 712/* 713 * proc_compare - comparison function for "qsort" 714 * Compares the resource consumption of two processes using five 715 * distinct keys. The keys (in descending order of importance) are: 716 * percent cpu, cpu ticks, state, resident set size, total virtual 717 * memory usage. The process states are ordered as follows (from least 718 * to most important): WAIT, zombie, sleep, stop, start, run. The 719 * array declaration below maps a process state index into a number 720 * that reflects this ordering. 721 */ 722 723static unsigned char sorted_state[] = 724{ 725 0, /* not used */ 726 3, /* sleep */ 727 1, /* ABANDONED (WAIT) */ 728 6, /* run */ 729 5, /* start */ 730 2, /* zombie */ 731 4 /* stop */ 732}; 733 734 735#define ORDERKEY_PCTCPU \ 736 if (lresult = (long) PP(p2, p_pctcpu) - (long) PP(p1, p_pctcpu), \ 737 (result = lresult > 0 ? 1 : lresult < 0 ? -1 : 0) == 0) 738 739#define ORDERKEY_CPTICKS \ 740 if ((result = PP(p2, p_runtime) - PP(p1, p_runtime)) == 0) 741 742#define ORDERKEY_STATE \ 743 if ((result = sorted_state[(unsigned char) PP(p2, p_stat)] - \ 744 sorted_state[(unsigned char) PP(p1, p_stat)]) == 0) 745 746#define ORDERKEY_PRIO \ 747 if ((result = PP(p2, p_priority) - PP(p1, p_priority)) == 0) 748 749#define ORDERKEY_RSSIZE \ 750 if ((result = VP(p2, vm_rssize) - VP(p1, vm_rssize)) == 0) 751 752#define ORDERKEY_MEM \ 753 if ( (result = PROCSIZE(p2) - PROCSIZE(p1)) == 0 ) 754 755/* compare_cpu - the comparison function for sorting by cpu percentage */ 756 757int 758#ifdef ORDER 759compare_cpu(pp1, pp2) 760#else 761proc_compare(pp1, pp2) 762#endif 763 764struct proc **pp1; 765struct proc **pp2; 766 767{ 768 register struct kinfo_proc *p1; 769 register struct kinfo_proc *p2; 770 register int result; 771 register pctcpu lresult; 772 773 /* remove one level of indirection */ 774 p1 = *(struct kinfo_proc **) pp1; 775 p2 = *(struct kinfo_proc **) pp2; 776 777 ORDERKEY_PCTCPU 778 ORDERKEY_CPTICKS 779 ORDERKEY_STATE 780 ORDERKEY_PRIO 781 ORDERKEY_RSSIZE 782 ORDERKEY_MEM 783 ; 784 785 return(result); 786} 787 788#ifdef ORDER 789/* compare routines */ 790int compare_size(), compare_res(), compare_time(), compare_prio(); 791 792int (*proc_compares[])() = { 793 compare_cpu, 794 compare_size, 795 compare_res, 796 compare_time, 797 compare_prio, 798 NULL 799}; 800 801/* compare_size - the comparison function for sorting by total memory usage */ 802 803int 804compare_size(pp1, pp2) 805 806struct proc **pp1; 807struct proc **pp2; 808 809{ 810 register struct kinfo_proc *p1; 811 register struct kinfo_proc *p2; 812 register int result; 813 register pctcpu lresult; 814 815 /* remove one level of indirection */ 816 p1 = *(struct kinfo_proc **) pp1; 817 p2 = *(struct kinfo_proc **) pp2; 818 819 ORDERKEY_MEM 820 ORDERKEY_RSSIZE 821 ORDERKEY_PCTCPU 822 ORDERKEY_CPTICKS 823 ORDERKEY_STATE 824 ORDERKEY_PRIO 825 ; 826 827 return(result); 828} 829 830/* compare_res - the comparison function for sorting by resident set size */ 831 832int 833compare_res(pp1, pp2) 834 835struct proc **pp1; 836struct proc **pp2; 837 838{ 839 register struct kinfo_proc *p1; 840 register struct kinfo_proc *p2; 841 register int result; 842 register pctcpu lresult; 843 844 /* remove one level of indirection */ 845 p1 = *(struct kinfo_proc **) pp1; 846 p2 = *(struct kinfo_proc **) pp2; 847 848 ORDERKEY_RSSIZE 849 ORDERKEY_MEM 850 ORDERKEY_PCTCPU 851 ORDERKEY_CPTICKS 852 ORDERKEY_STATE 853 ORDERKEY_PRIO 854 ; 855 856 return(result); 857} 858 859/* compare_time - the comparison function for sorting by total cpu time */ 860 861int 862compare_time(pp1, pp2) 863 864struct proc **pp1; 865struct proc **pp2; 866 867{ 868 register struct kinfo_proc *p1; 869 register struct kinfo_proc *p2; 870 register int result; 871 register pctcpu lresult; 872 873 /* remove one level of indirection */ 874 p1 = *(struct kinfo_proc **) pp1; 875 p2 = *(struct kinfo_proc **) pp2; 876 877 ORDERKEY_CPTICKS 878 ORDERKEY_PCTCPU 879 ORDERKEY_STATE 880 ORDERKEY_PRIO 881 ORDERKEY_RSSIZE 882 ORDERKEY_MEM 883 ; 884 885 return(result); 886 } 887 888/* compare_prio - the comparison function for sorting by cpu percentage */ 889 890int 891compare_prio(pp1, pp2) 892 893struct proc **pp1; 894struct proc **pp2; 895 896{ 897 register struct kinfo_proc *p1; 898 register struct kinfo_proc *p2; 899 register int result; 900 register pctcpu lresult; 901 902 /* remove one level of indirection */ 903 p1 = *(struct kinfo_proc **) pp1; 904 p2 = *(struct kinfo_proc **) pp2; 905 906 ORDERKEY_PRIO 907 ORDERKEY_CPTICKS 908 ORDERKEY_PCTCPU 909 ORDERKEY_STATE 910 ORDERKEY_RSSIZE 911 ORDERKEY_MEM 912 ; 913 914 return(result); 915} 916#endif 917 918/* 919 * proc_owner(pid) - returns the uid that owns process "pid", or -1 if 920 * the process does not exist. 921 * It is EXTREMLY IMPORTANT that this function work correctly. 922 * If top runs setuid root (as in SVR4), then this function 923 * is the only thing that stands in the way of a serious 924 * security problem. It validates requests for the "kill" 925 * and "renice" commands. 926 */ 927 928int proc_owner(pid) 929 930int pid; 931 932{ 933 register int cnt; 934 register struct kinfo_proc **prefp; 935 register struct kinfo_proc *pp; 936 937 prefp = pref; 938 cnt = pref_len; 939 while (--cnt >= 0) 940 { 941 pp = *prefp++; 942 if (PP(pp, p_pid) == (pid_t)pid) 943 { 944 return((int)EP(pp, e_pcred.p_ruid)); 945 } 946 } 947 return(-1); 948} 949 950 951/* 952 * swapmode is based on a program called swapinfo written 953 * by Kevin Lahey <kml@rokkaku.atl.ga.us>. 954 */ 955 956#define SVAR(var) __STRING(var) /* to force expansion */ 957#define KGET(idx, var) \ 958 KGET1(idx, &var, sizeof(var), SVAR(var)) 959#define KGET1(idx, p, s, msg) \ 960 KGET2(nlst[idx].n_value, p, s, msg) 961#define KGET2(addr, p, s, msg) \ 962 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \ 963 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \ 964 return (0); \ 965 } 966#define KGETRET(addr, p, s, msg) \ 967 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \ 968 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \ 969 return (0); \ 970 } 971 972 973int 974swapmode(retavail, retfree) 975 int *retavail; 976 int *retfree; 977{ 978 int n; 979 int pagesize = getpagesize(); 980 struct kvm_swap swapary[1]; 981 982 *retavail = 0; 983 *retfree = 0; 984 985#define CONVERT(v) ((quad_t)(v) * pagesize / 1024) 986 987 n = kvm_getswapinfo(kd, swapary, 1, 0); 988 if (n < 0 || swapary[0].ksw_total == 0) 989 return(0); 990 991 *retavail = CONVERT(swapary[0].ksw_total); 992 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used); 993 994 n = (int)((double)swapary[0].ksw_used * 100.0 / 995 (double)swapary[0].ksw_total); 996 return(n); 997} 998 999