Skip to content

Commit

Permalink
common: rename package common to internal/common. Add ENV helper funcs.
Browse files Browse the repository at this point in the history
Package common wasn't used for public functions. Place it in an
internal directory to prevent other packages from using.

Remove the distributed references to "HOST_PROC" and "HOST_SYS"
consts and combine into a common function. This also helps so that
if a env var is defined with a trailing slash all will continue to
work as expected.

Fixes shirou#100
  • Loading branch information
kardianos committed Oct 19, 2015
1 parent 6922174 commit 0af8952
Show file tree
Hide file tree
Showing 20 changed files with 94 additions and 76 deletions.
16 changes: 7 additions & 9 deletions cpu/cpu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
package cpu

import (
"path/filepath"
"fmt"
"errors"
"fmt"
"os/exec"
"strconv"
"strings"

common "github.com/shirou/gopsutil/common"
"github.com/shirou/gopsutil/internal/common"
)

var cpu_tick = float64(100)
Expand All @@ -27,7 +26,7 @@ func init() {
}

func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
filename := common.GetEnv("HOST_PROC", "/proc") + "/stat"
filename := common.HostProc("stat")
var lines = []string{}
if percpu {
var startIdx uint = 1
Expand Down Expand Up @@ -58,8 +57,7 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
}

func sysCpuPath(cpu int32, relPath string) string {
root := common.GetEnv("HOST_SYS", "/sys")
return filepath.Join(root, fmt.Sprintf("devices/system/cpu/cpu%d", cpu), relPath)
return common.HostSys(fmt.Sprintf("devices/system/cpu/cpu%d", cpu), relPath)
}

