Clock ticks per second

2,438 views
Skip to first unread message

Archos

unread,
Sep 20, 2010, 6:27:57 PM9/20/10
to golang-nuts
Is there any function in syscall to get the number of clock ticks per
second?

Brad Fitzpatrick

unread,
Sep 20, 2010, 6:42:03 PM9/20/10
to Archos, golang-nuts
This doesn't mean as much as it used to.  Lot of Linux machines now run tickless or with dynamic HZ.  Probably likewise on other platforms.

Jessta

unread,
Sep 20, 2010, 8:11:05 PM9/20/10
to Archos, golang-nuts
On Tue, Sep 21, 2010 at 8:27 AM, Archos <raul...@sent.com> wrote:
> Is there any function in syscall to get the number of clock ticks per
> second?

What would you use it for?


-jessta
--
=====================
http://jessta.id.au

ron minnich

unread,
Sep 20, 2010, 8:19:11 PM9/20/10
to Jessta, Archos, golang-nuts
On Mon, Sep 20, 2010 at 5:11 PM, Jessta <jes...@jessta.id.au> wrote:
> On Tue, Sep 21, 2010 at 8:27 AM, Archos <raul...@sent.com> wrote:
>> Is there any function in syscall to get the number of clock ticks per
>> second?
>
> What would you use it for?


or, what's it even mean? The TSC is not really constant on most x86
boxes. So what does a "tick" mean here?

ron

Archos

unread,
Sep 21, 2010, 5:01:37 AM9/21/10
to golang-nuts
It's to calculate the CPU usage from "/proc/stat".

The values that I need are expressed in units of 1/HZ seconds, where
HZ is the kernel clock tick rate. But, as Brad has said, the HZ value
used in the kernel may vary. Anyway, I can get it from
`gettimeofday` :)

On Sep 21, 12:11 am, Jessta <jes...@jessta.id.au> wrote:

Archos

unread,
Sep 22, 2010, 3:43:45 AM9/22/10
to golang-nuts
gettimeofday(2) can be used to calculate the CPU usage from "/proc/
stat". But it isn't useful when you only want to calculate the CPU
usage of some processes.

* * *
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>

double time_so_far()
{
struct timeval tp;

if (gettimeofday(&tp, (struct timezone *) NULL) == -1)
perror("gettimeofday");
return ((double) (tp.tv_sec)) +
(((double) tp.tv_usec) * 0.000001 );
}

int main() {
FILE *f1;
double ti, tf;
char c[10];
int i1,i2,i3,i4,i5,i6;

ti = time_so_far();
f1 = fopen("/proc/stat", "r");
fscanf(f1, "%s\t%d\t%d\t%d\n", c, &i1, &i2, &i3);
fclose(f1);

usleep(1000000);

tf = time_so_far();
f1 = fopen("/proc/stat", "r");
fscanf(f1, "%s\t%d\t%d\t%d\n", c, &i4, &i5, &i6);
fclose(f1);

int t = (i4+i5+i6)-(i1+i2+i3);
printf("cpu usage: %.1f%%\n", (t / ((tf-ti) * 100)) * 100);

return 0;
}
* * *

To calculate the CPU usage of some processes it's necessary to get
some fields expressed in number of CPU ticks. And as is said in
times(2) [2]:
"the number of clock ticks per second can be obtained using:
sysconf(_SC_CLK_TCK);"

So, can it get the value of "_SC_CLK_TCK" in package syscall? If no,
please add it, from it's necessary to read (interpret) values in /proc/
stat


[1]: http://www.linuxquestions.org/questions/programming-9/proc-stat-file-problem-for-cpu-usage-369302/
[2]: http://www.kernel.org/doc/man-pages/online/pages/man2/times.2.html

cfsal...@gmail.com

unread,
Oct 8, 2013, 1:14:42 AM10/8/13
to golan...@googlegroups.com
Looking the code at http://golang.org/src/pkg/os/user/lookup_unix.go I've found a way to do what you need:

package main

import (
    "fmt"
)

/*
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <stdlib.h>
*/
import "C"

func main() {
    var sc_clk_tck C.long
    sc_clk_tck = C.sysconf(C._SC_CLK_TCK)
    fmt.Printf("SysConf Clock Ticks: %d\n", sc_clk_tck)
}


Dave Cheney

unread,
Oct 8, 2013, 6:34:35 PM10/8/13
to cfsal...@gmail.com, golang-nuts
Have a play with http://godoc.org/github.com/davecheney/junk/clock,
there might be a clock type in there that will do what you want.
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages