1 |
|
2 |
|
3 | const summary = require('summary')
|
4 | const tf = require('@tensorflow/tfjs-core')
|
5 | const HMM = require('hidden-markov-model-tf')
|
6 | const util = require('util')
|
7 |
|
8 | // Disable warning about that `require('@tensorflow/tfjs-node')` is
|
9 | // recommended. There is no/minimal performance penalty and we avoid a
|
10 | // native addon.
|
11 | // NOTE: This is not a documented API.
|
12 | tf.ENV.set('IS_NODE', false)
|
13 |
|
14 | // There is no truth here. This parameter might need more tuning.
|
15 | const SEPARATION_THRESHOLD = 1
|
16 | const HHM_SEED = 0xa74b9cbd4047b4bbe79f365a9f247886ac0a8a9c23ef8c5c45d98badb8
|
17 |
|
18 | async function analyseCPU (processStatSubset, traceEventSubset) {
|
19 | const cpu = processStatSubset.map((d) => d.cpu)
|
20 | const summaryAll = summary(cpu)
|
21 |
|
22 | // For extremely small data, this algorithm doesn't work
|
23 | if (cpu.length < 4) {
|
24 | return summaryAll.max() < 0.9
|
25 | }
|
26 |
|
27 | // The CPU graph is typically composed of two "modes". An application mode
|
28 | // and a V8 mode. In the V8 mode, extra CPU threads are running garbage
|
29 | // collection and optimization. This causes the CPU usage for the
|
30 | // entire process, to be higher in these periods. For the analysing the
|
31 | // users application for I/O issues, the CPU usage data during the V8
|
32 | // mode is not of interest.
|
33 | //
|
34 | // | .--. ..- -.-
|
35 | // cpu | .- .. . . . - . . .
|
36 | // | . -. - . . - . - .. - ..
|
37 | // +----------------------------------------
|
38 | // app v8 app v8 app v8 app
|
39 | // Unfortunately, it is quite difficult to separate out the V8 data, even
|
40 | // with the traceEvent data from V8.
|
41 | // NOTE(@AndreasMadsen): I don't entirely know why.
|
42 | //
|
43 | // Instead, the V8 mode data will be removed using a statistical approach.
|
44 | // The statistical approach is "Gausian Mixture Model" (GMM), a better model
|
45 | // would be a "Hidden Markov Model" (HMM). However, this model is a bit more
|
46 | // complex and there doesn't exists an implementation of HMM where the data
|
47 | // is continues. HMM is better because it understands that the data is a
|
48 | // time series, which GMM doesn't. There is a comparison in the docs.
|
49 | //
|
50 | const hmm = new HMM({
|
51 | states: 2,
|
52 | dimensions: 1
|
53 | })
|
54 | const data = tf.tidy(() => tf.reshape(tf.tensor1d(cpu), [1, cpu.length, 1]))
|
55 |
|
56 | // Attempt to reach 0.001, but accept 0.01
|
57 | const results = await hmm.fit(data, {
|
58 | tolerance: 0.001,
|
59 | seed: HHM_SEED
|
60 | })
|
61 | /* istanbul ignore if: if HMM doesn't converge it is most likely a bug */
|
62 | if (results.tolerance >= 0.01) {
|
63 | throw new Error(`could not converge HMM model, tolerance: ${results.tolerance}`)
|
64 | }
|
65 |
|
66 | // Split data depending on the likelihood
|
67 | const group = [[], []]
|
68 | const state = await tf.tidy(() => tf.squeeze(hmm.inference(data))).data()
|
69 | for (let i = 0; i < cpu.length; i++) {
|
70 | group[state[i]].push(cpu[i])
|
71 | }
|
72 | const summary0 = summary(group[0])
|
73 | const summary1 = summary(group[1])
|
74 |
|
75 | // If one group is too small for a summary to be computed, just threat the
|
76 | // data as ungrouped.
|
77 | if (summary0.size() <= 1 || summary1.size() <= 1) {
|
78 | return summaryAll.quartile(0.9) < 0.9
|
79 | }
|
80 |
|
81 | // It is not always that there are two "modes". Determine if the groups are
|
82 | // separate by the separation coefficient.
|
83 | // https://en.wikipedia.org/wiki/Multimodal_distribution#Bimodal_separation
|
84 | const commonSd = 2 * (summary0.sd() + summary1.sd())
|
85 | const separation = (summary0.mean() - summary1.mean()) / commonSd
|
86 | // Threat the data as one "mode", if the separation coefficient is too small.
|
87 | if (Math.abs(separation) < SEPARATION_THRESHOLD) {
|
88 | return summaryAll.quartile(0.9) < 0.9
|
89 | }
|
90 |
|
91 | // The mode group with the highest mean is the V8 mode, the other is the
|
92 | // application mode. This is because V8 is multi-threaded, but javascript is
|
93 | // single-threaded.
|
94 | const summaryApplication = (
|
95 | summary0.mean() < summary1.mean() ? summary0 : summary1
|
96 | )
|
97 |
|
98 | // If the 90% quartile has less than 90% CPU load then the CPU is not
|
99 | // utilized optimally, likely because of some I/O delays. Highlight the
|
100 | // CPU curve in that case.
|
101 | return summaryApplication.quartile(0.9) < 0.9
|
102 | }
|
103 |
|
104 | // Wrap to be a callback function
|
105 | module.exports = util.callbackify(analyseCPU)
|
106 | module.exports[util.promisify.custom] = analyseCPU
|