[Toybox] Interpreting top -H -O CPU output

enh enh at google.com
Thu Feb 7 13:44:41 PST 2019


fwiw, running this:

#include <thread>

int main() {
std::thread t1([]() { while (true) {} });
std::thread t2([]() { while (true) {} });
std::thread t3([]() { while (true) {} });
std::thread t4([]() { while (true) {} });
while (true) {}
return 0;
}

got me (from desktop top):

desktop top -H:
 82877 enh       20   0   48052   1652   1492 R  99.9   0.0   1:22.08 threads
 82878 enh       20   0   48052   1652   1492 R  99.9   0.0   1:22.08 threads
 82879 enh       20   0   48052   1652   1492 R  99.9   0.0   1:22.08 threads
 82880 enh       20   0   48052   1652   1492 R  99.9   0.0   1:22.08 threads
 82881 enh       20   0   48052   1652   1492 R  99.9   0.0   1:22.08 threads

desktop top:
 82877 enh       20   0   48052   1652   1492 R 498.7   0.0   8:21.31 threads

so 5 * 99.9% for -H, and 1 * 500% for no -H. also, note how all the
values in the first column differ: although desktop top calls the
field PID, it is actually showing the TID there.

toybox top -H gets the main thread wrong:

 82877 enh          20   0 1.6M 1.6M 1.4M R  504   0.0   3:55.29 threads
 82877 enh          20   0 1.6M 1.6M 1.4M R  101   0.0   0:47.05 threads
 82877 enh          20   0 1.6M 1.6M 1.4M R  101   0.0   0:47.05 threads
 82877 enh          20   0 1.6M 1.6M 1.4M R  101   0.0   0:47.05 threads
 82877 enh          20   0 1.6M 1.6M 1.4M R  100   0.0   0:47.06 threads

toybox top:

 82877 enh          20   0 1.6M 1.6M 1.4M R  500   0.0  15:09.69 threads

note also that although RES and SHR are consistent between
toybox/desktop, the VIRT column is quite different. looks like the
tricksy code for those fields was invalidated at some point when slots
were renumbered. i've sent a patch for that to the list separately.

On Wed, Feb 6, 2019 at 3:44 PM enh <enh at google.com> wrote:
>
> On Wed, Feb 6, 2019 at 2:34 PM Rob Landley <rob at landley.net> wrote:
> >
> > On 2/6/19 12:56 PM, Dmitry Shmidt wrote:
> > > Hi Rob,
> > >
> > > I am using your top tool implementation for Android from
> > > toybox project.
> > > I am wondering if in the mode where it shows cpu usage
> > > per thread, the total usage per task (process) is included in initial
> > > ("main") thread?
> >
> > I don't really use threads much, Elliott provided most of the use cases for that.
> >
> > > For example: com.google.android.youtube.tv
> > > <http://com.google.android.youtube.tv> shows 153% usage
> > > for main thread and some more for other threads, like MainWebView - 31%.
> > > Is this 31% included in 153% report or not?
> >
> > It should never report more than 100%, so it sounds like it is combining CPU
> > usage from threads into the parent, yes.
> >
> > Hmmm... Top -H isn't showing TID by default,
>
> yeah, that seemed a bit weird to me, but it matches what the
> traditional implementation did. (though threads aren't as common on
> the desktop.)
>
> interestingly, i notice that our numbers don't add up. on the desktop,
> total == running + sleeping, but our sleeping count is a lot lower
> than it should be. (the desktop also says "Tasks:" or "Threads:"
> depending on whether you supplied -H, and we don't.)
>
> we also don't do a good job of sizing the PID field on machines with a
> large pid_max. this fixes both of those minor issues, but between the
> removal of the `const` on the array and the floating point math i
> assume you'll want to do this differently :-)
>
> diff --git a/toys/posix/ps.c b/toys/posix/ps.c
> index 079bdbd6..50f52b41 100644
> --- a/toys/posix/ps.c
> +++ b/toys/posix/ps.c
> @@ -314,9 +314,9 @@ struct procpid {
>  struct typography {
>    char *name, *help;
>    signed char width, slot;
> -} static const typos[] = TAGGED_ARRAY(PS,
> +} static /*const*/ typos[] = TAGGED_ARRAY(PS,
>    // Numbers. (What's in slot[] is what's displayed, sorted numerically.)
> -  {"PID", "Process ID", 5, SLOT_pid},
> +  {"PID", "Process ID", 2, SLOT_pid},
>    {"PPID", "Parent Process ID", 5, SLOT_ppid},
>    {"PRI", "Priority (dynamic 0 to 139)", 3, SLOT_priority},
>    {"NI", "Niceness (static 19 to -20)", 3, SLOT_nice},
> @@ -1262,6 +1262,16 @@ static void default_ko(char *s, void *fields, char *err,
> struct arg_list *arg)
>    if (x) help_help();
>  }
>
> +static void init_pid_width(void)
> +{
> +  FILE *fp = xfopen("/proc/sys/kernel/pid_max", "re");
> +  int pid_max;
> +
> +  fscanf(fp, "%d", &pid_max);
> +  fclose(fp);
> +  typos[0].width = ceil(log10(pid_max));
> +}
> +
>  void ps_main(void)
>  {
>    char **arg;
> @@ -1270,6 +1280,7 @@ void ps_main(void)
>    int i;
>
>    TT.ticks = sysconf(_SC_CLK_TCK); // units for starttime/uptime
> +  init_pid_width();
>
>    if (-1 != (i = tty_fd())) {
>      struct stat st;
> @@ -1546,8 +1557,9 @@ static void top_common(
>            for (i = 0; i<mix.count; i++)
>              run[1+stridx("RSTZ", *string_field(mix.tb[i], &field))]++;
>            sprintf(toybuf,
> -            "Tasks: %d total,%4ld running,%4ld sleeping,%4ld stopped,"
> -            "%4ld zombie", mix.count, run[1], run[2], run[3], run[4]);
> +            "%ss: %d total, %3ld running, %3ld sleeping, %3ld stopped, "
> +            "%3ld zombie", FLAG(H)?"Thread":"Task", mix.count,
> +            run[1], run[2], run[3], run[4]);
>            lines = header_line(lines, 0);
>
>            if (readfile("/proc/meminfo", toybuf, sizeof(toybuf))) {
> @@ -1697,6 +1709,7 @@ static void top_setup(char *defo, char *defk)
>  {
>    TT.ticks = sysconf(_SC_CLK_TCK); // units for starttime/uptime
>    TT.tty = tty_fd() != -1;
> +  init_pid_width();
>
>    // Are we doing "batch" output or interactive?
>    if (FLAG(b)) TT.width = TT.height = 99999;
>
>
> > and top -H -O TID is never showing
> > more than one instance of the same PID... until I sort by TID, and then I get a
> > bunch of chrome threads under the same PID, each with 1.5% of the CPU. So yeah,
> > CPU usage is per process here, not per thread.
> >
> > I'm trying to cut a release, but let me add that to the todo list for next
> > release. (I should try to come up with a better test case because y system's way
> > too loaded normally...)
> >
> > Rob
> > _______________________________________________
> > Toybox mailing list
> > Toybox at lists.landley.net
> > http://lists.landley.net/listinfo.cgi/toybox-landley.net



More information about the Toybox mailing list