Repository navigation
Expand file tree
/
Copy pathruntime-stats.tsx
More file actions
481 lines (451 loc) · 14.9 KB
/
Copy pathruntime-stats.tsx
File metadata and controls
481 lines (451 loc) · 14.9 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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
import * as C from '@/constants'
import * as React from 'react'
import * as Kb from '@/common-adapters'
import * as Styles from '@/styles'
// import {isIPhoneX} from '@/constants/platform'
import * as T from '@/constants/types'
const isIPhoneX = false as boolean
// import lagRadar from 'lag-radar'
type Props = {
stats: T.RPCGen.RuntimeStats
}
const yesNo = (v?: boolean) => (v ? 'YES' : 'NO')
const severityStyle = (s: T.RPCGen.StatsSeverityLevel) => {
switch (s) {
case T.RPCGen.StatsSeverityLevel.warning:
return styles.statWarning
case T.RPCGen.StatsSeverityLevel.severe:
return styles.statSevere
default:
return styles.statNormal
}
}
const processTypeString = (s: T.RPCGen.ProcessType) => {
switch (s) {
case T.RPCGen.ProcessType.main:
return 'Service'
case T.RPCGen.ProcessType.kbfs:
return 'KBFS'
default:
return ''
}
}
const dbTypeString = (s: T.RPCGen.DbType) => {
switch (s) {
case T.RPCGen.DbType.main:
return 'Core'
case T.RPCGen.DbType.chat:
return 'Chat'
case T.RPCGen.DbType.fsBlockCache:
return 'FSBlkCache'
case T.RPCGen.DbType.fsBlockCacheMeta:
return 'FSBlkCacheMeta'
case T.RPCGen.DbType.fsSyncBlockCache:
return 'FSSyncBlkCache'
case T.RPCGen.DbType.fsSyncBlockCacheMeta:
return 'FSSyncBlkCacheMeta'
default:
return ''
}
}
// let destroyRadar: (() => void) | undefined
// let radarNode: HTMLDivElement | undefined
// const radarSize = 30
// const makeRadar = (show: boolean) => {
// if (destroyRadar) {
// destroyRadar()
// destroyRadar = undefined
// }
// if (!radarNode || !show) {
// return
// }
// destroyRadar = lagRadar({
// frames: 5,
// inset: 1,
// parent: radarNode,
// size: radarSize,
// speed: 0.0017 * 0.7,
// })
// }
// simple bucketing of incoming log lines, we have a queue of incoming items, we bucket them
// and choose a max to show. We use refs a lot since we only want to figure stuff out based on an interval
// TODO mobile
const LogStats = (props: {num?: number}) => {
const {num} = props
const maxBuckets = num ?? 5
const bucketsRef = React.useRef<Array<{count: number; label: string; labelFull: string; updated: boolean}>>(
[]
)
const [, setDoRender] = React.useState(0)
const events = C.useConfigState(s => s.runtimeStats?.perfEvents)
const lastEventsRef = React.useRef(new WeakSet<ReadonlyArray<T.RPCGen.PerfEvent>>())
const eventsRef = React.useRef<Array<T.RPCGen.PerfEvent>>([])
if (events) {
// only if unprocessed
if (!lastEventsRef.current.has(events)) {
lastEventsRef.current.add(events)
eventsRef.current.push(...events)
}
}
Kb.useInterval(() => {
const events = eventsRef.current
eventsRef.current = []
const incoming = events.map(e => {
const parts = e.message.split(' ')
if (parts.length >= 2) {
const [prefix, body] = parts
const bodyParts = body?.split('.') ?? []
const b = bodyParts.length > 1 ? bodyParts.slice(-2).join('.') : bodyParts[0]
switch (prefix) {
case 'GET':
return `<w:${b}`
case 'POST':
return `>w:${b}`
case 'CallCompressed':
return `cc:${b}`
case 'FullCachingSource:':
return `fcs:${b}`
case 'Call':
return `c:${b}`
default:
return e.message
}
} else {
return e.message
}
})
// copy existing buckets
let newBuckets = bucketsRef.current.map(b => ({...b, updated: false}))
// find existing or add new ones
incoming.forEach((i, idx) => {
const existing = newBuckets.find(b => b.label === i)
const labelFull = events[idx]?.message ?? i
if (existing) {
existing.updated = true
existing.count++
existing.labelFull += '\n' + labelFull
} else {
newBuckets.push({count: 1, label: i, labelFull, updated: true})
}
})
// sort to remove unupdated or small ones
newBuckets = newBuckets.sort((a, b) => {
if (a.updated !== b.updated) {
return a.updated ? -1 : 1
}
return b.count - a.count
})
// clamp buckets
newBuckets = newBuckets.slice(0, maxBuckets)
// if no new ones, lets eliminate the last item
if (!incoming.length) {
newBuckets = newBuckets.reverse()
newBuckets.some(b => {
if (b.label) {
b.label = ''
b.count = 0
return true
}
return false
})
newBuckets = newBuckets.reverse()
}
// sort remainder by alpha so things don't move around a lot
newBuckets = newBuckets.sort((a, b) => a.label.localeCompare(b.label))
bucketsRef.current = newBuckets
setDoRender(r => r + 1)
}, 2000)
return (
<Kb.Box2
direction="vertical"
style={{
backgroundColor: 'rgba(0,0,0, 0.3)',
minHeight: (Styles.isMobile ? 12 : 20) * maxBuckets,
}}
fullWidth={true}
>
{!Styles.isMobile && (
<Kb.Text type="BodyTinyBold" style={styles.stat}>
Logs
</Kb.Text>
)}
{bucketsRef.current.map((b, i) => (
<Kb.Text
key={i}
type={b.updated ? 'BodyTinyBold' : 'BodyTiny'}
style={styles.logStat}
lineClamp={1}
title={b.labelFull}
>
{b.label && b.count > 1 ? b.count : ''} {b.label}
</Kb.Text>
))}
</Kb.Box2>
)
}
const RuntimeStatsDesktop = ({stats}: Props) => {
// const [showRadar, setShowRadar] = React.useState(false)
// const refContainer = React.useCallback(
// node => {
// radarNode = node
// makeRadar(showRadar)
// },
// [showRadar]
// )
// const toggleRadar = () => {
// const show = !showRadar
// setShowRadar(show)
// makeRadar(show)
// }
const [moreLogs, setMoreLogs] = React.useState(false)
return (
<>
<Kb.BoxGrow style={styles.boxGrow}>
<Kb.ClickableBox onClick={() => setMoreLogs(m => !m)}>
<Kb.Box2 direction="vertical" style={styles.container} gap="xxtiny" fullWidth={true}>
{!moreLogs &&
stats.processStats?.map((stat, i) => {
return (
<Kb.Box2 direction="vertical" key={`process${i}`} fullWidth={true} noShrink={true}>
<Kb.Text type="BodyTinyBold" style={styles.stat}>
{processTypeString(stat.type)}
</Kb.Text>
<Kb.Text
style={Styles.collapseStyles([styles.stat, severityStyle(stat.cpuSeverity)])}
type="BodyTiny"
>{`CPU: ${stat.cpu}`}</Kb.Text>
<Kb.Text
style={Styles.collapseStyles([styles.stat, severityStyle(stat.residentSeverity)])}
type="BodyTiny"
>{`Res: ${stat.resident}`}</Kb.Text>
<Kb.Text style={styles.stat} type="BodyTiny">{`Virt: ${stat.virt}`}</Kb.Text>
<Kb.Text style={styles.stat} type="BodyTiny">{`Free: ${stat.free}`}</Kb.Text>
<Kb.Text style={styles.stat} type="BodyTiny">{`GoHeap: ${stat.goheap}`}</Kb.Text>
<Kb.Text style={styles.stat} type="BodyTiny">{`GoHeapSys: ${stat.goheapsys}`}</Kb.Text>
<Kb.Text style={styles.stat} type="BodyTiny">{`GoReleased: ${stat.goreleased}`}</Kb.Text>
<Kb.Divider />
<Kb.Divider />
</Kb.Box2>
)
})}
{!moreLogs && <Kb.Divider />}
{!moreLogs && (
<Kb.Text type="BodyTinyBold" style={styles.stat}>
Chat Bkg Activity
</Kb.Text>
)}
{!moreLogs && (
<Kb.Text
style={Styles.collapseStyles([
styles.stat,
stats.convLoaderActive ? styles.statWarning : styles.statNormal,
])}
type="BodyTiny"
>{`BkgLoaderActive: ${yesNo(stats.convLoaderActive)}`}</Kb.Text>
)}
{!moreLogs && (
<Kb.Text
style={Styles.collapseStyles([
styles.stat,
stats.selectiveSyncActive ? styles.statWarning : styles.statNormal,
])}
type="BodyTiny"
>{`IndexerSyncActive: ${yesNo(stats.selectiveSyncActive)}`}</Kb.Text>
)}
{!moreLogs && <Kb.Divider />}
{!moreLogs && (
<Kb.Text type="BodyTinyBold" style={styles.stat}>
LevelDB Compaction
</Kb.Text>
)}
{!moreLogs &&
stats.dbStats?.map((stat, i) => {
return (
<Kb.Box2 direction="vertical" key={`db${i}`} fullWidth={true}>
<Kb.Text
type="BodyTiny"
style={Styles.collapseStyles([
styles.stat,
stat.memCompActive || stat.tableCompActive ? styles.statWarning : styles.statNormal,
])}
>
{`${dbTypeString(stat.type)}: ${yesNo(stat.memCompActive || stat.tableCompActive)}`}
</Kb.Text>
</Kb.Box2>
)
})}
{/*!moreLogs && (
<Kb.Box style={styles.radarContainer} forwardedRef={refContainer} onClick={toggleRadar} />
)*/}
<LogStats num={moreLogs ? 25 : 5} />
</Kb.Box2>
</Kb.ClickableBox>
</Kb.BoxGrow>
</>
)
}
const compactionActive = (dbStats: Props['stats']['dbStats'], typs: Array<T.RPCGen.DbType>) =>
dbStats?.some(stat => typs.includes(stat.type) && (stat.memCompActive || stat.tableCompActive))
const chatDbs = [T.RPCGen.DbType.chat, T.RPCGen.DbType.main]
const kbfsDbs = [
T.RPCGen.DbType.fsBlockCache,
T.RPCGen.DbType.fsBlockCacheMeta,
T.RPCGen.DbType.fsSyncBlockCache,
T.RPCGen.DbType.fsSyncBlockCacheMeta,
]
const RuntimeStatsMobile = ({stats}: Props) => {
const [showLogs, setShowLogs] = React.useState(true)
const processStat = stats.processStats?.[0]
const coreCompaction = compactionActive(stats.dbStats, chatDbs)
const kbfsCompaction = compactionActive(stats.dbStats, kbfsDbs)
return (
<>
<Kb.Box2
direction="vertical"
style={showLogs ? styles.modalLogStats : styles.modalLogStatsHidden}
gap="xtiny"
>
<Kb.ClickableBox onClick={() => setShowLogs(s => !s)}>
<LogStats />
</Kb.ClickableBox>
</Kb.Box2>
<Kb.Box2 direction="horizontal" style={styles.container} gap="xtiny" pointerEvents="none">
{processStat && (
<Kb.Box2 direction="vertical">
<Kb.Box2 direction="horizontal" gap="xxtiny" alignSelf="flex-end">
<Kb.Text
style={Styles.collapseStyles([styles.stat, severityStyle(processStat.cpuSeverity)])}
type="BodyTiny"
>{`C:${processStat.cpu}`}</Kb.Text>
<Kb.Text
style={Styles.collapseStyles([styles.stat, severityStyle(processStat.residentSeverity)])}
type="BodyTiny"
>{`R:${processStat.resident}`}</Kb.Text>
<Kb.Text style={styles.stat} type="BodyTiny">{`V:${processStat.virt}`}</Kb.Text>
<Kb.Text style={styles.stat} type="BodyTiny">{`F:${processStat.free}`}</Kb.Text>
</Kb.Box2>
<Kb.Box2 direction="horizontal" gap="xxtiny" alignSelf="flex-end">
<Kb.Text style={styles.stat} type="BodyTiny">{`GH:${processStat.goheap}`}</Kb.Text>
<Kb.Text style={styles.stat} type="BodyTiny">{`GS:${processStat.goheapsys}`}</Kb.Text>
<Kb.Text style={styles.stat} type="BodyTiny">{`GR:${processStat.goreleased}`}</Kb.Text>
</Kb.Box2>
</Kb.Box2>
)}
<Kb.Box2 direction="vertical">
<Kb.Text
style={Styles.collapseStyles([
styles.stat,
stats.convLoaderActive ? styles.statWarning : styles.statNormal,
])}
type="BodyTiny"
>{`CLA: ${yesNo(stats.convLoaderActive)}`}</Kb.Text>
<Kb.Text
style={Styles.collapseStyles([
styles.stat,
stats.selectiveSyncActive ? styles.statWarning : styles.statNormal,
])}
type="BodyTiny"
>{`SSA: ${yesNo(stats.selectiveSyncActive)}`}</Kb.Text>
</Kb.Box2>
<Kb.Box2 direction="vertical">
<Kb.Text
style={Styles.collapseStyles([
styles.stat,
coreCompaction ? styles.statWarning : styles.statNormal,
])}
type="BodyTiny"
>{`LC: ${yesNo(coreCompaction)}`}</Kb.Text>
<Kb.Text
style={Styles.collapseStyles([
styles.stat,
kbfsCompaction ? styles.statWarning : styles.statNormal,
])}
type="BodyTiny"
>{`LK: ${yesNo(kbfsCompaction)}`}</Kb.Text>
</Kb.Box2>
</Kb.Box2>
</>
)
}
const RuntimeStats = () => {
const stats = C.useConfigState(s => s.runtimeStats)
return stats ? (
Styles.isMobile ? (
<RuntimeStatsMobile stats={stats} />
) : (
<RuntimeStatsDesktop stats={stats} />
)
) : null
}
const styles = Styles.styleSheetCreate(() => ({
boxGrow: Styles.platformStyles({
isElectron: {
overflow: 'auto',
},
}),
container: Styles.platformStyles({
common: {backgroundColor: Styles.globalColors.blackOrBlack},
isElectron: {
overflow: 'auto',
padding: Styles.globalMargins.tiny,
position: 'relative',
},
isMobile: {
bottom: isIPhoneX ? 15 : 0,
position: 'absolute',
right: isIPhoneX ? 10 : 0,
},
}),
logStat: Styles.platformStyles({
common: {color: Styles.globalColors.whiteOrWhite},
isElectron: {wordBreak: 'break-all'},
isMobile: {
fontFamily: 'Courier',
fontSize: 12,
lineHeight: 16,
},
}),
modalLogStats: {
position: 'absolute',
right: 0,
top: 20,
width: 130,
},
modalLogStatsHidden: {
backgroundColor: 'yellow',
position: 'absolute',
right: 0,
top: 20,
width: 20,
},
// radarContainer: Styles.platformStyles({
// isElectron: {
// backgroundColor: Styles.globalColors.white_20,
// borderRadius: '50%',
// height: radarSize,
// position: 'absolute',
// right: Styles.globalMargins.tiny,
// top: Styles.globalMargins.tiny,
// width: radarSize,
// },
// }),
stat: Styles.platformStyles({
common: {color: Styles.globalColors.whiteOrGreenDark},
isElectron: {wordBreak: 'break-all'},
isMobile: {
fontFamily: 'Courier',
fontSize: 10,
lineHeight: 14,
},
}),
statNormal: {
color: Styles.globalColors.whiteOrGreenDark,
},
statSevere: {
color: Styles.globalColors.red,
},
statWarning: {
color: Styles.globalColors.yellowOrYellowAlt,
},
}))
export default RuntimeStats