619
619
unsigned busy_jifs;
622
/* what info of the processes is shown */
623
printf(OPT_BATCH_MODE ? "%.*s" : ESC"[7m" "%.*s" ESC"[m", scr_width,
624
" PID PPID USER STAT VSZ %VSZ"
625
IF_FEATURE_TOP_SMP_PROCESS(" CPU")
626
IF_FEATURE_TOP_CPU_USAGE_PERCENTAGE(" %CPU")
622
630
#if ENABLE_FEATURE_TOP_DECIMALS
623
631
# define UPSCALE 1000
624
typedef struct { unsigned quot, rem; } bb_div_t;
625
/* Used to have "div_t name = div((val), 10)" here
626
* (IOW: intended to use libc-compatible way to divide and use
627
* both result and remainder, but musl does not inline div()...)
628
* Oh well. Modern compilers detect "N/d, N%d" idiom by themselves:
630
# define CALC_STAT(name, val) bb_div_t name = { (val) / 10, (val) % 10 }
632
# define CALC_STAT(name, val) div_t name = div((val), 10)
631
633
# define SHOW_STAT(name) name.quot, '0'+name.rem
632
634
# define FMT "%3u.%c"
636
638
# define SHOW_STAT(name) name
637
639
# define FMT "%4u%%"
640
/* what info of the processes is shown */
641
printf(OPT_BATCH_MODE ? "%.*s" : ESC"[7m" "%.*s" ESC"[m", scr_width,
642
" PID PPID USER STAT VSZ %VSZ"
643
IF_FEATURE_TOP_SMP_PROCESS(" CPU")
644
IF_FEATURE_TOP_CPU_USAGE_PERCENTAGE(" %CPU")
649
642
* %VSZ = s->vsz/MemTotal
714
707
if (n != 6+6+8) {
715
708
/* Format PID PPID USER part into 6+6+8 chars:
716
* shrink PID/PPID if possible, then truncate USER.
717
* Tested on Linux 5.18.0:
718
* sysctl kernel.pid_max=4194304 is the maximum allowed,
719
* so PID and PPID are 7 chars wide at most.
709
* shrink PID/PPID if possible, then truncate USER
722
712
if (*ppu == ' ') {
860
850
(*lines_rem_p) -= 3;
863
/* see http://en.wikipedia.org/wiki/Tera */
864
static void ulltoa5_and_space(unsigned long long ul, char buf[6])
853
static void ulltoa6_and_space(unsigned long long ul, char buf[6])
855
/* see http://en.wikipedia.org/wiki/Tera */
866
856
smart_ulltoa5(ul, buf, " mgtpezy")[0] = ' ';
868
static void ulltoa4_and_space(unsigned long long ul, char buf[5])
870
smart_ulltoa4(ul, buf, " mgtpezy")[0] = ' ';
873
859
static NOINLINE void display_topmem_process_list(int lines_rem, int scr_width)
894
880
while (--lines_rem >= 0) {
895
881
/* PID VSZ VSZRW RSS (SHR) DIRTY (SHR) COMMAND */
896
882
int n = sprintf(line_buf, "%5u ", s->pid);
898
/* PID is 7 chars long (up to 4194304) */
899
ulltoa4_and_space(s->vsz , &line_buf[8]);
900
ulltoa4_and_space(s->vszrw, &line_buf[8+5]);
901
/* the next field (RSS) starts at 8+10 = 3*6 */
903
if (n == 7) /* PID is 6 chars long */
904
ulltoa4_and_space(s->vsz, &line_buf[7]);
905
/* the next field (VSZRW) starts at 7+5 = 2*6 */
906
else /* PID is 5 chars or less */
907
ulltoa5_and_space(s->vsz, &line_buf[6]);
908
ulltoa5_and_space(s->vszrw, &line_buf[2*6]);
910
ulltoa5_and_space(s->rss , &line_buf[3*6]);
911
ulltoa5_and_space(s->rss_sh , &line_buf[4*6]);
912
ulltoa5_and_space(s->dirty , &line_buf[5*6]);
913
ulltoa5_and_space(s->dirty_sh, &line_buf[6*6]);
914
ulltoa5_and_space(s->stack , &line_buf[7*6]);
883
ulltoa6_and_space(s->vsz , &line_buf[1*6]);
884
if (n > 7 || (n == 7 && line_buf[6] != ' '))
885
/* PID and VSZ are clumped together, truncate PID */
887
ulltoa6_and_space(s->vszrw , &line_buf[2*6]);
888
ulltoa6_and_space(s->rss , &line_buf[3*6]);
889
ulltoa6_and_space(s->rss_sh , &line_buf[4*6]);
890
ulltoa6_and_space(s->dirty , &line_buf[5*6]);
891
ulltoa6_and_space(s->dirty_sh, &line_buf[6*6]);
892
ulltoa6_and_space(s->stack , &line_buf[7*6]);
915
893
line_buf[8*6] = '\0';
916
894
if (scr_width > (int)MIN_WIDTH) {
917
895
read_cmdline(&line_buf[8*6], scr_width - MIN_WIDTH, s->pid, s->comm);