-
Notifications
You must be signed in to change notification settings - Fork 0
/
printer.go
155 lines (132 loc) · 3.85 KB
/
printer.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
package jqtop
import (
"fmt"
"io"
"os"
"sort"
"strconv"
"time"
"github.com/pkg/errors"
"github.com/rcrowley/go-metrics"
"github.com/sirupsen/logrus"
"github.com/struCoder/pidusage"
)
//counters is used for sorting
type sortedCounters struct {
Name string
Value int64
Percentage float64
}
type printerStats struct {
sysinfo *pidusage.SysInfo
timeElapsed time.Duration
}
func getSortedCounters(fMap *map[string]int, fName string, lastCounters *[]map[string]int64) (ss []sortedCounters) {
countersSlice.RLock()
r := countersSlice.counters[(*fMap)[fName]]
countersSlice.RUnlock()
var counters []sortedCounters
// Sort
var total float64
r.Each(func(name string, i interface{}) {
c := i.(*metrics.StandardCounter)
rate := c.Count() - (*lastCounters)[(*fMap)[fName]][name]
(*lastCounters)[(*fMap)[fName]][name] = c.Count()
counters = append(counters, sortedCounters{name, rate, 0})
total += float64(rate)
})
sort.Slice(counters, func(i, j int) bool {
return counters[i].Value > counters[j].Value
})
// Return asked subset
if len(counters) > Config.MaxResult {
counters = counters[:Config.MaxResult]
}
// Get percentages
for i := range counters {
if counters[i].Value != 0 {
counters[i].Percentage = float64(counters[i].Value) / total * 100
} else {
counters[i].Percentage = 0
}
}
return counters
}
func printCounters(out io.Writer, counters map[string][]sortedCounters, stats printerStats) {
if Config.Clearscreen {
fmt.Println("\033[H\033[2J")
}
var totalCounters int64
for _, fieldName := range getFieldsInOrder(Config.Fields) {
fmt.Fprintf(out, "➤ %s\n", fieldName)
indent := "└──"
for _, counterData := range counters[fieldName] {
fmt.Fprintf(out, "%s %4s [%.1f%%]: %s\n",
indent, strconv.FormatInt(counterData.Value, 10), counterData.Percentage, counterData.Name)
totalCounters = totalCounters + counterData.Value
}
fmt.Fprintln(out, "")
}
fmt.Fprintf(out, "\n✖ Parse error rate: %d, CPU: %.2f%%, Mem(RSS): %.2fMB, Took: %s, Counters: %d\n",
parseErrors.Rate(), stats.sysinfo.CPU, stats.sysinfo.Memory/1024.0/1024.0, stats.timeElapsed, totalCounters)
}
// DumpCounters is a function to print stats to io.Writer
// nil io.Write is stdout
func DumpCounters(out io.Writer, totalIter int) {
if out == nil {
out = os.Stdout
}
// Fields
allFields, err := extractFields(Config.Fields)
if err != nil {
logrus.Fatalf("Error parsing fields, existing")
}
fMap := getFieldIndexMap(allFields.FieldsInOrder)
// Holds last value (avoid timers)
// Array of map[fieldname](counterValue)
var lastCounters []map[string]int64
initLastCounters(&lastCounters, allFields)
// Keep track of iteractions
var curIter int
ticker := time.NewTicker(time.Millisecond * time.Duration(Config.Interval))
for range ticker.C {
if shouldBreakLoop(&curIter, totalIter) {
break
}
now := time.Now()
stats := printerStats{}
sysinfo, _ := getResUsage()
stats.sysinfo = sysinfo
counters := make(map[string][]sortedCounters)
for _, fieldName := range getFieldsInOrder(Config.Fields) {
ss := getSortedCounters(&fMap, fieldName, &lastCounters)
counters[fieldName] = ss
}
timeElapsed := time.Since(now).Round(time.Millisecond)
stats.timeElapsed = timeElapsed
printCounters(out, counters, stats)
}
}
func shouldBreakLoop(n *int, total int) bool {
if total == 0 {
return false
}
*n = *n + 1
if *n > total {
return true
}
return false
}
func initLastCounters(counters *[]map[string]int64, allFields Fields) {
*counters = make([]map[string]int64, len(allFields.FieldsInOrder))
for i := range allFields.FieldsInOrder {
(*counters)[i] = make(map[string]int64, 0)
}
}
func getResUsage() (sysInfo *pidusage.SysInfo, err error) {
sysInfo, err = pidusage.GetStat(os.Getpid())
if err != nil {
return &pidusage.SysInfo{}, errors.Wrap(err, "Error getting CPU usage")
}
return sysInfo, nil
}