1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-28 13:48:49 +08:00

Merge branch 'master' of github.com:shirou/gopsutil

This commit is contained in:
Shirou WAKAYAMA 2014-04-30 15:37:07 +09:00
commit 9a32bb9bd0
25 changed files with 279 additions and 277 deletions

4
cpu.go
View File

@ -4,7 +4,7 @@ import (
"runtime"
)
type CPU_TimesStat struct {
type CPUTimesStat struct {
Cpu string `json:"cpu"`
User float32 `json:"user"`
System float32 `json:"system"`
@ -19,6 +19,6 @@ type CPU_TimesStat struct {
Stolen float32 `json:"stolen"`
}
func Cpu_counts(logical bool) (int, error) {
func CpuCounts(logical bool) (int, error) {
return runtime.NumCPU(), nil
}

View File

@ -22,7 +22,7 @@ const (
)
// TODO: get per cpus
func Cpu_times(percpu bool) ([]CPU_TimesStat, error) {
func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
ret := make([]CPU_TimesStat, 0)
cpu_time, err := do_sysctrl("kern.cp_time")

View File

@ -5,7 +5,7 @@ import (
)
func TestCpu_times(t *testing.T) {
v, err := Cpu_times(false)
v, err := CPUTimes(false)
if err != nil {
t.Errorf("error %v", err)
}
@ -21,7 +21,7 @@ func TestCpu_times(t *testing.T) {
}
func TestCpu_counts(t *testing.T) {
v, err := Cpu_counts(true)
v, err := CpuCounts(true)
if err != nil {
t.Errorf("error %v", err)
}

View File

@ -7,8 +7,8 @@ import (
"unsafe"
)
func Cpu_times(percpu bool) ([]CPU_TimesStat, error) {
ret := make([]CPU_TimesStat, 0)
func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
var ret []CPUTimesStat
var lpIdleTime FILETIME
var lpKernelTime FILETIME
@ -21,14 +21,14 @@ func Cpu_times(percpu bool) ([]CPU_TimesStat, error) {
return ret, syscall.GetLastError()
}
LO_T := float64(0.0000001)
HI_T := (LO_T * 4294967296.0)
idle := ((HI_T * float64(lpIdleTime.DwHighDateTime)) + (LO_T * float64(lpIdleTime.DwLowDateTime)))
user := ((HI_T * float64(lpUserTime.DwHighDateTime)) + (LO_T * float64(lpUserTime.DwLowDateTime)))
kernel := ((HI_T * float64(lpKernelTime.DwHighDateTime)) + (LO_T * float64(lpKernelTime.DwLowDateTime)))
LOT := float64(0.0000001)
HIT := (LOT * 4294967296.0)
idle := ((HIT * float64(lpIdleTime.DwHighDateTime)) + (LOT * float64(lpIdleTime.DwLowDateTime)))
user := ((HIT * float64(lpUserTime.DwHighDateTime)) + (LOT * float64(lpUserTime.DwLowDateTime)))
kernel := ((HIT * float64(lpKernelTime.DwHighDateTime)) + (LOT * float64(lpKernelTime.DwLowDateTime)))
system := (kernel - idle)
ret = append(ret, CPU_TimesStat{
ret = append(ret, CPUTimesStat{
Idle: float32(idle),
User: float32(user),
System: float32(system),

View File

@ -1,6 +1,6 @@
package gopsutil
type Disk_usageStat struct {
type DiskUsageStat struct {
Path string `json:"path"`
Total uint64 `json:"total"`
Free uint64 `json:"free"`
@ -8,14 +8,14 @@ type Disk_usageStat struct {
UsedPercent float64 `json:"usedPercent"`
}
type Disk_partitionStat struct {
type DiskPartitionStat struct {
Device string `json:"device"`
Mountpoint string `json:"mountpoint"`
Fstype string `json:"fstype"`
Opts string `json:"opts"`
}
type Disk_IO_CountersStat struct {
type DiskIOCountersStat struct {
ReadCount uint64 `json:"readCount"`
WriteCount uint64 `json:"writeCount"`
ReadBytes uint64 `json:"readBytes"`

View File

@ -8,7 +8,7 @@ import (
"unsafe"
)
func Disk_partitions(all bool) ([]Disk_partitionStat, error) {
func DiskPartitions(all bool) ([]DiskPartitionStat, error) {
ret := make([]Disk_partitionStat, 0)
// get length
@ -82,12 +82,12 @@ func Disk_partitions(all bool) ([]Disk_partitionStat, error) {
return ret, nil
}
func Disk_io_counters() (map[string]Disk_IO_CountersStat, error) {
func DiskIOCounters() (map[string]DiskIOCountersStat, error) {
ret := make(map[string]Disk_IO_CountersStat, 0)
return ret, errors.New("Not implemented yet")
return ret, errors.New("not implemented yet")
}
// This is borrowed from pkg/syscall/syscall_freebsd.go
// Getfsstat is borrowed from pkg/syscall/syscall_freebsd.go
// change Statfs_t to Statfs in order to get more information
func Getfsstat(buf []Statfs, flags int) (n int, err error) {
var _p0 unsafe.Pointer

View File

@ -13,7 +13,7 @@ const (
// Get disk partitions.
// should use setmntent(3) but this implement use /etc/mtab file
func Disk_partitions(all bool) ([]Disk_partitionStat, error) {
func DiskPartitions(all bool) ([]DiskPartitionStat, error) {
ret := make([]Disk_partitionStat, 0)
filename := "/etc/mtab"
@ -35,7 +35,7 @@ func Disk_partitions(all bool) ([]Disk_partitionStat, error) {
return ret, nil
}
func Disk_io_counters() (map[string]Disk_IO_CountersStat, error) {
func DiskIOCounters() (map[string]DiskIOCountersStat, error) {
ret := make(map[string]Disk_IO_CountersStat, 0)
// determine partitions we want to look for

View File

@ -11,7 +11,7 @@ func TestDisk_usage(t *testing.T) {
if runtime.GOOS == "windows" {
path = "C:"
}
_, err := Disk_usage(path)
_, err := DiskUsage(path)
if err != nil {
t.Errorf("error %v", err)
}
@ -20,14 +20,14 @@ func TestDisk_usage(t *testing.T) {
}
func TestDisk_partitions(t *testing.T) {
_, err := Disk_partitions(false)
_, err := DiskPartitions(false)
if err != nil {
t.Errorf("error %v", err)
}
}
func TestDisk_io_counters(t *testing.T) {
ret, err := Disk_io_counters()
ret, err := DiskIOCounters()
if err != nil {
t.Errorf("error %v", err)
}

View File

@ -4,7 +4,7 @@ package gopsutil
import "syscall"
func Disk_usage(path string) (Disk_usageStat, error) {
func DiskUsage(path string) (DiskUsageStat, error) {
stat := syscall.Statfs_t{}
err := syscall.Statfs(path, &stat)
if err != nil {

View File

@ -21,8 +21,8 @@ var (
FILE_READ_ONLY_VOLUME = int64(524288) // 0x00080000
)
func Disk_usage(path string) (Disk_usageStat, error) {
ret := Disk_usageStat{}
func DiskUsage(path string) (DiskUsageStat, error) {
ret := DiskUsageStat{}
ret.Path = path
lpFreeBytesAvailable := int64(0)
@ -45,8 +45,8 @@ func Disk_usage(path string) (Disk_usageStat, error) {
return ret, nil
}
func Disk_partitions(all bool) ([]Disk_partitionStat, error) {
ret := make([]Disk_partitionStat, 0)
func DiskPartitions(all bool) ([]DiskPartitionStat, error) {
ret := make([]DiskPartitionStat, 0)
lpBuffer := make([]byte, 254)
diskret, _, err := procGetLogicalDriveStringsW.Call(
uintptr(len(lpBuffer)),
@ -94,7 +94,7 @@ func Disk_partitions(all bool) ([]Disk_partitionStat, error) {
opts += ".compress"
}
d := Disk_partitionStat{
d := DiskPartitionStat{
Mountpoint: path,
Device: path,
Fstype: string(bytes.Replace(lpFileSystemNameBuffer, []byte("\x00"), []byte(""), -1)),
@ -107,7 +107,7 @@ func Disk_partitions(all bool) ([]Disk_partitionStat, error) {
return ret, nil
}
func Disk_io_counters() (map[string]Disk_IO_CountersStat, error) {
ret := make(map[string]Disk_IO_CountersStat, 0)
return ret, errors.New("Not implemented yet")
func DiskIOCounters() (map[string]DiskIOCountersStat, error) {
ret := make(map[string]DiskIOCountersStat, 0)
return ret, errors.New("not implemented yet")
}

View File

@ -24,7 +24,7 @@ func HostInfo() (HostInfoStat, error) {
return ret, nil
}
func Boot_time() (int64, error) {
func BootTime() (int64, error) {
values, err := do_sysctrl("kern.boottime")
if err != nil {
return 0, err

View File

@ -15,7 +15,7 @@ func TestHostInfo(t *testing.T) {
}
func TestBoot_time(t *testing.T) {
v, err := Boot_time()
v, err := BootTime()
if err != nil {
t.Errorf("error %v", err)
}

View File

@ -39,7 +39,7 @@ func HostInfo() (HostInfoStat, error) {
return ret, nil
}
func Boot_time() (int64, error) {
func BootTime() (int64, error) {
var lpSystemTimeAsFileTime FILETIME
r, _, _ := procGetSystemTimeAsFileTime.Call(uintptr(unsafe.Pointer(&lpSystemTimeAsFileTime)))
@ -61,7 +61,7 @@ func Boot_time() (int64, error) {
}
func Users() ([]UserStat, error) {
ret := make([]UserStat, 0)
var ret []UserStat
return ret, nil
}

4
mem.go
View File

@ -1,6 +1,6 @@
package gopsutil
type Virtual_memoryStat struct {
type VirtualMemoryStat struct {
Total uint64 `json:"total"`
Available uint64 `json:"available"`
Used uint64 `json:"used"`
@ -14,7 +14,7 @@ type Virtual_memoryStat struct {
Shared uint64 `json:"shared"`
}
type Swap_memoryStat struct {
type SwapMemoryStat struct {
Total uint64 `json:"total"`
Used uint64 `json:"used"`
Free uint64 `json:"free"`

View File

@ -2,13 +2,13 @@
package gopsutil
func Virtual_memory() (Virtual_memoryStat, error) {
func VirtualMemory() (VirtualMemoryStat, error) {
ret := Virtual_memoryStat{}
return ret, nil
}
func Swap_memory() (Swap_memoryStat, error) {
func SwapMemory() (SwapMemoryStat, error) {
ret := Swap_memoryStat{}
return ret, nil

View File

@ -6,7 +6,7 @@ import (
"syscall"
)
func Virtual_memory() (Virtual_memoryStat, error) {
func VirtualMemory() (VirtualMemoryStat, error) {
ret := Virtual_memoryStat{}
sysinfo := &syscall.Sysinfo_t{}
@ -35,7 +35,7 @@ func Virtual_memory() (Virtual_memoryStat, error) {
return ret, nil
}
func Swap_memory() (Swap_memoryStat, error) {
func SwapMemory() (SwapMemoryStat, error) {
ret := Swap_memoryStat{}
sysinfo := &syscall.Sysinfo_t{}

View File

@ -7,7 +7,7 @@ import (
)
func TestVirtual_memory(t *testing.T) {
v, err := Virtual_memory()
v, err := VirtualMemory()
if err != nil {
t.Errorf("error %v", err)
}
@ -16,7 +16,7 @@ func TestVirtual_memory(t *testing.T) {
}
func TestSwap_memory(t *testing.T) {
v, err := Swap_memory()
v, err := SwapMemory()
if err != nil {
t.Errorf("error %v", err)
}

View File

@ -23,8 +23,8 @@ type MEMORYSTATUSEX struct {
ullAvailExtendedVirtual uint64
}
func Virtual_memory() (Virtual_memoryStat, error) {
ret := Virtual_memoryStat{}
func VirtualMemory() (VirtualMemoryStat, error) {
ret := VirtualMemoryStat{}
var memInfo MEMORYSTATUSEX
memInfo.cbSize = uint32(unsafe.Sizeof(memInfo))
@ -40,8 +40,8 @@ func Virtual_memory() (Virtual_memoryStat, error) {
return ret, nil
}
func Swap_memory() (Swap_memoryStat, error) {
ret := Swap_memoryStat{}
func SwapMemory() (SwapMemoryStat, error) {
ret := SwapMemoryStat{}
return ret, nil
}

22
net.go
View File

@ -1,15 +1,15 @@
package gopsutil
type Net_io_countersStat struct {
Name string `json:"name""` // interface name
Bytes_sent uint64 `json:"bytes_sent""` // number of bytes sent
Bytes_recv uint64 `json:"bytes_recv"` // number of bytes received
Packets_sent uint64 `json:"packets_sent"` // number of packets sent
Packets_recv uint64 `json:"packets_recv"` // number of packets received
Errin uint64 `json:"errin"` // total number of errors while receiving
Errout uint64 `json:"errout"` // total number of errors while sending
Dropin uint64 `json:"dropin"` // total number of incoming packets which were dropped
Dropout uint64 `json:"dropout"` // total number of outgoing packets which were dropped (always 0 on OSX and BSD)
type NetIOCountersStat struct {
Name string `json:"name""` // interface name
BytesSent uint64 `json:"bytes_sent""` // number of bytes sent
BytesRecv uint64 `json:"bytes_recv"` // number of bytes received
PacketsSent uint64 `json:"packets_sent"` // number of packets sent
PacketsRecv uint64 `json:"packets_recv"` // number of packets received
Errin uint64 `json:"errin"` // total number of errors while receiving
Errout uint64 `json:"errout"` // total number of errors while sending
Dropin uint64 `json:"dropin"` // total number of incoming packets which were dropped
Dropout uint64 `json:"dropout"` // total number of outgoing packets which were dropped (always 0 on OSX and BSD)
}
type Addr struct {
@ -17,7 +17,7 @@ type Addr struct {
Port uint32 `json:"port""`
}
type Net_connectionStat struct {
type NetConnectionStat struct {
Fd uint32 `json:"fd""`
Family uint32 `json:"family""`
Type uint32 `json:"type""`

View File

@ -6,7 +6,7 @@ import (
"strings"
)
func Net_io_counters() ([]Net_io_countersStat, error) {
func NetIOCounters() ([]NetIOCountersStat, error) {
filename := "/proc/net/dev"
lines, err := ReadLines(filename)
if err != nil {

View File

@ -4,12 +4,12 @@ type Process struct {
Pid int32 `json:"pid"`
}
type Open_filesStat struct {
type OpenFilesStat struct {
Path string `json:"path"`
Fd uint64 `json:"fd"`
}
type Memory_infoStat struct {
type MemoryInfoStat struct {
RSS uint64 `json:"rss"` // bytes
VMS uint64 `json:"vms"` // bytes
}
@ -20,14 +20,14 @@ type RlimitStat struct {
Hard int32 `json:"hard"`
}
type Io_countersStat struct {
Read_count int32 `json:"read_count"`
Write_count int32 `json:"write_count"`
Read_bytes int32 `json:"read_bytes"`
Write_bytes int32 `json:"write_bytes"`
type IoCountersStat struct {
ReadCount int32 `json:"read_count"`
WriteCount int32 `json:"write_count"`
ReadBytes int32 `json:"read_bytes"`
WriteBytes int32 `json:"write_bytes"`
}
func Pid_exists(pid int32) (bool, error) {
func PidExists(pid int32) (bool, error) {
pids, err := Pids()
if err != nil {
return false, err

View File

@ -10,15 +10,15 @@ import (
"unsafe"
)
// Memory_info_ex is different between OSes
type Memory_info_exStat struct {
// MemoryInfoExStat is different between OSes
type MemoryInfoExStat struct {
}
type Memory_mapsStat struct {
type MemoryMapsStat struct {
}
func Pids() ([]int32, error) {
ret := make([]int32, 0)
var ret []int32
procs, err := processes()
if err != nil {
return ret, nil
@ -32,107 +32,107 @@ func Pids() ([]int32, error) {
}
func (p *Process) Ppid() (int32, error) {
return 0, errors.New("Not implemented yet")
return 0, errors.New("not implemented yet")
}
func (p *Process) Name() (string, error) {
name := ""
return name, errors.New("Not implemented yet")
return name, errors.New("not implemented yet")
}
func (p *Process) Exe() (string, error) {
return "", errors.New("Not implemented yet")
return "", errors.New("not implemented yet")
}
func (p *Process) Cmdline() (string, error) {
return "", errors.New("Not implemented yet")
return "", errors.New("not implemented yet")
}
func (p *Process) Cwd() (string, error) {
return "", errors.New("Not implemented yet")
return "", errors.New("not implemented yet")
}
func (p *Process) Parent() (*Process, error) {
return p, errors.New("Not implemented yet")
return p, errors.New("not implemented yet")
}
func (p *Process) Status() (string, error) {
return "", errors.New("Not implemented yet")
return "", errors.New("not implemented yet")
}
func (p *Process) Username() (string, error) {
return "", errors.New("Not implemented yet")
return "", errors.New("not implemented yet")
}
func (p *Process) Uids() ([]int32, error) {
uids := make([]int32, 0)
return uids, errors.New("Not implemented yet")
var uids []int32
return uids, errors.New("not implemented yet")
}
func (p *Process) Gids() ([]int32, error) {
gids := make([]int32, 0)
return gids, errors.New("Not implemented yet")
var gids []int32
return gids, errors.New("not implemented yet")
}
func (p *Process) Terminal() (string, error) {
return "", errors.New("Not implemented yet")
return "", errors.New("not implemented yet")
}
func (p *Process) Nice() (int32, error) {
return 0, errors.New("Not implemented yet")
return 0, errors.New("not implemented yet")
}
func (p *Process) Ionice() (int32, error) {
return 0, errors.New("Not implemented yet")
return 0, errors.New("not implemented yet")
}
func (p *Process) Rlimit() ([]RlimitStat, error) {
rlimit := make([]RlimitStat, 0)
return rlimit, errors.New("Not implemented yet")
var rlimit []RlimitStat
return rlimit, errors.New("not implemented yet")
}
func (p *Process) Io_counters() (*Io_countersStat, error) {
return nil, errors.New("Not implemented yet")
func (p *Process) IOCounters() (*IOCountersStat, error) {
return nil, errors.New("not implemented yet")
}
func (p *Process) Num_ctx_switches() (int32, error) {
return 0, errors.New("Not implemented yet")
func (p *Process) NumCtxSwitches() (int32, error) {
return 0, errors.New("not implemented yet")
}
func (p *Process) Num_fds() (int32, error) {
return 0, errors.New("Not implemented yet")
func (p *Process) NumFDs() (int32, error) {
return 0, errors.New("not implemented yet")
}
func (p *Process) Num_Threads() (int32, error) {
return 0, errors.New("Not implemented yet")
func (p *Process) NumThreads() (int32, error) {
return 0, errors.New("not implemented yet")
}
func (p *Process) Threads() (map[string]string, error) {
ret := make(map[string]string, 0)
return ret, errors.New("Not implemented yet")
return ret, errors.New("not implemented yet")
}
func (p *Process) Cpu_times() (*CPU_TimesStat, error) {
return nil, errors.New("Not implemented yet")
func (p *Process) CPUTimes() (*CPUTimesStat, error) {
return nil, errors.New("not implemented yet")
}
func (p *Process) Cpu_percent() (int32, error) {
return 0, errors.New("Not implemented yet")
func (p *Process) CPUPercent() (int32, error) {
return 0, errors.New("not implemented yet")
}
func (p *Process) Cpu_affinity() ([]int32, error) {
return nil, errors.New("Not implemented yet")
func (p *Process) CPUAffinity() ([]int32, error) {
return nil, errors.New("not implemented yet")
}
func (p *Process) Memory_info() (*Memory_infoStat, error) {
return nil, errors.New("Not implemented yet")
func (p *Process) MemoryInfo() (*MemoryInfoStat, error) {
return nil, errors.New("not implemented yet")
}
func (p *Process) Memory_info_ex() (*Memory_info_exStat, error) {
return nil, errors.New("Not implemented yet")
func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) {
return nil, errors.New("not implemented yet")
}
func (p *Process) Memory_percent() (float32, error) {
return 0, errors.New("Not implemented yet")
func (p *Process) MemoryPercent() (float32, error) {
return 0, errors.New("not implemented yet")
}
func (p *Process) Children() ([]*Process, error) {
return nil, errors.New("Not implemented yet")
return nil, errors.New("not implemented yet")
}
func (p *Process) Open_files() ([]Open_filesStat, error) {
return nil, errors.New("Not implemented yet")
func (p *Process) OpenFiles() ([]OpenFilesStat, error) {
return nil, errors.New("not implemented yet")
}
func (p *Process) Connections() ([]Net_connectionStat, error) {
return nil, errors.New("Not implemented yet")
func (p *Process) Connections() ([]NetConnectionStat, error) {
return nil, errors.New("not implemented yet")
}
func (p *Process) Is_running() (bool, error) {
return true, errors.New("Not implemented yet")
func (p *Process) IsRunning() (bool, error) {
return true, errors.New("not implemented yet")
}
func (p *Process) Memory_Maps(grouped bool) (*[]Memory_mapsStat, error) {
ret := make([]Memory_mapsStat, 0)
return &ret, errors.New("Not implemented yet")
func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) {
var ret []MemoryMapsStat
return &ret, errors.New("not implemented yet")
}
func copy_params(k *Kinfo_proc, p *Process) error {
func copyParams(k *Kinfo_proc, p *Process) error {
return nil
}
@ -141,20 +141,20 @@ func processes() ([]Process, error) {
results := make([]Process, 0, 50)
mib := []int32{CTL_KERN, KERN_PROC, KERN_PROC_PROC, 0}
buf, length, err := call_syscall(mib)
buf, length, err := callSyscall(mib)
if err != nil {
return results, err
}
// get kinfo_proc size
k := Kinfo_proc{}
procinfo_len := int(unsafe.Sizeof(k))
count := int(length / uint64(procinfo_len))
procinfoLen := int(unsafe.Sizeof(k))
count := int(length / uint64(procinfoLen))
// parse buf to procs
for i := 0; i < count; i++ {
b := buf[i*procinfo_len : i*procinfo_len+procinfo_len]
k, err := parse_kinfo_proc(b)
b := buf[i*procinfoLen : i*procinfoLen+procinfoLen]
k, err := parseKinfoProc(b)
if err != nil {
continue
}
@ -162,7 +162,7 @@ func processes() ([]Process, error) {
if err != nil {
continue
}
copy_params(&k, p)
copyParams(&k, p)
results = append(results, *p)
}
@ -170,8 +170,8 @@ func processes() ([]Process, error) {
return results, nil
}
func parse_kinfo_proc(buf []byte) (Kinfo_proc, error) {
var k Kinfo_proc
func parseKinfoProc(buf []byte) (KinfoProc, error) {
var k KinfoProc
br := bytes.NewReader(buf)
err := binary.Read(br, binary.LittleEndian, &k)
if err != nil {
@ -181,7 +181,7 @@ func parse_kinfo_proc(buf []byte) (Kinfo_proc, error) {
return k, nil
}
func call_syscall(mib []int32) ([]byte, uint64, error) {
func callSyscall(mib []int32) ([]byte, uint64, error) {
miblen := uint64(len(mib))
// get required buffer size
@ -195,11 +195,11 @@ func call_syscall(mib []int32) ([]byte, uint64, error) {
0,
0)
if err != 0 {
b := make([]byte, 0)
var b []byte
return b, length, err
}
if length == 0 {
b := make([]byte, 0)
var b []byte
return b, length, err
}
// get proc info itself
@ -227,16 +227,16 @@ func NewProcess(pid int32) (*Process, error) {
if err != nil {
return nil, err
}
proc_k := Kinfo_proc{}
if length != uint64(unsafe.Sizeof(proc_k)) {
procK := KinfoProc{}
if length != uint64(unsafe.Sizeof(procK)) {
return nil, err
}
k, err := parse_kinfo_proc(buf)
k, err := parseKinfoProc(buf)
if err != nil {
return nil, err
}
copy_params(&k, p)
copyParams(&k, p)
return p, nil
}

View File

@ -16,8 +16,8 @@ const (
PRIO_PROCESS = 0 // linux/resource.h
)
// Memory_info_ex is different between OSes
type Memory_info_exStat struct {
// MemoryInfoExStat is different between OSes
type MemoryInfoExStat struct {
RSS uint64 `json:"rss"` // bytes
VMS uint64 `json:"vms"` // bytes
Shared uint64 `json:"shared"` // bytes
@ -27,18 +27,18 @@ type Memory_info_exStat struct {
Dirty uint64 `json:"dirty"` // bytes
}
type Memory_mapsStat struct {
Path string `json:"path"`
Rss uint64 `json:"rss"`
Size uint64 `json:"size"`
Pss uint64 `json:"pss"`
Shared_clean uint64 `json:"shared_clean"`
Shared_dirty uint64 `json:"shared_dirty"`
Private_clean uint64 `json:"private_clean"`
Private_dirty uint64 `json:"private_dirty"`
Referenced uint64 `json:"referenced"`
Anonymous uint64 `json:"anonymous"`
Swap uint64 `json:"swap"`
type MemoryMapsStat struct {
Path string `json:"path"`
Rss uint64 `json:"rss"`
Size uint64 `json:"size"`
Pss uint64 `json:"pss"`
SharedClean uint64 `json:"shared_clean"`
SharedDirty uint64 `json:"shared_dirty"`
PrivateClean uint64 `json:"private_clean"`
PrivateDirty uint64 `json:"private_dirty"`
Referenced uint64 `json:"referenced"`
Anonymous uint64 `json:"anonymous"`
Swap uint64 `json:"swap"`
}
// Create new Process instance
@ -74,7 +74,7 @@ func (p *Process) Cwd() (string, error) {
return p.fillFromCwd()
}
func (p *Process) Parent() (*Process, error) {
return nil, errors.New("Not implemented yet")
return nil, errors.New("not implemented yet")
}
func (p *Process) Status() (string, error) {
_, status, _, _, _, err := p.fillFromStatus()
@ -115,82 +115,82 @@ func (p *Process) Nice() (int32, error) {
return nice, nil
}
func (p *Process) Ionice() (int32, error) {
return 0, errors.New("Not implemented yet")
return 0, errors.New("not implemented yet")
}
func (p *Process) Rlimit() ([]RlimitStat, error) {
return nil, errors.New("Not implemented yet")
return nil, errors.New("not implemented yet")
}
func (p *Process) Io_counters() (*Io_countersStat, error) {
return nil, errors.New("Not implemented yet")
func (p *Process) IOCounters() (*IOCountersStat, error) {
return nil, errors.New("not implemented yet")
}
func (p *Process) Num_ctx_switches() (int32, error) {
return 0, errors.New("Not implemented yet")
func (p *Process) NumCtxSwitches() (int32, error) {
return 0, errors.New("not implemented yet")
}
func (p *Process) Num_fds() (int32, error) {
return 0, errors.New("Not implemented yet")
func (p *Process) NumFDs() (int32, error) {
return 0, errors.New("not implemented yet")
}
func (p *Process) Num_Threads() (int32, error) {
_, _, _, _, num_threads, err := p.fillFromStatus()
func (p *Process) NumThreads() (int32, error) {
_, _, _, _, numThreads, err := p.fillFromStatus()
if err != nil {
return 0, err
}
return num_threads, nil
return numThreads, nil
}
func (p *Process) Threads() (map[string]string, error) {
ret := make(map[string]string, 0)
return ret, nil
}
func (p *Process) Cpu_times() (*CPU_TimesStat, error) {
_, _, cpu_times, _, _, err := p.fillFromStat()
func (p *Process) CPUTimes() (*CPUTimesStat, error) {
_, _, cpuTimes, _, _, err := p.fillFromStat()
if err != nil {
return nil, err
}
return cpu_times, nil
return cpuTimes, nil
}
func (p *Process) Cpu_percent() (int32, error) {
return 0, errors.New("Not implemented yet")
func (p *Process) CPUPpercent() (int32, error) {
return 0, errors.New("not implemented yet")
}
func (p *Process) Cpu_affinity() ([]int32, error) {
return nil, errors.New("Not implemented yet")
func (p *Process) CPUAffinity() ([]int32, error) {
return nil, errors.New("not implemented yet")
}
func (p *Process) Memory_info() (*Memory_infoStat, error) {
mem_info, _, err := p.fillFromStatm()
func (p *Process) MemoryInfo() (*MemoryInfoStat, error) {
memInfo, _, err := p.fillFromStatm()
if err != nil {
return nil, err
}
return mem_info, nil
return memInfo, nil
}
func (p *Process) Memory_info_ex() (*Memory_info_exStat, error) {
_, mem_info_ex, err := p.fillFromStatm()
func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) {
_, memInfoEx, err := p.fillFromStatm()
if err != nil {
return nil, err
}
return mem_info_ex, nil
return memInfoEx, nil
}
func (p *Process) Memory_percent() (float32, error) {
return 0, errors.New("Not implemented yet")
func (p *Process) MemoryPercent() (float32, error) {
return 0, errors.New("not implemented yet")
}
func (p *Process) Children() ([]*Process, error) {
return nil, errors.New("Not implemented yet")
return nil, errors.New("not implemented yet")
}
func (p *Process) Open_files() ([]Open_filesStat, error) {
return nil, errors.New("Not implemented yet")
func (p *Process) OpenFiles() ([]OpenFilesStat, error) {
return nil, errors.New("not implemented yet")
}
func (p *Process) Connections() ([]Net_connectionStat, error) {
return nil, errors.New("Not implemented yet")
return nil, errors.New("not implemented yet")
}
func (p *Process) Is_running() (bool, error) {
return true, errors.New("Not implemented yet")
func (p *Process) IsRunning() (bool, error) {
return true, errors.New("not implemented yet")
}
// Get memory maps from /proc/(pid)/smaps
func (p *Process) Memory_Maps(grouped bool) (*[]Memory_mapsStat, error) {
// MemoryMaps get memory maps from /proc/(pid)/smaps
func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) {
pid := p.Pid
ret := make([]Memory_mapsStat, 0)
var ret []MemoryMapsStat
smapsPath := filepath.Join("/", "proc", strconv.Itoa(int(pid)), "smaps")
contents, err := ioutil.ReadFile(smapsPath)
if err != nil {
@ -199,8 +199,8 @@ func (p *Process) Memory_Maps(grouped bool) (*[]Memory_mapsStat, error) {
lines := strings.Split(string(contents), "\n")
// function of parsing a block
get_block := func(first_line []string, block []string) Memory_mapsStat {
m := Memory_mapsStat{}
getBlock := func(first_line []string, block []string) MemoryMapsStat {
m := MemoryMapsStat{}
m.Path = first_line[len(first_line)-1]
for _, line := range block {
@ -217,13 +217,13 @@ func (p *Process) Memory_Maps(grouped bool) (*[]Memory_mapsStat, error) {
case "Pss":
m.Pss = parseUint64(v)
case "Shared_Clean":
m.Shared_clean = parseUint64(v)
m.SharedClean = parseUint64(v)
case "Shared_Dirty":
m.Shared_dirty = parseUint64(v)
m.SharedDirty = parseUint64(v)
case "Private_Clean":
m.Private_clean = parseUint64(v)
m.PrivateClean = parseUint64(v)
case "Private_Dirty":
m.Private_dirty = parseUint64(v)
m.PrivateDirty = parseUint64(v)
case "Referenced":
m.Referenced = parseUint64(v)
case "Anonymous":
@ -241,7 +241,7 @@ func (p *Process) Memory_Maps(grouped bool) (*[]Memory_mapsStat, error) {
if strings.HasSuffix(field[0], ":") == false {
// new block section
if len(blocks) > 0 {
ret = append(ret, get_block(field, blocks))
ret = append(ret, getBlock(field, blocks))
}
// starts new block
blocks = make([]string, 16)
@ -267,9 +267,9 @@ func (p *Process) fillFromfd() (int32, []*Open_filesStat, error) {
}
defer d.Close()
fnames, err := d.Readdirnames(-1)
num_fds := int32(len(fnames))
numFDs := int32(len(fnames))
openfiles := make([]*Open_filesStat, num_fds)
openfiles := make([]*OpenFilesStat, numFDs)
for _, fd := range fnames {
fpath := filepath.Join(statPath, fd)
filepath, err := os.Readlink(fpath)
@ -283,7 +283,7 @@ func (p *Process) fillFromfd() (int32, []*Open_filesStat, error) {
openfiles = append(openfiles, o)
}
return num_fds, openfiles, nil
return numFDs, openfiles, nil
}
// Get cwd from /proc/(pid)/cwd
@ -339,11 +339,11 @@ func (p *Process) fillFromStatm() (*Memory_infoStat, *Memory_info_exStat, error)
rss := parseUint64(fields[0]) * PAGESIZE
vms := parseUint64(fields[1]) * PAGESIZE
mem_info := &Memory_infoStat{
memInfo := &MemoryInfoStat{
RSS: rss,
VMS: vms,
}
mem_info_ex := &Memory_info_exStat{
memInfoEx := &MemoryInfoExStat{
RSS: rss,
VMS: vms,
Shared: parseUint64(fields[2]) * PAGESIZE,
@ -352,7 +352,7 @@ func (p *Process) fillFromStatm() (*Memory_infoStat, *Memory_info_exStat, error)
Dirty: parseUint64(fields[5]) * PAGESIZE,
}
return mem_info, mem_info_ex, nil
return memInfo, memInfoEx, nil
}
// Get various status from /proc/(pid)/status
@ -367,9 +367,9 @@ func (p *Process) fillFromStatus() (string, string, []int32, []int32, int32, err
name := ""
status := ""
var num_threads int32
uids := make([]int32, 0)
gids := make([]int32, 0)
var numThreads int32
var uids []int32
var gids []int32
for _, line := range lines {
field := strings.Split(line, ":")
if len(field) < 2 {
@ -394,14 +394,14 @@ func (p *Process) fillFromStatus() (string, string, []int32, []int32, int32, err
gids = append(gids, parseInt32(i))
}
case "Threads":
num_threads = parseInt32(field[1])
numThreads = parseInt32(field[1])
}
}
return name, status, uids, gids, num_threads, nil
return name, status, uids, gids, numThreads, nil
}
func (p *Process) fillFromStat() (string, int32, *CPU_TimesStat, int64, int32, error) {
func (p *Process) fillFromStat() (string, int32, *CPUTimesStat, int64, int32, error) {
pid := p.Pid
statPath := filepath.Join("/", "proc", strconv.Itoa(int(pid)), "stat")
contents, err := ioutil.ReadFile(statPath)
@ -420,26 +420,26 @@ func (p *Process) fillFromStat() (string, int32, *CPU_TimesStat, int64, int32, e
utime, _ := strconv.ParseFloat(fields[13], 64)
stime, _ := strconv.ParseFloat(fields[14], 64)
cpu_times := &CPU_TimesStat{
cpuTimes := &CPUTimesStat{
Cpu: "cpu",
User: float32(utime * (1000 / CLOCK_TICKS)),
System: float32(stime * (1000 / CLOCK_TICKS)),
}
boot_time, _ := Boot_time()
ctime := ((parseUint64(fields[21]) / uint64(CLOCK_TICKS)) + uint64(boot_time)) * 1000
create_time := int64(ctime)
bootTime, _ := BootTime()
ctime := ((parseUint64(fields[21]) / uint64(CLOCK_TICKS)) + uint64(bootTime)) * 1000
createTime := int64(ctime)
// p.Nice = parseInt32(fields[18])
// use syscall instead of parse Stat file
snice, _ := syscall.Getpriority(PRIO_PROCESS, int(pid))
nice := int32(snice) // FIXME: is this true?
return terminal, ppid, cpu_times, create_time, nice, nil
return terminal, ppid, cpuTimes, createTime, nice, nil
}
func Pids() ([]int32, error) {
ret := make([]int32, 0)
var ret []int32
d, err := os.Open("/proc")
if err != nil {

View File

@ -24,7 +24,7 @@ func Test_Pid_exists(t *testing.T) {
check_pid = 0
}
ret, err := Pid_exists(int32(check_pid))
ret, err := PidExists(int32(check_pid))
if err != nil {
t.Errorf("error %v", err)
}
@ -57,7 +57,7 @@ func Test_Process_memory_maps(t *testing.T) {
return
ret, err := NewProcess(int32(check_pid))
mmaps, err := ret.Memory_Maps(false)
mmaps, err := ret.MemoryMaps(false)
if err != nil {
t.Errorf("memory map get error %v", err)
}

View File

@ -19,7 +19,7 @@ type SYSTEM_PROCESS_INFORMATION struct {
NumberOfThreads uint64
Reserved1 [48]byte
Reserved2 [3]byte
UniqueProcessId uintptr
UniqueProcessID uintptr
Reserved3 uintptr
HandleCount uint64
Reserved4 [4]byte
@ -30,15 +30,15 @@ type SYSTEM_PROCESS_INFORMATION struct {
}
// Memory_info_ex is different between OSes
type Memory_info_exStat struct {
type MemoryInfoExStat struct {
}
type Memory_mapsStat struct {
type MemoryMapsStat struct {
}
func Pids() ([]int32, error) {
ret := make([]int32, 0)
var ret []int32
procs, err := processes()
if err != nil {
@ -60,7 +60,7 @@ func (p *Process) Ppid() (int32, error) {
}
func (p *Process) Name() (string, error) {
name := ""
return name, errors.New("Not implemented yet")
return name, errors.New("not implemented yet")
}
func (p *Process) Exe() (string, error) {
_, _, ret, err := p.getFromSnapProcess(p.Pid)
@ -70,51 +70,53 @@ func (p *Process) Exe() (string, error) {
return ret, nil
}
func (p *Process) Cmdline() (string, error) {
return "", errors.New("Not implemented yet")
return "", errors.New("not implemented yet")
}
func (p *Process) Cwd() (string, error) {
return "", errors.New("Not implemented yet")
return "", errors.New("not implemented yet")
}
func (p *Process) Parent() (*Process, error) {
return p, errors.New("Not implemented yet")
return p, errors.New("not implemented yet")
}
func (p *Process) Status() (string, error) {
return "", errors.New("Not implemented yet")
return "", errors.New("not implemented yet")
}
func (p *Process) Username() (string, error) {
return "", errors.New("Not implemented yet")
return "", errors.New("not implemented yet")
}
func (p *Process) Uids() ([]int32, error) {
uids := make([]int32, 0)
return uids, errors.New("Not implemented yet")
var uids []int32
return uids, errors.New("not implemented yet")
}
func (p *Process) Gids() ([]int32, error) {
gids := make([]int32, 0)
return gids, errors.New("Not implemented yet")
var gids []int32
return gids, errors.New("not implemented yet")
}
func (p *Process) Terminal() (string, error) {
return "", errors.New("Not implemented yet")
return "", errors.New("not implemented yet")
}
func (p *Process) Nice() (int32, error) {
return 0, errors.New("Not implemented yet")
return 0, errors.New("not implemented yet")
}
func (p *Process) Ionice() (int32, error) {
return 0, errors.New("Not implemented yet")
return 0, errors.New("not implemented yet")
}
func (p *Process) Rlimit() ([]RlimitStat, error) {
rlimit := make([]RlimitStat, 0)
return rlimit, errors.New("Not implemented yet")
var rlimit []RlimitStat
return rlimit, errors.New("not implemented yet")
}
func (p *Process) Io_counters() (*Io_countersStat, error) {
return nil, errors.New("Not implemented yet")
func (p *Process) IoCounters() (*IoCountersStat, error) {
return nil, errors.New("not implemented yet")
}
func (p *Process) Num_ctx_switches() (int32, error) {
return 0, errors.New("Not implemented yet")
func (p *Process) NumCtxSwitches() (int32, error) {
return 0, errors.New("not implemented yet")
}
func (p *Process) Num_fds() (int32, error) {
return 0, errors.New("Not implemented yet")
func (p *Process) NumFDs() (int32, error) {
return 0, errors.New("not implemented yet")
}
func (p *Process) Num_Threads() (int32, error) {
func (p *Process) NumThreads() (int32, error) {
_, ret, _, err := p.getFromSnapProcess(p.Pid)
if err != nil {
return 0, err
@ -123,45 +125,45 @@ func (p *Process) Num_Threads() (int32, error) {
}
func (p *Process) Threads() (map[string]string, error) {
ret := make(map[string]string, 0)
return ret, errors.New("Not implemented yet")
return ret, errors.New("not implemented yet")
}
func (p *Process) Cpu_times() (*CPU_TimesStat, error) {
return nil, errors.New("Not implemented yet")
func (p *Process) CPUTimes() (*CPUTimesStat, error) {
return nil, errors.New("not implemented yet")
}
func (p *Process) Cpu_percent() (int32, error) {
return 0, errors.New("Not implemented yet")
func (p *Process) CPUPercent() (int32, error) {
return 0, errors.New("not implemented yet")
}
func (p *Process) Cpu_affinity() ([]int32, error) {
return nil, errors.New("Not implemented yet")
func (p *Process) CPUAffinity() ([]int32, error) {
return nil, errors.New("not implemented yet")
}
func (p *Process) Memory_info() (*Memory_infoStat, error) {
return nil, errors.New("Not implemented yet")
func (p *Process) MemoryInfo() (*MemoryInfoStat, error) {
return nil, errors.New("not implemented yet")
}
func (p *Process) Memory_info_ex() (*Memory_info_exStat, error) {
return nil, errors.New("Not implemented yet")
func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) {
return nil, errors.New("not implemented yet")
}
func (p *Process) Memory_percent() (float32, error) {
return 0, errors.New("Not implemented yet")
func (p *Process) MemoryPercent() (float32, error) {
return 0, errors.New("not implemented yet")
}
func (p *Process) Children() ([]*Process, error) {
return nil, errors.New("Not implemented yet")
return nil, errors.New("not implemented yet")
}
func (p *Process) Open_files() ([]Open_filesStat, error) {
return nil, errors.New("Not implemented yet")
func (p *Process) OpenFiles() ([]OpenFilesStat, error) {
return nil, errors.New("not implemented yet")
}
func (p *Process) Connections() ([]Net_connectionStat, error) {
return nil, errors.New("Not implemented yet")
func (p *Process) Connections() ([]NetConnectionStat, error) {
return nil, errors.New("not implemented yet")
}
func (p *Process) Is_running() (bool, error) {
return true, errors.New("Not implemented yet")
func (p *Process) IsRunning() (bool, error) {
return true, errors.New("not implemented yet")
}
func (p *Process) Memory_Maps(grouped bool) (*[]Memory_mapsStat, error) {
return nil, errors.New("Not implemented yet")
func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) {
return nil, errors.New("not implemented yet")
}
func NewProcess(pid int32) (*Process, error) {
@ -170,21 +172,21 @@ func NewProcess(pid int32) (*Process, error) {
return p, nil
}
func (p *Process) Send_signal(sig syscall.Signal) error {
return errors.New("Not implemented yet")
func (p *Process) SendSignal(sig syscall.Signal) error {
return errors.New("not implemented yet")
}
func (p *Process) Suspend() error {
return errors.New("Not implemented yet")
return errors.New("not implemented yet")
}
func (p *Process) Resume() error {
return errors.New("Not implemented yet")
return errors.New("not implemented yet")
}
func (p *Process) Terminate() error {
return errors.New("Not implemented yet")
return errors.New("not implemented yet")
}
func (p *Process) Kill() error {
return errors.New("Not implemented yet")
return errors.New("not implemented yet")
}
func (p *Process) getFromSnapProcess(pid int32) (int32, int32, string, error) {
@ -216,14 +218,14 @@ func (p *Process) getFromSnapProcess(pid int32) (int32, int32, string, error) {
// Get processes
func processes() ([]*Process, error) {
ps := make([]uint32, 255)
var read uint32 = 0
var read uint32
if w32.EnumProcesses(ps, uint32(len(ps)), &read) == false {
return nil, syscall.GetLastError()
}
results := make([]*Process, 0)
dward_size := uint32(4)
for _, pid := range ps[:read/dward_size] {
var results []*Process
dwardSize := uint32(4)
for _, pid := range ps[:read/dwardSize] {
if pid == 0 {
continue
}
@ -237,14 +239,14 @@ func processes() ([]*Process, error) {
return results, nil
}
func get_proc_info(pid int32) (*SYSTEM_PROCESS_INFORMATION, error) {
func getProcInfo(pid int32) (*SYSTEM_PROCESS_INFORMATION, error) {
initialBufferSize := uint64(0x4000)
bufferSize := initialBufferSize
buffer := make([]byte, bufferSize)
var sys_proc_info SYSTEM_PROCESS_INFORMATION
var sysProcInfo SYSTEM_PROCESS_INFORMATION
ret, _, _ := procNtQuerySystemInformation.Call(
uintptr(unsafe.Pointer(&sys_proc_info)),
uintptr(unsafe.Pointer(&sysProcInfo)),
uintptr(unsafe.Pointer(&buffer[0])),
uintptr(unsafe.Pointer(&bufferSize)),
uintptr(unsafe.Pointer(&bufferSize)))
@ -252,5 +254,5 @@ func get_proc_info(pid int32) (*SYSTEM_PROCESS_INFORMATION, error) {
return nil, syscall.GetLastError()
}
return &sys_proc_info, nil
return &sysProcInfo, nil
}