summaryrefslogtreecommitdiff
path: root/process/process.go
blob: a30693de940112b25fc0a484c375239c68a71ee0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package process

import (
	"fmt"
	"github.com/buetow/gotop/utils"
	"io/ioutil"
	"log"
	"regexp"
	"strconv"
	"strings"
	"time"
)

type Process struct {
	Id        string
	Timestamp int32
	Pid       int
	Cmdline   string
	Count     map[string]int
	debug     string
	Last      bool
}

func new(pidstr string) (Process, error) {
	pid, err := strconv.Atoi(pidstr)
	if err != nil {
		return Process{}, err
	}

	timestamp := int32(time.Now().Unix())
	p := Process{Pid: pid, Timestamp: timestamp}
	var rawIo string

	if err = utils.Slurp(&rawIo, fmt.Sprintf("/proc/%d/io", pid)); err != nil {
		return p, err
	}

	if err = p.parseRawIo(rawIo); err != nil {
		return p, err
	}

	err = utils.Slurp(&p.Cmdline, fmt.Sprintf("/proc/%d/cmdline", pid))
	p.Id = fmt.Sprintf("(%s) %s", pidstr, p.Cmdline)
	return p, err
}

func (self *Process) gatherRaw(what *string, pathf string) error {
	bytes, err := ioutil.ReadFile(fmt.Sprintf(pathf, self.Pid))
	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 += fmt.Sprintf("PID: %d\n", self.Pid)
	str += fmt.Sprintf("Cmdline: %s\n", self.Cmdline)
	str += fmt.Sprintf("Timestamp: %s\n", self.Timestamp)
	for key, val := range self.Count {
		str += fmt.Sprintf("%s=%d\n", key, val)
	}
	if self.debug != "" {
		str += fmt.Sprintf("debug: %s\n", self.debug)
	}

	return str
}

func (self *Process) Print() {
	fmt.Println(self)
}

func Gather(pTxChan chan<- Process) {
	re, _ := regexp.Compile("^[0-9]+$")

	dir, err := ioutil.ReadDir("/proc/")
	if err != nil {
		log.Fatal(err)
	}

	for _, direntry := range dir {
		name := direntry.Name()
		if re.MatchString(name) {
			p, err := new(name)
			if err == nil {
				pTxChan <- p
			}
		}
	}

	pTxChan <- Process{Last: true}
}