process, v3/process: use SC_CLK_TCK sysconf value instead of hard-coding clock ticks

The github.com/tklauser/go-sysconf package is already a dependency used
in the cpu and v3/cpu packages to determine clock ticks using
`sysconf.Sysconf(sysconf.SC_CLK_TCK)`, see #1036. Use the same in
packages process and v3/process as well instead of hard-coding clock
ticks to 100.
pull/1081/head
Tobias Klauser 4 years ago
parent 25b4a07b16
commit 611c8b576e

@ -14,6 +14,7 @@ import (
"github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
"github.com/shirou/gopsutil/net" "github.com/shirou/gopsutil/net"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
@ -27,9 +28,15 @@ const (
KernProcPathname = 12 // path to executable KernProcPathname = 12 // path to executable
) )
const ( var ClockTicks = 100 // default value
ClockTicks = 100 // C.sysconf(C._SC_CLK_TCK)
) func init() {
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
// ignore errors
if err == nil {
ClockTicks = int(clkTck)
}
}
type _Ctype_struct___0 struct { type _Ctype_struct___0 struct {
Pad uint64 Pad uint64
@ -314,7 +321,7 @@ func convertCPUTimes(s string) (ret float64, err error) {
t += h * ClockTicks t += h * ClockTicks
h, err = strconv.Atoi(_t[1]) h, err = strconv.Atoi(_t[1])
t += h t += h
return float64(t) / ClockTicks, nil return float64(t) / float64(ClockTicks), nil
} }
func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) { func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) {

@ -18,15 +18,23 @@ import (
"github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
"github.com/shirou/gopsutil/net" "github.com/shirou/gopsutil/net"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
var PageSize = uint64(os.Getpagesize()) var PageSize = uint64(os.Getpagesize())
const ( const PrioProcess = 0 // linux/resource.h
PrioProcess = 0 // linux/resource.h
ClockTicks = 100 // C.sysconf(C._SC_CLK_TCK) var ClockTicks = 100 // default value
)
func init() {
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
// ignore errors
if err == nil {
ClockTicks = int(clkTck)
}
}
// MemoryInfoExStat is different between OSes // MemoryInfoExStat is different between OSes
type MemoryInfoExStat struct { type MemoryInfoExStat struct {
@ -1031,9 +1039,9 @@ func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (ui
cpuTimes := &cpu.TimesStat{ cpuTimes := &cpu.TimesStat{
CPU: "cpu", CPU: "cpu",
User: float64(utime / ClockTicks), User: utime / float64(ClockTicks),
System: float64(stime / ClockTicks), System: stime / float64(ClockTicks),
Iowait: float64(iotime / ClockTicks), Iowait: iotime / float64(ClockTicks),
} }
bootTime, _ := common.BootTimeWithContext(ctx) bootTime, _ := common.BootTimeWithContext(ctx)

@ -14,6 +14,7 @@ import (
"github.com/shirou/gopsutil/v3/cpu" "github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/internal/common" "github.com/shirou/gopsutil/v3/internal/common"
"github.com/shirou/gopsutil/v3/net" "github.com/shirou/gopsutil/v3/net"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
@ -27,9 +28,15 @@ const (
KernProcPathname = 12 // path to executable KernProcPathname = 12 // path to executable
) )
const ( var clockTicks = 100 // default value
clockTicks = 100 // C.sysconf(C._SC_CLK_TCK)
) func init() {
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
// ignore errors
if err == nil {
clockTicks = int(clkTck)
}
}
type _Ctype_struct___0 struct { type _Ctype_struct___0 struct {
Pad uint64 Pad uint64
@ -314,7 +321,7 @@ func convertCPUTimes(s string) (ret float64, err error) {
t += h * clockTicks t += h * clockTicks
h, err = strconv.Atoi(_t[1]) h, err = strconv.Atoi(_t[1])
t += h t += h
return float64(t) / clockTicks, nil return float64(t) / float64(clockTicks), nil
} }
func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) { func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) {

@ -18,15 +18,23 @@ import (
"github.com/shirou/gopsutil/v3/cpu" "github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/internal/common" "github.com/shirou/gopsutil/v3/internal/common"
"github.com/shirou/gopsutil/v3/net" "github.com/shirou/gopsutil/v3/net"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
var pageSize = uint64(os.Getpagesize()) var pageSize = uint64(os.Getpagesize())
const ( const prioProcess = 0 // linux/resource.h
prioProcess = 0 // linux/resource.h
clockTicks = 100 // C.sysconf(C._SC_CLK_TCK) var clockTicks = 100 // default value
)
func init() {
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
// ignore errors
if err == nil {
clockTicks = int(clkTck)
}
}
// MemoryInfoExStat is different between OSes // MemoryInfoExStat is different between OSes
type MemoryInfoExStat struct { type MemoryInfoExStat struct {
@ -1026,9 +1034,9 @@ func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (ui
cpuTimes := &cpu.TimesStat{ cpuTimes := &cpu.TimesStat{
CPU: "cpu", CPU: "cpu",
User: float64(utime / clockTicks), User: utime / float64(clockTicks),
System: float64(stime / clockTicks), System: stime / float64(clockTicks),
Iowait: float64(iotime / clockTicks), Iowait: iotime / float64(clockTicks),
} }
bootTime, _ := common.BootTimeWithContext(ctx) bootTime, _ := common.BootTimeWithContext(ctx)

Loading…
Cancel
Save