How to get HZ with ADB shell

2019-05-26 02:37发布

How can I know HZ value of Android kernel through ADB shell? (without any coding)

I checked How to check HZ in the terminal?, but this is not work with Android ADB shell.

Any suggestion?

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-05-26 03:23

Provided you have busybox installed:

T1=`grep gp_timer /proc/interrupts| busybox awk '{print$2}'`;sleep 1;T2=`grep gp_timer /proc/interrupts| busybox awk '{print$2}'`;echo $((T2-T1))
查看更多
ゆ 、 Hurt°
3楼-- · 2019-05-26 03:35

Theory

You can derive the kernel space HZ value by taking an elapsed time in jiffies and dividing it by the elapsed time in seconds.

You can get both the system uptime in jiffies and in nanoseconds from the /proc/timer_list file.

  • System uptime in nanoseconds
    • Search for the now at <nanoseconds> nsecs line
    • <nanoseconds> will be the uptime in nanoseconds
  • System uptime in jiffies
    • Search for the jiffies: <jiffies> line (any processor will do)
    • <jiffies> will be the uptime in jiffies
      • NOTE: This value is typically offset forward by INITIAL_JIFFIES in the kernel -- so it may not start counting at 0. Nevertheless, this shouldn't affect the outcome of our calculation since we're concerned with the elapsed time rather than obsolute uptime.

By taking these two values over an elapsed period of time, you can calculate the HZ value using the following equation:

         (jiffies_after - jiffies_before)
HZ = -----------------------------------------
     ((nsecs_after - nsecs_before)/1000000000)

Example

If you happen to have awk (perhaps via BusyBox), you can automate this calculation:

$ awk '/^now at/ { nsec=$3; } /^jiffies/ { jiffies=$2; } END { print nsec, jiffies; system("sleep 1"); }' /proc/timer_list | awk 'NR==1 { nsec1=$1; jiffies1=$2; } /^now at/ NR>1 { nsec2=$3; } /^jiffies/ NR>1 { jiffies2=$2; } END { dsec=(nsec2-nsec1)/1e9; djiff=(jiffies2-jiffies1); print int(djiff/dsec); }' - /proc/timer_list

Due to rounding errors, the HZ value may be slightly off; you might want to perform this calculation several times and average it. Most modern kernels have kernel space HZ set to 250.


Breakdown

Here's the same command spread over several lines to clarify how it works:

$ awk '
> /^now at/ { nsec=$3; }
> /^jiffies/ { jiffies=$2; }
> END {
>       print nsec, jiffies;
>       system("sleep 1");
> }
> ' /proc/timer_list | awk '
> NR==1 { nsec1=$1; jiffies1=$2; }
> /^now at/ NR>1 { nsec2=$3; }
> /^jiffies/ NR>1 { jiffies2=$2; }
> END {
>       dsec=(nsec2-nsec1)/1e9;
>       djiff=(jiffies2-jiffies1);
>       print int(djiff/dsec);
> }
> ' - /proc/timer_list
  • /^now at/ { nsec=$3; }
    • Save the number of nanoseconds to the variable nsec
  • /^jiffies/ { jiffies=$2; }
    • Save the number of jiffies to the variable jiffies
  • END {
    • print nsec, jiffies;
      • Print out nanoseconds and jiffies delimited by a space
    • system("sleep 1");
      • Sleep for a second to prevent a division by 0 when we calculate the HZ value
  • ' /proc/timer_list | awk '
    • Process the /proc/timer_list file
    • Pipe output to a new instance of awk
  • NR==1 { nsec1=$1; jiffies1=$2; }
    • Set the nsec and jiffies values from the previous awk to nsec1 and jiffies1 respectively
  • /^now at/ NR>1 { nsec2=$3; }
    • Save the number of nanoseconds to the variable nsec2
  • /^jiffies/ NR>1 { jiffies2=$2; }
    • Save the number of jiffies to the variable jiffies2
  • END {
    • dsec=(nsec2-nsec1)/1e9;
      • Calculate the change in seconds
    • djiff=(jiffies2-jiffies1);
      • Calculate the change in jiffies
    • print int(djiff/dsec);
      • Print out the HZ value as an integer
  • ' - /proc/timer_list
    • Process the standard input followed by the /proc/timer_list file
查看更多
登录 后发表回答