Skip to content

Commit

Permalink
cpu, v3/cpu: use sysconf package instead of exec'ing getconf
Browse files Browse the repository at this point in the history
Currently, ClocksPerSec is determined by exec'ing getconf in func init,
i.e. on startup of every program importing the package. getconf might
not be present on some systems or is not executable by the current user.
To avoid this hard to control dependency, use the
github.com/tklauser/go-sysconf package which implements sysconf(3)
entirely in Go without cgo. The package is supported on all platforms
currently supported by the cpu and v3/cpu package of gopsutil.
  • Loading branch information
tklauser committed Feb 16, 2021
1 parent 3585d27 commit 93c8cb1
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 131 deletions.
17 changes: 17 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Expand Up @@ -33,6 +33,10 @@
name = "github.com/stretchr/testify"
version = "1.2.2"

[[constraint]]
name = "github.com/tklauser/go-sysconf"
version = "0.3.3"

[[constraint]]
branch = "master"
name = "golang.org/x/sys"
Expand Down
13 changes: 3 additions & 10 deletions cpu/cpu_darwin.go
Expand Up @@ -4,10 +4,10 @@ package cpu

import (
"context"
"os/exec"
"strconv"
"strings"

"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix"
)

Expand All @@ -25,17 +25,10 @@ const (
var ClocksPerSec = float64(128)

func init() {
getconf, err := exec.LookPath("getconf")
if err != nil {
return
}
out, err := invoke.Command(getconf, "CLK_TCK")
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
// ignore errors
if err == nil {
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
if err == nil {
ClocksPerSec = float64(i)
}
ClocksPerSec = float64(clkTck)
}
}

Expand Down
13 changes: 3 additions & 10 deletions cpu/cpu_dragonfly.go
Expand Up @@ -3,7 +3,6 @@ package cpu
import (
"context"
"fmt"
"os/exec"
"reflect"
"regexp"
"runtime"
Expand All @@ -12,6 +11,7 @@ import (
"unsafe"

"github.com/shirou/gopsutil/internal/common"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix"
)

Expand All @@ -25,17 +25,10 @@ var cpuTimesSize int
var emptyTimes cpuTimes

func init() {
getconf, err := exec.LookPath("getconf")
if err != nil {
return
}
out, err := invoke.Command(getconf, "CLK_TCK")
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
// ignore errors
if err == nil {
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
if err == nil {
ClocksPerSec = float64(i)
}
ClocksPerSec = float64(clkTck)
}
}

Expand Down
13 changes: 3 additions & 10 deletions cpu/cpu_freebsd.go
Expand Up @@ -3,7 +3,6 @@ package cpu
import (
"context"
"fmt"
"os/exec"
"reflect"
"regexp"
"runtime"
Expand All @@ -12,6 +11,7 @@ import (
"unsafe"

"github.com/shirou/gopsutil/internal/common"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix"
)

Expand All @@ -26,17 +26,10 @@ var cpuTimesSize int
var emptyTimes cpuTimes

func init() {
getconf, err := exec.LookPath("getconf")
if err != nil {
return
}
out, err := invoke.Command(getconf, "CLK_TCK")
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
// ignore errors
if err == nil {
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
if err == nil {
ClocksPerSec = float64(i)
}
ClocksPerSec = float64(clkTck)
}
}

Expand Down
13 changes: 3 additions & 10 deletions cpu/cpu_linux.go
Expand Up @@ -6,28 +6,21 @@ import (
"context"
"errors"
"fmt"
"os/exec"
"path/filepath"
"strconv"
"strings"

"github.com/shirou/gopsutil/internal/common"
"github.com/tklauser/go-sysconf"
)

var ClocksPerSec = float64(100)

func init() {
getconf, err := exec.LookPath("getconf")
if err != nil {
return
}
out, err := invoke.CommandWithContext(context.Background(), getconf, "CLK_TCK")
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
// ignore errors
if err == nil {
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
if err == nil {
ClocksPerSec = i
}
ClocksPerSec = float64(clkTck)
}
}

Expand Down
22 changes: 7 additions & 15 deletions cpu/cpu_openbsd.go
Expand Up @@ -7,13 +7,13 @@ import (
"context"
"encoding/binary"
"fmt"
"os/exec"
"runtime"
"strconv"
"strings"
"syscall"

"github.com/shirou/gopsutil/internal/common"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix"
)

Expand All @@ -39,20 +39,12 @@ const (
var ClocksPerSec = float64(128)

func init() {
func() {
getconf, err := exec.LookPath("getconf")
if err != nil {
return
}
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)
}
}
}()
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
// ignore errors
if err == nil {
ClocksPerSec = float64(clkTck)
}

func() {
v, err := unix.Sysctl("kern.osrelease") // can't reuse host.PlatformInformation because of circular import
if err != nil {
Expand Down
13 changes: 4 additions & 9 deletions cpu/cpu_solaris.go
Expand Up @@ -10,22 +10,17 @@ import (
"sort"
"strconv"
"strings"

"github.com/tklauser/go-sysconf"
)

var ClocksPerSec = float64(128)

func init() {
getconf, err := exec.LookPath("getconf")
if err != nil {
return
}
out, err := invoke.Command(getconf, "CLK_TCK")
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
// ignore errors
if err == nil {
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
if err == nil {
ClocksPerSec = float64(i)
}
ClocksPerSec = float64(clkTck)
}
}

Expand Down
13 changes: 3 additions & 10 deletions v3/cpu/cpu_darwin.go
Expand Up @@ -4,10 +4,10 @@ package cpu

import (
"context"
"os/exec"
"strconv"
"strings"

"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix"
)

Expand All @@ -25,17 +25,10 @@ const (
var ClocksPerSec = float64(128)

func init() {
getconf, err := exec.LookPath("getconf")
if err != nil {
return
}
out, err := invoke.Command(getconf, "CLK_TCK")
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
// ignore errors
if err == nil {
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
if err == nil {
ClocksPerSec = float64(i)
}
ClocksPerSec = float64(clkTck)
}
}

Expand Down
13 changes: 3 additions & 10 deletions v3/cpu/cpu_dragonfly.go
Expand Up @@ -3,7 +3,6 @@ package cpu
import (
"context"
"fmt"
"os/exec"
"reflect"
"regexp"
"runtime"
Expand All @@ -12,6 +11,7 @@ import (
"unsafe"

"github.com/shirou/gopsutil/v3/internal/common"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix"
)

Expand All @@ -25,17 +25,10 @@ var cpuTimesSize int
var emptyTimes cpuTimes

func init() {
getconf, err := exec.LookPath("getconf")
if err != nil {
return
}
out, err := invoke.Command(getconf, "CLK_TCK")
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
// ignore errors
if err == nil {
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
if err == nil {
ClocksPerSec = float64(i)
}
ClocksPerSec = float64(clkTck)
}
}

Expand Down
13 changes: 3 additions & 10 deletions v3/cpu/cpu_freebsd.go
Expand Up @@ -3,7 +3,6 @@ package cpu
import (
"context"
"fmt"
"os/exec"
"reflect"
"regexp"
"runtime"
Expand All @@ -12,6 +11,7 @@ import (
"unsafe"

"github.com/shirou/gopsutil/v3/internal/common"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix"
)

Expand All @@ -26,17 +26,10 @@ var cpuTimesSize int
var emptyTimes cpuTimes

func init() {
getconf, err := exec.LookPath("getconf")
if err != nil {
return
}
out, err := invoke.Command(getconf, "CLK_TCK")
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
// ignore errors
if err == nil {
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
if err == nil {
ClocksPerSec = float64(i)
}
ClocksPerSec = float64(clkTck)
}
}

Expand Down
13 changes: 3 additions & 10 deletions v3/cpu/cpu_linux.go
Expand Up @@ -6,28 +6,21 @@ import (
"context"
"errors"
"fmt"
"os/exec"
"path/filepath"
"strconv"
"strings"

"github.com/shirou/gopsutil/v3/internal/common"
"github.com/tklauser/go-sysconf"
)

var ClocksPerSec = float64(100)

func init() {
getconf, err := exec.LookPath("getconf")
if err != nil {
return
}
out, err := invoke.CommandWithContext(context.Background(), getconf, "CLK_TCK")
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
// ignore errors
if err == nil {
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
if err == nil {
ClocksPerSec = i
}
ClocksPerSec = float64(clkTck)
}
}

Expand Down

0 comments on commit 93c8cb1

Please sign in to comment.