get CLOCK TICK by using getconf.

pull/58/head
Shirou WAKAYAMA 10 years ago
parent 06a4ba734e
commit 8c17a750c6

@ -21,10 +21,18 @@ const (
CPUStates = 5 CPUStates = 5
) )
// time.h var ClocksPerSec = float64(128)
const (
ClocksPerSec = 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) { func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
var ret []CPUTimesStat var ret []CPUTimesStat

@ -4,6 +4,7 @@ package cpu
import ( import (
"fmt" "fmt"
"os/exec"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -21,10 +22,18 @@ const (
CPUStates = 5 CPUStates = 5
) )
// time.h var ClocksPerSec = float64(128)
const (
ClocksPerSec = 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) { func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
var ret []CPUTimesStat var ret []CPUTimesStat

@ -4,12 +4,26 @@ package cpu
import ( import (
"errors" "errors"
"os/exec"
"strconv" "strconv"
"strings" "strings"
common "github.com/shirou/gopsutil/common" 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) { func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
filename := "/proc/stat" filename := "/proc/stat"
var lines = []string{} var lines = []string{}
@ -151,7 +165,6 @@ func parseStatLine(line string) (*CPUTimesStat, error) {
return nil, err return nil, err
} }
cpu_tick := float64(100) // TODO: how to get _SC_CLK_TCK ?
ct := &CPUTimesStat{ ct := &CPUTimesStat{
CPU: cpu, CPU: cpu,
User: float64(user) / cpu_tick, User: float64(user) / cpu_tick,

Loading…
Cancel
Save