Skip to content

Commit

Permalink
process, v3/process: use SC_CLK_TCK sysconf value instead of hard-cod…
Browse files Browse the repository at this point in the history
…ing 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 shirou#1036. Use the same in
packages process and v3/process as well instead of hard-coding clock
ticks to 100.
  • Loading branch information
tklauser committed Jun 2, 2021
1 parent 25b4a07 commit 611c8b5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 22 deletions.
15 changes: 11 additions & 4 deletions process/process_darwin.go
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/internal/common"
"github.com/shirou/gopsutil/net"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix"
)

Expand All @@ -27,9 +28,15 @@ const (
KernProcPathname = 12 // path to executable
)

const (
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)
}
}

type _Ctype_struct___0 struct {
Pad uint64
Expand Down Expand Up @@ -314,7 +321,7 @@ func convertCPUTimes(s string) (ret float64, err error) {
t += h * ClockTicks
h, err = strconv.Atoi(_t[1])
t += h
return float64(t) / ClockTicks, nil
return float64(t) / float64(ClockTicks), nil
}

func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) {
Expand Down
22 changes: 15 additions & 7 deletions process/process_linux.go
Expand Up @@ -18,15 +18,23 @@ import (
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/internal/common"
"github.com/shirou/gopsutil/net"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix"
)

var PageSize = uint64(os.Getpagesize())

const (
PrioProcess = 0 // linux/resource.h
ClockTicks = 100 // C.sysconf(C._SC_CLK_TCK)
)
const PrioProcess = 0 // linux/resource.h

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
type MemoryInfoExStat struct {
Expand Down Expand Up @@ -1031,9 +1039,9 @@ func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (ui

cpuTimes := &cpu.TimesStat{
CPU: "cpu",
User: float64(utime / ClockTicks),
System: float64(stime / ClockTicks),
Iowait: float64(iotime / ClockTicks),
User: utime / float64(ClockTicks),
System: stime / float64(ClockTicks),
Iowait: iotime / float64(ClockTicks),
}

bootTime, _ := common.BootTimeWithContext(ctx)
Expand Down
15 changes: 11 additions & 4 deletions v3/process/process_darwin.go
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/internal/common"
"github.com/shirou/gopsutil/v3/net"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix"
)

Expand All @@ -27,9 +28,15 @@ const (
KernProcPathname = 12 // path to executable
)

const (
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)
}
}

type _Ctype_struct___0 struct {
Pad uint64
Expand Down Expand Up @@ -314,7 +321,7 @@ func convertCPUTimes(s string) (ret float64, err error) {
t += h * clockTicks
h, err = strconv.Atoi(_t[1])
t += h
return float64(t) / clockTicks, nil
return float64(t) / float64(clockTicks), nil
}

func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) {
Expand Down
22 changes: 15 additions & 7 deletions v3/process/process_linux.go
Expand Up @@ -18,15 +18,23 @@ import (
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/internal/common"
"github.com/shirou/gopsutil/v3/net"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix"
)

var pageSize = uint64(os.Getpagesize())

const (
prioProcess = 0 // linux/resource.h
clockTicks = 100 // C.sysconf(C._SC_CLK_TCK)
)
const prioProcess = 0 // linux/resource.h

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
type MemoryInfoExStat struct {
Expand Down Expand Up @@ -1026,9 +1034,9 @@ func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (ui

cpuTimes := &cpu.TimesStat{
CPU: "cpu",
User: float64(utime / clockTicks),
System: float64(stime / clockTicks),
Iowait: float64(iotime / clockTicks),
User: utime / float64(clockTicks),
System: stime / float64(clockTicks),
Iowait: iotime / float64(clockTicks),
}

bootTime, _ := common.BootTimeWithContext(ctx)
Expand Down

0 comments on commit 611c8b5

Please sign in to comment.