Getting hertz of linux system using GO


(Bidhan Bastola) #1

How can i get hertz of linux system using go lang ??
Is there any packages that help to get hertz directly ???


(Lutz Horn) #2

This information can be retrieved from /proc/cpuinfo. On the bash:

$ cat /proc/cpuinfo | grep "cpu MHz"
cpu MHz         : 800.000
cpu MHz         : 800.000
cpu MHz         : 800.000
cpu MHz         : 800.000

(Bidhan Bastola) #3

can i add all the cpu hertz to calculate overall cpu hertz ?


(Lutz Horn) #4

No. The output tells me that my computer has four cores with 800 MHz each.

But how is this related to programming in Go?


(Bidhan Bastola) #5

I want to find the cpu usage of each process running in linux system
and i have found a formula : https://stackoverflow.com/questions/16726779/how-do-i-get-the-total-cpu-usage-of-an-application-from-proc-pid-stat

And i am using GO lang to extract this information


(Lutz Horn) #6

How are you using Go for this. Please show us the code you have tried. Does it work? If it does not work, please tell us what happens: Do you get an error? Is the result not what you expect?


(Bidhan Bastola) #7

Give me a time and i will show you code if some problems arise

For now can you explain this function

func newUnixProcess(pid int) ( *Unixprocess, error){
	p := &Unixprocess{pid:pid}
	return p, p.Refresh()
}

Here Unixprocess is the structure Refresh() is another function
What does p variable denotes ?


(Lutz Horn) #8

There is no Unixprocess in the Go library. Which library do you import to get it? Or did you write it yourself?


(Bidhan Bastola) #9

I write it myself


(Lutz Horn) #10

Then you should know what p is.

I have the impression that you and @Socrates27 ask very similar questions. Are you two working for the same company or do you have to solve the same class assignments?


(Dhirendra ) #11

Yes Sir we are classmates and both of us are beginners in go


(Bidhan Bastola) #12

&Unixprocess{pid:pid}

What does this mean ?


(Lutz Horn) #13

Did you took the Go tour? It includes sections about pointers and struct literals. These two concepts apply to

&Unixprocess{pid:pid}

(Bidhan Bastola) #14

thank you
I will take a GO tour on this sections


(Norbert Melzer) #15

The “Hertz” mentioned in that SO thread is different from the CPU frequency.

The SO thread is talking about the clocks internal resolution and you need to ask your system using the CLI tool getconf as mentioned in that thread. For C it also mentiones a function call as an alternative.

I’m not sure if there is a binding available to that function, so I’d go with simply using os/exec to execute getconf CLK_TCK and parse its output.

Currently I can’t see another way to get the required information from inside of go.


(Bidhan Bastola) #16

I dont get it can you please show me a code as an example


(Norbert Melzer) #17

Something like this:

package main

import (
	"bytes"
	"fmt"
	"os/exec"
)

func main() {
	cmd := exec.Command("getconf", "CLK_TCK")
	out := new(bytes.Buffer)
	cmd.Stdout = out
	_ = cmd.Run()
	fmt.Printf("CLK_TCK: %v", out.String())
}

Of course you should add some error handling around it, and you will need to properly parse the content of out to make it an integer to actually work with.

But this is the direction you need to go.

Also in future you (both of you) should put a bit more of effort into your questions.

The “hertz of a linux system” can mean a lot of things, as you already learnt in this thread.

  1. The maximum CPU frequency (usually the same across all cores)
  2. The current CPU frequency (which might be different for each core on modern systems)
  3. The kernels clock resolution (which you meant in this thread)
  4. Frequencies of varius hardware buses in the system
  5. Refresh rate of the display (of which we can have multiple at different rates)
  6. Perhaps other things

So beeing a bit more specific from the first post on would have been much more helpful for you.

Even after you have provided the link to SO, there still was some confusion about what you actually want to achive and you were still guided to the wrong direction.

This happens because a lot of people do not actually read those links but only glance over them, as they assume you have already done the heavy reading and did the recherche.

Also often you and your friend, you are only posting snippets of code, which do not seem to be related to the question on the first and second look. Or they lack the necessary context to understand them.

Please do not get me wrong, I really like to help, but I like it even more when I see in advance that the people asking did their homework first and do not want to have some ready made and polished code they can hand over at the end of the school term…


(Bidhan Bastola) #18

Thank you for your kind information :slight_smile:


(Jay Ts) #19

According to man pages on my Debian system, the use of CLK_TCK is now obsolete, although it still works. I can run the getconf command and get the correct answer:

$ getconf CLK_TCK
100

(100 ticks per second is normal for modern Linux systems.)

Here is a non-obsolete way to get the ticks per second from a Linux system using Go (cgo, actually), with the ticks per second going into the variable “hz”:

package main

/*
#include <unistd.h>

long sysconf(int name);

static long gethz(void)
{
        return sysconf(_SC_CLK_TCK);
}
*/
import "C"
import "fmt"

func main() {
        hz := C.gethz()
        fmt.Printf("%v clock ticks per second\n",hz)
}

It’s simply a wrapper around a C program to call the sysconf() C library function. See the sysconf(3) manual page and cgo documentation for details about how I created this.

That was more complicated than it needed to be, but it shows what’s going on better than this more concise way:

package main

/* #include <unistd.h> */
import "C"
import "fmt"

func main() {
        hz := C.sysconf(C._SC_CLK_TCK)
        fmt.Printf("%v clock ticks per second\n",hz)
}