~vcs-imports/busybox/trunk

« back to all changes in this revision

Viewing changes to procps/top.c

  • Committer: Denys Vlasenko
  • Author(s): Roger Knecht
  • Date: 2022-06-30 15:18:12 UTC
  • Revision ID: git-v1:20a4f70ecaad79bb932af09b7317a058872cd867
tree: new applet

Adds the tree program to list directories and files in a tree structure.

function                                             old     new   delta
tree_print                                             -     343    +343
scandir64                                              -     330    +330
scandir                                                -     330    +330
tree_main                                              -      86     +86
.rodata                                           105150  105228     +78
packed_usage                                       34511   34557     +46
alphasort64                                            -      31     +31
alphasort                                              -      31     +31
strcoll                                                -       5      +5
applet_names                                        2801    2806      +5
applet_main                                         1616    1620      +4
applet_suid                                          101     102      +1
applet_install_loc                                   202     203      +1
------------------------------------------------------------------------------
(add/remove: 11/0 grow/shrink: 6/0 up/down: 1291/0)          Total: 1291 bytes

Signed-off-by: Roger Knecht <rknecht@pm.me>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
619
619
        unsigned busy_jifs;
620
620
#endif
621
621
 
 
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")
 
627
                " COMMAND");
 
628
        lines_rem--;
 
629
 
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:
629
 
 */
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"
633
635
#else
636
638
# define SHOW_STAT(name) name
637
639
# define FMT "%4u%%"
638
640
#endif
639
 
 
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")
645
 
                " COMMAND");
646
 
        lines_rem--;
647
 
 
648
641
        /*
649
642
         * %VSZ = s->vsz/MemTotal
650
643
         */
713
706
                ppu = ppubuf;
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
720
710
                         */
721
711
                        char *p, *pp;
722
712
                        if (*ppu == ' ') {
860
850
        (*lines_rem_p) -= 3;
861
851
}
862
852
 
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])
865
854
{
 
855
        /* see http://en.wikipedia.org/wiki/Tera */
866
856
        smart_ulltoa5(ul, buf, " mgtpezy")[0] = ' ';
867
857
}
868
 
static void ulltoa4_and_space(unsigned long long ul, char buf[5])
869
 
{
870
 
        smart_ulltoa4(ul, buf, " mgtpezy")[0] = ' ';
871
 
}
872
858
 
873
859
static NOINLINE void display_topmem_process_list(int lines_rem, int scr_width)
874
860
{
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);
897
 
                if (n > 7) {
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 */
902
 
                } else {
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]);
909
 
                }
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 */
 
886
                        line_buf[5] = '.';
 
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);