@cryptotaxi247 / netdata-1 / commits / 207a743c7

Skip training of constant metrics. (#12212)

Detect dimensions whose values do not change, and skip them from training. This allows us to reduce the number of training operations by ~40-50%. Notice that we don't skip the very 1st training iteration, because a dimension's value might change at any point in time, and we need to have a trained model in order to compute its anomaly score.

vkalintiris committed Feb 23, 2022 at 15:59 UTC 207a743c77fe471b6cca644f11f52472cab6f193
2 files changed +11
ml/Dimension.cc
+5
@@ -128,6 +128,7 @@ MLResult TrainableDimension::trainModel() {
128 SamplesBuffer SB = SamplesBuffer(CNs, N, 1, Cfg.DiffN, Cfg.SmoothN, Cfg.LagN);
129 KM.train(SB, Cfg.MaxKMeansIters);
130 Trained = true;
131 + ConstantModel = true;
132
133 delete[] CNs;
134 return MLResult::Success;
@@ -146,6 +147,10 @@ void PredictableDimension::addValue(CalculatedNumber Value, bool Exists) {
147 }
148
149 std::rotate(std::begin(CNs), std::begin(CNs) + 1, std::end(CNs));
150 +
151 + if (CNs[N - 1] != Value)
152 + ConstantModel = false;
153 +
154 CNs[N - 1] = Value;
155 }
156
ml/Dimension.h
+6
@@ -55,6 +55,9 @@ public:
55 }
56
57 bool shouldTrain(const TimePoint &TP) const {
58 + if (ConstantModel)
59 + return false;
60 +
61 return (LastTrainedAt + TrainEvery) < TP;
62 }
63
@@ -70,6 +73,9 @@ private:
73 public:
74 TimePoint LastTrainedAt{Seconds{0}};
75
76 +protected:
77 + std::atomic<bool> ConstantModel{false};
78 +
79 private:
80 Seconds TrainEvery;
81 KMeans KM;