summaryrefslogtreecommitdiffstats
path: root/ml/Host.h
diff options
context:
space:
mode:
Diffstat (limited to 'ml/Host.h')
-rw-r--r--ml/Host.h106
1 files changed, 38 insertions, 68 deletions
diff --git a/ml/Host.h b/ml/Host.h
index 52a0cd09..289cb5ab 100644
--- a/ml/Host.h
+++ b/ml/Host.h
@@ -3,97 +3,67 @@
#ifndef ML_HOST_H
#define ML_HOST_H
+#include "Mutex.h"
#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 RrdHost {
-public:
- RrdHost(RRDHOST *RH) : RH(RH) {};
-
- 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() {};
+class Host {
-protected:
- RRDHOST *RH;
-
- // Protect dimension and lock maps
- std::mutex Mutex;
-
- std::unordered_map<RRDDIM *, Dimension *> DimensionsMap;
- std::unordered_map<Dimension *, std::mutex> LocksMap;
-};
+friend void* train_main(void *);
+friend void *detect_main(void *);
-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));
- }
+ Host(RRDHOST *RH) :
+ RH(RH),
+ MLS(),
+ TS(),
+ HostAnomalyRate(0.0),
+ ThreadsRunning(false),
+ ThreadsCancelled(false),
+ ThreadsJoined(false)
+ {}
+
+ void addChart(Chart *C);
+ void removeChart(Chart *C);
+ void getConfigAsJson(nlohmann::json &Json) const;
void getModelsAsJson(nlohmann::json &Json);
-
-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 getDetectionInfoAsJson(nlohmann::json &Json) const;
void startAnomalyDetectionThreads();
- void stopAnomalyDetectionThreads();
+ void stopAnomalyDetectionThreads(bool join);
- void getDetectionInfoAsJson(nlohmann::json &Json) const;
+ void scheduleForTraining(TrainingRequest TR);
+ void train();
-private:
void detect();
void detectOnce();
private:
- std::thread TrainingThread;
- std::thread DetectionThread;
-
+ RRDHOST *RH;
+ MachineLearningStats MLS;
+ TrainingStats TS;
CalculatedNumber HostAnomalyRate{0.0};
+ std::atomic<bool> ThreadsRunning;
+ std::atomic<bool> ThreadsCancelled;
+ std::atomic<bool> ThreadsJoined;
- size_t NumAnomalousDimensions{0};
- size_t NumNormalDimensions{0};
- size_t NumTrainedDimensions{0};
- size_t NumActiveDimensions{0};
-};
+ Queue<TrainingRequest> TrainingQueue;
-using Host = DetectableHost;
+ Mutex M;
+ std::unordered_map<RRDSET *, Chart *> Charts;
+
+ netdata_thread_t TrainingThread;
+ netdata_thread_t DetectionThread;
+};
} // namespace ml