GithubHelp home page GithubHelp logo

gopsutil's Introduction

gopsutil: psutil for golang

Test Coverage Status Go Reference Go Documentation Calendar Versioning

This is a port of psutil (https://github.com/giampaolo/psutil). The challenge is porting all psutil functions on some architectures.

v3 migration

From v3.20.10, gopsutil becomes v3 which breaks backwards compatibility. See v3Changes.md for more detailed changes.

Tag semantics

gopsutil tag policy is almost same as Semantic Versioning, but automatically increases like Ubuntu versioning.

For example, v2.17.04 means

  • v2: major version
  • 17: release year, 2017
  • 04: release month

gopsutil aims to keep backwards compatibility until major version change.

Tagged at every end of month, but if there are only a few commits, it can be skipped.

Available Architectures

  • FreeBSD i386/amd64/arm
  • Linux i386/amd64/arm(raspberry pi)
  • Windows i386/amd64/arm/arm64
  • Darwin amd64/arm64
  • OpenBSD i386/amd64/armv7/arm64/riscv64 (Thank you @mpfz0r!)
  • Solaris amd64 (developed and tested on SmartOS/Illumos, Thank you @jen20!)

These have partial support:

  • CPU on DragonFly BSD (#893, Thank you @gballet!)
  • host on Linux RISC-V (#896, Thank you @tklauser!)

All works are implemented without cgo by porting C structs to golang structs.

Usage

package main

import (
    "fmt"

    "github.com/shirou/gopsutil/v3/mem"
    // "github.com/shirou/gopsutil/mem"  // to use v2
)

func main() {
    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)

    // 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...)}

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.

You can set an alternative location to /etc by setting the HOST_ETC environment variable.

You can set an alternative location to /var by setting the HOST_VAR environment variable.

You can set an alternative location to /run by setting the HOST_RUN environment variable.

You can set an alternative location to /dev by setting the HOST_DEV environment variable.

You can set an alternative location to / by setting the HOST_ROOT environment variable.

You can set an alternative location to /proc/N/mountinfo by setting the HOST_PROC_MOUNTINFO environment variable.

Adding settings using context (from v3.23.6)

As of v3.23.6, it is now possible to pass a path location using context: import "github.com/shirou/gopsutil/v3/common" and pass a context with common.EnvMap set to common.EnvKey, and the location will be used within each function.

	ctx := context.WithValue(context.Background(), 
		common.EnvKey, common.EnvMap{common.HostProcEnvKey: "/myproc"},
	)
	v, err := mem.VirtualMemoryWithContext(ctx)

First priority is given to the value set in context, then the value from the environment variable, and finally the default location.

Caching

As of v3.24.1, it is now possible to cached some values. These values default to false, not cached.

Be very careful that enabling the cache may cause inconsistencies. For example, if you enable caching of boottime on Linux, be aware that unintended values may be returned if the boottime is changed by NTP after booted.

  • host
    • EnableBootTimeCache
  • process
    • EnableBootTimeCache

Documentation

See https://pkg.go.dev/github.com/shirou/gopsutil/v3 or https://godocs.io/github.com/shirou/gopsutil/v3

Requirements

  • go1.16 or above is required.

More Info

Several methods have been added which are not present in psutil, but will provide useful information.

  • host/HostInfo() (linux)
    • Hostname
    • Uptime
    • Procs
    • OS (ex: "linux")
    • Platform (ex: "ubuntu", "arch")
    • PlatformFamily (ex: "debian")
    • PlatformVersion (ex: "Ubuntu 13.10")
    • VirtualizationSystem (ex: "LXC")
    • VirtualizationRole (ex: "guest"/"host")
  • IOCounters
  • cpu/CPUInfo() (linux, freebsd)
    • CPU (ex: 0, 1, ...)
    • VendorID (ex: "GenuineIntel")
    • Family
    • Model
    • Stepping
    • PhysicalID
    • CoreID
    • Cores (ex: 2)
    • ModelName (ex: "Intel(R) Core(TM) i7-2640M CPU @ 2.80GHz")
    • Mhz
    • CacheSize
    • Flags (ex: "fpu vme de pse tsc msr pae mce cx8 ...")
    • Microcode
  • load/Avg() (linux, freebsd, solaris)
    • Load1
    • Load5
    • Load15
  • docker/GetDockerIDList() (linux only)
    • container id list ([]string)
  • docker/CgroupCPU() (linux only)
    • user
    • system
  • docker/CgroupMem() (linux only)
    • various status
  • net_protocols (linux only)
    • system wide stats on network protocols (i.e IP, TCP, UDP, etc.)
    • sourced from /proc/net/snmp
  • iptables nf_conntrack (linux only)
    • system wide stats on netfilter conntrack module
    • sourced from /proc/sys/net/netfilter/nf_conntrack_count

Some code is ported from Ohai. Many thanks.

Current Status

  • x: works
  • b: almost works, but something is broken
name Linux FreeBSD OpenBSD macOS Windows Solaris Plan 9
cpu_times x x x x x b
cpu_count x x x x x x
cpu_percent x x x x x
cpu_times_percent x x x x x
virtual_memory x x x x x b x
swap_memory x x x x x
disk_partitions x x x x x
disk_io_counters x x x
disk_usage x x x x x
net_io_counters x x x b x
boot_time x x x x x
users x x x x x
pids x x x x x
pid_exists x x x x x
net_connections x x x x
net_protocols x
net_if_addrs
net_if_stats
netfilter_conntrack x

Process class

name Linux FreeBSD OpenBSD macOS Windows
pid x x x x x
ppid x x x x x
name x x x x x
cmdline x x x x
create_time x x x
status x x x x
cwd x x x
exe x x x x
uids x x x x
gids x x x x
terminal x x x
io_counters x x x x
nice x x x x x
num_fds x
num_ctx_switches x
num_threads x x x x x
cpu_times x x
memory_info x x x x x
memory_info_ex x
memory_maps x
open_files x
send_signal x x x x
suspend x x x x
resume x x x x
terminate x x x x x
kill x x x x
username x x x x x
ionice
rlimit x
num_handlers
threads x
cpu_percent x x x x
cpu_affinity
memory_percent x x
parent x x x x
children x x x x x
connections x x x
is_running
page_faults x

Original Metrics

item Linux FreeBSD OpenBSD macOS Windows Solaris
HostInfo
hostname x x x x x x
uptime x x x x x
process x x x x
os x x x x x x
platform x x x x x
platformfamily x x x x x
virtualization x
CPU
VendorID x x x x x x
Family x x x x x x
Model x x x x x x
Stepping x x x x x x
PhysicalID x x
CoreID x x
Cores x x x
ModelName x x x x x x
Microcode x x
LoadAvg
Load1 x x x x
Load5 x x x x
Load15 x x x x
GetDockerID
container id x no no no no
CgroupsCPU
user x no no no no
system x no no no no
CgroupsMem
various x no no no no
  • future work
    • process_iter
    • wait_procs
    • Process class
      • as_dict
      • wait

License

New BSD License (same as psutil)

Related Works

I have been influenced by the following great works:

How to Contribute

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

English is not my native language, so PRs correcting grammar or spelling are welcome and appreciated.

gopsutil's People

Contributors

ablagoev avatar atakancolak avatar atoulme avatar botherder avatar danielnelson avatar darkgeek avatar def avatar dependabot[bot] avatar gabibguti avatar gballet avatar jen20 avatar kardianos avatar kenjitakahashi avatar lomanic avatar mayowa avatar mmorel-35 avatar mpfz0r avatar mznet avatar omar-polo avatar punya avatar rasa avatar scop avatar sean- avatar secdre4mer avatar shirou avatar sparrc avatar tbarker25 avatar tklauser avatar tmm1 avatar walles avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gopsutil's Issues

build fails due to wrong types in host.Users() api call

host.Users() uses acrh specific utmp struct types. In raspberry pi this returns byte array straight instead of int8 array for u.User field.

go version : 1.5.1
OS: Linux, Ubuntu 15.05,
Arch: armhf (Raspberry Pi 2)

../../shirou/gopsutil/host/host_linux.go:110: cannot use u.User[:] (type []byte) as type []int8 in argument to common.IntToString
../../shirou/gopsutil/host/host_linux.go:111: cannot use u.Line[:] (type []byte) as type []int8 in argument to common.IntToString
../../shirou/gopsutil/host/host_linux.go:112: cannot use u.Host[:] (type []byte) as type []int8 in argument to common.IntToString

Re-enable error handling

A lot of simple error conditions are currently suppressed by functions like "mustParse...". I'd strongly suggest to remove those functions at let functions that originally used them give an error instead of silencing the condition.

Doesn't compile on windows

C:\Users\ben.aldrich\go\src\github.com\shirou\gopsutil\disk [master]> go build
# github.com/shirou/gopsutil/disk
.\disk_windows.go:43: no new variables on left side of :=
.\disk_windows.go:43: cannot use DiskUsageStat literal (type *DiskUsageStat) as type DiskUsageStat in assignment
.\disk_windows.go:54: cannot use ret (type DiskUsageStat) as type *DiskUsageStat in return argument

common.StringContains() is exactly equal compare, but used for "contains".

For example: https://github.com/shirou/gopsutil/blob/master/host/host_linux.go#L340

In Linux:

$ grep kvm /proc/modules
kvm_intel 143109 6 - Live 0x0000000000000000
kvm 451552 1 kvm_intel, Live 0x0000000000000000

in the following code to test contains would be fail:

contents, err := common.ReadLines("/proc/modules")
common.StringContains(contents, "kvm")

The common.StringContains() is :

// Check the target string slice containes src or not
func StringContains(target []string, src string) bool {
    for _, t := range target {
        if strings.TrimSpace(t) == src {
            return true
        }
    }
    return false
}

This is a common func in gopsutil, but it has a confused name. "String" can not display a "string list" mean, and "Contains" can not display a "contain this sub str" mean.

build failed on mac about cgo

i meet a problem when build gopsutil on mac

full error output:
not-type:1:35: error: use of undeclared identifier 'cgo_undefined'
void *cgo_f_1_2(void) { GoBytes __cgo_undefined; }

not-const:1:63: error: invalid operands to binary expression ('GoBytes (_)(void *, int)' and 'int')
void cgo_f_1_3(void) { enum { __cgo__undefined = (GoBytes)_1 }; }

not-type:2:52: error: use of undeclared identifier 'cgo_undefined'
void *cgo_f_2_2(void) { HOST_CPU_LOAD_INFO_COUNT __cgo_undefined; }

not-const:3:81: error: indirection requires pointer operand ('int' invalid)
void cgo_f_3_3(void) { enum { __cgo__undefined = (host_cpu_load_info_data_t)*1 }; }

not-const:4:67: error: indirection requires pointer operand ('int' invalid)
void cgo_f_4_3(void) { enum { __cgo__undefined = (host_info_t)*1 }; }

not-type:5:47: error: use of undeclared identifier 'cgo_undefined'
void *cgo_f_5_2(void) { host_processor_info __cgo_undefined; }

not-const:5:75: error: invalid operands to binary expression ('kern_return_t (_)(host_t, processor_flavor_t, natural_t *, processor_info_array_t *, mach_msg_type_number_t *)' and 'int')
void cgo_f_5_3(void) { enum { __cgo__undefined = (host_processor_info)_1 }; }

not-type:6:43: error: use of undeclared identifier 'cgo_undefined'
void *cgo_f_6_2(void) { host_statistics __cgo_undefined; }

not-const:6:71: error: invalid operands to binary expression ('kern_return_t (_)(host_t, host_flavor_t, host_info_t, mach_msg_type_number_t *)' and 'int')
void cgo_f_6_3(void) { enum { __cgo__undefined = (host_statistics)_1 }; }

not-const:7:62: error: indirection requires pointer operand ('int' invalid)
void cgo_f_7_3(void) { enum { __cgo__undefined = (host_t)*1 }; }

Redhat Machines Returns Error in searching the /proc files

Problem

    [root@compute ~]# ./testing
-1 open /proc/917/stat: no such file or directory
<nil>
readlink /proc/917/exe: no such file or directory
0 open /proc/917/stat: no such file or directory
readlink /proc/917/cwd: no such file or directory
<nil> not implemented yet
<nil>
open /proc/917/stat: no such file or directory
<nil> <nil>
<nil> open /proc/917/statm: no such file or directory
true not implemented yet
{"hostname":"compute","uptime":1435308047,"procs":0,"os":"linux","platform":"redhat","platform_family":"rhel","platform_version":"7.1","virtualization_system":"","virtualization_role":""}
1435308047
[{"user":"reboot","terminal":"~","host":"3.10.0-229.el7.x86_64","started":1435308054} {"user":"LOGIN","terminal":"tty1","host":"","started":1435308063} {"user":"runlevel","terminal":"~","host":"3.10.0-229.el7.x86_64","started":1435308066} {"user":"root","terminal":"pts/0","host":"115.249.70.178","started":1435309212} {"user":"root","terminal":"pts/1","host":"115.249.70.178","started":1435312674}]
redhat rhel 7.1 <nil>
<nil>

Code for this

package main

import (
"fmt"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/process"
_ "time"
)

func Testing() {
h, _ := host.HostInfo()
b, _ := host.BootTime()
v, _ := process.NewProcess(917)
u, _ := host.Users()

fmt.Println(v.Ppid())
fmt.Println(v.Name())
fmt.Println(v.Exe())
fmt.Println(v.CreateTime())
fmt.Println(v.Cwd())
fmt.Println(v.Parent())
fmt.Println(v.Status())
fmt.Println(v.Terminal())
fmt.Println(v.MemoryInfo())
fmt.Println(v.MemoryInfoEx())
fmt.Println(v.IsRunning())
fmt.Println(h)
fmt.Println(b)
fmt.Println(u)
fmt.Println(host.GetPlatformInformation())
fmt.Println(host.GetVirtualization())
}

func main() {
Testing()
}

On windows, calling disk.DiskIOCounters() causes WMI service to disrupt write activity in elasticsearch

Our application wraps an elasticsearch instance which is constantly indexing (writing) while periodically collecting stats using the following gopsutil calls:

cpu.CPUPercent(0, true)
disk.DiskIOCounters()
mem.VirtualMemory()
disk.DiskPartitions(true)
disk.DiskUsage(...)

We noticed using jstack that every 15 minutes, elasticsearch writes were blocking for a short window, but long enough to fall behind and be disruptive. We noticed that stopping and disabling the WMI service made the problem disappear. Ultimately, we found that not calling the disk.DiskIOCounters() method fixed the problem while the WMI process was running, suggesting that method was doing something to cause the disruptive WMI behavior.

Propper usage of CPUPercent()

Hi!

cpu.CPUPercent() works very strange on consequent calls - it returns zero CPU usage almost on every call.

Some code:

package main

import (
    "fmt"
    psutil "github.com/shirou/gopsutil/cpu"
    "syscall"
    "time"
)

func main() {
    for i := 0; i < 10; i++ {
        cpu_res, e := psutil.CPUPercent(0, false)
        if e == nil {
            fmt.Printf("%d cpu: %v\n", i, cpu_res)
        } else {
            fmt.Printf("Error getting cpu usage: %s\n", e.Error())
            syscall.Exit(1)
        }
        time.Sleep(200 * time.Millisecond)
    }
}

Results:

0 cpu: [0]
1 cpu: [0.152439024712165]
2 cpu: [0]
3 cpu: [0]
4 cpu: [0]
5 cpu: [0]
6 cpu: [0]
7 cpu: [0]
8 cpu: [0]
9 cpu: [0]

What am I doing wrong?
OS Debian Wheezy

TestCPUPercent tests fail on small machines

=== RUN TestCPUPercent
--- FAIL: TestCPUPercent (0.02s)
cpu_test.go:86: CPUPercent value is invalid: 100.000002
=== RUN TestCPUPercentPerCpu
--- FAIL: TestCPUPercentPerCpu (0.02s)
cpu_test.go:86: CPUPercent value is invalid: 100.000002

Need to add a small margin in test or do math differently in methods.

Enhancement: Avg req size for disk io

I don't know how hard this would be to add but it would be really nice if the disk plugin exported avg request size. It would really help discern if you have sequential or random IO going on. I could take a stab at making this work but I don't know any Go so the code might be horrible

Using gopsutil in a container

So there is a default /proc file system within a container that is used for container level pids etc... It would be nice to override where gopsutil looks so you could bind mount the host /proc file system into the container.

Expose data in /proc/net/snmp

I would like to get access to the information on each network protocol available in /proc/net/snmp on linux systems. I think adding a new type like NetProtoCountersStat might work I can provide a linux implementation that reads the proc file directly. The only utility to my knowledge that parses /proc/net/snmp is netstat -s but its output is not very parser friendly. Also /proc/net/snmp is defined to be an ASCII snmp format so its structure hasn't and shouldn't change anytime soon.

Thoughts?

Make host/linux.go continue if os.Hostname fails.

Hi, thanks so much for this project! We love it.

in host/linux.go we have this function, but os.Hostname failure causes it to fail completely.

In other places, failure just means "zero value".

My proposal is to

  • on os.Hostname failure, just use "" for host.
  • always return "nil" for error

Ideally I would like to "return by value" and not a *HostInfoStat but that might break compatibility.

Im happy to send in a pull request.

with kind regards,

n

func HostInfo() (*HostInfoStat, error) {
    hostname, err := os.Hostname()
    if err != nil {
        return nil, err /// CHANGE -->  hostname = "" and keep going
    }

    ret := &HostInfoStat{
        Hostname: hostname,
        OS:       runtime.GOOS,
    }

    platform, family, version, err := GetPlatformInformation()
    if err == nil {
        ret.Platform = platform
        ret.PlatformFamily = family
        ret.PlatformVersion = version
    }
    system, role, err := GetVirtualization()
    if err == nil {
        ret.VirtualizationSystem = system
        ret.VirtualizationRole = role
    }
    uptime, err := BootTime()
    if err == nil {
        ret.Uptime = uptime
    }

    return ret, nil
}

Wrong type for cpu.CPUTimesStat

The code is

type CPUTimesStat struct {
    CPU       string  `json:"cpu"`
    User      float32 `json:"user"`
    System    float32 `json:"system"`
    Idle      float32 `json:"idle"`
    Nice      float32 `json:"nice"`
    Iowait    float32 `json:"iowait"`
    Irq       float32 `json:"irq"`
    Softirq   float32 `json:"softirq"`
    Steal     float32 `json:"steal"`
    Guest     float32 `json:"guest"`
    GuestNice float32 `json:"guest_nice"`
    Stolen    float32 `json:"stolen"`
}

But shouldn't the type Nice Idle, User, System be the type of uint64 ?

do not have Pids()

i get the project but told me

github.com/shirou/gopsutil

../../../go_parograms/src/github.com/shirou/gopsutil/process.go:70: undefined: Pids
how to solve?

Doesn't handle processes with spaces in name

As per http://man7.org/linux/man-pages/man5/proc.5.html, the second field is the filename of the executable in parentheses:

  1. comm %s
    The filename of the executable, in parentheses.
    This is visible whether or not the executable is
    swapped out.

In the case of spaces in the filename (as I observed today) /proc/[pid]/stat looks like:

1591 (Plex Media Serv) S 1 1591 1591 0 -1 1077952768 19063 0 306 0 257 95 0 0 20 0 17 0 1353 596594688 13330 18446744073709551615 1 1 0 0 0 0 0 4097 17646 18446744073709551615 0 0 17 4 0 0 69 0 0 0 0 0 0 0 0 0 0

Rather than hard coding the field numbers they should be offset from the field ending in ).

Allow to optionally specify process id when querying NetIOCounters

My use case is that I want to collect network statistics of various docker containers. At least in Linux every process has it's own version of /proc/net/dev in /proc/PID/net/dev. Usually those are identical but in docker containers the PID-specific net/dev only shows the container specific network namespace. This makes it very easy to parse container specific

Only thing missing is the possibility to override the static "net/dev" in NetIOCounters function with "PID/net/dev". It would be great if you could add

func NetIOCountersByPID(pid int, pernic bool) ([]NetIOCountersStat, error) {
  filename := common.HostProc(pid, "net/dev")
[...]
}

or something similar.

cpu.CPUInfo() compilation error on darwin go1.3.3

Go version

$ go version
go version go1.3.3 darwin/amd64

Code

package main

import (
  "fmt"
  "github.com/shirou/gopsutil/cpu"
)

func main() {
  v, err := cpu.CPUInfo()
  if err == nil {
    fmt.Println(v)
  } else {
    fmt.Println(err)
  }
}

Build result

$ go build
# github.com/shirou/gopsutil/cpu
../github.com/shirou/gopsutil/cpu/cpu_darwin_cgo.go:99: index out of bounds
../github.com/shirou/gopsutil/cpu/cpu_darwin_cgo.go:100: index out of bounds
../github.com/shirou/gopsutil/cpu/cpu_darwin_cgo.go:101: index out of bounds
../github.com/shirou/gopsutil/cpu/cpu_darwin_cgo.go:102: index out of bounds

cpu.CpuInfo() return empty data on Raspberry Pi

cpu.CPUInfo() returns empty data on Raspberry Pi 2 (quadcore, ARM v7) running ubuntu 15.04 (kernel 3.17) . This is due to /proc/cpuinfo being differently formatted. Following is an example:

processor : 0
model name  : ARMv7 Processor rev 5 (v7l)
BogoMIPS  : 57.60
Features  : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part  : 0xc07
CPU revision  : 5

CPU frequency cant be obtained from /proc/cpuinfo on ARM. There are two ways to get this data:
a) Invoke lscpu and parse the resulting data
b) read /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq (one entry for each core)

