top: code shrink

function                                             old     new   delta
display_process_list                                1186    1168     -18

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2022-05-10 14:04:34 +02:00
parent 69f82e305b
commit 1099a27696

View File

@ -690,7 +690,7 @@ static NOINLINE void display_process_list(int lines_rem, int scr_width)
s = top + G_scroll_ofs; s = top + G_scroll_ofs;
while (--lines_rem >= 0) { while (--lines_rem >= 0) {
int n; int n;
char *pp; char *ppu;
char ppubuf[sizeof(int)*3 * 2 + 12]; char ppubuf[sizeof(int)*3 * 2 + 12];
char vsz_str_buf[8]; char vsz_str_buf[8];
unsigned col; unsigned col;
@ -703,37 +703,35 @@ static NOINLINE void display_process_list(int lines_rem, int scr_width)
smart_ulltoa5(s->vsz, vsz_str_buf, " mgtpezy"); smart_ulltoa5(s->vsz, vsz_str_buf, " mgtpezy");
/* PID PPID USER STAT VSZ %VSZ [%CPU] COMMAND */ /* PID PPID USER STAT VSZ %VSZ [%CPU] COMMAND */
n = sprintf(ppubuf, "%5u %5u %-8.8s", s->pid, s->ppid, get_cached_username(s->uid)); n = sprintf(ppubuf, "%5u %5u %-8.8s", s->pid, s->ppid, get_cached_username(s->uid));
ppu = ppubuf;
if (n != 6+6+8) { if (n != 6+6+8) {
/* Format PID PPID USER part into 6+6+8 chars: /* Format PID PPID USER part into 6+6+8 chars:
* shrink PID/PPID if possible, then truncate USER * shrink PID/PPID if possible, then truncate USER
*/ */
char *p; char *p, *pp;
pp = ppubuf; if (*ppu == ' ') {
if (*pp == ' ') {
do { do {
pp++, n--; ppu++, n--;
if (n == 6+6+8) if (n == 6+6+8)
goto shortened; goto shortened;
} while (*pp == ' '); } while (*ppu == ' ');
overlapping_strcpy(ppubuf, pp); /* shrink PID */
} }
pp = p = skip_non_whitespace(ppubuf) + 1; pp = p = skip_non_whitespace(ppu) + 1;
if (*p == ' ') { if (*p == ' ') {
do do
p++, n--; p++, n--;
while (n != 6+6+8 && *p == ' '); while (n != 6+6+8 && *p == ' ');
overlapping_strcpy(pp, p); /* shrink PPID */ overlapping_strcpy(pp, p); /* shrink PPID */
} }
ppubuf[6+6+8] = '\0'; /* truncate USER */ ppu[6+6+8] = '\0'; /* truncate USER */
} }
pp = ppubuf;
shortened: shortened:
col = snprintf(line_buf, scr_width, col = snprintf(line_buf, scr_width,
"\n" "%s %s %.5s" FMT "\n" "%s %s %.5s" FMT
IF_FEATURE_TOP_SMP_PROCESS(" %3d") IF_FEATURE_TOP_SMP_PROCESS(" %3d")
IF_FEATURE_TOP_CPU_USAGE_PERCENTAGE(FMT) IF_FEATURE_TOP_CPU_USAGE_PERCENTAGE(FMT)
" ", " ",
pp, ppu,
s->state, vsz_str_buf, s->state, vsz_str_buf,
SHOW_STAT(pmem) SHOW_STAT(pmem)
IF_FEATURE_TOP_SMP_PROCESS(, s->last_seen_on_cpu) IF_FEATURE_TOP_SMP_PROCESS(, s->last_seen_on_cpu)