-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstat.go
More file actions
280 lines (251 loc) · 5.95 KB
/
Copy pathstat.go
File metadata and controls
280 lines (251 loc) · 5.95 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
package main
import (
"context"
"fmt"
"log"
"sort"
"strings"
"sync"
"time"
"github.com/lugu/qiloop/bus"
sd "github.com/lugu/qiloop/bus/services"
"github.com/mum4k/termdash/container"
)
type entry struct {
count bus.MethodStatistics
action string
}
type gallery []entry
func (e gallery) Len() int { return len(e) }
func (e gallery) Swap(i, j int) { e[i], e[j] = e[j], e[i] }
func (e gallery) Less(i, j int) bool {
if e[i].count.Count == e[j].count.Count {
return e[i].count.Wall.CumulatedValue >
e[j].count.Wall.CumulatedValue
}
return e[i].count.Count > e[j].count.Count
}
func getObject(sess bus.Session, info sd.ServiceInfo) (bus.ObjectProxy, error) {
proxy, err := sess.Proxy(info.Name, 1)
if err != nil {
return nil, fmt.Errorf("failed to connect service (%s): %s", info.Name, err)
}
return bus.MakeObject(proxy), nil
}
func ignoreAction(id uint32) bool {
if id < 0x50 || id > 0x53 {
return false
}
return true
}
// periodic executes the provided closure periodically every interval.
// Exits when the context expires.
func periodic(ctx context.Context, interval time.Duration, fn func()) {
}
type highlight struct {
services map[string]bus.ObjectProxy
actions map[string]string
servicesMutex sync.Mutex
}
func newHighlighter(ctx context.Context, cancel context.CancelFunc, c *container.Container, w *widgets) (*highlight, error) {
h := &highlight{
services: map[string]bus.ObjectProxy{},
actions: map[string]string{},
}
err := h.initServices(ctx, sess, cancel)
if err != nil {
return nil, err
}
updater, err := h.updater(ctx, sess, cancel)
if err != nil {
return nil, err
}
onSelect := func(index int, line string) error {
if index == 0 {
setLayout(c, w, layoutTop)
if w.collector != nil {
w.collector.cancel()
w.collector = nil
}
if w.logger != nil {
w.logger.cancel()
w.logger = nil
}
return nil
}
setLayout(c, w, layoutTopTraceLogs)
labels := strings.SplitN(line, " | ", 5)
if len(labels) != 5 {
return fmt.Errorf("invalid line: %s", line)
}
desc := strings.SplitN(labels[4], ".", 2)
if len(desc) != 2 {
return fmt.Errorf("invalid service.action: %s", labels[4])
}
err := selectMethod(c, w, desc[0], desc[1])
if err != nil {
return err
}
return nil
}
w.topList.Configure([]string{}, onSelect)
go func() {
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
lines, err := updater()
if err != nil {
mainErr = err
cancel()
}
w.topList.Configure(lines, onSelect)
case <-ctx.Done():
return
}
}
}()
return h, nil
}
func (h *highlight) updateService(serviceName string, info sd.ServiceInfo) error {
obj, err := getObject(sess, info)
if err != nil {
return err
}
err = obj.ClearStats()
if err != nil {
return err
}
err = obj.EnableStats(true)
if err != nil {
return err
}
meta, err := obj.MetaObject(obj.Proxy().ObjectID())
if err != nil {
return err
}
h.servicesMutex.Lock()
defer h.servicesMutex.Unlock()
h.services[serviceName] = obj
for id, method := range meta.Methods {
if ignoreAction(id) {
continue
}
actionID := fmt.Sprintf("%s.%d", serviceName, id)
actionName := fmt.Sprintf("%s.%s", serviceName, method.Name)
h.actions[actionID] = actionName
}
return nil
}
// returns a function which update the top statistics
func (h *highlight) initServices(ctx context.Context, sess bus.Session, cancel context.CancelFunc) error {
directory, err := sd.ServiceDirectory(sess)
if err != nil {
return err
}
directory.Proxy().OnDisconnect(func(err error) {
mainErr = fmt.Errorf("Service directory disconnection: %s", err)
cancel()
})
serviceList, err := directory.Services()
if err != nil {
return err
}
for _, info := range serviceList {
err = h.updateService(info.Name, info)
if err != nil {
return err
}
}
cancelAdded, added, err := directory.SubscribeServiceAdded()
if err != nil {
return err
}
cancelRemoved, removed, err := directory.SubscribeServiceRemoved()
if err != nil {
return err
}
go func() {
for {
select {
case <-ctx.Done():
cancelAdded()
cancelRemoved()
return
case srv := <-added:
info, err := directory.Service(srv.Name)
if err != nil {
log.Print(err)
continue
}
err = h.updateService(srv.Name, info)
if err != nil {
log.Print(err)
continue
}
case srv := <-removed:
h.servicesMutex.Lock()
if _, ok := h.services[srv.Name]; ok {
delete(h.services, srv.Name)
}
h.servicesMutex.Unlock()
}
}
}()
go func(ctx context.Context) {
<-ctx.Done()
for _, obj := range h.services {
obj.EnableStats(false)
}
}(ctx)
return nil
}
func (h *highlight) updater(ctx context.Context, sess bus.Session, cancel context.CancelFunc) (func() ([]string, error), error) {
return func() ([]string, error) {
for {
counter := map[string]bus.MethodStatistics{}
h.servicesMutex.Lock()
for name, obj := range h.services {
stats, err := obj.Stats()
if err != nil {
continue
}
for action, stat := range stats {
if ignoreAction(action) {
continue
}
entry := fmt.Sprintf("%s.%d", name, action)
action, ok := h.actions[entry]
if !ok {
continue
}
counter[action] = stat
}
}
h.servicesMutex.Unlock()
topC := make([]entry, 0)
for action, count := range counter {
if count.Count == 0 {
continue
}
topC = append(topC, entry{
action: action,
count: count,
})
}
sort.Sort(gallery(topC))
lines := make([]string, len(topC)+1)
lines[0] = " count | min (us) | max (us) | avg (us) | Service.Method"
for i, entry := range topC {
lines[i+1] = fmt.Sprintf(" %5d | %8.0f | %8.0f | %8.0f | %s",
entry.count.Count,
entry.count.Wall.MinValue*1000000.0,
entry.count.Wall.MaxValue*1000000.0,
entry.count.Wall.CumulatedValue*1000000.0/float32(entry.count.Count),
entry.action)
}
return lines, nil
}
}, nil
}