I dont know whats the preferred way to implement this, @shirou if you can give some feedback, i'd be happy to implement this :-)
thanks for this awesome library

cpu: CPUInfo needs to clarify or correct topology.

nomad currently uses cpu.CPUInfo to count cores. It takes the total CPUInfo struct count and adds each core count in each cpu to get the total number of cores. For example my six core machine will result in reporting 36 total cores when counted this way.

Probably Processor / Cores need better topology reporting on all architectures. For example under "/sys/.../cpuX/topology/ it can report which cores are in each processor.

exit status 1 on Mac OS X, go test fails

When calling CPUPercent on OS X (Darwin) I get an error returned with the message "Exit status 1".
Tracing it through gopsutil reveals that doSysctrl() with argument "kern.cp_time" is throwing an error.
This is because OS X doesn't have "kern.cp_time" in /usr/sbin/sysctl

As a result, go test fails on OS X.

--- FAIL: TestCpu_times (0.00 seconds)
    cpu_test.go:13: error exit status 1
    cpu_test.go:16: could not get CPUs  exit status 1
--- FAIL: TestCPUPercent (0.00 seconds)
    cpu_test.go:64: error exit status 1
    cpu_test.go:68: wrong number of entries from CPUPercent: []
--- FAIL: TestCPUPercentPerCpu (0.00 seconds)
    cpu_test.go:64: error exit status 1
    cpu_test.go:68: wrong number of entries from CPUPercent: []

