mirror of
https://github.com/shirou/gopsutil.git
synced 2025-05-08 19:29:25 +08:00
Merge pull request #6 from lukaslueg/master
Fix missing error handling, gofmt
This commit is contained in:
commit
bc8d9df079
@ -8,11 +8,11 @@ package gopsutil
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var NotImplementedError = errors.New("not implemented yet")
|
var NotImplementedError = errors.New("not implemented yet")
|
||||||
|
@ -10,17 +10,17 @@ import (
|
|||||||
|
|
||||||
// sys/resource.h
|
// sys/resource.h
|
||||||
const (
|
const (
|
||||||
CP_USER = 0
|
CPUser = 0
|
||||||
CP_NICE = 1
|
CPNice = 1
|
||||||
CP_SYS = 2
|
CPSys = 2
|
||||||
CP_INTR = 3
|
CPIntr = 3
|
||||||
CP_IDLE = 4
|
CPIdle = 4
|
||||||
CPUSTATES = 5
|
CPUStates = 5
|
||||||
)
|
)
|
||||||
|
|
||||||
// time.h
|
// time.h
|
||||||
const (
|
const (
|
||||||
CLOCKS_PER_SEC = 128
|
ClocksPerSec = 128
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: get per cpus
|
// TODO: get per cpus
|
||||||
@ -32,18 +32,18 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
|
|||||||
return ret, err
|
return ret, err
|
||||||
}
|
}
|
||||||
|
|
||||||
user, _ := strconv.ParseFloat(cpuTime[CP_USER], 32)
|
user, _ := strconv.ParseFloat(cpuTime[CPUser], 32)
|
||||||
nice, _ := strconv.ParseFloat(cpuTime[CP_NICE], 32)
|
nice, _ := strconv.ParseFloat(cpuTime[CPNice], 32)
|
||||||
sys, _ := strconv.ParseFloat(cpuTime[CP_SYS], 32)
|
sys, _ := strconv.ParseFloat(cpuTime[CPSys], 32)
|
||||||
idle, _ := strconv.ParseFloat(cpuTime[CP_IDLE], 32)
|
idle, _ := strconv.ParseFloat(cpuTime[CPIdle], 32)
|
||||||
intr, _ := strconv.ParseFloat(cpuTime[CP_INTR], 32)
|
intr, _ := strconv.ParseFloat(cpuTime[CPIntr], 32)
|
||||||
|
|
||||||
c := CPUTimesStat{
|
c := CPUTimesStat{
|
||||||
User: float32(user / CLOCKS_PER_SEC),
|
User: float32(user / ClocksPerSec),
|
||||||
Nice: float32(nice / CLOCKS_PER_SEC),
|
Nice: float32(nice / ClocksPerSec),
|
||||||
System: float32(sys / CLOCKS_PER_SEC),
|
System: float32(sys / ClocksPerSec),
|
||||||
Idle: float32(idle / CLOCKS_PER_SEC),
|
Idle: float32(idle / ClocksPerSec),
|
||||||
Irq: float32(intr / CLOCKS_PER_SEC), // FIXME: correct?
|
Irq: float32(intr / ClocksPerSec),
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = append(ret, c)
|
ret = append(ret, c)
|
||||||
|
@ -10,17 +10,17 @@ import (
|
|||||||
|
|
||||||
// sys/resource.h
|
// sys/resource.h
|
||||||
const (
|
const (
|
||||||
CP_USER = 0
|
CPUser = 0
|
||||||
CP_NICE = 1
|
CPNice = 1
|
||||||
CP_SYS = 2
|
CPSys = 2
|
||||||
CP_INTR = 3
|
CPIntr = 3
|
||||||
CP_IDLE = 4
|
CPIdle = 4
|
||||||
CPUSTATES = 5
|
CPUStates = 5
|
||||||
)
|
)
|
||||||
|
|
||||||
// time.h
|
// time.h
|
||||||
const (
|
const (
|
||||||
CLOCKS_PER_SEC = 128
|
ClocksPerSec = 128
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: get per cpus
|
// TODO: get per cpus
|
||||||
@ -32,18 +32,18 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
|
|||||||
return ret, err
|
return ret, err
|
||||||
}
|
}
|
||||||
|
|
||||||
user, _ := strconv.ParseFloat(cpuTime[CP_USER], 32)
|
user, _ := strconv.ParseFloat(cpuTime[CPUser], 32)
|
||||||
nice, _ := strconv.ParseFloat(cpuTime[CP_NICE], 32)
|
nice, _ := strconv.ParseFloat(cpuTime[CPNice], 32)
|
||||||
sys, _ := strconv.ParseFloat(cpuTime[CP_SYS], 32)
|
sys, _ := strconv.ParseFloat(cpuTime[CPSys], 32)
|
||||||
idle, _ := strconv.ParseFloat(cpuTime[CP_IDLE], 32)
|
idle, _ := strconv.ParseFloat(cpuTime[CPIdle], 32)
|
||||||
intr, _ := strconv.ParseFloat(cpuTime[CP_INTR], 32)
|
intr, _ := strconv.ParseFloat(cpuTime[CPIntr], 32)
|
||||||
|
|
||||||
c := CPUTimesStat{
|
c := CPUTimesStat{
|
||||||
User: float32(user / CLOCKS_PER_SEC),
|
User: float32(user / ClocksPerSec),
|
||||||
Nice: float32(nice / CLOCKS_PER_SEC),
|
Nice: float32(nice / ClocksPerSec),
|
||||||
System: float32(sys / CLOCKS_PER_SEC),
|
System: float32(sys / ClocksPerSec),
|
||||||
Idle: float32(idle / CLOCKS_PER_SEC),
|
Idle: float32(idle / ClocksPerSec),
|
||||||
Irq: float32(intr / CLOCKS_PER_SEC), // FIXME: correct?
|
Irq: float32(intr / ClocksPerSec),
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = append(ret, c)
|
ret = append(ret, c)
|
||||||
|
@ -11,7 +11,7 @@ func TestCpu_times(t *testing.T) {
|
|||||||
t.Errorf("error %v", err)
|
t.Errorf("error %v", err)
|
||||||
}
|
}
|
||||||
if len(v) == 0 {
|
if len(v) == 0 {
|
||||||
t.Errorf("could not get CPUs ", err)
|
t.Error("could not get CPUs ", err)
|
||||||
}
|
}
|
||||||
empty := CPUTimesStat{}
|
empty := CPUTimesStat{}
|
||||||
for _, vv := range v {
|
for _, vv := range v {
|
||||||
|
@ -11,62 +11,62 @@ func DiskPartitions(all bool) ([]DiskPartitionStat, error) {
|
|||||||
var ret []DiskPartitionStat
|
var ret []DiskPartitionStat
|
||||||
|
|
||||||
// get length
|
// get length
|
||||||
count, err := syscall.Getfsstat(nil, MNT_WAIT)
|
count, err := syscall.Getfsstat(nil, MntWait)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ret, err
|
return ret, err
|
||||||
}
|
}
|
||||||
|
|
||||||
fs := make([]Statfs, count)
|
fs := make([]Statfs, count)
|
||||||
_, err = Getfsstat(fs, MNT_WAIT)
|
_, err = Getfsstat(fs, MntWait)
|
||||||
|
|
||||||
for _, stat := range fs {
|
for _, stat := range fs {
|
||||||
opts := "rw"
|
opts := "rw"
|
||||||
if stat.FFlags&MNT_RDONLY != 0 {
|
if stat.FFlags&MntReadOnly != 0 {
|
||||||
opts = "ro"
|
opts = "ro"
|
||||||
}
|
}
|
||||||
if stat.FFlags&MNT_SYNCHRONOUS != 0 {
|
if stat.FFlags&MntSynchronous != 0 {
|
||||||
opts += ",sync"
|
opts += ",sync"
|
||||||
}
|
}
|
||||||
if stat.FFlags&MNT_NOEXEC != 0 {
|
if stat.FFlags&MntNoExec != 0 {
|
||||||
opts += ",noexec"
|
opts += ",noexec"
|
||||||
}
|
}
|
||||||
if stat.FFlags&MNT_NOSUID != 0 {
|
if stat.FFlags&MntNoSuid != 0 {
|
||||||
opts += ",nosuid"
|
opts += ",nosuid"
|
||||||
}
|
}
|
||||||
if stat.FFlags&MNT_UNION != 0 {
|
if stat.FFlags&MntUnion != 0 {
|
||||||
opts += ",union"
|
opts += ",union"
|
||||||
}
|
}
|
||||||
if stat.FFlags&MNT_ASYNC != 0 {
|
if stat.FFlags&MntAsync != 0 {
|
||||||
opts += ",async"
|
opts += ",async"
|
||||||
}
|
}
|
||||||
if stat.FFlags&MNT_SUIDDIR != 0 {
|
if stat.FFlags&MntSuidDir != 0 {
|
||||||
opts += ",suiddir"
|
opts += ",suiddir"
|
||||||
}
|
}
|
||||||
if stat.FFlags&MNT_SOFTDEP != 0 {
|
if stat.FFlags&MntSoftDep != 0 {
|
||||||
opts += ",softdep"
|
opts += ",softdep"
|
||||||
}
|
}
|
||||||
if stat.FFlags&MNT_NOSYMFOLLOW != 0 {
|
if stat.FFlags&MntNoSymFollow != 0 {
|
||||||
opts += ",nosymfollow"
|
opts += ",nosymfollow"
|
||||||
}
|
}
|
||||||
if stat.FFlags&MNT_GJOURNAL != 0 {
|
if stat.FFlags&MntGEOMJournal != 0 {
|
||||||
opts += ",gjounalc"
|
opts += ",gjounalc"
|
||||||
}
|
}
|
||||||
if stat.FFlags&MNT_MULTILABEL != 0 {
|
if stat.FFlags&MntMultilabel != 0 {
|
||||||
opts += ",multilabel"
|
opts += ",multilabel"
|
||||||
}
|
}
|
||||||
if stat.FFlags&MNT_ACLS != 0 {
|
if stat.FFlags&MntACLs != 0 {
|
||||||
opts += ",acls"
|
opts += ",acls"
|
||||||
}
|
}
|
||||||
if stat.FFlags&MNT_NOATIME != 0 {
|
if stat.FFlags&MntNoATime != 0 {
|
||||||
opts += ",noattime"
|
opts += ",noattime"
|
||||||
}
|
}
|
||||||
if stat.FFlags&MNT_NOCLUSTERR != 0 {
|
if stat.FFlags&MntClusterRead != 0 {
|
||||||
opts += ",nocluster"
|
opts += ",nocluster"
|
||||||
}
|
}
|
||||||
if stat.FFlags&MNT_NOCLUSTERW != 0 {
|
if stat.FFlags&MntClusterWrite != 0 {
|
||||||
opts += ",noclusterw"
|
opts += ",noclusterw"
|
||||||
}
|
}
|
||||||
if stat.FFlags&MNT_NFS4ACLS != 0 {
|
if stat.FFlags&MntNFS4ACLs != 0 {
|
||||||
opts += ",nfs4acls"
|
opts += ",nfs4acls"
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,13 +88,13 @@ func DiskIOCounters() (map[string]DiskIOCountersStat, error) {
|
|||||||
// /usr/include/devinfo.h
|
// /usr/include/devinfo.h
|
||||||
|
|
||||||
// get length
|
// get length
|
||||||
count, err := Getfsstat(nil, MNT_WAIT)
|
count, err := Getfsstat(nil, MntWait)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
fs := make([]Statfs, count)
|
fs := make([]Statfs, count)
|
||||||
_, err = Getfsstat(fs, MNT_WAIT)
|
_, err = Getfsstat(fs, MntWait)
|
||||||
|
|
||||||
ret := make(map[string]DiskIOCountersStat, 0)
|
ret := make(map[string]DiskIOCountersStat, 0)
|
||||||
for _, stat := range fs {
|
for _, stat := range fs {
|
||||||
|
@ -4,29 +4,29 @@
|
|||||||
package gopsutil
|
package gopsutil
|
||||||
|
|
||||||
const (
|
const (
|
||||||
MNT_WAIT = 1
|
MntWait = 1
|
||||||
MFSNAMELEN = 16 /* length of type name including null */
|
MfsNameLen = 16 /* length of type name including null */
|
||||||
MNAMELEN = 88 /* size of on/from name bufs */
|
MNameLen = 88 /* size of on/from name bufs */
|
||||||
)
|
)
|
||||||
|
|
||||||
// sys/mount.h
|
// sys/mount.h
|
||||||
const (
|
const (
|
||||||
MNT_RDONLY = 0x00000001 /* read only filesystem */
|
MntReadOnly = 0x00000001 /* read only filesystem */
|
||||||
MNT_SYNCHRONOUS = 0x00000002 /* filesystem written synchronously */
|
MntSynchronous = 0x00000002 /* filesystem written synchronously */
|
||||||
MNT_NOEXEC = 0x00000004 /* can't exec from filesystem */
|
MntNoExec = 0x00000004 /* can't exec from filesystem */
|
||||||
MNT_NOSUID = 0x00000008 /* don't honor setuid bits on fs */
|
MntNoSuid = 0x00000008 /* don't honor setuid bits on fs */
|
||||||
MNT_UNION = 0x00000020 /* union with underlying filesystem */
|
MntUnion = 0x00000020 /* union with underlying filesystem */
|
||||||
MNT_ASYNC = 0x00000040 /* filesystem written asynchronously */
|
MntAsync = 0x00000040 /* filesystem written asynchronously */
|
||||||
MNT_SUIDDIR = 0x00100000 /* special handling of SUID on dirs */
|
MntSuidDir = 0x00100000 /* special handling of SUID on dirs */
|
||||||
MNT_SOFTDEP = 0x00200000 /* soft updates being done */
|
MntSoftDep = 0x00200000 /* soft updates being done */
|
||||||
MNT_NOSYMFOLLOW = 0x00400000 /* do not follow symlinks */
|
MntNoSymFollow = 0x00400000 /* do not follow symlinks */
|
||||||
MNT_GJOURNAL = 0x02000000 /* GEOM journal support enabled */
|
MntGEOMJournal = 0x02000000 /* GEOM journal support enabled */
|
||||||
MNT_MULTILABEL = 0x04000000 /* MAC support for individual objects */
|
MntMultilabel = 0x04000000 /* MAC support for individual objects */
|
||||||
MNT_ACLS = 0x08000000 /* ACL support enabled */
|
MntACLs = 0x08000000 /* ACL support enabled */
|
||||||
MNT_NOATIME = 0x10000000 /* disable update of file access time */
|
MntNoATime = 0x10000000 /* disable update of file access time */
|
||||||
MNT_NOCLUSTERR = 0x40000000 /* disable cluster read */
|
MntClusterRead = 0x40000000 /* disable cluster read */
|
||||||
MNT_NOCLUSTERW = 0x80000000 /* disable cluster write */
|
MntClusterWrite = 0x80000000 /* disable cluster write */
|
||||||
MNT_NFS4ACLS = 0x00000010
|
MntNFS4ACLs = 0x00000010
|
||||||
)
|
)
|
||||||
|
|
||||||
type Statfs struct {
|
type Statfs struct {
|
||||||
@ -49,9 +49,9 @@ type Statfs struct {
|
|||||||
FOwner uint32 /* user that mounted the filesystem */
|
FOwner uint32 /* user that mounted the filesystem */
|
||||||
FFsid int32 /* filesystem id */
|
FFsid int32 /* filesystem id */
|
||||||
FCharspare [80]byte /* spare string space */
|
FCharspare [80]byte /* spare string space */
|
||||||
FFstypename [MFSNAMELEN]byte /* filesystem type name */
|
FFstypename [MfsNameLen]byte /* filesystem type name */
|
||||||
FMntfromname [MNAMELEN]byte /* mounted filesystem */
|
FMntfromname [MNameLen]byte /* mounted filesystem */
|
||||||
FMntonname [MNAMELEN]byte /* directory on which mounted */
|
FMntonname [MNameLen]byte /* directory on which mounted */
|
||||||
}
|
}
|
||||||
|
|
||||||
// /usr/include/devstat.h
|
// /usr/include/devstat.h
|
||||||
|
@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
SECTOR_SIZE = 512
|
SectorSize = 512
|
||||||
)
|
)
|
||||||
|
|
||||||
// Get disk partitions.
|
// Get disk partitions.
|
||||||
@ -54,8 +54,8 @@ func DiskIOCounters() (map[string]DiskIOCountersStat, error) {
|
|||||||
wbytes := mustParseUint64(fields[9])
|
wbytes := mustParseUint64(fields[9])
|
||||||
wtime := mustParseUint64(fields[10])
|
wtime := mustParseUint64(fields[10])
|
||||||
d := DiskIOCountersStat{
|
d := DiskIOCountersStat{
|
||||||
ReadBytes: rbytes * SECTOR_SIZE,
|
ReadBytes: rbytes * SectorSize,
|
||||||
WriteBytes: wbytes * SECTOR_SIZE,
|
WriteBytes: wbytes * SectorSize,
|
||||||
ReadCount: reads,
|
ReadCount: reads,
|
||||||
WriteCount: writes,
|
WriteCount: writes,
|
||||||
ReadTime: rtime,
|
ReadTime: rtime,
|
||||||
|
@ -16,8 +16,8 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
FILE_FILE_COMPRESSION = int64(16) // 0x00000010
|
FileFileCompression = int64(16) // 0x00000010
|
||||||
FILE_READ_ONLY_VOLUME = int64(524288) // 0x00080000
|
FileReadOnlyVolume = int64(524288) // 0x00080000
|
||||||
)
|
)
|
||||||
|
|
||||||
func DiskUsage(path string) (DiskUsageStat, error) {
|
func DiskUsage(path string) (DiskUsageStat, error) {
|
||||||
@ -86,10 +86,10 @@ func DiskPartitions(all bool) ([]DiskPartitionStat, error) {
|
|||||||
return ret, err
|
return ret, err
|
||||||
}
|
}
|
||||||
opts := "rw"
|
opts := "rw"
|
||||||
if lpFileSystemFlags&FILE_READ_ONLY_VOLUME != 0 {
|
if lpFileSystemFlags&FileReadOnlyVolume != 0 {
|
||||||
opts = "ro"
|
opts = "ro"
|
||||||
}
|
}
|
||||||
if lpFileSystemFlags&FILE_FILE_COMPRESSION != 0 {
|
if lpFileSystemFlags&FileFileCompression != 0 {
|
||||||
opts += ".compress"
|
opts += ".compress"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,34 +11,34 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type CgroupMemStat struct {
|
type CgroupMemStat struct {
|
||||||
ContainerID string `json:"containerid"`
|
ContainerID string `json:"containerid"`
|
||||||
Cache uint64 `json:"cache"`
|
Cache uint64 `json:"cache"`
|
||||||
RSS uint64 `json:"rss"`
|
RSS uint64 `json:"rss"`
|
||||||
Rss_huge uint64 `json:"rss_huge"`
|
RSSHuge uint64 `json:"rss_huge"`
|
||||||
Mapped_file uint64 `json:"mapped_file"`
|
MappedFile uint64 `json:"mapped_file"`
|
||||||
Pgpgin uint64 `json:"pgpgin"`
|
Pgpgin uint64 `json:"pgpgin"`
|
||||||
Pgpgout uint64 `json:"pgpgout"`
|
Pgpgout uint64 `json:"pgpgout"`
|
||||||
Pgfault uint64 `json:"pgfault"`
|
Pgfault uint64 `json:"pgfault"`
|
||||||
Pgmajfault uint64 `json:"pgmajfault"`
|
Pgmajfault uint64 `json:"pgmajfault"`
|
||||||
Inactive_anon uint64 `json:"inactive_anon"`
|
InactiveAnon uint64 `json:"inactive_anon"`
|
||||||
Active_anon uint64 `json:"active_anon"`
|
ActiveAnon uint64 `json:"active_anon"`
|
||||||
Inactive_file uint64 `json:"inactive_file"`
|
InctiveFile uint64 `json:"inactive_file"`
|
||||||
Active_file uint64 `json:"active_file"`
|
ActiveFile uint64 `json:"active_file"`
|
||||||
Unevictable uint64 `json:"unevictable"`
|
Unevictable uint64 `json:"unevictable"`
|
||||||
Hierarchical_memory_limit uint64 `json:"hierarchical_memory_limit"`
|
HierarchicalMemoryLimit uint64 `json:"hierarchical_memory_limit"`
|
||||||
Total_cache uint64 `json:"total_cache"`
|
TotalCache uint64 `json:"total_cache"`
|
||||||
Total_rss uint64 `json:"total_rss"`
|
TotalRSS uint64 `json:"total_rss"`
|
||||||
Total_rss_huge uint64 `json:"total_rss_huge"`
|
TotalRSSHuge uint64 `json:"total_rss_huge"`
|
||||||
Total_mapped_file uint64 `json:"total_mapped_file"`
|
TotalMappedFile uint64 `json:"total_mapped_file"`
|
||||||
Total_pgpgin uint64 `json:"total_pgpgin"`
|
TotalPgpgIn uint64 `json:"total_pgpgin"`
|
||||||
Total_pgpgout uint64 `json:"total_pgpgout"`
|
TotalPgpgOut uint64 `json:"total_pgpgout"`
|
||||||
Total_pgfault uint64 `json:"total_pgfault"`
|
TotalPgFault uint64 `json:"total_pgfault"`
|
||||||
Total_pgmajfault uint64 `json:"total_pgmajfault"`
|
TotalPgMajFault uint64 `json:"total_pgmajfault"`
|
||||||
Total_inactive_anon uint64 `json:"total_inactive_anon"`
|
TotalInactiveAnon uint64 `json:"total_inactive_anon"`
|
||||||
Total_active_anon uint64 `json:"total_active_anon"`
|
TotalActiveAnon uint64 `json:"total_active_anon"`
|
||||||
Total_inactive_file uint64 `json:"total_inactive_file"`
|
TotalInactiveFile uint64 `json:"total_inactive_file"`
|
||||||
Total_active_file uint64 `json:"total_active_file"`
|
TotalActiveFile uint64 `json:"total_active_file"`
|
||||||
Total_unevictable uint64 `json:"total_unevictable"`
|
TotalUnevictable uint64 `json:"total_unevictable"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDockerIDList returnes a list of DockerID.
|
// GetDockerIDList returnes a list of DockerID.
|
||||||
@ -120,9 +120,9 @@ func CgroupMem(containerid string, base string) (*CgroupMemStat, error) {
|
|||||||
case "rss":
|
case "rss":
|
||||||
ret.RSS = v
|
ret.RSS = v
|
||||||
case "rss_huge":
|
case "rss_huge":
|
||||||
ret.Rss_huge = v
|
ret.RSSHuge = v
|
||||||
case "mapped_file":
|
case "mapped_file":
|
||||||
ret.Mapped_file = v
|
ret.MappedFile = v
|
||||||
case "pgpgin":
|
case "pgpgin":
|
||||||
ret.Pgpgin = v
|
ret.Pgpgin = v
|
||||||
case "pgpgout":
|
case "pgpgout":
|
||||||
@ -132,43 +132,43 @@ func CgroupMem(containerid string, base string) (*CgroupMemStat, error) {
|
|||||||
case "pgmajfault":
|
case "pgmajfault":
|
||||||
ret.Pgmajfault = v
|
ret.Pgmajfault = v
|
||||||
case "inactive_anon":
|
case "inactive_anon":
|
||||||
ret.Inactive_anon = v
|
ret.InactiveAnon = v
|
||||||
case "active_anon":
|
case "active_anon":
|
||||||
ret.Active_anon = v
|
ret.ActiveAnon = v
|
||||||
case "inactive_file":
|
case "inactive_file":
|
||||||
ret.Inactive_file = v
|
ret.InctiveFile = v
|
||||||
case "active_file":
|
case "active_file":
|
||||||
ret.Active_file = v
|
ret.ActiveFile = v
|
||||||
case "unevictable":
|
case "unevictable":
|
||||||
ret.Unevictable = v
|
ret.Unevictable = v
|
||||||
case "hierarchical_memory_limit":
|
case "hierarchical_memory_limit":
|
||||||
ret.Hierarchical_memory_limit = v
|
ret.HierarchicalMemoryLimit = v
|
||||||
case "total_cache":
|
case "total_cache":
|
||||||
ret.Total_cache = v
|
ret.TotalCache = v
|
||||||
case "total_rss":
|
case "total_rss":
|
||||||
ret.Total_rss = v
|
ret.TotalRSS = v
|
||||||
case "total_rss_huge":
|
case "total_rss_huge":
|
||||||
ret.Total_rss_huge = v
|
ret.TotalRSSHuge = v
|
||||||
case "total_mapped_file":
|
case "total_mapped_file":
|
||||||
ret.Total_mapped_file = v
|
ret.TotalMappedFile = v
|
||||||
case "total_pgpgin":
|
case "total_pgpgin":
|
||||||
ret.Total_pgpgin = v
|
ret.TotalPgpgIn = v
|
||||||
case "total_pgpgout":
|
case "total_pgpgout":
|
||||||
ret.Total_pgpgout = v
|
ret.TotalPgpgOut = v
|
||||||
case "total_pgfault":
|
case "total_pgfault":
|
||||||
ret.Total_pgfault = v
|
ret.TotalPgFault = v
|
||||||
case "total_pgmajfault":
|
case "total_pgmajfault":
|
||||||
ret.Total_pgmajfault = v
|
ret.TotalPgMajFault = v
|
||||||
case "total_inactive_anon":
|
case "total_inactive_anon":
|
||||||
ret.Total_inactive_anon = v
|
ret.TotalInactiveAnon = v
|
||||||
case "total_active_anon":
|
case "total_active_anon":
|
||||||
ret.Total_active_anon = v
|
ret.TotalActiveAnon = v
|
||||||
case "total_inactive_file":
|
case "total_inactive_file":
|
||||||
ret.Total_inactive_file = v
|
ret.TotalInactiveFile = v
|
||||||
case "total_active_file":
|
case "total_active_file":
|
||||||
ret.Total_active_file = v
|
ret.TotalActiveFile = v
|
||||||
case "total_unevictable":
|
case "total_unevictable":
|
||||||
ret.Total_unevictable = v
|
ret.TotalUnevictable = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ret, nil
|
return ret, nil
|
||||||
|
@ -10,11 +10,11 @@ func TestGetDockerIDList(t *testing.T) {
|
|||||||
// If there is not docker environment, this test always fail.
|
// If there is not docker environment, this test always fail.
|
||||||
// not tested here
|
// not tested here
|
||||||
/*
|
/*
|
||||||
_, err := GetDockerIDList()
|
_, err := GetDockerIDList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("error %v", err)
|
t.Errorf("error %v", err)
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCgroupCPU(t *testing.T) {
|
func TestCgroupCPU(t *testing.T) {
|
||||||
|
@ -15,21 +15,21 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
UTX_USERSIZE = 256 /* include/NetBSD/utmpx.h */
|
UTXUserSize = 256 /* include/NetBSD/utmpx.h */
|
||||||
UTX_IDSIZE = 4
|
UTXIDSize = 4
|
||||||
UTX_LINESIZE = 32
|
UTXLineSize = 32
|
||||||
UTX_HOSTSIZE = 256
|
UTXHostSize = 256
|
||||||
)
|
)
|
||||||
|
|
||||||
type utmpx32 struct {
|
type utmpx32 struct {
|
||||||
UtUser [UTX_USERSIZE]byte /* login name */
|
UtUser [UTXUserSize]byte /* login name */
|
||||||
UtId [UTX_IDSIZE]byte /* id */
|
UtID [UTXIDSize]byte /* id */
|
||||||
UtLine [UTX_LINESIZE]byte /* tty name */
|
UtLine [UTXLineSize]byte /* tty name */
|
||||||
//TODO UtPid pid_t /* process id creating the entry */
|
//TODO UtPid pid_t /* process id creating the entry */
|
||||||
UtType [4]byte /* type of this entry */
|
UtType [4]byte /* type of this entry */
|
||||||
//TODO UtTv timeval32 /* time entry was created */
|
//TODO UtTv timeval32 /* time entry was created */
|
||||||
UtHost [UTX_HOSTSIZE]byte /* host name */
|
UtHost [UTXHostSize]byte /* host name */
|
||||||
UtPad [16]byte /* reserved for future use */
|
UtPad [16]byte /* reserved for future use */
|
||||||
}
|
}
|
||||||
|
|
||||||
func HostInfo() (*HostInfoStat, error) {
|
func HostInfo() (*HostInfoStat, error) {
|
||||||
|
@ -4,14 +4,14 @@
|
|||||||
package gopsutil
|
package gopsutil
|
||||||
|
|
||||||
const (
|
const (
|
||||||
UT_NAMESIZE = 16 /* see MAXLOGNAME in <sys/param.h> */
|
UTNameSize = 16 /* see MAXLOGNAME in <sys/param.h> */
|
||||||
UT_LINESIZE = 8
|
UTLineSize = 8
|
||||||
UT_HOSTSIZE = 16
|
UTHostSize = 16
|
||||||
)
|
)
|
||||||
|
|
||||||
type utmp struct {
|
type utmp struct {
|
||||||
UtLine [UT_LINESIZE]byte
|
UtLine [UTLineSize]byte
|
||||||
UtName [UT_NAMESIZE]byte
|
UtName [UTNameSize]byte
|
||||||
UtHost [UT_HOSTSIZE]byte
|
UtHost [UTHostSize]byte
|
||||||
UtTime int32
|
UtTime int32
|
||||||
}
|
}
|
||||||
|
@ -151,10 +151,7 @@ func getLSB() (*LSB, error) {
|
|||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetPlatformInformation() (string, string, string, error) {
|
func GetPlatformInformation() (platform string, family string, version string, err error) {
|
||||||
platform := ""
|
|
||||||
family := ""
|
|
||||||
version := ""
|
|
||||||
|
|
||||||
lsb, err := getLSB()
|
lsb, err := getLSB()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -262,9 +259,8 @@ func getRedhatishVersion(contents []string) string {
|
|||||||
}
|
}
|
||||||
if matches := regexp.MustCompile(`release (\d[\d.]*)`).FindStringSubmatch(c); matches != nil {
|
if matches := regexp.MustCompile(`release (\d[\d.]*)`).FindStringSubmatch(c); matches != nil {
|
||||||
return matches[1]
|
return matches[1]
|
||||||
} else {
|
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func getRedhatishPlatform(contents []string) string {
|
func getRedhatishPlatform(contents []string) string {
|
||||||
|
@ -16,15 +16,15 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
TCP_TABLE_BASIC_LISTENER = iota
|
TCPTableBasicListener = iota
|
||||||
TCP_TABLE_BASIC_CONNECTIONS
|
TCPTableBasicConnections
|
||||||
TCP_TABLE_BASIC_ALL
|
TCPTableBasicAll
|
||||||
TCP_TABLE_OWNER_PID_LISTENER
|
TCPTableOwnerPIDListener
|
||||||
TCP_TABLE_OWNER_PID_CONNECTIONS
|
TCPTableOwnerPIDConnections
|
||||||
TCP_TABLE_OWNER_PID_ALL
|
TCPTableOwnerPIDAll
|
||||||
TCP_TABLE_OWNER_MODULE_LISTENER
|
TCPTableOwnerModuleListener
|
||||||
TCP_TABLE_OWNER_MODULE_CONNECTIONS
|
TCPTableOwnerModuleConnections
|
||||||
TCP_TABLE_OWNER_MODULE_ALL
|
TCPTableOwnerModuleAll
|
||||||
)
|
)
|
||||||
|
|
||||||
func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
|
func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
|
||||||
|
@ -200,7 +200,7 @@ func copyParams(k *KinfoProc, p *Process) error {
|
|||||||
func processes() ([]Process, error) {
|
func processes() ([]Process, error) {
|
||||||
results := make([]Process, 0, 50)
|
results := make([]Process, 0, 50)
|
||||||
|
|
||||||
mib := []int32{CTL_KERN, KERN_PROC, KERN_PROC_PROC, 0}
|
mib := []int32{CTLKern, KernProc, KernProcProc, 0}
|
||||||
buf, length, err := callSyscall(mib)
|
buf, length, err := callSyscall(mib)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return results, err
|
return results, err
|
||||||
@ -280,7 +280,7 @@ func callSyscall(mib []int32) ([]byte, uint64, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Process) getKProc() (*KinfoProc, error) {
|
func (p *Process) getKProc() (*KinfoProc, error) {
|
||||||
mib := []int32{CTL_KERN, KERN_PROC, KERN_PROC_PID, p.Pid}
|
mib := []int32{CTLKern, KernProc, KernProcPID, p.Pid}
|
||||||
|
|
||||||
buf, length, err := callSyscall(mib)
|
buf, length, err := callSyscall(mib)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -5,11 +5,11 @@ package gopsutil
|
|||||||
|
|
||||||
// copied from sys/sysctl.h
|
// copied from sys/sysctl.h
|
||||||
const (
|
const (
|
||||||
CTL_KERN = 1 // "high kernel": proc, limits
|
CTLKern = 1 // "high kernel": proc, limits
|
||||||
KERN_PROC = 14 // struct: process entries
|
KernProc = 14 // struct: process entries
|
||||||
KERN_PROC_PID = 1 // by process id
|
KernProcPID = 1 // by process id
|
||||||
KERN_PROC_PROC = 8 // only return procs
|
KernProcProc = 8 // only return procs
|
||||||
KERN_PROC_PATHNAME = 12 // path to executable
|
KernProcPathname = 12 // path to executable
|
||||||
)
|
)
|
||||||
|
|
||||||
// copied from sys/user.h
|
// copied from sys/user.h
|
||||||
|
@ -198,7 +198,7 @@ func copyParams(k *KinfoProc, p *Process) error {
|
|||||||
func processes() ([]Process, error) {
|
func processes() ([]Process, error) {
|
||||||
results := make([]Process, 0, 50)
|
results := make([]Process, 0, 50)
|
||||||
|
|
||||||
mib := []int32{CTL_KERN, KERN_PROC, KERN_PROC_PROC, 0}
|
mib := []int32{CTLKern, KernProc, KernProcProc, 0}
|
||||||
buf, length, err := callSyscall(mib)
|
buf, length, err := callSyscall(mib)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return results, err
|
return results, err
|
||||||
@ -278,7 +278,7 @@ func callSyscall(mib []int32) ([]byte, uint64, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Process) getKProc() (*KinfoProc, error) {
|
func (p *Process) getKProc() (*KinfoProc, error) {
|
||||||
mib := []int32{CTL_KERN, KERN_PROC, KERN_PROC_PID, p.Pid}
|
mib := []int32{CTLKern, KernProc, KernProcPID, p.Pid}
|
||||||
|
|
||||||
buf, length, err := callSyscall(mib)
|
buf, length, err := callSyscall(mib)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -5,11 +5,11 @@ package gopsutil
|
|||||||
|
|
||||||
// copied from sys/sysctl.h
|
// copied from sys/sysctl.h
|
||||||
const (
|
const (
|
||||||
CTL_KERN = 1 // "high kernel": proc, limits
|
CTLKern = 1 // "high kernel": proc, limits
|
||||||
KERN_PROC = 14 // struct: process entries
|
KernProc = 14 // struct: process entries
|
||||||
KERN_PROC_PID = 1 // by process id
|
KernProcPID = 1 // by process id
|
||||||
KERN_PROC_PROC = 8 // only return procs
|
KernProcProc = 8 // only return procs
|
||||||
KERN_PROC_PATHNAME = 12 // path to executable
|
KernProcPathname = 12 // path to executable
|
||||||
)
|
)
|
||||||
|
|
||||||
// copied from sys/user.h
|
// copied from sys/user.h
|
||||||
|
@ -13,7 +13,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
PRIO_PROCESS = 0 // linux/resource.h
|
PrioProcess = 0 // linux/resource.h
|
||||||
)
|
)
|
||||||
|
|
||||||
// MemoryInfoExStat is different between OSes
|
// MemoryInfoExStat is different between OSes
|
||||||
@ -390,8 +390,8 @@ func (p *Process) fillFromStatm() (*MemoryInfoStat, *MemoryInfoExStat, error) {
|
|||||||
}
|
}
|
||||||
fields := strings.Split(string(contents), " ")
|
fields := strings.Split(string(contents), " ")
|
||||||
|
|
||||||
rss := mustParseUint64(fields[0]) * PAGESIZE
|
rss := mustParseUint64(fields[0]) * PageSize
|
||||||
vms := mustParseUint64(fields[1]) * PAGESIZE
|
vms := mustParseUint64(fields[1]) * PageSize
|
||||||
memInfo := &MemoryInfoStat{
|
memInfo := &MemoryInfoStat{
|
||||||
RSS: rss,
|
RSS: rss,
|
||||||
VMS: vms,
|
VMS: vms,
|
||||||
@ -399,10 +399,10 @@ func (p *Process) fillFromStatm() (*MemoryInfoStat, *MemoryInfoExStat, error) {
|
|||||||
memInfoEx := &MemoryInfoExStat{
|
memInfoEx := &MemoryInfoExStat{
|
||||||
RSS: rss,
|
RSS: rss,
|
||||||
VMS: vms,
|
VMS: vms,
|
||||||
Shared: mustParseUint64(fields[2]) * PAGESIZE,
|
Shared: mustParseUint64(fields[2]) * PageSize,
|
||||||
Text: mustParseUint64(fields[3]) * PAGESIZE,
|
Text: mustParseUint64(fields[3]) * PageSize,
|
||||||
Lib: mustParseUint64(fields[4]) * PAGESIZE,
|
Lib: mustParseUint64(fields[4]) * PageSize,
|
||||||
Dirty: mustParseUint64(fields[5]) * PAGESIZE,
|
Dirty: mustParseUint64(fields[5]) * PageSize,
|
||||||
}
|
}
|
||||||
|
|
||||||
return memInfo, memInfoEx, nil
|
return memInfo, memInfoEx, nil
|
||||||
@ -485,17 +485,17 @@ func (p *Process) fillFromStat() (string, int32, *CPUTimesStat, int64, int32, er
|
|||||||
|
|
||||||
cpuTimes := &CPUTimesStat{
|
cpuTimes := &CPUTimesStat{
|
||||||
CPU: "cpu",
|
CPU: "cpu",
|
||||||
User: float32(utime * (1000 / CLOCK_TICKS)),
|
User: float32(utime * (1000 / ClockTicks)),
|
||||||
System: float32(stime * (1000 / CLOCK_TICKS)),
|
System: float32(stime * (1000 / ClockTicks)),
|
||||||
}
|
}
|
||||||
|
|
||||||
bootTime, _ := BootTime()
|
bootTime, _ := BootTime()
|
||||||
ctime := ((mustParseUint64(fields[21]) / uint64(CLOCK_TICKS)) + uint64(bootTime)) * 1000
|
ctime := ((mustParseUint64(fields[21]) / uint64(ClockTicks)) + uint64(bootTime)) * 1000
|
||||||
createTime := int64(ctime)
|
createTime := int64(ctime)
|
||||||
|
|
||||||
// p.Nice = mustParseInt32(fields[18])
|
// p.Nice = mustParseInt32(fields[18])
|
||||||
// use syscall instead of parse Stat file
|
// use syscall instead of parse Stat file
|
||||||
snice, _ := syscall.Getpriority(PRIO_PROCESS, int(pid))
|
snice, _ := syscall.Getpriority(PrioProcess, int(pid))
|
||||||
nice := int32(snice) // FIXME: is this true?
|
nice := int32(snice) // FIXME: is this true?
|
||||||
|
|
||||||
return terminal, ppid, cpuTimes, createTime, nice, nil
|
return terminal, ppid, cpuTimes, createTime, nice, nil
|
||||||
|
@ -4,6 +4,6 @@
|
|||||||
package gopsutil
|
package gopsutil
|
||||||
|
|
||||||
const (
|
const (
|
||||||
CLOCK_TICKS = 100 // C.sysconf(C._SC_CLK_TCK)
|
ClockTicks = 100 // C.sysconf(C._SC_CLK_TCK)
|
||||||
PAGESIZE = 4096 // C.sysconf(C._SC_PAGE_SIZE)
|
PageSize = 4096 // C.sysconf(C._SC_PAGE_SIZE)
|
||||||
)
|
)
|
||||||
|
@ -4,6 +4,6 @@
|
|||||||
package gopsutil
|
package gopsutil
|
||||||
|
|
||||||
const (
|
const (
|
||||||
CLOCK_TICKS = 100 // C.sysconf(C._SC_CLK_TCK)
|
ClockTicks = 100 // C.sysconf(C._SC_CLK_TCK)
|
||||||
PAGESIZE = 4096 // C.sysconf(C._SC_PAGE_SIZE)
|
PageSize = 4096 // C.sysconf(C._SC_PAGE_SIZE)
|
||||||
)
|
)
|
||||||
|
@ -41,7 +41,9 @@ func getTerminalMap() (map[uint64]string, error) {
|
|||||||
|
|
||||||
for _, name := range termfiles {
|
for _, name := range termfiles {
|
||||||
stat := syscall.Stat_t{}
|
stat := syscall.Stat_t{}
|
||||||
syscall.Stat(name, &stat)
|
if err = syscall.Stat(name, &stat); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
rdev := uint64(stat.Rdev)
|
rdev := uint64(stat.Rdev)
|
||||||
ret[rdev] = strings.Replace(name, "/dev", "", -1)
|
ret[rdev] = strings.Replace(name, "/dev", "", -1)
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func test_getProcess() Process {
|
func testGetProcess() Process {
|
||||||
checkPid := os.Getpid()
|
checkPid := os.Getpid()
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
checkPid = 7960
|
checkPid = 7960
|
||||||
@ -81,7 +81,7 @@ func Test_Process_memory_maps(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_Process_Ppid(t *testing.T) {
|
func Test_Process_Ppid(t *testing.T) {
|
||||||
p := test_getProcess()
|
p := testGetProcess()
|
||||||
|
|
||||||
v, err := p.Ppid()
|
v, err := p.Ppid()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -94,7 +94,7 @@ func Test_Process_Ppid(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_Process_IOCounters(t *testing.T) {
|
func Test_Process_IOCounters(t *testing.T) {
|
||||||
p := test_getProcess()
|
p := testGetProcess()
|
||||||
|
|
||||||
v, err := p.IOCounters()
|
v, err := p.IOCounters()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -108,7 +108,7 @@ func Test_Process_IOCounters(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_Process_NumCtx(t *testing.T) {
|
func Test_Process_NumCtx(t *testing.T) {
|
||||||
p := test_getProcess()
|
p := testGetProcess()
|
||||||
|
|
||||||
_, err := p.NumCtxSwitches()
|
_, err := p.NumCtxSwitches()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -11,11 +11,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ERROR_NO_MORE_FILES = 0x12
|
NoMoreFiles = 0x12
|
||||||
MAX_PATH = 260
|
MaxPathLength = 260
|
||||||
)
|
)
|
||||||
|
|
||||||
type SYSTEM_PROCESS_INFORMATION struct {
|
type SystemProcessInformation struct {
|
||||||
NextEntryOffset uint64
|
NextEntryOffset uint64
|
||||||
NumberOfThreads uint64
|
NumberOfThreads uint64
|
||||||
Reserved1 [48]byte
|
Reserved1 [48]byte
|
||||||
@ -241,12 +241,12 @@ func processes() ([]*Process, error) {
|
|||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getProcInfo(pid int32) (*SYSTEM_PROCESS_INFORMATION, error) {
|
func getProcInfo(pid int32) (*SystemProcessInformation, error) {
|
||||||
initialBufferSize := uint64(0x4000)
|
initialBufferSize := uint64(0x4000)
|
||||||
bufferSize := initialBufferSize
|
bufferSize := initialBufferSize
|
||||||
buffer := make([]byte, bufferSize)
|
buffer := make([]byte, bufferSize)
|
||||||
|
|
||||||
var sysProcInfo SYSTEM_PROCESS_INFORMATION
|
var sysProcInfo SystemProcessInformation
|
||||||
ret, _, _ := procNtQuerySystemInformation.Call(
|
ret, _, _ := procNtQuerySystemInformation.Call(
|
||||||
uintptr(unsafe.Pointer(&sysProcInfo)),
|
uintptr(unsafe.Pointer(&sysProcInfo)),
|
||||||
uintptr(unsafe.Pointer(&buffer[0])),
|
uintptr(unsafe.Pointer(&buffer[0])),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user