summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow (europa) <paul@buetow.org>2015-05-25 00:54:49 +0100
committerPaul Buetow (europa) <paul@buetow.org>2015-05-25 00:54:49 +0100
commit986250ccd3b42672e84c974bec4fb07367cf0c60 (patch)
tree6b4ef0eaf3fba01339fc4083c0f2da91edb4c898
parent9b865d25ac5dde4045e3d3b6c4a333dfb51af7fc (diff)
cann parse raw io
-rw-r--r--process/process.go62
1 files changed, 51 insertions, 11 deletions
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
}