mem Usage about CgroupMemStat

hi @shirou Very glad to see you add a metric related to docker. But there has a little question. Why do not you return docker memory like docker API

"memory_stats" : {
        "stats" : {
           "total_pgmajfault" : 0,
           "cache" : 0,
           "mapped_file" : 0,
           "total_inactive_file" : 0,
           "pgpgout" : 414,
           "rss" : 6537216,
           "total_mapped_file" : 0,
           "writeback" : 0,
           "unevictable" : 0,
           "pgpgin" : 477,
           "total_unevictable" : 0,
           "pgmajfault" : 0,
           "total_rss" : 6537216,
           "total_rss_huge" : 6291456,
           "total_writeback" : 0,
           "total_inactive_anon" : 0,
           "rss_huge" : 6291456,
           "hierarchical_memory_limit" : 67108864,
           "total_pgfault" : 964,
           "total_active_file" : 0,
           "active_anon" : 6537216,
           "total_active_anon" : 6537216,
           "total_pgpgout" : 414,
           "total_cache" : 0,
           "inactive_anon" : 0,
           "active_file" : 0,
           "pgfault" : 964,
           "inactive_file" : 0,
           "total_pgpgin" : 477
        },
        "max_usage" : 6651904,
        "usage" : 6537216,
        "failcnt" : 0,
        "limit" : 67108864
     }

