From aa2fe8ccbfcb117efa207d10229eeeac5d0f97c7 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 6 Feb 2023 17:11:30 +0100 Subject: Adding upstream version 1.38.0. Signed-off-by: Daniel Baumann --- ml/Host.h | 106 ++++++++++++++++++++++---------------------------------------- 1 file changed, 38 insertions(+), 68 deletions(-) (limited to 'ml/Host.h') diff --git a/ml/Host.h b/ml/Host.h index 52a0cd095..289cb5ab7 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 DimensionsMap; - std::unordered_map 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 Lock(ResourceUsageMutex); - getrusage(RUSAGE_THREAD, &ResourceUsage); - } - - void getResourceUsage(struct rusage *RU) { - std::lock_guard 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> 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 ThreadsRunning; + std::atomic ThreadsCancelled; + std::atomic ThreadsJoined; - size_t NumAnomalousDimensions{0}; - size_t NumNormalDimensions{0}; - size_t NumTrainedDimensions{0}; - size_t NumActiveDimensions{0}; -}; + Queue TrainingQueue; -using Host = DetectableHost; + Mutex M; + std::unordered_map Charts; + + netdata_thread_t TrainingThread; + netdata_thread_t DetectionThread; +}; } // namespace ml -- cgit v1.2.3