Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Common internal #102

Merged
merged 4 commits into from
Oct 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ The output is below.
{"total":3179569152,"available":492572672,"used":2895335424,"usedPercent":84.50819439828305, (snip)}

You can set an alternative location to /proc by setting the HOST_PROC environment variable.
You can set an alternative location to /sys by setting the HOST_SYS environment variable.

Documentation
------------------------
Expand Down
56 changes: 47 additions & 9 deletions cpu/cpu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ package cpu

import (
"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 @@ -25,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 @@ -55,26 +56,54 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
return ret, nil
}

func sysCpuPath(cpu int32, relPath string) string {
return common.HostSys(fmt.Sprintf("devices/system/cpu/cpu%d", cpu), relPath)
}

func finishCPUInfo(c *CPUInfoStat) error {
if c.Mhz == 0 {
lines, err := common.ReadLines(sysCpuPath(c.CPU, "cpufreq/cpuinfo_max_freq"))
if err == nil {
value, err := strconv.ParseFloat(lines[0], 64)
if err != nil {
return err
}
c.Mhz = value
}
}
if len(c.CoreID) == 0 {
lines, err := common.ReadLines(sysCpuPath(c.CPU, "topology/core_id"))
if err == nil {
c.CoreID = lines[0]
}
}
return nil
}

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

var ret []CPUInfoStat

var c CPUInfoStat
c := CPUInfoStat{CPU: -1}
for _, line := range lines {
fields := strings.Split(line, ":")
if len(fields) < 2 {
if c.VendorID != "" {
ret = append(ret, c)
}
continue
}
key := strings.TrimSpace(fields[0])
value := strings.TrimSpace(fields[1])

switch key {
case "processor":
if c.CPU >= 0 {
err := finishCPUInfo(&c)
if err != nil {
return ret, err
}
ret = append(ret, c)
}
c = CPUInfoStat{}
t, err := strconv.ParseInt(value, 10, 64)
if err != nil {
Expand Down Expand Up @@ -117,9 +146,18 @@ func CPUInfo() ([]CPUInfoStat, error) {
return ret, err
}
c.Cores = int32(t)
case "flags":
c.Flags = strings.Split(value, ",")
case "flags", "Features":
c.Flags = strings.FieldsFunc(value, func(r rune) bool {
return r == ',' || r == ' '
})
}
}
if c.CPU >= 0 {
err := finishCPUInfo(&c)
if err != nil {
return ret, err
}
ret = append(ret, c)
}
return ret, nil
}
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
Loading