Merge pull request #1036 from tklauser/drop-getconf

cpu, v3/cpu: use sysconf package instead of exec'ing getconf
pull/1039/head
shirou 4 years ago committed by GitHub
commit a346c31dc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

17
Gopkg.lock generated

@ -48,6 +48,22 @@
version = "v1.6.0" version = "v1.6.0"
[[projects]] [[projects]]
digest = "1:935df8a10a215392af270f19b14ef39efd151d598cf96e4851e73ba678cdf290"
name = "github.com/tklauser/go-sysconf"
packages = ["."]
pruneopts = "UT"
revision = "6c733a705a84e0dfcaf092758e3cef627dc03034"
version = "v0.3.4"
[[projects]]
digest = "1:e7bf47a37e6d0fb2f4bd0f65f574f8a92922f048cab3f106b0a7a413cbb14714"
name = "github.com/tklauser/numcpus"
packages = ["."]
pruneopts = "UT"
revision = "c03481b99a3f0b56b010e938559fddce19d3cf53"
version = "v0.1.0"
[[projects]]
branch = "master" branch = "master"
digest = "1:0afe79d034c63eea8c5977401f5fc135394751c1213ee6c991b0c6bec44f8f60" digest = "1:0afe79d034c63eea8c5977401f5fc135394751c1213ee6c991b0c6bec44f8f60"
name = "golang.org/x/sys" name = "golang.org/x/sys"
@ -76,6 +92,7 @@
"github.com/StackExchange/wmi", "github.com/StackExchange/wmi",
"github.com/stretchr/testify/assert", "github.com/stretchr/testify/assert",
"github.com/stretchr/testify/require", "github.com/stretchr/testify/require",
"github.com/tklauser/go-sysconf",
"golang.org/x/sys/unix", "golang.org/x/sys/unix",
"golang.org/x/sys/windows", "golang.org/x/sys/windows",
"golang.org/x/sys/windows/svc", "golang.org/x/sys/windows/svc",

@ -34,6 +34,10 @@
version = "1.2.2" version = "1.2.2"
[[constraint]] [[constraint]]
name = "github.com/tklauser/go-sysconf"
version = "0.3.4"
[[constraint]]
branch = "master" branch = "master"
name = "golang.org/x/sys" name = "golang.org/x/sys"

