📄 oldps.c
字号:
if (!(CL_sort || CL_forest)) /* when sorting and forest are both */ retbuf = &buf; /* off we can use a static buffer */ while ((next = readproc(tab,retbuf))) { /* read next process */ n++; /* Now either: */ if (CL_forest) { /* add process to tree */ /* FIXME: this static is disgusting, but to keep binary * compatibility within the 1.2 series I'll just go from * 256 bytes to 1024 bytes, which should be good enough * for most people.... :-( In the future, the format * format function should probably dynamically allocate * strings. */ static char s[1024]; (mode[CL_fmt].format)(s, next); if (CL_fmt != PS_V && CL_fmt != PS_M) show_time(s+strlen(s), next); add_node(s, next); } else if (CL_sort) { /* add process to table */ ptable = xrealloc(ptable, n*sizeof(proc_t*)); ptable[n-1] = next; } else { /* or show it right away */ show_a_proc(&buf, maxcmd); if (buf.cmdline) free((void*)(buf.cmdline[0])); if (buf.environ) free((void*)(buf.environ[0])); } } if (!n) { fprintf(stderr, "No processes available.\n"); exit(1); } if (CL_sort && !CL_forest) { /* just print sorted table */ int i; qsort(ptable, n, sizeof(proc_t*), (void*)mult_lvl_cmp); for (i = 0; i < n; i++) { show_a_proc(ptable[i], maxcmd); freeproc(ptable[i]); } free(ptable); } else if (CL_forest) show_forest();}/* show the trailing command and environment in available space. * use abbreviated cmd if requested, NULL list, or singleton NULL string */void show_cmd_env(char* tskcmd, char** cmd, char** env, unsigned maxch) { if (CL_kern_comm) /* no () when explicit request for tsk cmd */ maxch = print_str(stdout, tskcmd, maxch); else if (!cmd || !*cmd || (!cmd[1] && !*cmd)) { /* no /proc//cmdline ==> bounding () */ if (maxch) { fputc('(', stdout); maxch--; } maxch = print_str(stdout, tskcmd, maxch); if (maxch) { fputc(')', stdout); maxch--; } } else maxch = print_strlist(stdout, cmd, " ", maxch); if (CL_show_env && env) print_strlist(stdout, env, " ", maxch); fputc('\n', stdout);}/* format a single process for output. */void show_a_proc(proc_t* p, unsigned maxch) { static char s[2048]; (mode[CL_fmt].format)(s, p); if (CL_fmt != PS_V && CL_fmt != PS_M) show_time(s+strlen(s), p); printf("%s", s); show_cmd_env(p->cmd, p->cmdline, p->environ, maxch);}/* The format functions for the various formatting modes follow */void show_short(char *s, proc_t *p) { sprintf(s, "%5d %3s %s", p->pid, ttyc(p->tty,p->pid), status(p));}void show_long(char *s, proc_t *p) { char wchanb[32]; if (GL_wchan_nout) sprintf(wchanb, " %-9lx ", p->wchan & 0xffffffff); else sprintf(wchanb, "%-11.11s", wchan(p->wchan)); sprintf(s, "%6lx %5d %5d %5d %3ld %3ld %6ld %5ld %-11.11s %s%3s", p->flags, p->euid, p->pid, p->ppid, p->priority, p->nice, p->vsize >> 10, p->rss << (PAGE_SHIFT - 10), wchanb, status(p), ttyc(p->tty,p->pid));}void show_jobs(char *s, proc_t *p) { sprintf(s, "%5d %5d %5d %5d %3s %5d %s %5d ", p->ppid, p->pid, p->pgrp, p->session, ttyc(p->tty,p->pid), p->tpgid, status(p), p->euid);}void show_user(char *s, proc_t *p) { long pmem, total_time, seconds; time_t start; unsigned int pcpu; if (CL_num_outp) s += sprintf(s, "%5d ", p->euid); else s += sprintf(s, "%-8s ", p->euser); /* Hertz, being UL, forces 64-bit calculation on 64-bit machines */ seconds = (((GL_current_time * Hertz) - p->start_time) / Hertz); start = GL_time_now - seconds; total_time = (p->utime + p->stime + (CL_Sum ? p->cutime + p->cstime : 0)); pcpu = seconds ? (total_time * 10 * 100 / Hertz) / seconds : 0; if (pcpu > 999) pcpu = 999; pmem = p->rss * 1000 / (GL_main_mem >> PAGE_SHIFT); sprintf(s, "%5d %2u.%u %2ld.%ld %5ld %5ld %3s %s %.6s ", p->pid, pcpu / 10, pcpu % 10, pmem / 10, pmem % 10, p->vsize >> 10, p->rss << (PAGE_SHIFT - 10), ttyc(p->tty,p->pid), status(p), ctime(&start) + (GL_time_now - start > 3600*24 ? 4 : 10));}/* TODO: this is broken... but does anyone care? */void show_sig(char *s, proc_t *p) {#undef SIGNAL_STRING /* not filled in by the function oldps calls */#ifdef SIGNAL_STRING#define TAIL(s) ((s)+(long)strlen(s)-8) sprintf(s, "%5d %5d %s %s %s %s %s %3s ", p->euid, p->pid, TAIL(p->signal), TAIL(p->blocked), TAIL(p->sigignore), TAIL(p->sigcatch), status(p), ttyc(p->tty,p->pid) );#else sprintf(s, "%5d %5d %08x %08x %08x %08x %s %3s ", p->euid, p->pid, (unsigned)p->signal, (unsigned)p->blocked, (unsigned)p->sigignore, (unsigned)p->sigcatch, status(p), ttyc(p->tty,p->pid) );#endif}void show_vm(char *s, proc_t *p) { int pmem; s += sprintf(s,"%5d %3s %s", p->pid, ttyc(p->tty,p->pid), status(p)); show_time(s, p); s += strlen(s); s += sprintf(s, " %6ld %4ld %4ld %4ld ", p->maj_flt + (CL_Sum ? p->cmaj_flt : 0), p->vsize ? (p->end_code - p->start_code) >> 10 : 0, p->vsize ? (p->vsize - p->end_code + p->start_code) >> 10 : 0, p->rss << (PAGE_SHIFT - 10)); if(p->rss_rlim == RLIM_INFINITY) s += sprintf(s, " xx "); else s += sprintf(s, "%5ld ", p->rss_rlim >> 10); pmem = p->rss * 1000 / (GL_main_mem >> PAGE_SHIFT); sprintf(s, "%2d.%d ", pmem / 10, pmem % 10);}void show_m(char *s, proc_t *p) { sprintf(s, "%5d %3s %6ld %6ld %5ld %5ld %5ld %5ld %5ld %5ld %5ld %3ld ", p->pid, ttyc(p->tty,p->pid), p->maj_flt + (CL_Sum ? p->cmaj_flt : 0), p->min_flt + (CL_Sum ? p->cmin_flt : 0), p->trs << CL_pg_shift, p->drs << CL_pg_shift, p->size << CL_pg_shift, (p->size - p->resident) << CL_pg_shift, p->resident << CL_pg_shift, p->share << CL_pg_shift, p->lrs << CL_pg_shift, p->dt);}void show_regs(char *s, proc_t *p) { char time1[16], time2[16]; sprintf(s, "%2ld %5d %8lx %8lx %8lx %s %s %s %3s ", p->start_code >> 26, p->pid, p->start_stack, p->kstk_esp, p->kstk_eip, prtime(time1, p->timeout, GL_current_time*Hertz), prtime(time2, p->it_real_value, 0), status(p), ttyc(p->tty,p->pid));}char *prtime(char *s, unsigned long t, unsigned long rel) { if (t == 0) { sprintf(s, " "); return s; } if ((long) t == -1) { sprintf(s, " xx"); return s; } if ((long) (t -= rel) < 0) t = 0; if (t > 9999) sprintf(s, "%5lu", t / 100); else sprintf(s, "%2lu.%02lu", t / 100, t % 100); return s;}void show_time(char *s, proc_t * p) { unsigned t; t = (p->utime + p->stime) / Hertz; if (CL_Sum) t += (p->cutime + p->cstime) / Hertz; sprintf(s, "%3d:%02d ", t / 60, t % 60);}/* fancy process family tree based cmdline printing. Building the tree should be relegated to libproc and only the printing logic should remain here.*/struct tree_node * node; /* forest mode globals */int nodes = 0;int maxnodes = 0;void add_node(char *s, proc_t *task) { if (maxnodes == 0) { maxnodes = 64; node = (struct tree_node *) xmalloc(sizeof(struct tree_node) * maxnodes); } if (nodes >= maxnodes) { maxnodes *= 2; node = (struct tree_node *) xrealloc(node, sizeof(struct tree_node) * maxnodes); } node[nodes].proc = task; node[nodes].pid = task->pid; node[nodes].ppid = task->ppid; node[nodes].line = strdup(s); node[nodes].cmd = task->cmd; node[nodes].cmdline = task->cmdline; node[nodes].environ = task->environ; node[nodes].children = 0; node[nodes].have_parent = 0; nodes++;}int node_cmp(const void *s1, const void *s2) { struct tree_node *n1 = (struct tree_node *) s1; struct tree_node *n2 = (struct tree_node *) s2; return n1->pid - n2->pid;}void show_tree(int n, int depth, char *continued) { int i, cols = 0; fprintf(stdout, "%s", node[n].line); for (i = 0; i < depth; i++) { if (cols + 4 >= cmdspc - 1) break; if (i == depth - 1) printf(" \\_ "); else if (continued[i]) printf(" | "); else printf(" "); cols += 4; } show_cmd_env(node[n].cmd, node[n].cmdline, node[n].environ, cmdspc - cols); for (i = 0; i < node[n].children; i++) { continued[depth] = i != node[n].children - 1; show_tree(node[n].child[i], depth + 1, continued); }}void show_forest() { register int i, j; int parent; char continued[1024]; if (CL_sort) qsort((void*)node, nodes, sizeof(struct tree_node), (void*)node_mult_lvl_cmp); for (i = 0; i < nodes; i++) { if (node[i].ppid > 1 && node[i].pid != node[i].ppid) { parent = -1; for (j=0; j<nodes; j++) if (node[j].pid==node[i].ppid) parent = j; } else parent = -1; if (parent >= 0) { node[i].have_parent++; if (node[parent].children == 0) { node[parent].child = (int*)xmalloc(16 * sizeof(int*)); node[parent].maxchildren = 16; } else if (node[parent].children == node[parent].maxchildren) { node[parent].maxchildren *= 2; node[parent].child = (int*)xrealloc(node[parent].child, node[parent].maxchildren * sizeof(int*)); } node[parent].child[node[parent].children++] = i; } } for (i = 0; i < nodes; i++) { if (!node[i].have_parent) show_tree(i, 0, continued); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -