Skip to content

Commit

Permalink
[BREAKING CHANGE] rename functions to pass golint. ex) net.NetIOCount…
Browse files Browse the repository at this point in the history
…ers -> net.IOCounters
  • Loading branch information
shirou committed Mar 22, 2016
1 parent 70ba280 commit ea152ea
Show file tree
Hide file tree
Showing 52 changed files with 331 additions and 283 deletions.
16 changes: 8 additions & 8 deletions cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
)

type CPUTimesStat struct {
type TimesStat struct {
CPU string `json:"cpu"`
User float64 `json:"user"`
System float64 `json:"system"`
Expand All @@ -22,7 +22,7 @@ type CPUTimesStat struct {
Stolen float64 `json:"stolen"`
}

type CPUInfoStat struct {
type InfoStat struct {
CPU int32 `json:"cpu"`
VendorID string `json:"vendor_id"`
Family string `json:"family"`
Expand All @@ -37,14 +37,14 @@ type CPUInfoStat struct {
Flags []string `json:"flags"`
}

var lastCPUTimes []CPUTimesStat
var lastPerCPUTimes []CPUTimesStat
var lastCPUTimes []TimesStat
var lastPerCPUTimes []TimesStat

func CPUCounts(logical bool) (int, error) {
func Counts(logical bool) (int, error) {
return runtime.NumCPU(), nil
}

func (c CPUTimesStat) String() string {
func (c TimesStat) String() string {
v := []string{
`"cpu":"` + c.CPU + `"`,
`"user":` + strconv.FormatFloat(c.User, 'f', 1, 64),
Expand All @@ -64,13 +64,13 @@ func (c CPUTimesStat) String() string {
}

// Total returns the total number of seconds in a CPUTimesStat
func (c CPUTimesStat) Total() float64 {
func (c TimesStat) Total() float64 {
total := c.User + c.System + c.Nice + c.Iowait + c.Irq + c.Softirq + c.Steal +
c.Guest + c.GuestNice + c.Idle + c.Stolen
return total
}

func (c CPUInfoStat) String() string {
func (c InfoStat) String() string {
s, _ := json.Marshal(c)
return string(s)
}
8 changes: 4 additions & 4 deletions cpu/cpu_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
// default value. from time.h
var ClocksPerSec = float64(128)

func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
func Times(percpu bool) ([]TimesStat, error) {
if percpu {
return perCPUTimes()
}
Expand All @@ -30,15 +30,15 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
}

// Returns only one CPUInfoStat on FreeBSD
func CPUInfo() ([]CPUInfoStat, error) {
var ret []CPUInfoStat
func Info() ([]InfoStat, error) {
var ret []InfoStat

out, err := exec.Command("/usr/sbin/sysctl", "machdep.cpu").Output()
if err != nil {
return ret, err
}

c := CPUInfoStat{}
c := InfoStat{}
for _, line := range strings.Split(string(out), "\n") {
values := strings.Fields(line)
if len(values) < 1 {
Expand Down
12 changes: 6 additions & 6 deletions cpu/cpu_darwin_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

// these CPU times for darwin is borrowed from influxdb/telegraf.

func perCPUTimes() ([]CPUTimesStat, error) {
func perCPUTimes() ([]TimesStat, error) {
var (
count C.mach_msg_type_number_t
cpuload *C.processor_cpu_load_info_data_t
Expand Down Expand Up @@ -59,15 +59,15 @@ func perCPUTimes() ([]CPUTimesStat, error) {

bbuf := bytes.NewBuffer(buf)

var ret []CPUTimesStat
var ret []TimesStat

for i := 0; i < int(ncpu); i++ {
err := binary.Read(bbuf, binary.LittleEndian, &cpu_ticks)
if err != nil {
return nil, err
}

c := CPUTimesStat{
c := TimesStat{
CPU: fmt.Sprintf("cpu%d", i),
User: float64(cpu_ticks[C.CPU_STATE_USER]) / ClocksPerSec,
System: float64(cpu_ticks[C.CPU_STATE_SYSTEM]) / ClocksPerSec,
Expand All @@ -81,7 +81,7 @@ func perCPUTimes() ([]CPUTimesStat, error) {
return ret, nil
}

func allCPUTimes() ([]CPUTimesStat, error) {
func allCPUTimes() ([]TimesStat, error) {
var count C.mach_msg_type_number_t = C.HOST_CPU_LOAD_INFO_COUNT
var cpuload C.host_cpu_load_info_data_t

Expand All @@ -94,14 +94,14 @@ func allCPUTimes() ([]CPUTimesStat, error) {
return nil, fmt.Errorf("host_statistics error=%d", status)
}

c := CPUTimesStat{
c := TimesStat{
CPU: "cpu-total",
User: float64(cpuload.cpu_ticks[C.CPU_STATE_USER]) / ClocksPerSec,
System: float64(cpuload.cpu_ticks[C.CPU_STATE_SYSTEM]) / ClocksPerSec,
Nice: float64(cpuload.cpu_ticks[C.CPU_STATE_NICE]) / ClocksPerSec,
Idle: float64(cpuload.cpu_ticks[C.CPU_STATE_IDLE]) / ClocksPerSec,
}

return []CPUTimesStat{c}, nil
return []TimesStat{c}, nil

}
8 changes: 4 additions & 4 deletions cpu/cpu_darwin_nocgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ package cpu

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

func perCPUTimes() ([]CPUTimesStat, error) {
return []CPUTimesStat{}, common.NotImplementedError
func perCPUTimes() ([]TimesStat, error) {
return []TimesStat{}, common.NotImplementedError
}

func allCPUTimes() ([]CPUTimesStat, error) {
return []CPUTimesStat{}, common.NotImplementedError
func allCPUTimes() ([]TimesStat, error) {
return []TimesStat{}, common.NotImplementedError
}
14 changes: 7 additions & 7 deletions cpu/cpu_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ func init() {
}
}

func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
var ret []CPUTimesStat
func Times(percpu bool) ([]TimesStat, error) {
var ret []TimesStat

var sysctlCall string
var ncpu int
if percpu {
sysctlCall = "kern.cp_times"
ncpu, _ = CPUCounts(true)
ncpu, _ = Counts(true)
} else {
sysctlCall = "kern.cp_time"
ncpu = 1
Expand Down Expand Up @@ -76,7 +76,7 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
return ret, err
}

c := CPUTimesStat{
c := TimesStat{
User: float64(user / ClocksPerSec),
Nice: float64(nice / ClocksPerSec),
System: float64(sys / ClocksPerSec),
Expand All @@ -96,13 +96,13 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
}

// Returns only one CPUInfoStat on FreeBSD
func CPUInfo() ([]CPUInfoStat, error) {
func Info() ([]InfoStat, error) {
filename := "/var/run/dmesg.boot"
lines, _ := common.ReadLines(filename)

var ret []CPUInfoStat
var ret []InfoStat

c := CPUInfoStat{}
c := InfoStat{}
for _, line := range lines {
if matches := regexp.MustCompile(`CPU:\s+(.+) \(([\d.]+).+\)`).FindStringSubmatch(line); matches != nil {
c.ModelName = matches[1]
Expand Down
18 changes: 9 additions & 9 deletions cpu/cpu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func init() {
}
}

func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
func Times(percpu bool) ([]TimesStat, error) {
filename := common.HostProc("stat")
var lines = []string{}
if percpu {
Expand All @@ -43,7 +43,7 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
lines, _ = common.ReadLinesOffsetN(filename, 0, 1)
}

ret := make([]CPUTimesStat, 0, len(lines))
ret := make([]TimesStat, 0, len(lines))

for _, line := range lines {
ct, err := parseStatLine(line)
Expand All @@ -60,7 +60,7 @@ func sysCpuPath(cpu int32, relPath string) string {
return common.HostSys(fmt.Sprintf("devices/system/cpu/cpu%d", cpu), relPath)
}

func finishCPUInfo(c *CPUInfoStat) error {
func finishCPUInfo(c *InfoStat) error {
if c.Mhz == 0 {
lines, err := common.ReadLines(sysCpuPath(c.CPU, "cpufreq/cpuinfo_max_freq"))
if err == nil {
Expand All @@ -87,13 +87,13 @@ func finishCPUInfo(c *CPUInfoStat) error {
// Sockets often come with many physical CPU cores.
// For example a single socket board with two cores each with HT will
// return 4 CPUInfoStat structs on Linux and the "Cores" field set to 1.
func CPUInfo() ([]CPUInfoStat, error) {
func Info() ([]InfoStat, error) {
filename := common.HostProc("cpuinfo")
lines, _ := common.ReadLines(filename)

var ret []CPUInfoStat
var ret []InfoStat

c := CPUInfoStat{CPU: -1, Cores: 1}
c := InfoStat{CPU: -1, Cores: 1}
for _, line := range lines {
fields := strings.Split(line, ":")
if len(fields) < 2 {
Expand All @@ -111,7 +111,7 @@ func CPUInfo() ([]CPUInfoStat, error) {
}
ret = append(ret, c)
}
c = CPUInfoStat{Cores: 1}
c = InfoStat{Cores: 1}
t, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return ret, err
Expand Down Expand Up @@ -163,7 +163,7 @@ func CPUInfo() ([]CPUInfoStat, error) {
return ret, nil
}

func parseStatLine(line string) (*CPUTimesStat, error) {
func parseStatLine(line string) (*TimesStat, error) {
fields := strings.Fields(line)

if strings.HasPrefix(fields[0], "cpu") == false {
Expand Down Expand Up @@ -204,7 +204,7 @@ func parseStatLine(line string) (*CPUTimesStat, error) {
return nil, err
}

ct := &CPUTimesStat{
ct := &TimesStat{
CPU: cpu,
User: float64(user) / cpu_tick,
Nice: float64(nice) / cpu_tick,
Expand Down
14 changes: 7 additions & 7 deletions cpu/cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
)

func TestCpu_times(t *testing.T) {
v, err := CPUTimes(false)
v, err := Times(false)
if err != nil {
t.Errorf("error %v", err)
}
if len(v) == 0 {
t.Error("could not get CPUs ", err)
}
empty := CPUTimesStat{}
empty := TimesStat{}
for _, vv := range v {
if vv == empty {
t.Errorf("could not get CPU User: %v", vv)
Expand All @@ -25,7 +25,7 @@ func TestCpu_times(t *testing.T) {
}

func TestCpu_counts(t *testing.T) {
v, err := CPUCounts(true)
v, err := Counts(true)
if err != nil {
t.Errorf("error %v", err)
}
Expand All @@ -35,7 +35,7 @@ func TestCpu_counts(t *testing.T) {
}

func TestCPUTimeStat_String(t *testing.T) {
v := CPUTimesStat{
v := TimesStat{
CPU: "cpu0",
User: 100.1,
System: 200.1,
Expand All @@ -48,7 +48,7 @@ func TestCPUTimeStat_String(t *testing.T) {
}

func TestCpuInfo(t *testing.T) {
v, err := CPUInfo()
v, err := Info()
if err != nil {
t.Errorf("error %v", err)
}
Expand All @@ -68,7 +68,7 @@ func testCPUPercent(t *testing.T, percpu bool) {

if runtime.GOOS != "windows" {
testCount = 100
v, err := CPUPercent(time.Millisecond, percpu)
v, err := Percent(time.Millisecond, percpu)
if err != nil {
t.Errorf("error %v", err)
}
Expand All @@ -81,7 +81,7 @@ func testCPUPercent(t *testing.T, percpu bool) {
}
for i := 0; i < testCount; i++ {
duration := time.Duration(10) * time.Microsecond
v, err := CPUPercent(duration, percpu)
v, err := Percent(duration, percpu)
if err != nil {
t.Errorf("error %v", err)
}
Expand Down
10 changes: 5 additions & 5 deletions cpu/cpu_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
"time"
)

func CPUPercent(interval time.Duration, percpu bool) ([]float64, error) {
getAllBusy := func(t CPUTimesStat) (float64, float64) {
func Percent(interval time.Duration, percpu bool) ([]float64, error) {
getAllBusy := func(t TimesStat) (float64, float64) {
busy := t.User + t.System + t.Nice + t.Iowait + t.Irq +
t.Softirq + t.Steal + t.Guest + t.GuestNice + t.Stolen
return busy + t.Idle, busy
}

calculate := func(t1, t2 CPUTimesStat) float64 {
calculate := func(t1, t2 TimesStat) float64 {
t1All, t1Busy := getAllBusy(t1)
t2All, t2Busy := getAllBusy(t2)

Expand All @@ -28,7 +28,7 @@ func CPUPercent(interval time.Duration, percpu bool) ([]float64, error) {
}

// Get CPU usage at the start of the interval.
cpuTimes1, err := CPUTimes(percpu)
cpuTimes1, err := Times(percpu)
if err != nil {
return nil, err
}
Expand All @@ -38,7 +38,7 @@ func CPUPercent(interval time.Duration, percpu bool) ([]float64, error) {
}

// And at the end of the interval.
cpuTimes2, err := CPUTimes(percpu)
cpuTimes2, err := Times(percpu)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit ea152ea

Please sign in to comment.