@ -4,10 +4,10 @@ package cpu
import ( import (
"context" "context"
"os/exec"
"strconv" "strconv"
"strings" "strings"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
@ -25,17 +25,10 @@ const (
var ClocksPerSec = float64(128) var ClocksPerSec = float64(128)
func init() { func init() {
getconf, err := exec.LookPath("getconf") clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
if err != nil {
return
}
out, err := invoke.Command(getconf, "CLK_TCK")
// ignore errors // ignore errors
if err == nil { if err == nil {
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64) ClocksPerSec = float64(clkTck)
if err == nil {
ClocksPerSec = float64(i)
}
} }
} }

@ -3,7 +3,6 @@ package cpu
import ( import (
"context" "context"
"fmt" "fmt"
"os/exec"
"reflect" "reflect"
"regexp" "regexp"
"runtime" "runtime"
@ -12,6 +11,7 @@ import (
"unsafe" "unsafe"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
@ -25,17 +25,10 @@ var cpuTimesSize int
var emptyTimes cpuTimes var emptyTimes cpuTimes
func init() { func init() {
getconf, err := exec.LookPath("getconf") clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
if err != nil {
return
}
out, err := invoke.Command(getconf, "CLK_TCK")
// ignore errors // ignore errors
if err == nil { if err == nil {
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64) ClocksPerSec = float64(clkTck)
if err == nil {
ClocksPerSec = float64(i)
}
} }
} }

@ -3,7 +3,6 @@ package cpu
import ( import (
"context" "context"
"fmt" "fmt"
"os/exec"
"reflect" "reflect"
"regexp" "regexp"
"runtime" "runtime"
@ -12,6 +11,7 @@ import (
"unsafe" "unsafe"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
@ -26,17 +26,10 @@ var cpuTimesSize int
var emptyTimes cpuTimes var emptyTimes cpuTimes
func init() { func init() {
getconf, err := exec.LookPath("getconf") clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
if err != nil {
return
}
out, err := invoke.Command(getconf, "CLK_TCK")
// ignore errors // ignore errors
if err == nil { if err == nil {
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64) ClocksPerSec = float64(clkTck)
if err == nil {
ClocksPerSec = float64(i)
}
} }
} }

@ -6,28 +6,21 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"os/exec"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
"github.com/tklauser/go-sysconf"
) )
var ClocksPerSec = float64(100) var ClocksPerSec = float64(100)
func init() { func init() {
getconf, err := exec.LookPath("getconf") clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
if err != nil {
return
}
out, err := invoke.CommandWithContext(context.Background(), getconf, "CLK_TCK")
// ignore errors // ignore errors
if err == nil { if err == nil {
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64) ClocksPerSec = float64(clkTck)
if err == nil {
ClocksPerSec = i
}
} }
} }

@ -7,13 +7,13 @@ import (
"context" "context"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"os/exec"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"syscall" "syscall"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
@ -39,20 +39,12 @@ const (
var ClocksPerSec = float64(128) var ClocksPerSec = float64(128)
func init() { func init() {
func() { clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
getconf, err := exec.LookPath("getconf") // ignore errors
if err != nil { if err == nil {
return ClocksPerSec = float64(clkTck)
} }
out, err := invoke.Command(getconf, "CLK_TCK")
// ignore errors
if err == nil {
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
if err == nil {
ClocksPerSec = float64(i)
}
}
}()
func() { func() {
v, err := unix.Sysctl("kern.osrelease") // can't reuse host.PlatformInformation because of circular import v, err := unix.Sysctl("kern.osrelease") // can't reuse host.PlatformInformation because of circular import
if err != nil { if err != nil {

@ -10,22 +10,17 @@ import (
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"github.com/tklauser/go-sysconf"
) )
var ClocksPerSec = float64(128) var ClocksPerSec = float64(128)
func init() { func init() {
getconf, err := exec.LookPath("getconf") clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
if err != nil {
return
}
out, err := invoke.Command(getconf, "CLK_TCK")
// ignore errors // ignore errors
if err == nil { if err == nil {
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64) ClocksPerSec = float64(clkTck)
if err == nil {
ClocksPerSec = float64(i)
}
} }
} }

@ -4,10 +4,10 @@ package cpu
import ( import (
"context" "context"
"os/exec"
"strconv" "strconv"
"strings" "strings"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
@ -25,17 +25,10 @@ const (
var ClocksPerSec = float64(128) var ClocksPerSec = float64(128)
func init() { func init() {
getconf, err := exec.LookPath("getconf") clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
if err != nil {
return
}
out, err := invoke.Command(getconf, "CLK_TCK")
// ignore errors // ignore errors
if err == nil { if err == nil {
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64) ClocksPerSec = float64(clkTck)
if err == nil {
ClocksPerSec = float64(i)
}
} }
} }

@ -3,7 +3,6 @@ package cpu
import ( import (
"context" "context"
"fmt" "fmt"
"os/exec"
"reflect" "reflect"
"regexp" "regexp"
"runtime" "runtime"
@ -12,6 +11,7 @@ import (
"unsafe" "unsafe"
"github.com/shirou/gopsutil/v3/internal/common" "github.com/shirou/gopsutil/v3/internal/common"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
@ -25,17 +25,10 @@ var cpuTimesSize int
var emptyTimes cpuTimes var emptyTimes cpuTimes
func init() { func init() {
getconf, err := exec.LookPath("getconf") clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
if err != nil {
return
}
out, err := invoke.Command(getconf, "CLK_TCK")
// ignore errors // ignore errors
if err == nil { if err == nil {
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64) ClocksPerSec = float64(clkTck)
if err == nil {
ClocksPerSec = float64(i)
}
} }
} }

@ -3,7 +3,6 @@ package cpu
import ( import (
"context" "context"
"fmt" "fmt"
"os/exec"
"reflect" "reflect"
"regexp" "regexp"
"runtime" "runtime"
@ -12,6 +11,7 @@ import (
"unsafe" "unsafe"
"github.com/shirou/gopsutil/v3/internal/common" "github.com/shirou/gopsutil/v3/internal/common"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
@ -26,17 +26,10 @@ var cpuTimesSize int
var emptyTimes cpuTimes var emptyTimes cpuTimes
func init() { func init() {
getconf, err := exec.LookPath("getconf") clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
if err != nil {
return
}
out, err := invoke.Command(getconf, "CLK_TCK")
// ignore errors // ignore errors
if err == nil { if err == nil {
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64) ClocksPerSec = float64(clkTck)
if err == nil {
ClocksPerSec = float64(i)
}
} }
} }

@ -6,28 +6,21 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"os/exec"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"github.com/shirou/gopsutil/v3/internal/common" "github.com/shirou/gopsutil/v3/internal/common"
"github.com/tklauser/go-sysconf"
) )
var ClocksPerSec = float64(100) var ClocksPerSec = float64(100)
func init() { func init() {
getconf, err := exec.LookPath("getconf") clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
if err != nil {
return
}
out, err := invoke.CommandWithContext(context.Background(), getconf, "CLK_TCK")
// ignore errors // ignore errors
if err == nil { if err == nil {
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64) ClocksPerSec = float64(clkTck)
if err == nil {
ClocksPerSec = i
}
} }
} }

@ -7,13 +7,13 @@ import (
"context" "context"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"os/exec"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"syscall" "syscall"
"github.com/shirou/gopsutil/v3/internal/common" "github.com/shirou/gopsutil/v3/internal/common"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
@ -39,20 +39,12 @@ const (
var ClocksPerSec = float64(128) var ClocksPerSec = float64(128)
func init() { func init() {
func() { clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
getconf, err := exec.LookPath("getconf") // ignore errors
if err != nil { if err == nil {
return ClocksPerSec = float64(clkTck)
} }
out, err := invoke.Command(getconf, "CLK_TCK")
// ignore errors
if err == nil {
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
if err == nil {
ClocksPerSec = float64(i)
}
}
}()
func() { func() {
v, err := unix.Sysctl("kern.osrelease") // can't reuse host.PlatformInformation because of circular import v, err := unix.Sysctl("kern.osrelease") // can't reuse host.PlatformInformation because of circular import
if err != nil { if err != nil {

@ -10,22 +10,17 @@ import (
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"github.com/tklauser/go-sysconf"
) )
var ClocksPerSec = float64(128) var ClocksPerSec = float64(128)
func init() { func init() {
getconf, err := exec.LookPath("getconf") clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
if err != nil {
return
}
out, err := invoke.Command(getconf, "CLK_TCK")
// ignore errors // ignore errors
if err == nil { if err == nil {
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64) ClocksPerSec = float64(clkTck)
if err == nil {
ClocksPerSec = float64(i)
}
} }
} }

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

@ -9,8 +9,13 @@ 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/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 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20201024232916-9f70ab9862d5 h1:iCaAy5bMeEvwANu3YnJfWwI0kWAGkEa2RXPdweI/ysk= github.com/tklauser/go-sysconf v0.3.4 h1:HT8SVixZd3IzLdfs/xlpq0jeSfTX57g1v6wB1EuzV7M=
golang.org/x/sys v0.0.0-20201024232916-9f70ab9862d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 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=
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/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

Loading…
Cancel
Save