there has the max_usage and usage and limit clear.

in mac/ubuntu/centos can't get cpu time.

$ go test -v
=== RUN TestCpu_times
--- FAIL: TestCpu_times (0.00s)
cpu_test.go:13: error could not get cpu time
cpu_test.go:16: could not get CPUs could not get cpu time
=== RUN TestCpu_counts
--- PASS: TestCpu_counts (0.00s)
=== RUN TestCPUTimeStat_String
--- PASS: TestCPUTimeStat_String (0.00s)
=== RUN TestCpuInfo
--- PASS: TestCpuInfo (0.00s)
=== RUN TestCPUPercent
--- FAIL: TestCPUPercent (0.00s)
cpu_test.go:72: error could not get cpu time
cpu_test.go:75: wrong number of entries from CPUPercent: []
=== RUN TestCPUPercentPerCpu
--- FAIL: TestCPUPercentPerCpu (0.00s)
cpu_test.go:72: error could not get cpu time
cpu_test.go:75: wrong number of entries from CPUPercent: []
FAIL
exit status 1
FAIL github.com/shirou/gopsutil/cpu 0.007s

Cannot compile on FreeBSD 32bit.

Issuing go get github.com/shirou/gopsutil ends in

go/src/github.com/shirou/gopsutil/disk_freebsd.go:14: undefined: MntWait
go/src/github.com/shirou/gopsutil/disk_freebsd.go:19: undefined: Statfs
go/src/github.com/shirou/gopsutil/disk_freebsd.go:20: undefined: MntWait
go/src/github.com/shirou/gopsutil/disk_freebsd.go:24: undefined: MntReadOnly
go/src/github.com/shirou/gopsutil/disk_freebsd.go:27: undefined: MntSynchronous
go/src/github.com/shirou/gopsutil/disk_freebsd.go:30: undefined: MntNoExec
go/src/github.com/shirou/gopsutil/disk_freebsd.go:117: undefined: Statfs
go/src/github.com/shirou/gopsutil/process_freebsd.go:190: undefined: KinfoProc
go/src/github.com/shirou/gopsutil/process_freebsd.go:228: undefined: KinfoProc
go/src/github.com/shirou/gopsutil/process_freebsd.go:277: undefined: KinfoProc
go/src/github.com/shirou/gopsutil/disk_freebsd.go:30: too many errors

