Skip to content

Commit

Permalink
get CLOCK TICK by using getconf.
Browse files Browse the repository at this point in the history
  • Loading branch information
shirou committed Jul 17, 2015
1 parent 06a4ba7 commit 8c17a75
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
16 changes: 12 additions & 4 deletions cpu/cpu_darwin.go
Expand Up @@ -21,10 +21,18 @@ const (
CPUStates = 5
)

// time.h
const (
ClocksPerSec = 128
)
var ClocksPerSec = float64(128)

func init() {
out, err := exec.Command("/usr/bin/getconf", "CLK_TCK").Output()
// ignore errors
if err == nil {
i, err := strconv.ParseFloat(string(out), 64)
if err == nil {
ClocksPerSec = float64(i)
}
}
}

func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
var ret []CPUTimesStat
Expand Down
17 changes: 13 additions & 4 deletions cpu/cpu_freebsd.go
Expand Up @@ -4,6 +4,7 @@ package cpu

import (
"fmt"
"os/exec"
"regexp"
"strconv"
"strings"
Expand All @@ -21,10 +22,18 @@ const (
CPUStates = 5
)

// time.h
const (
ClocksPerSec = 128
)
var ClocksPerSec = float64(128)

func init() {
out, err := exec.Command("/usr/bin/getconf", "CLK_TCK").Output()
// ignore errors
if err == nil {
i, err := strconv.ParseFloat(string(out), 64)
if err == nil {
ClocksPerSec = float64(i)
}
}
}

func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
var ret []CPUTimesStat
Expand Down
15 changes: 14 additions & 1 deletion cpu/cpu_linux.go
Expand Up @@ -4,12 +4,26 @@ package cpu

import (
"errors"
"os/exec"
"strconv"
"strings"

common "github.com/shirou/gopsutil/common"
)

var cpu_tick = float64(100)

func init() {
out, err := exec.Command("/usr/bin/getconf", "CLK_TCK").Output()
// ignore errors
if err == nil {
i, err := strconv.ParseFloat(string(out), 64)
if err == nil {
cpu_tick = float64(i)
}
}
}

func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
filename := "/proc/stat"
var lines = []string{}
Expand Down Expand Up @@ -151,7 +165,6 @@ func parseStatLine(line string) (*CPUTimesStat, error) {
return nil, err
}

cpu_tick := float64(100) // TODO: how to get _SC_CLK_TCK ?
ct := &CPUTimesStat{
CPU: cpu,
User: float64(user) / cpu_tick,
Expand Down

1 comment on commit 8c17a75

@gwind
Copy link

@gwind gwind commented on 8c17a75 Jul 18, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so fast !

Please sign in to comment.