How to Check Hz in the Terminal

How to check HZ in the terminal?

There's no uniform answer to this questions, as in some cases your kernel may be compiled "tickless" and not use a regular timer interrupt at all. But if you're on a traditional kernel and a traditional distro, you can find the current kernel's .config file under /boot with something like grep 'CONFIG_HZ=' /boot/config-$(uname -r).

How to get HZ with ADB shell


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

Determine CPU frequency from command line

This should work:

grep 'cpu MHz' /proc/cpuinfo | head -1 | awk -F: '{print $2/1024}'

(when I run this in my cygwin terminal I get 1.85156).

C++ Linux: Get the refresh rate of a monitor

Use XRandr API (man 3 Xrandr). See here for an example:

  • http://www.blitzbasic.com/Community/posts.php?topic=86911

You can also look at the code for xrandr(1).


Edit1: For posterity sake:

Sample code slightly adjusted so its more of a demo:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <iostream>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>

int main()
{
int num_sizes;
Rotation current_rotation;

Display *dpy = XOpenDisplay(NULL);
Window root = RootWindow(dpy, 0);
XRRScreenSize *xrrs = XRRSizes(dpy, 0, &num_sizes);
//
// GET CURRENT RESOLUTION AND FREQUENCY
//
XRRScreenConfiguration *conf = XRRGetScreenInfo(dpy, root);
short current_rate = XRRConfigCurrentRate(conf);
SizeID current_size_id = XRRConfigCurrentConfiguration(conf, ¤t_rotation);

int current_width = xrrs[current_size_id].width;
int current_height = xrrs[current_size_id].height;
std::cout << "current_rate = " << current_rate << std::endl;
std::cout << "current_width = " << current_width << std::endl;
std::cout << "current_height = " << current_height << std::endl;

XCloseDisplay(dpy);
}

Compile with:

g++ 17797636.cpp -o 17797636 -lX11 -lXrandr

Output:

$ ./17797636 
current_rate = 50
current_width = 1920
current_height = 1080

Bash script to find the frequency of every letter in a file

Just one awk command

awk -vFS="" '{for(i=1;i<=NF;i++)w[$i]++}END{for(i in w) print i,w[i]}' file

if you want case insensitive, add tolower()

awk -vFS="" '{for(i=1;i<=NF;i++)w[tolower($i)]++}END{for(i in w) print i,w[i]}' file

and if you want only characters,

awk -vFS="" '{for(i=1;i<=NF;i++){ if($i~/[a-zA-Z]/) { w[tolower($i)]++} } }END{for(i in w) print i,w[i]}' file

and if you want only digits, change /[a-zA-Z]/ to /[0-9]/

if you do not want to show unicode, do export LC_ALL=C

frequency count for file column in bash

You can just awk to do this:

awk -F '|' '{freq[$8]++} END{for (i in freq) print freq[i], i}' file

This awk command uses | as delimiter and uses an array seen with key as $8. When it finds a key $8 increments the frequency (value) by 1.
Btw you need to add custom delimiter | in your command and use it like this:

awk -F '|' '{print $8}' file | sort | uniq -c

How to create a frequency list of every word in a file?

Not sed and grep, but tr, sort, uniq, and awk:

% (tr ' ' '\n' | sort | uniq -c | awk '{print $2"@"$1}') <<EOF
This is a file with many words.
Some of the words appear more than once.
Some of the words only appear one time.
EOF

a@1
appear@2
file@1
is@1
many@1
more@1
of@2
once.@1
one@1
only@1
Some@2
than@1
the@2
This@1
time.@1
with@1
words@2
words.@1

In most cases you also want to remove numbers and punctuation, convert everything to lowercase (otherwise "THE", "The" and "the" are counted separately) and suppress an entry for a zero length word. For ASCII text you can do all these with this modified command:

sed -e  's/[^A-Za-z]/ /g' text.txt | tr 'A-Z' 'a-z' | tr ' ' '\n' | grep -v '^$'| sort | uniq -c | sort -rn


Related Topics



Leave a reply



Submit