That's obviously because these symbols are only defined in the _amd64 files. I don't know why they were separated for 64bit only (I'm assuming there was a reason), so I don't know how to fix it. Therefore leaving an issue instead.

BTW: Same thing probably applies to other systems 32bit, not tested though.

Recommended structural changes

Factor out all the new calls:
common.GetEnv -> common.GetProc and common.GetSys.
Call filepath.Join within the GetProc and GetSys calls.
Currently lots of room for copy and paste errors.

Move common package into "internal" directory. No need to make API promise on that one, correct?

wmic tests - error exit status 44210 on Win 7 64-bit

D:\dev\go\gopath\src\github.com\shirou\gopsutil\process [master...origin/master]> go test --test.run=Test_Process_Name
--- FAIL: Test_Process_Name (0.19s)
        process_test.go:188: geting name error exit status 44210
        process_test.go:191: invalid Exe
FAIL
exit status 1
FAIL    github.com/shirou/gopsutil/process      0.257s

I have wmic (from C:\Windows\System32\wbem) in mypath.

go version go1.4.2 windows/amd64

[RFC] Separate subdirectory

I write all files under the root directory. But each part is not related and some user may want to use just a/some feature.

I want to separate to subdirectory like

gopsutil
   - common
        - common_linux.go
        - common_freebsd.go
   - host
        - host_linux.go
        - host_freebsd.go
   - disk
        - disk_linux.go
        - disk_linux.go

I know this will break compatibility. But I think it can be more structured and become easy to maintain and enhance.
How do you feel it? please tell what you think.

VirtualMemoryStat.UsedPercent misleading

For example, on on OS X, here is some sample output

Total: 4294967296
Free: 16121856
Used: 4278845440
Utilization: 83

Whereas it should be 99%. The reason for this is that instead of using only Total, and Used, Total is reduced by Available, which includes Cached.

So either it should be explicitly documented that the utilization is reduced by the potentially available Cached memory or โ€“ and this is what I suggest โ€“ the utilization should show the current state of the memory consumption as follows:

ret.UsedPercent = float64(ret.Used) * (float64(100)/float64(ret.Total))

Please let me know which road to take, I'll be happy to submit an according pull request.

About CPUTimes

Hi @shirou i have a question CPUTimes
if i give the param percpu false. Through the code, I think I get to be the first CPU information but you write as "cpu-total" Both are equal it?

process cpu_percentage is incorrect (centos 7)

I was getting unusually high numbers for a proc that was consuming 0.3% of a cpu in top Process.CPUPercent() was giving me between 3% - 7%. So I gave it a pid of a process that was consuming consistently 467% according to top and it gives me 4556.54044031905... How does this number compare to top? Should I be able to do something simple to get close to top numbers?

start to using CGO on cpu/cpu_darwin

I know there are many people who want to get CPUTimes on darwin using CGO. I had rejected because of easy cross-compiling. I had wrote using helper process. But yes, this is bad approach. And I think there is no way to get host_processer_info from pure Go because this is belongs to Mach part of darwin.

So, now, I have changed my mind.

Since 47f6760, cpu_darwin uses CGO. Current code is borrowed from influxdb/telegraf. Thank you using gopsutil!)

