implements Process.IOCounters on linux.

This commit is contained in:
Shirou WAKAYAMA 2014-05-01 18:27:43 +09:00
parent 2186ef1768
commit 02a91378b8
3 changed files with 58 additions and 8 deletions

View File

@ -87,6 +87,7 @@ Current Status
- uids (linux, freebsd)
- gids (linux, freebsd)
- terminal (linux, freebsd)
- io_counters (linux)
- nice (linux)
- num_fds (linux)
- num_threads (linux, freebsd, windows)
@ -111,7 +112,6 @@ Current Status
- username
- ionice
- rlimit
- io_counters
- num_ctx_switches
- num_handlers
- threads

View File

@ -129,7 +129,7 @@ func (p *Process) Rlimit() ([]RlimitStat, error) {
return nil, errors.New("not implemented yet")
}
func (p *Process) IOCounters() (*IOCountersStat, error) {
return nil, errors.New("not implemented yet")
return p.fillFromIO()
}
func (p *Process) NumCtxSwitches() (int32, error) {
return 0, errors.New("not implemented yet")
@ -335,6 +335,37 @@ func (p *Process) fillFromCmdline() (string, error) {
return ret, nil
}
// Get IO status from /proc/(pid)/io
func (p *Process) fillFromIO() (*IOCountersStat, error) {
pid := p.Pid
ioPath := filepath.Join("/", "proc", strconv.Itoa(int(pid)), "io")
ioline, err := ioutil.ReadFile(ioPath)
if err != nil {
return nil, err
}
lines := strings.Split(string(ioline), "\n")
ret := &IOCountersStat{}
for _, line := range lines {
field := strings.Split(line, ":")
if len(field) < 2 {
continue
}
switch field[0] {
case "rchar":
ret.ReadCount = parseInt32(strings.Trim(field[1], " \t"))
case "wchar":
ret.WriteCount = parseInt32(strings.Trim(field[1], " \t"))
case "read_bytes":
ret.ReadBytes = parseInt32(strings.Trim(field[1], " \t"))
case "write_bytes":
ret.WriteBytes = parseInt32(strings.Trim(field[1], " \t"))
}
}
return ret, nil
}
// Get memory info from /proc/(pid)/statm
func (p *Process) fillFromStatm() (*MemoryInfoStat, *MemoryInfoExStat, error) {
pid := p.Pid

View File

@ -8,6 +8,15 @@ import (
"testing"
)
func test_getProcess() Process {
checkPid := os.Getpid()
if runtime.GOOS == "windows" {
checkPid = 7960
}
ret, _ := NewProcess(int32(checkPid))
return *ret
}
func Test_Pids(t *testing.T) {
ret, err := Pids()
if err != nil {
@ -68,13 +77,9 @@ func Test_Process_memory_maps(t *testing.T) {
}
func Test_Process_Ppid(t *testing.T) {
checkPid := os.Getpid()
if runtime.GOOS == "windows" {
checkPid = 7960
}
ret, err := NewProcess(int32(checkPid))
p := test_getProcess()
v, err := ret.Ppid()
v, err := p.Ppid()
if err != nil {
t.Errorf("geting ppid error %v", err)
}
@ -83,3 +88,17 @@ func Test_Process_Ppid(t *testing.T) {
}
}
func Test_Process_IOCounters(t *testing.T) {
p := test_getProcess()
v, err := p.IOCounters()
if err != nil {
t.Errorf("geting ppid error %v", err)
}
fmt.Println(v)
if v.ReadCount == 0 {
t.Errorf("return value is 0 %v", v)
}
}