From 986250ccd3b42672e84c974bec4fb07367cf0c60 Mon Sep 17 00:00:00 2001 From: "Paul Buetow (europa)" Date: Mon, 25 May 2015 00:54:49 +0100 Subject: cann parse raw io --- process/process.go | 62 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 11 deletions(-) (limited to 'process/process.go') diff --git a/process/process.go b/process/process.go index 91f1f97..1bfa2a6 100644 --- a/process/process.go +++ b/process/process.go @@ -7,13 +7,15 @@ import ( "log" "regexp" "strconv" + "strings" ) type Process struct { - Pid int - Cmdline string - rawIo string - firstErr error + Pid int + Cmdline string + + Count map[string]int + debug string } func newError() (Process, error) { @@ -23,28 +25,66 @@ func newError() (Process, error) { func new(pidstr string) (Process, error) { pid, _ := strconv.Atoi(pidstr) process := Process{Pid: pid} + var rawIo string + + { + err := process.gatherRaw(&rawIo, "/proc/%d/io") + if err != nil { + return process, err + } + } + { + err := process.parseRawIo(rawIo) + if err != nil { + return process, err + } + } - process.gatherRaw(&process.Cmdline, "/proc/%d/cmdline") - process.gatherRaw(&process.rawIo, "/proc/%d/io") + err := process.gatherRaw(&process.Cmdline, "/proc/%d/cmdline") + return process, err +} - return process, process.firstErr +func (self *Process) gatherParseRawIo(rawIo string) error { + return nil } -func (self *Process) gatherRaw(what *string, pathf string) { +func (self *Process) gatherRaw(what *string, pathf string) error { bytes, err := ioutil.ReadFile(fmt.Sprintf(pathf, self.Pid)) - if err != nil && self.firstErr == nil { - self.firstErr = err + if err != nil { + return err } else { *what = string(bytes) + return nil } } +func (self *Process) parseRawIo(rawIo string) error { + countMap := make(map[string]int) + for _, line := range strings.Split(rawIo, "\n") { + keyval := strings.Split(line, ": ") + if len(keyval) == 2 { + count, err := strconv.Atoi(keyval[1]) + if err != nil { + return err + } + countMap[keyval[0]] = count + } + } + self.Count = countMap + return nil +} + func (self *Process) String() string { str := "=========================\n" str = str + fmt.Sprintf("PID: %d\n", self.Pid) str = str + fmt.Sprintf("Cmdline: %s\n", self.Cmdline) - str = str + fmt.Sprintf("rawIo: %s\n", self.rawIo) + for key, val := range self.Count { + str = str + fmt.Sprintf("%s=%d\n", key, val) + } + if self.debug != "" { + str = str + fmt.Sprintf("debug: %s\n", self.debug) + } return str } -- cgit v1.2.3