-
Notifications
You must be signed in to change notification settings - Fork 11
/
stats.go
260 lines (231 loc) · 6.27 KB
/
stats.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package j8a
import (
"fmt"
"github.com/hako/durafmt"
"github.com/rs/zerolog/log"
"github.com/shirou/gopsutil/process"
"github.com/simonmittag/procspy"
"net"
"os"
"runtime/pprof"
"strconv"
"strings"
"sync"
"syscall"
"time"
)
type sample struct {
pid int32
cpuPc float64
mPc float32
rssBytes uint64
vmsBytes uint64
swapBytes uint64
time time.Time
dwnOpenTcpConns uint64
dwnMaxOpenTcpConns uint64
upOpenTcpConns uint64
upMaxOpenTcpConns uint64
threads int
ulimit uint64
}
type growthRate struct {
v float64
high bool
}
type samples []sample
const cpuSampleMilliSeconds = 2000
const logSamplerSleepSeconds = 60
const historySamplerSleepSeconds = 3600
const historyMaxSamples = 24
const growthRateThreshold float64 = 2.0
var procStatsLock sync.Mutex
var procHistory samples
const pid = "pid"
const pidCPUCorePct = "pidCpuCorePct"
const pidMemPct = "pidMemPct"
const pidRssBytes = "pidMemRssBytes"
const pidVmsBytes = "pidMemVmsBytes"
const pidSwapBytes = "pidMemSwapBytes"
const pidDwnOpenTcpConns = "pidDwnOpenTcpConns"
const pidDwnMaxOpenTcpConns = "pidDwnMaxOpenTcpConns"
const pidUpOpenTcpConns = "pidUpOpenTcpConns"
const pidUpMaxOpenTcpConns = "pidUpMaxOpenTcpConns"
const pidOSThreads = "pidOSThreads"
const pidOSUlimit = "pidOSUlimit"
const serverPerformance = "server performance"
const pcd2f = "%.2f"
const rssMemIncrease = "RSS memory increase for previous %s with high factor >=%s, monitor actively."
func (s sample) log() {
log.Info().
Int32(pid, s.pid).
Str(pidCPUCorePct, fmt.Sprintf(pcd2f, s.cpuPc)).
Str(pidMemPct, fmt.Sprintf(pcd2f, s.mPc)).
Uint64(pidDwnOpenTcpConns, s.dwnOpenTcpConns).
Uint64(pidDwnMaxOpenTcpConns, s.dwnMaxOpenTcpConns).
Uint64(pidUpOpenTcpConns, s.upOpenTcpConns).
Uint64(pidUpMaxOpenTcpConns, s.upMaxOpenTcpConns).
Uint64(pidRssBytes, s.rssBytes).
Uint64(pidVmsBytes, s.vmsBytes).
Uint64(pidSwapBytes, s.swapBytes).
Int(pidOSThreads, s.threads).
Uint64(pidOSUlimit, s.ulimit).
Msg(serverPerformance)
}
func (samples *samples) append(s sample) {
l := len(*samples)
if l >= historyMaxSamples {
(*samples)[0] = *new(sample)
*samples = (*samples)[1:]
}
*samples = append(*samples, s)
}
func (samples *samples) log() []growthRate {
var growthRates = make([]growthRate, len(*samples))
for l := len(*samples) - 1; l >= 0; l-- {
if (*samples)[l].pid == 0 {
//effectively does nothing to log here because of insufficient data.
return growthRates
}
}
for l := len(*samples) - 1; l >= 0; l-- {
growthRates[l].v = float64((*samples)[l].rssBytes) / float64((*samples)[0].rssBytes)
if growthRates[l].v >= growthRateThreshold {
growthRates[l].high = true
}
}
High:
for m := len(*samples) - 1; m >= 0; m-- {
if growthRates[m].high {
log.Info().
Msgf(rssMemIncrease,
durafmt.Parse(time.Duration(time.Second*historySamplerSleepSeconds*historyMaxSamples)).LimitFirstN(1).String(),
fmt.Sprintf("%.1f", growthRateThreshold))
break High
}
}
return growthRates
}
func (rt *Runtime) getSample(proc *process.Process) sample {
procStatsLock.Lock()
var threadProfile = pprof.Lookup("threadcreate")
cpuPc, _ := proc.Percent(time.Millisecond * cpuSampleMilliSeconds)
mPc, _ := proc.MemoryPercent()
mInfo, _ := proc.MemoryInfo()
var rLimit syscall.Rlimit
e := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
var ulimit uint64 = 0
if e == nil {
ulimit = rLimit.Cur
}
cs, e := rt.FindUpConns()
if e != nil {
//if this fails, skip upconns and continue with other sample data.
_ = e
} else {
d := rt.CountUpConns(proc, cs, rt.LookUpResourceIps())
rt.ConnectionWatcher.SetUp(uint64(d))
rt.ConnectionWatcher.UpdateMaxUp(uint64(d))
}
_ = cs
procStatsLock.Unlock()
return sample{
pid: proc.Pid,
cpuPc: cpuPc,
mPc: mPc,
rssBytes: mInfo.RSS,
vmsBytes: mInfo.VMS,
swapBytes: mInfo.Swap,
time: time.Now(),
dwnOpenTcpConns: rt.ConnectionWatcher.DwnCount(),
dwnMaxOpenTcpConns: rt.ConnectionWatcher.DwnMaxCount(),
upOpenTcpConns: rt.ConnectionWatcher.UpCount(),
upMaxOpenTcpConns: rt.ConnectionWatcher.UpMaxCount(),
threads: threadProfile.Count(),
ulimit: ulimit,
}
}
func (rt *Runtime) FindUpConns() (procspy.ConnIter, error) {
cs, e := procspy.Connections(true)
return cs, e
}
func (rt *Runtime) CountUpConns(proc *process.Process, cs procspy.ConnIter, ips map[string][]net.IP) int {
d := 0
UpConn:
for c := cs.Next(); c != nil; c = cs.Next() {
if c.PID == uint(proc.Pid) {
for _, v := range rt.Config.Resources {
for _, r := range v {
rup, _ := strconv.Atoi(r.URL.Port)
if c.RemotePort == uint16(rup) {
for _, ip := range ips[r.URL.Host] {
if ip.Equal(c.RemoteAddress) {
d++
continue UpConn
}
}
}
}
}
}
}
return d
}
func (rt *Runtime) LookUpResourceIps() map[string][]net.IP {
var ips = make(map[string][]net.IP)
for _, v := range rt.Resources {
for _, r := range v {
is := make([]net.IP, 1)
h := strings.TrimLeft(r.URL.Host, "[")
h = strings.TrimRight(h, "]")
is[0] = net.ParseIP(h)
if is[0] == nil {
is, _ = net.LookupIP(r.URL.Host)
}
ips[r.URL.Host] = is
}
}
return ips
}
// log proc samples infinite loop
func (rt *Runtime) logRuntimeStats(proc *process.Process) {
go func() {
for {
rt.getSample(proc).log()
time.Sleep(time.Second * logSamplerSleepSeconds)
}
}()
go func() {
procHistory = make(samples, historyMaxSamples)
lazy := rt.getSample(proc)
for k := 0; k < historyMaxSamples; k++ {
procHistory[k] = lazy
}
for {
procHistory.append(rt.getSample(proc))
time.Sleep(time.Second * historySamplerSleepSeconds)
}
}()
go func() {
for {
time.Sleep(time.Second * historySamplerSleepSeconds)
procHistory.log()
}
}()
}
const uptimeMicros = "uptimeMicros"
func (rt *Runtime) logUptime() {
go func() {
for {
upNanos := time.Since(rt.Start)
if upNanos > time.Second*10 {
uptime := durafmt.Parse(upNanos).LimitFirstN(1).String()
log.Info().
Int(pid, os.Getpid()).
Int64(uptimeMicros, int64(upNanos/1000)).
Msgf(fmt.Sprintf("server upTime is %s", uptime))
}
time.Sleep(time.Hour * 24)
}
}()
}