-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.go
More file actions
447 lines (388 loc) · 11 KB
/
stats.go
File metadata and controls
447 lines (388 loc) · 11 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// Copyright CattleCloud LLC 2025, 2026
// SPDX-License-Identifier: BSD-3-Clause
package memc
import (
"bufio"
"cmp"
"io"
"regexp"
"slices"
"strconv"
"strings"
)
type Statistics struct {
Runtime struct {
PID int `json:"pid"`
Uptime int `json:"uptime"`
Time int `json:"time"`
Version string `json:"version"`
LibEvent string `json:"libevent"`
PointerSize int `json:"pointer_size"`
Threads int `json:"threads"`
}
Resources struct {
RUsageUser float64 `json:"rusage_user"`
RUsageSystem float64 `json:"rusage_system"`
}
Connections struct {
Max int `json:"max_connections"`
Current int `json:"curr_connections"`
Total int `json:"total_connections"`
Rejected int `json:"rejected_connections"`
Structures int `json:"connection_structures"`
}
Commands struct {
Get int `json:"cmd_get"`
Set int `json:"cmd_set"`
Flush int `json:"cmd_flush"`
Touch int `json:"cmd_touch"`
Meta int `json:"cmd_meta"`
Hit struct {
Get int `json:"get_hits"`
Delete int `json:"delete_hits"`
Increment int `json:"incr_hits"`
Decrement int `json:"decr_hits"`
Touch int `json:"touch_hits"`
CAS int `json:"cas_hits"`
}
Miss struct {
Get int `json:"get_misses"`
Delete int `json:"delete_misses"`
Increment int `json:"incr_misses"`
Decrement int `json:"decr_misses"`
Touch int `json:"touch_misses"`
CAS int `json:"cas_misses"`
}
Failure struct {
GetExpired int `json:"get_expired"`
GetFlushed int `json:"get_flushed"`
CASBadValue int `json:"cas_badval"`
}
}
Items struct {
Bytes int `json:"bytes"`
Current int `json:"curr_items"`
Total int `json:"total_items"`
}
}
func stats(r io.Reader) (*Statistics, error) {
scanner := bufio.NewScanner(r)
m := make(map[string]string)
SCAN:
// parse the contents of the stats output line by line
for scanner.Scan() {
line := scanner.Text()
switch line {
case "END":
break SCAN
case "ERROR":
return nil, ErrCommandIssue
default:
fields := strings.Fields(line)
if len(fields) < 3 || fields[0] != "STAT" {
continue
}
key := fields[1]
value := fields[2]
m[key] = value
}
}
// make sure the scanner was successful
if err := scanner.Err(); err != nil {
return nil, err
}
s := new(Statistics)
// map Runtime
s.Runtime.PID = toInt(m["pid"])
s.Runtime.Uptime = toInt(m["uptime"])
s.Runtime.Time = toInt(m["time"])
s.Runtime.Version = m["version"]
s.Runtime.LibEvent = m["libevent"]
s.Runtime.PointerSize = toInt(m["pointer_size"])
s.Runtime.Threads = toInt(m["threads"])
// map Resources
s.Resources.RUsageUser = toFloat64(m["rusage_user"])
s.Resources.RUsageSystem = toFloat64(m["rusage_system"])
// map Connections
s.Connections.Max = toInt(m["max_connections"])
s.Connections.Current = toInt(m["curr_connections"])
s.Connections.Total = toInt(m["total_connections"])
s.Connections.Rejected = toInt(m["rejected_connections"])
s.Connections.Structures = toInt(m["connection_structures"])
// map Commands
s.Commands.Get = toInt(m["cmd_get"])
s.Commands.Set = toInt(m["cmd_set"])
s.Commands.Flush = toInt(m["cmd_flush"])
s.Commands.Touch = toInt(m["cmd_touch"])
s.Commands.Meta = toInt(m["cmd_meta"])
// map Hits
s.Commands.Hit.Get = toInt(m["get_hits"])
s.Commands.Hit.Delete = toInt(m["delete_hits"])
s.Commands.Hit.Increment = toInt(m["incr_hits"])
s.Commands.Hit.Decrement = toInt(m["decr_hits"])
s.Commands.Hit.Touch = toInt(m["touch_hits"])
s.Commands.Hit.CAS = toInt(m["cas_hits"])
// map Misses
s.Commands.Miss.Get = toInt(m["get_misses"])
s.Commands.Miss.Delete = toInt(m["delete_misses"])
s.Commands.Miss.Increment = toInt(m["incr_misses"])
s.Commands.Miss.Decrement = toInt(m["decr_misses"])
s.Commands.Miss.Touch = toInt(m["touch_misses"])
s.Commands.Miss.CAS = toInt(m["cas_misses"])
// map Failures
s.Commands.Failure.GetExpired = toInt(m["get_expired"])
s.Commands.Failure.GetFlushed = toInt(m["get_flushed"])
s.Commands.Failure.CASBadValue = toInt(m["cas_badval"])
// map Items
s.Items.Bytes = toInt(m["bytes"])
s.Items.Current = toInt(m["curr_items"])
s.Items.Total = toInt(m["total_items"])
return s, nil
}
func toInt(s string) int {
v, _ := strconv.Atoi(s)
return v
}
func toFloat64(s string) float64 {
v, _ := strconv.ParseFloat(s, 64)
return v
}
type SlabStatistics struct {
ActiveSlabs int `json:"active_slabs"`
TotalMalloced int `json:"total_malloced"`
Slabs []*Slab `json:"slabs"`
}
type Slab struct {
Class int `json:"slab_class"`
ChunkSize int `json:"chunk_size"`
ChunksPerPage int `json:"chunks_per_page"`
TotalPages int `json:"total_pages"`
TotalChunks int `json:"total_chunks"`
UsedChunks int `json:"used_chunks"`
FreeChunks int `json:"free_chunks"`
FreeChunksEnd int `json:"free_chunks_end"`
GetHits int `json:"get_hits"`
CmdSet int `json:"cmd_set"`
DeleteHits int `json:"delete_hits"`
IncrementHits int `json:"incr_hits"`
DecrementHits int `json:"decr_hits"`
CASHits int `json:"cas_hits"`
CASBadVal int `json:"cas_badval"`
TouchHits int `json:"touch_hits"`
}
var (
statsSlabRe = regexp.MustCompile(`STAT (\d+):(\S+)\s+(\d+)`)
)
func slabs(r io.Reader) (*SlabStatistics, error) {
scanner := bufio.NewScanner(r)
stats := &SlabStatistics{
Slabs: make([]*Slab, 0, 4),
}
m := make(map[int]*Slab)
SCAN:
for scanner.Scan() {
line := scanner.Text()
switch {
case line == "END":
break SCAN
case line == "ERROR":
return nil, ErrCommandIssue
case strings.HasPrefix(line, "STAT active_slabs"):
fields := strings.Fields(line)
active := toInt(fields[2])
stats.ActiveSlabs = active
case strings.HasPrefix(line, "STAT total_malloced"):
fields := strings.Fields(line)
malloced := toInt(fields[2])
stats.TotalMalloced = malloced
default:
fields := statsSlabRe.FindStringSubmatch(line)
if len(fields) != 4 {
continue
}
slabclass := toInt(fields[1])
name := fields[2]
value := toInt(fields[3])
if _, exists := m[slabclass]; !exists {
m[slabclass] = &Slab{Class: slabclass}
}
slab := m[slabclass]
switch name {
case "chunk_size":
slab.ChunkSize = value
case "chunks_per_page":
slab.ChunksPerPage = value
case "total_pages":
slab.TotalPages = value
case "total_chunks":
slab.TotalChunks = value
case "used_chunks":
slab.UsedChunks = value
case "free_chunks":
slab.FreeChunks = value
case "free_chunks_end":
slab.FreeChunksEnd = value
case "get_hits":
slab.GetHits = value
case "cmd_set":
slab.CmdSet = value
case "delete_hits":
slab.DeleteHits = value
case "incr_hits":
slab.IncrementHits = value
case "decr_hits":
slab.DecrementHits = value
case "cas_hits":
slab.CASHits = value
case "cas_badval":
slab.CASBadVal = value
case "touch_hits":
slab.TouchHits = value
}
}
}
// ensure the scan was a success
if err := scanner.Err(); err != nil {
return nil, err
}
for _, v := range m {
stats.Slabs = append(stats.Slabs, v)
}
// order the slab classes ascending
slices.SortFunc(stats.Slabs, func(a, b *Slab) int {
return cmp.Compare(a.Class, b.Class)
})
return stats, nil
}
var (
statsItemsRe = regexp.MustCompile(`STAT items:(\d+):(\S+)\s+(\d+)`)
)
type ItemStatistics struct {
Class int `json:"slab_class"`
Number int `json:"number"`
NumberHot int `json:"number_hot"`
NumberWarm int `json:"number_warm"`
NumberCold int `json:"number_cold"`
AgeHot int `json:"age_hot"`
AgeWarm int `json:"age_warm"`
Age int `json:"age"`
MemRequested int `json:"mem_requested"`
Evicted int `json:"evicted"`
EvictedNonZero int `json:"evicted_nonzero"`
EvictedTime int `json:"evicted_time"`
OutOfMemory int `json:"outofmemory"`
TailRepairs int `json:"tailrepairs"`
Reclaimed int `json:"reclaimed"`
ExpiredUnfetched int `json:"expired_unfetched"`
EvictedUnfetched int `json:"evicted_unfetched"`
EvictedActive int `json:"evicted_active"`
CrawlerReclaimed int `json:"crawler_reclaimed"`
CrawlerItemsChecked int `json:"crawler_items_checked"`
LRUTailReflocked int `json:"lrutail_reflocked"`
MovesToCold int `json:"moves_to_cold"`
MovesToWarm int `json:"moves_to_warm"`
MovesWithinLRU int `json:"moves_within_lru"`
DirectReclaims int `json:"direct_reclaims"`
HitsToHot int `json:"hits_to_hot"`
HitsToWarm int `json:"hits_to_warm"`
HitsToCold int `json:"hits_to_cold"`
HitsToTemp int `json:"hits_to_temp"`
}
func items(r io.Reader) ([]*ItemStatistics, error) {
scanner := bufio.NewScanner(r)
m := make(map[int]*ItemStatistics, 4)
SCAN:
for scanner.Scan() {
line := scanner.Text()
switch line {
case "END":
break SCAN
case "ERROR":
return nil, ErrCommandIssue
default:
fields := statsItemsRe.FindStringSubmatch(line)
if len(fields) != 4 {
continue
}
slabclass := toInt(fields[1])
name := fields[2]
value := toInt(fields[3])
if _, exists := m[slabclass]; !exists {
m[slabclass] = new(ItemStatistics)
}
slab := m[slabclass]
switch name {
case "number":
slab.Number = value
case "number_hot":
slab.NumberHot = value
case "number_warm":
slab.NumberWarm = value
case "number_cold":
slab.NumberCold = value
case "age_hot":
slab.AgeHot = value
case "age_warm":
slab.AgeWarm = value
case "age":
slab.Age = value
case "mem_requested":
slab.MemRequested = value
case "evicted":
slab.Evicted = value
case "evicted_nonzero":
slab.EvictedNonZero = value
case "evicted_time":
slab.EvictedTime = value
case "outofmemory":
slab.OutOfMemory = value
case "tailrepairs":
slab.TailRepairs = value
case "reclaimed":
slab.Reclaimed = value
case "expired_unfetched":
slab.ExpiredUnfetched = value
case "evicted_unfetched":
slab.EvictedUnfetched = value
case "evicted_active":
slab.EvictedActive = value
case "crawler_reclaimed":
slab.CrawlerReclaimed = value
case "crawler_items_checked":
slab.CrawlerItemsChecked = value
case "lrutail_reflocked":
slab.LRUTailReflocked = value
case "moves_to_cold":
slab.MovesToCold = value
case "moves_to_warm":
slab.MovesToWarm = value
case "moves_within_lru":
slab.MovesWithinLRU = value
case "direct_reclaims":
slab.DirectReclaims = value
case "hits_to_hot":
slab.HitsToHot = value
case "hits_to_warm":
slab.HitsToWarm = value
case "hits_to_cold":
slab.HitsToCold = value
case "hits_to_temp":
slab.HitsToTemp = value
}
}
}
// ensure the scan was a success
if err := scanner.Err(); err != nil {
return nil, err
}
results := make([]*ItemStatistics, 0, len(m))
for slabclass, v := range m {
v.Class = slabclass
results = append(results, v)
}
// order by slab class ascending
slices.SortFunc(results, func(a, b *ItemStatistics) int {
return cmp.Compare(a.Class, b.Class)
})
return results, nil
}