func finishCPUInfo(c *CPUInfoStat) error {
Expand All @@ -75,15 +73,15 @@ func finishCPUInfo(c *CPUInfoStat) error {
}
if len(c.CoreID) == 0 {
lines, err := common.ReadLines(sysCpuPath(c.CPU, "topology/core_id"))
if err == nil {
if err == nil {
c.CoreID = lines[0]
}
}
return nil
}

func CPUInfo() ([]CPUInfoStat, error) {
filename := filepath.Join(common.GetEnv("HOST_PROC", "/proc"), "cpuinfo")
filename := common.HostProc("cpuinfo")
lines, _ := common.ReadLines(filename)

var ret []CPUInfoStat
Expand Down Expand Up @@ -148,7 +146,7 @@ func CPUInfo() ([]CPUInfoStat, error) {
return ret, err
}
c.Cores = int32(t)
case "flags","Features":
case "flags", "Features":
c.Flags = strings.FieldsFunc(value, func(r rune) bool {
return r == ',' || r == ' '
})
Expand Down
4 changes: 2 additions & 2 deletions disk/disk_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"
"syscall"

common "github.com/shirou/gopsutil/common"
"github.com/shirou/gopsutil/internal/common"
)

const (
Expand Down Expand Up @@ -238,7 +238,7 @@ func DiskPartitions(all bool) ([]DiskPartitionStat, error) {
}

func DiskIOCounters() (map[string]DiskIOCountersStat, error) {
filename := common.GetEnv("HOST_PROC", "/proc") + "/diskstats"
filename := common.HostProc("diskstats")
lines, err := common.ReadLines(filename)
if err != nil {
return nil, err
Expand Down
16 changes: 8 additions & 8 deletions docker/docker_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ package docker

import (
"encoding/json"
"os"
"os"
"os/exec"
"path"
"strconv"
"strings"

common "github.com/shirou/gopsutil/common"
cpu "github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/internal/common"
)

// GetDockerIDList returnes a list of DockerID.
Expand Down Expand Up @@ -49,9 +49,9 @@ func CgroupCPU(containerid string, base string) (*cpu.CPUTimesStat, error) {
}
statfile := path.Join(base, containerid, "cpuacct.stat")

if _, err := os.Stat(statfile); os.IsNotExist(err) {
statfile = path.Join("/sys/fs/cgroup/cpuacct/system.slice", "docker-" + containerid + ".scope", "cpuacct.stat")
}
if _, err := os.Stat(statfile); os.IsNotExist(err) {
statfile = path.Join("/sys/fs/cgroup/cpuacct/system.slice", "docker-"+containerid+".scope", "cpuacct.stat")
}

lines, err := common.ReadLines(statfile)
if err != nil {
Expand Down Expand Up @@ -91,9 +91,9 @@ func CgroupMem(containerid string, base string) (*CgroupMemStat, error) {
}
statfile := path.Join(base, containerid, "memory.stat")

if _, err := os.Stat(statfile); os.IsNotExist(err) {
statfile = path.Join("/sys/fs/cgroup/memory/system.slice", "docker-" + containerid + ".scope", "memory.stat")
}
if _, err := os.Stat(statfile); os.IsNotExist(err) {
statfile = path.Join("/sys/fs/cgroup/memory/system.slice", "docker-"+containerid+".scope", "memory.stat")
}

// empty containerid means all cgroup
if len(containerid) == 0 {
Expand Down
12 changes: 6 additions & 6 deletions host/host_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"strings"
"unsafe"

common "github.com/shirou/gopsutil/common"
"github.com/shirou/gopsutil/internal/common"
)

type LSB struct {
Expand Down Expand Up @@ -56,7 +56,7 @@ func HostInfo() (*HostInfoStat, error) {

// BootTime returns the system boot time expressed in seconds since the epoch.
func BootTime() (uint64, error) {
filename := common.GetEnv("HOST_PROC", "/proc") + "/stat"
filename := common.HostProc("stat")
lines, err := common.ReadLines(filename)
if err != nil {
return 0, err
Expand Down Expand Up @@ -321,7 +321,7 @@ func GetVirtualization() (string, string, error) {
var system string
var role string

filename := common.GetEnv("HOST_PROC", "/proc") + "/xen"
filename := common.HostProc("xen")
if common.PathExists(filename) {
system = "xen"
role = "guest" // assume guest
Expand All @@ -336,7 +336,7 @@ func GetVirtualization() (string, string, error) {
}
}

filename = common.GetEnv("HOST_PROC", "/proc") + "/modules"
filename = common.HostProc("modules")
if common.PathExists(filename) {
contents, err := common.ReadLines(filename)
if err == nil {
Expand All @@ -353,7 +353,7 @@ func GetVirtualization() (string, string, error) {
}
}

filename = common.GetEnv("HOST_PROC", "/proc") + "/cpuinfo"
filename = common.HostProc("cpuinfo")
if common.PathExists(filename) {
contents, err := common.ReadLines(filename)
if err == nil {
Expand All @@ -366,7 +366,7 @@ func GetVirtualization() (string, string, error) {
}
}

filename = common.GetEnv("HOST_PROC", "/proc")
filename = common.HostProc()
if common.PathExists(filename + "/bc/0") {
system = "openvz"
role = "host"
Expand Down
50 changes: 25 additions & 25 deletions host/host_linux_386.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,39 @@
package host

const (
sizeofPtr = 0x4
sizeofShort = 0x2
sizeofInt = 0x4
sizeofLong = 0x4
sizeofLongLong = 0x8
sizeofPtr = 0x4
sizeofShort = 0x2
sizeofInt = 0x4
sizeofLong = 0x4
sizeofLongLong = 0x8
)

type (
_C_short int16
_C_int int32
_C_long int32
_C_long_long int64
_C_short int16
_C_int int32
_C_long int32
_C_long_long int64
)

type utmp struct {
Type int16
Pad_cgo_0 [2]byte
Pid int32
Line [32]int8
Id [4]int8
User [32]int8
Host [256]int8
Exit exit_status
Session int32
Tv UtTv
Addr_v6 [4]int32
X__unused [20]int8
Type int16
Pad_cgo_0 [2]byte
Pid int32
Line [32]int8
Id [4]int8
User [32]int8
Host [256]int8
Exit exit_status
Session int32
Tv UtTv
Addr_v6 [4]int32
X__unused [20]int8
}
type exit_status struct {
Termination int16
Exit int16
Termination int16
Exit int16
}
type UtTv struct {
TvSec int32
TvUsec int32
TvSec int32
TvUsec int32
}
25 changes: 23 additions & 2 deletions common/common.go → internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os"
"os/exec"
"path"
"path/filepath"
"reflect"
"runtime"
"strconv"
Expand Down Expand Up @@ -209,10 +210,30 @@ func PathExists(filename string) bool {
}

//GetEnv retreives the environment variable key. If it does not exist it returns the default.
func GetEnv(key string, dfault string) string {
func GetEnv(key string, dfault string, combineWith ...string) string {
value := os.Getenv(key)
if value == "" {
value = dfault
}
return value

switch len(combineWith) {
case 0:
return value
case 1:
return filepath.Join(value, combineWith[0])
default:
all := make([]string, len(combineWith)+1)
all[0] = value
copy(all[1:], combineWith)
return filepath.Join(all...)
}
panic("invalid switch case")
}

func HostProc(combineWith ...string) string {
return GetEnv("HOST_PROC", "/proc", combineWith...)
}

func HostSys(combineWith ...string) string {
return GetEnv("HOST_SYS", "/sys", combineWith...)
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
package common

import (
"syscall"
"os/exec"
"strings"
"syscall"
"unsafe"
)

Expand Down Expand Up @@ -58,4 +58,3 @@ func CallSyscall(mib []int32) ([]byte, uint64, error) {

return buf, length, nil
}

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions load/load_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"strconv"
"strings"

common "github.com/shirou/gopsutil/common"
"github.com/shirou/gopsutil/internal/common"
)

func LoadAvg() (*LoadAvgStat, error) {
filename := common.GetEnv("HOST_PROC", "/proc") + "/loadavg"
filename := common.HostProc("loadavg")
line, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions mem/mem_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"strings"
"syscall"

common "github.com/shirou/gopsutil/common"
"github.com/shirou/gopsutil/internal/common"
)

func VirtualMemory() (*VirtualMemoryStat, error) {
filename := common.GetEnv("HOST_PROC", "/proc") + "/meminfo"
filename := common.HostProc("meminfo")
lines, _ := common.ReadLines(filename)
// flag if MemAvailable is in /proc/meminfo (kernel 3.14+)
memavail := false
Expand Down Expand Up @@ -74,7 +74,7 @@ func SwapMemory() (*SwapMemoryStat, error) {
} else {
ret.UsedPercent = 0
}
filename := common.GetEnv("HOST_PROC", "/proc") + "/vmstat"
filename := common.HostProc("vmstat")
lines, _ := common.ReadLines(filename)
for _, l := range lines {
fields := strings.Fields(l)
Expand Down
2 changes: 1 addition & 1 deletion net/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
"syscall"

"github.com/shirou/gopsutil/common"
"github.com/shirou/gopsutil/internal/common"
)

var invoke common.Invoker
Expand Down
4 changes: 2 additions & 2 deletions net/net_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strconv"
"strings"

common "github.com/shirou/gopsutil/common"
"github.com/shirou/gopsutil/internal/common"
)

// NetIOCounters returnes network I/O statistics for every network
Expand All @@ -15,7 +15,7 @@ import (
// every network interface installed on the system is returned
// separately.
func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
filename := common.GetEnv("HOST_PROC", "/proc") + "/net/dev"
filename := common.HostProc("net/dev")
lines, err := common.ReadLines(filename)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion net/net_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package net
import (
"strings"

"github.com/shirou/gopsutil/common"
"github.com/shirou/gopsutil/internal/common"
)

// Return a list of network connections opened.
Expand Down
2 changes: 1 addition & 1 deletion process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"runtime"
"time"

"github.com/shirou/gopsutil/common"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/internal/common"
)

var invoke common.Invoker
Expand Down
Loading

0 comments on commit 0af8952

Please sign in to comment.