summaryrefslogtreecommitdiffstats
path: root/ml/Host.h
diff options
context:
space:
mode:
authorvkalintiris <vasilis@netdata.cloud>2022-12-22 13:18:55 +0200
committerGitHub <noreply@github.com>2022-12-22 13:18:55 +0200
commit6f42311c4b32d42798f78de1fd43f53694f24e6e (patch)
treea48e85baea0d2feabdcddf1426a6a3c8c46c5568 /ml/Host.h
parentc1aec98b30d8a4e80813cfccd636c31999c7ae3e (diff)
Revert "Refactor ML code and add support for multiple KMeans models. … (#14172)
Diffstat (limited to 'ml/Host.h')
-rw-r--r--ml/Host.h93
1 files changed, 67 insertions, 26 deletions
diff --git a/ml/Host.h b/ml/Host.h
index ccf3a44fc4..52a0cd095d 100644
--- a/ml/Host.h
+++ b/ml/Host.h
@@ -5,55 +5,96 @@
#include "Config.h"
#include "Dimension.h"
-#include "Chart.h"
-#include "Queue.h"
#include "ml-private.h"
#include "json/single_include/nlohmann/json.hpp"
-namespace ml
-{
+namespace ml {
-class Host {
+class RrdHost {
public:
- Host(RRDHOST *RH) :
- RH(RH),
- MLS(),
- TS(),
- HostAnomalyRate(0.0)
- { }
+ RrdHost(RRDHOST *RH) : RH(RH) {};
- void addChart(Chart *C);
- void removeChart(Chart *C);
+ RRDHOST *getRH() { return RH; }
+
+ unsigned updateEvery() { return RH->rrd_update_every; }
+
+ std::string getUUID() {
+ char S[UUID_STR_LEN];
+ uuid_unparse_lower(RH->host_uuid, S);
+ return S;
+ }
+
+ void addDimension(Dimension *D);
+ void removeDimension(Dimension *D);
void getConfigAsJson(nlohmann::json &Json) const;
+
+ virtual ~RrdHost() {};
+
+protected:
+ RRDHOST *RH;
+
+ // Protect dimension and lock maps
+ std::mutex Mutex;
+
+ std::unordered_map<RRDDIM *, Dimension *> DimensionsMap;
+ std::unordered_map<Dimension *, std::mutex> LocksMap;
+};
+
+class TrainableHost : public RrdHost {
+public:
+ TrainableHost(RRDHOST *RH) : RrdHost(RH) {}
+
+ void train();
+
+ void updateResourceUsage() {
+ std::lock_guard<std::mutex> Lock(ResourceUsageMutex);
+ getrusage(RUSAGE_THREAD, &ResourceUsage);
+ }
+
+ void getResourceUsage(struct rusage *RU) {
+ std::lock_guard<std::mutex> Lock(ResourceUsageMutex);
+ memcpy(RU, &ResourceUsage, sizeof(struct rusage));
+ }
+
void getModelsAsJson(nlohmann::json &Json);
- void getDetectionInfoAsJson(nlohmann::json &Json) const;
+
+private:
+ std::pair<Dimension *, Duration<double>> findDimensionToTrain(const TimePoint &NowTP);
+ void trainDimension(Dimension *D, const TimePoint &NowTP);
+
+ struct rusage ResourceUsage{};
+ std::mutex ResourceUsageMutex;
+};
+
+class DetectableHost : public TrainableHost {
+public:
+ DetectableHost(RRDHOST *RH) : TrainableHost(RH) {}
void startAnomalyDetectionThreads();
void stopAnomalyDetectionThreads();
- void scheduleForTraining(TrainingRequest TR);
- void train();
+ void getDetectionInfoAsJson(nlohmann::json &Json) const;
+private:
void detect();
void detectOnce();
private:
- RRDHOST *RH;
- MachineLearningStats MLS;
- TrainingStats TS;
- CalculatedNumber HostAnomalyRate{0.0};
-
- Queue<TrainingRequest> TrainingQueue;
-
- std::mutex Mutex;
- std::unordered_map<RRDSET *, Chart *> Charts;
-
std::thread TrainingThread;
std::thread DetectionThread;
+
+ CalculatedNumber HostAnomalyRate{0.0};
+
+ size_t NumAnomalousDimensions{0};
+ size_t NumNormalDimensions{0};
+ size_t NumTrainedDimensions{0};
+ size_t NumActiveDimensions{0};
};
+using Host = DetectableHost;
+
} // namespace ml
#endif /* ML_HOST_H */