1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-29 13:49:21 +08:00

Merge pull request #1081 from tklauser/process-sysconf-clk-tck

process, v3/process: use SC_CLK_TCK sysconf value instead of hard-coding clock ticks
This commit is contained in:
shirou 2021-06-12 19:11:31 +09:00 committed by GitHub
commit b60e8d1895
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 64 additions and 34 deletions

6
Gopkg.lock generated
View File

@ -48,12 +48,12 @@
version = "v1.6.0"
[[projects]]
digest = "1:935df8a10a215392af270f19b14ef39efd151d598cf96e4851e73ba678cdf290"
digest = "1:75b5c63e0eae3f1b290c91ff3084b80c9a51066c6dff1a6aca31ccbdab4aeee4"
name = "github.com/tklauser/go-sysconf"
packages = ["."]
pruneopts = "UT"
revision = "6c733a705a84e0dfcaf092758e3cef627dc03034"
version = "v0.3.4"
revision = "bf420f795f20e170808c4d27a6b083f08725911f"
version = "v0.3.6"
[[projects]]
digest = "1:e7bf47a37e6d0fb2f4bd0f65f574f8a92922f048cab3f106b0a7a413cbb14714"

View File

@ -35,7 +35,7 @@
[[constraint]]
name = "github.com/tklauser/go-sysconf"
version = "0.3.4"
version = "0.3.6"
[[constraint]]
branch = "master"

View File

@ -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"
)
@ -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
@ -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) {

View File

@ -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 {
@ -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)

View File

@ -6,6 +6,6 @@ require (
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/stretchr/testify v1.6.1
github.com/tklauser/go-sysconf v0.3.4
golang.org/x/sys v0.0.0-20210217105451-b926d437f341
github.com/tklauser/go-sysconf v0.3.6
golang.org/x/sys v0.0.0-20210316164454-77fc1eacc6aa
)

View File

@ -9,12 +9,12 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tklauser/go-sysconf v0.3.4 h1:HT8SVixZd3IzLdfs/xlpq0jeSfTX57g1v6wB1EuzV7M=
github.com/tklauser/go-sysconf v0.3.4/go.mod h1:Cl2c8ZRWfHD5IrfHo9VN+FX9kCFjIOyVklgXycLB6ek=
github.com/tklauser/numcpus v0.2.1 h1:ct88eFm+Q7m2ZfXJdan1xYoXKlmwsfP+k88q05KvlZc=
github.com/tklauser/numcpus v0.2.1/go.mod h1:9aU+wOc6WjUIZEwWMP62PL/41d65P+iks1gBkr4QyP8=
golang.org/x/sys v0.0.0-20210217105451-b926d437f341 h1:2/QtM1mL37YmcsT8HaDNHDgTqqFVw+zr8UzMiBVLzYU=
golang.org/x/sys v0.0.0-20210217105451-b926d437f341/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
github.com/tklauser/go-sysconf v0.3.6 h1:oc1sJWvKkmvIxhDHeKWvZS4f6AW+YcoguSfRF2/Hmo4=
github.com/tklauser/go-sysconf v0.3.6/go.mod h1:MkWzOF4RMCshBAMXuhXJs64Rte09mITnppBXY/rYEFI=
github.com/tklauser/numcpus v0.2.2 h1:oyhllyrScuYI6g+h/zUvNXNp1wy7x8qQy3t/piefldA=
github.com/tklauser/numcpus v0.2.2/go.mod h1:x3qojaO3uyYt0i56EW/VUYs7uBvdl2fkfZFu0T9wgjM=
golang.org/x/sys v0.0.0-20210316164454-77fc1eacc6aa h1:ZYxPR6aca/uhfRJyaOAtflSHjJYiktO7QnJC5ut7iY4=
golang.org/x/sys v0.0.0-20210316164454-77fc1eacc6aa/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=

View File

@ -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"
)
@ -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
@ -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) {

View File

@ -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 {
@ -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)