If someone want to remove CGO dependency, please write comments for this issue. I will remove quickly.

I always grateful to such a many people using, discussing, and contributing gopsutil.

reference

Should be separated by platform

Note: [WIP]

So many files are in the same place. It looks like messy, should be separated by platform.

One idea is following.

  • linux.go
  • freebsd.go <-- these have only a set of API
  • freebsd
    • host_freebsd.go
    • memory_freebsd.go
  • linux
    • host_linux.go
    • memory_linux.go

The other application which want to use gopsutil, just import top package.

in the cpu.go file, there is a interface like

type CPU interface {
    CPUTimes()
    CPUInfo()
}

Is it works?

and this breaks current API and also an original psutil API.

cpu.CPUInfo() returns empty []cpu.CPUInfoStat object on linux

The cpu library appears to be broken on linux, found while chasing down hashicorp/nomad#302

Tested with the following code:

// snippets2
package main

import (
"github.com/davecgh/go-spew/spew"
"github.com/shirou/gopsutil/cpu"
)

func main() {
info, err := cpu.CPUInfo()
if err != nil {
    panic(err)
}
spew.Dump(info)
}

On darwin I get this:

([]cpu.CPUInfoStat) (len=1 cap=1) {
 (cpu.CPUInfoStat) {"cpu":0,"vendor_id":"GenuineIntel","family":"6","model":"70","stepping":1,"physical_id":"","core_id":"","cores":4,"model_name":"Intel(R) Core(TM) i7-4850HQ CPU @ 2.30GHz","mhz":2300,"cache_size":256,"flags":["fpu","vme","de","pse","tsc","msr","pae","mce","cx8","apic","sep","mtrr","pge","mca","cmov","pat","pse36","clfsh","ds","acpi","mmx","fxsr","sse","sse2","ss","htt","tm","pbe","sse3","pclmulqdq","dtes64","mon","dscpl","vmx","smx","est","tm2","ssse3","fma","cx16","tpr","pdcm","sse4.1","sse4.2","x2apic","movbe","popcnt","aes","pcid","xsave","osxsave","seglim64","tsctmr","avx1.0","rdrand","f16c","smep","erms","rdwrfsgs","tsc_thread_offset","bmi1","hle","avx2","bmi2","invpcid","rtm","syscall","xd","1gbpage","em64t","lahf","lzcnt","rdtscp","tsci"]}

}

On CentOS Linux release 7.1.1503 (Core)

([]cpu.CPUInfoStat) <nil>

This is with go version 1.5.1 in both places. Running as root doesn't make a difference. Tested with a 'go get' of the library a few minutes ago.

Failed to install on windows

I got this error when installing it on windows.

# github.com/shirou/gopsutil
..\..\github.com\shirou\gopsutil\process_windows.go:10: syntax error: unexpected semicolon or newline

CPUPercent can't convert float64 to string

When I try to convert CPUPercent from float64 to string I get the type error below. I think it's because of the surrounding [] that's returned. Not entirely sure why they're there, when the rest of the CPU returns don't have [].

cpuPercent, _ := cpu.CPUPercent(time.Duration(1) * time.Second, false)
cpuusedprctConv := strconv.FormatFloat(cpuPercent, 'f', -1, 64)

Error: cannot use cpuPercent (type []float64) as type float64 in argument to strconv.FormatFloat

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.