This repository has been archived by the owner on Dec 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 81
/
cpustat-raw.go
executable file
·160 lines (139 loc) · 3.77 KB
/
cpustat-raw.go
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
"github.com/uber-common/cpustat/lib"
)
type pidList []int
func (pl *pidList) String() string {
if pl == nil {
return ""
}
ss := make([]string, len(*pl))
for i, pid := range *pl {
ss[i] = strconv.Itoa(pid)
}
return strings.Join(ss, ",")
}
func (pl *pidList) Set(s string) error {
parts := strings.Split(s, ",")
for _, part := range parts {
pid, err := strconv.Atoi(part)
if err != nil {
return fmt.Errorf("invalid pid list component %q, expected an integer", part)
}
*pl = append(*pl, pid)
}
return nil
}
func main() {
if err := run(); err != nil {
log.Fatalln(err)
}
}
func run() error {
var (
interval = flag.Duration("i", 200*time.Millisecond, "duration between measurements")
sampleCount = flag.Uint("n", 0, "Maximum number of samples to capture")
pids pidList
)
flag.Var(&pids, "p", "Comma separated PID list to profile")
flag.Parse()
if len(pids) == 0 {
fmt.Fprintf(os.Stderr, "no pids provided")
os.Exit(1)
}
procStats := cpustat.ProcStats{}
procStatsReaderCount := len(pids)
procStatsReaders := make([]*cpustat.ProcStatsSeekReader, procStatsReaderCount)
samplesRemaining := int64(*sampleCount)
if samplesRemaining == 0 {
samplesRemaining = -1
}
for i, pid := range pids {
procStatsReader := cpustat.ProcStatsSeekReader{
PID: pid,
}
procStatsReaders[i] = &procStatsReader
procStatsInitError := procStatsReader.Initialize()
if procStatsInitError != nil {
procStatsReaders[i] = nil
procStatsReaderCount--
}
}
var (
bufw = bufio.NewWriterSize(os.Stdout, 4096) // output buffer
tmp [256]byte // scratch space for marshaling integers
)
for i, part := range []string{
"pid",
"time",
"proc.utime",
"proc.stime",
"proc.cutime",
"proc.cstime",
"proc.numthreads",
"proc.rss",
"proc.guesttime",
"proc.cguesttime",
} {
if i > 0 {
_, _ = bufw.WriteRune(',')
}
_, _ = bufw.WriteString(part)
}
_, _ = bufw.WriteRune('\n')
for procStatsReaderCount > 0 && samplesRemaining != 0 {
startOfRead := time.Now()
for i, procStatsReader := range procStatsReaders {
if procStatsReader == nil {
continue
}
procStatsError := procStatsReader.ReadStats(&procStats)
if procStatsError != nil {
procStatsReaders[i] = nil
procStatsReaderCount--
continue
}
_, _ = bufw.Write(strconv.AppendInt(tmp[:0], int64(procStatsReader.PID), 10))
_, _ = bufw.WriteRune(',')
_, _ = bufw.Write(strconv.AppendInt(tmp[:0], procStats.CaptureTime.UnixNano()/1e6, 10))
_, _ = bufw.WriteRune(',')
_, _ = bufw.Write(strconv.AppendUint(tmp[:0], procStats.Utime, 10))
_, _ = bufw.WriteRune(',')
_, _ = bufw.Write(strconv.AppendUint(tmp[:0], procStats.Stime, 10))
_, _ = bufw.WriteRune(',')
_, _ = bufw.Write(strconv.AppendUint(tmp[:0], procStats.Cutime, 10))
_, _ = bufw.WriteRune(',')
_, _ = bufw.Write(strconv.AppendUint(tmp[:0], procStats.Cstime, 10))
_, _ = bufw.WriteRune(',')
_, _ = bufw.Write(strconv.AppendUint(tmp[:0], procStats.Numthreads, 10))
_, _ = bufw.WriteRune(',')
_, _ = bufw.Write(strconv.AppendUint(tmp[:0], procStats.Rss, 10))
_, _ = bufw.WriteRune(',')
_, _ = bufw.Write(strconv.AppendUint(tmp[:0], procStats.Guesttime, 10))
_, _ = bufw.WriteRune(',')
_, _ = bufw.Write(strconv.AppendUint(tmp[:0], procStats.Cguesttime, 10))
_, _ = bufw.WriteRune('\n')
}
if err := bufw.Flush(); err != nil {
return err
}
if samplesRemaining > 0 {
samplesRemaining--
}
if procStatsReaderCount > 0 && samplesRemaining != 0 {
if adjustedSleep := *interval - time.Now().Sub(startOfRead); adjustedSleep > 0 {
// TODO actually probably some minimum sleep tolerance
time.Sleep(adjustedSleep)
}
}
}
return bufw.Flush()
}