Skip to content

Commit

Permalink
Merge pull request #177 from shirou/v2
Browse files Browse the repository at this point in the history
change to version2 with many of incompatiblity.
  • Loading branch information
shirou committed Apr 7, 2016
2 parents 37d8908 + 1cc575d commit e8f7a95
Show file tree
Hide file tree
Showing 65 changed files with 757 additions and 547 deletions.
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.PHONY: help check
.DEFAULT_GOAL := help

SUBPKGS=cpu disk docker host internal load mem net process

help: ## Show help
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

check: ## Check
errcheck -ignore="Close|Run|Write" ./...
golint ./... | egrep -v 'underscores|HttpOnly|should have comment|comment on exported|CamelCase|VM|UID'

build_test: ## test only buildable
GOOS=linux go test ./... | grep -v "exec format error"
GOOS=freebsd go test ./... | grep -v "exec format error"
CGO_ENABLED=0 GOOS=darwin go test ./... | grep -v "exec format error"
CGO_ENABLED=1 GOOS=darwin go test ./... | grep -v "exec format error"
GOOS=windows go test ./...| grep -v "exec format error"
30 changes: 19 additions & 11 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ This is a port of psutil (http://pythonhosted.org/psutil/). The challenge is por
psutil functions on some architectures...


.. highlights:: Breaking Changes will comes!
.. highlights:: Breaking Changes!

We introduced versioning by using gopkgin. And breaking changes will be introduced at v2. See `issue 174 <https://github.com/shirou/gopsutil/issues/174>`_ .
Breaking changes is introduced at v2. See `issue 174 <https://github.com/shirou/gopsutil/issues/174>`_ .


Migrating to v2
-------------------------

On gopsutil itself, `v2migration.sh <https://github.com/shirou/gopsutil/blob/v2/v2migration.sh>`_ is used for migration. It can not be commly used, but it may help to your migration.


Available Architectures
Expand All @@ -33,32 +39,34 @@ All works are implemented without cgo by porting c struct to golang struct.
Usage
---------

Note: gopsutil.v2 breaks compatibility. If you want to stay with compatibility, please use `gopkg.in/shirou/gopsutil.v1`.
Note: gopsutil v2 breaks compatibility. If you want to stay with compatibility, please use v1 branch and vendoring.

.. code:: go
package main
import (
"fmt"
"fmt"
mem "gopkg.in/shirou/gopsutil.v2/mem"
"github.com/shirou/gopsutil/mem"
)
func main() {
v, _ := mem.VirtualMemory()
v, _ := mem.VirtualMemory()
// almost every return value is a struct
fmt.Printf("Total: %v, Free:%v, UsedPercent:%f%%\n", v.Total, v.Free, v.UsedPercent)
// almost every return value is a struct
fmt.Printf("Total: %v, Free:%v, UsedPercent:%f%%\n", v.Total, v.Free, v.UsedPercent)
// convert to JSON. String() is also implemented
fmt.Println(v)
// convert to JSON. String() is also implemented
fmt.Println(v)
}
The output is below.

::

Total: 3179569152, Free:284233728, UsedPercent:84.508194%
{"total":3179569152,"available":492572672,"used":2895335424,"usedPercent":84.50819439828305, (snip)}
{"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.
Expand Down
30 changes: 15 additions & 15 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 @@ -18,33 +18,33 @@ type CPUTimesStat struct {
Softirq float64 `json:"softirq"`
Steal float64 `json:"steal"`
Guest float64 `json:"guest"`
GuestNice float64 `json:"guest_nice"`
GuestNice float64 `json:"guestNice"`
Stolen float64 `json:"stolen"`
}

type CPUInfoStat struct {
type InfoStat struct {
CPU int32 `json:"cpu"`
VendorID string `json:"vendor_id"`
VendorID string `json:"vendorId"`
Family string `json:"family"`
Model string `json:"model"`
Stepping int32 `json:"stepping"`
PhysicalID string `json:"physical_id"`
CoreID string `json:"core_id"`
PhysicalID string `json:"physicalId"`
CoreID string `json:"coreId"`
Cores int32 `json:"cores"`
ModelName string `json:"model_name"`
ModelName string `json:"modelName"`
Mhz float64 `json:"mhz"`
CacheSize int32 `json:"cache_size"`
CacheSize int32 `json:"cacheSize"`
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 @@ -56,21 +56,21 @@ func (c CPUTimesStat) String() string {
`"softirq":` + strconv.FormatFloat(c.Softirq, 'f', 1, 64),
`"steal":` + strconv.FormatFloat(c.Steal, 'f', 1, 64),
`"guest":` + strconv.FormatFloat(c.Guest, 'f', 1, 64),
`"guest_nice":` + strconv.FormatFloat(c.GuestNice, 'f', 1, 64),
`"guestNice":` + strconv.FormatFloat(c.GuestNice, 'f', 1, 64),
`"stolen":` + strconv.FormatFloat(c.Stolen, 'f', 1, 64),
}

return `{` + strings.Join(v, ",") + `}`
}

// 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)
}
17 changes: 10 additions & 7 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,18 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
}

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

out, err := exec.Command("/usr/sbin/sysctl", "machdep.cpu").Output()
func Info() ([]InfoStat, error) {
var ret []InfoStat
sysctl, err := exec.LookPath("/usr/sbin/sysctl")
if err != nil {
return ret, err
}
out, err := exec.Command(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 Expand Up @@ -87,7 +90,7 @@ func CPUInfo() ([]CPUInfoStat, error) {

// Use the rated frequency of the CPU. This is a static value and does not
// account for low power or Turbo Boost modes.
out, err = exec.Command("/usr/sbin/sysctl", "hw.cpufrequency").Output()
out, err = exec.Command(sysctl, "hw.cpufrequency").Output()
if err != nil {
return ret, err
}
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.ErrNotImplementedError
}

func allCPUTimes() ([]CPUTimesStat, error) {
return []CPUTimesStat{}, common.NotImplementedError
func allCPUTimes() ([]TimesStat, error) {
return []TimesStat{}, common.ErrNotImplementedError
}
20 changes: 12 additions & 8 deletions cpu/cpu_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ const (
var ClocksPerSec = float64(128)

func init() {
out, err := exec.Command("/usr/bin/getconf", "CLK_TCK").Output()
getconf, err := exec.LookPath("/usr/bin/getconf")
if err != nil {
return
}
out, err := exec.Command(getconf, "CLK_TCK").Output()
// ignore errors
if err == nil {
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
Expand All @@ -35,14 +39,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 +80,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 +100,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
Loading

0 comments on commit e8f7a95

Please sign in to comment.