📄 output.c
字号:
case 2: return snprintf(outbuf, COLWID, "RR"); // SCHED_RR case 3: return snprintf(outbuf, COLWID, "B"); // SCHED_BATCH? (will be "B") case 4: return snprintf(outbuf, COLWID, "#4"); // SCHED_ISO? (Con Kolivas) case 5: return snprintf(outbuf, COLWID, "#5"); // case 8: return snprintf(outbuf, COLWID, "#8"); // default: return snprintf(outbuf, COLWID, "?"); // unknown value }}// Based on "type", FreeBSD would do:// REALTIME "real:%u", prio// NORMAL "normal"// IDLE "idle:%u", prio// default "%u:%u", type, prio// We just print the priority, and have other keywords for type.static int pr_rtprio(char *restrict const outbuf, const proc_t *restrict const pp){ if(pp->sched==0 || pp->sched==-1) return snprintf(outbuf, COLWID, "-"); return snprintf(outbuf, COLWID, "%ld", pp->rtprio);}static int pr_sched(char *restrict const outbuf, const proc_t *restrict const pp){ if(pp->sched==-1) return snprintf(outbuf, COLWID, "-"); return snprintf(outbuf, COLWID, "%ld", pp->sched);}////////////////////////////////////////////////////////////////////////////////static int pr_wchan(char *restrict const outbuf, const proc_t *restrict const pp){/* * Unix98 says "blank if running" and also "no blanks"! :-( * Unix98 also says to use '-' if something is meaningless. * Digital uses both '*' and '-', with undocumented differences. * (the '*' for -1 (rare) and the '-' for 0) * Sun claims to use a blank AND use '-', in the same man page. * Perhaps "blank" should mean '-'. * * AIX uses '-' for running processes, the location when there is * only one thread waiting in the kernel, and '*' when there is * more than one thread waiting in the kernel. * * The output should be truncated to maximal columns width -- overflow * is not supported for the "wchan". */ const char *w; size_t len; if(!(pp->wchan & 0xffffff)) return memcpy(outbuf,"-",2),1; if(wchan_is_number) return snprintf(outbuf, COLWID, "%x", (unsigned)(pp->wchan) & 0xffffffu); w = lookup_wchan(pp->wchan, pp->XXXID); len = strlen(w); if(len>max_rightward) len=max_rightward; memcpy(outbuf, w, len); outbuf[len] = '\0'; return len;}static int pr_wname(char *restrict const outbuf, const proc_t *restrict const pp){/* SGI's IRIX always uses a number for "wchan", so "wname" is provided too. * * We use '-' for running processes, the location when there is * only one thread waiting in the kernel, and '*' when there is * more than one thread waiting in the kernel. * * The output should be truncated to maximal columns width -- overflow * is not supported for the "wchan". */ const char *w; size_t len; if(!(pp->wchan & 0xffffff)) return memcpy(outbuf,"-",2),1; w = lookup_wchan(pp->wchan, pp->XXXID); len = strlen(w); if(len>max_rightward) len=max_rightward; memcpy(outbuf, w, len); outbuf[len] = '\0'; return len;}static int pr_nwchan(char *restrict const outbuf, const proc_t *restrict const pp){ if(!(pp->wchan & 0xffffff)) return memcpy(outbuf,"-",2),1; return snprintf(outbuf, COLWID, "%x", (unsigned)(pp->wchan) & 0xffffffu);}/* Terrible trunctuation, like BSD crap uses: I999 J999 K999 *//* FIXME: disambiguate /dev/tty69 and /dev/pts/69. */static int pr_tty4(char *restrict const outbuf, const proc_t *restrict const pp){/* snprintf(outbuf, COLWID, "%02x:%02x", pp->tty>>8, pp->tty&0xff); */ return dev_to_tty(outbuf, 4, pp->tty, pp->XXXID, ABBREV_DEV|ABBREV_TTY|ABBREV_PTS);}/* Unix98: format is unspecified, but must match that used by who(1). */static int pr_tty8(char *restrict const outbuf, const proc_t *restrict const pp){/* snprintf(outbuf, COLWID, "%02x:%02x", pp->tty>>8, pp->tty&0xff); */ return dev_to_tty(outbuf, COLWID, pp->tty, pp->XXXID, ABBREV_DEV);}#if 0/* This BSD state display may contain spaces, which is illegal. */static int pr_oldstate(char *restrict const outbuf, const proc_t *restrict const pp){ return snprintf(outbuf, COLWID, "%s", status(pp));}#endif// This state display is Unix98 compliant and has lots of info like BSD.static int pr_stat(char *restrict const outbuf, const proc_t *restrict const pp){ int end = 0; outbuf[end++] = pp->state;// if(pp->rss==0 && pp->state!='Z') outbuf[end++] = 'W'; // useless "swapped out" if(pp->nice < 0) outbuf[end++] = '<'; if(pp->nice > 0) outbuf[end++] = 'N';// In this order, NetBSD would add:// traced 'X'// systrace 'x'// exiting 'E' (not printed for zombies)// vforked 'V'// system 'K' (and do not print 'L' too) if(pp->vm_lock) outbuf[end++] = 'L'; if(pp->session == pp->tgid) outbuf[end++] = 's'; // session leader if(pp->nlwp > 1) outbuf[end++] = 'l'; // multi-threaded if(pp->pgrp == pp->tpgid) outbuf[end++] = '+'; // in foreground process group outbuf[end] = '\0'; return end;}/* This minimal state display is Unix98 compliant, like SCO and SunOS 5 */static int pr_s(char *restrict const outbuf, const proc_t *restrict const pp){ outbuf[0] = pp->state; outbuf[1] = '\0'; return 1;}static int pr_flag(char *restrict const outbuf, const proc_t *restrict const pp){ /* Unix98 requires octal flags */ /* this user-hostile and volatile junk gets 1 character */ return snprintf(outbuf, COLWID, "%o", (unsigned)(pp->flags>>6U)&0x7U);}// plus these: euid,ruid,egroup,rgroup (elsewhere in this file)/*********** non-standard ***********//*** BSDsess session pointer(SCO has:Process session leader ID as a decimal value. (SESSION))jobc job control countcpu short-term cpu usage factor (for scheduling)sl sleep time (in seconds; 127 = infinity)re core residency time (in seconds; 127 = infinity)pagein pageins (same as majflt)lim soft memory limittsiz text size (in Kbytes)***/static int pr_stackp(char *restrict const outbuf, const proc_t *restrict const pp){ return snprintf(outbuf, COLWID, "%08x", (unsigned)(pp->start_stack));}static int pr_esp(char *restrict const outbuf, const proc_t *restrict const pp){ return snprintf(outbuf, COLWID, "%08x", (unsigned)(pp->kstk_esp));}static int pr_eip(char *restrict const outbuf, const proc_t *restrict const pp){ return snprintf(outbuf, COLWID, "%08x", (unsigned)(pp->kstk_eip));}/* This function helps print old-style time formats */static int old_time_helper(char *dst, unsigned long long t, unsigned long long rel) { if(!t) return snprintf(dst, COLWID, " -"); if(t == ~0ULL) return snprintf(dst, COLWID, " xx"); if((long long)(t-=rel) < 0) t=0ULL; if(t>9999ULL) return snprintf(dst, COLWID, "%5Lu", t/100ULL); else return snprintf(dst, COLWID, "%2u.%02u", (unsigned)t/100U, (unsigned)t%100U);}static int pr_bsdtime(char *restrict const outbuf, const proc_t *restrict const pp){ unsigned long long t; unsigned u; t = pp->utime + pp->stime; if(include_dead_children) t += (pp->cutime + pp->cstime); u = t / Hertz; return snprintf(outbuf, COLWID, "%3u:%02u", u/60U, u%60U);}static int pr_bsdstart(char *restrict const outbuf, const proc_t *restrict const pp){ time_t start; time_t seconds_ago; start = time_of_boot + pp->start_time / Hertz; seconds_ago = seconds_since_1970 - start; if(seconds_ago < 0) seconds_ago=0; if(seconds_ago > 3600*24) strcpy(outbuf, ctime(&start)+4); else strcpy(outbuf, ctime(&start)+10); outbuf[6] = '\0'; return 6;}static int pr_alarm(char *restrict const outbuf, const proc_t *restrict const pp){ return old_time_helper(outbuf, pp->alarm, 0ULL);}/* HP-UX puts this in pages and uses "vsz" for kB */static int pr_sz(char *restrict const outbuf, const proc_t *restrict const pp){ return snprintf(outbuf, COLWID, "%lu", (pp->vm_size)/(page_size/1024));}/* * FIXME: trs,drs,tsiz,dsiz,m_trs,m_drs,vm_exe,vm_data,trss * I suspect some/all of those are broken. They seem to have been * inherited by Linux and AIX from early BSD systems. FreeBSD only * retains tsiz. The prefixed versions come from Debian. * Sun and Digital have none of this crap. The code here comes * from an old Linux ps, and might not be correct for ELF executables. * * AIX TRS size of resident-set (real memory) of text * AIX TSIZ size of text (shared-program) image * FreeBSD tsiz text size (in Kbytes) * 4.3BSD NET/2 trss text resident set size (in Kbytes) * 4.3BSD NET/2 tsiz text size (in Kbytes) *//* kB data size. See drs, tsiz & trs. */static int pr_dsiz(char *restrict const outbuf, const proc_t *restrict const pp){ long dsiz = 0; if(pp->vsize) dsiz += (pp->vsize - pp->end_code + pp->start_code) >> 10; return snprintf(outbuf, COLWID, "%ld", dsiz);}/* kB text (code) size. See trs, dsiz & drs. */static int pr_tsiz(char *restrict const outbuf, const proc_t *restrict const pp){ long tsiz = 0; if(pp->vsize) tsiz += (pp->end_code - pp->start_code) >> 10; return snprintf(outbuf, COLWID, "%ld", tsiz);}/* kB _resident_ data size. See dsiz, tsiz & trs. */static int pr_drs(char *restrict const outbuf, const proc_t *restrict const pp){ long drs = 0; if(pp->vsize) drs += (pp->vsize - pp->end_code + pp->start_code) >> 10; return snprintf(outbuf, COLWID, "%ld", drs);}/* kB text _resident_ (code) size. See tsiz, dsiz & drs. */static int pr_trs(char *restrict const outbuf, const proc_t *restrict const pp){ long trs = 0; if(pp->vsize) trs += (pp->end_code - pp->start_code) >> 10; return snprintf(outbuf, COLWID, "%ld", trs);}/* approximation to: kB of address space that could end up in swap */static int pr_swapable(char *restrict const outbuf, const proc_t *restrict const pp){ return snprintf(outbuf, COLWID, "%ld", pp->vm_data + pp->vm_stack);}/* nasty old Debian thing */static int pr_size(char *restrict const outbuf, const proc_t *restrict const pp){ return snprintf(outbuf, COLWID, "%ld", pp->size);}static int pr_minflt(char *restrict const outbuf, const proc_t *restrict const pp){ long flt = pp->min_flt; if(include_dead_children) flt += pp->cmin_flt; return snprintf(outbuf, COLWID, "%ld", flt);}static int pr_majflt(char *restrict const outbuf, const proc_t *restrict const pp){ long flt = pp->maj_flt; if(include_dead_children) flt += pp->cmaj_flt; return snprintf(outbuf, COLWID, "%ld", flt);}static int pr_lim(char *restrict const outbuf, const proc_t *restrict const pp){ if(pp->rss_rlim == RLIM_INFINITY){ outbuf[0] = 'x'; outbuf[1] = 'x'; outbuf[2] = '\0'; return 2; } return snprintf(outbuf, COLWID, "%5ld", pp->rss_rlim >> 10);}/* should print leading tilde ('~') if process is bound to the CPU */static int pr_psr(char *restrict const outbuf, const proc_t *restrict const pp){ return snprintf(outbuf, COLWID, "%d", pp->processor);}static int pr_rss(char *restrict const outbuf, const proc_t *restrict const pp){ return snprintf(outbuf, COLWID, "%lu", pp->vm_rss);}/* pp->vm_rss * 1000 would overflow on 32-bit systems with 64 GB memory */static int pr_pmem(char *restrict const outbuf, const proc_t *restrict const pp){ unsigned long pmem = 0; pmem = pp->vm_rss * 1000ULL / kb_main_total; if (pmem > 999) pmem = 999; return snprintf(outbuf, COLWID, "%2u.%u", (unsigned)(pmem/10), (unsigned)(pmem%10));}static int pr_lstart(char *restrict const outbuf, const proc_t *restrict const pp){ time_t t; t = time_of_boot + pp->start_time / Hertz; return snprintf(outbuf, COLWID, "%24.24s", ctime(&t));}/* Unix98 specifies a STIME header for a column that shows the start * time of the process, but does not specify a format or format specifier. * From the general Unix98 rules, we know there must not be any spaces. * Most systems violate that rule, though the Solaris documentation * claims to print the column without spaces. (NOT!)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -