Host.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef ML_HOST_H
  3. #define ML_HOST_H
  4. #include "BitRateWindow.h"
  5. #include "Config.h"
  6. #include "Database.h"
  7. #include "Dimension.h"
  8. #include "ml-private.h"
  9. namespace ml {
  10. class RrdHost {
  11. public:
  12. RrdHost(RRDHOST *RH) : RH(RH) {
  13. AnomalyRateRS = rrdset_create(
  14. RH,
  15. "anomaly_detection",
  16. "anomaly_rates",
  17. NULL, // name
  18. "anomaly_rates",
  19. NULL, // ctx
  20. "Average anomaly rate",
  21. "anomaly rate",
  22. "netdata",
  23. "ml",
  24. 39189,
  25. Cfg.DBEngineAnomalyRateEvery,
  26. RRDSET_TYPE_LINE
  27. );
  28. AnomalyRateRS->flags = static_cast<RRDSET_FLAGS>(
  29. static_cast<int>(AnomalyRateRS->flags) | RRDSET_FLAG_HIDDEN
  30. );
  31. }
  32. RRDHOST *getRH() { return RH; }
  33. unsigned updateEvery() { return RH->rrd_update_every; }
  34. std::string getUUID() {
  35. char S[UUID_STR_LEN];
  36. uuid_unparse_lower(RH->host_uuid, S);
  37. return S;
  38. }
  39. void addDimension(Dimension *D);
  40. void removeDimension(Dimension *D);
  41. void getConfigAsJson(nlohmann::json &Json) const;
  42. virtual ~RrdHost() {};
  43. protected:
  44. RRDHOST *RH;
  45. RRDSET *AnomalyRateRS;
  46. // Protect dimension and lock maps
  47. std::mutex Mutex;
  48. std::unordered_map<RRDDIM *, Dimension *> DimensionsMap;
  49. std::unordered_map<Dimension *, std::mutex> LocksMap;
  50. };
  51. class TrainableHost : public RrdHost {
  52. public:
  53. TrainableHost(RRDHOST *RH) : RrdHost(RH) {}
  54. void train();
  55. void updateResourceUsage() {
  56. std::lock_guard<std::mutex> Lock(ResourceUsageMutex);
  57. getrusage(RUSAGE_THREAD, &ResourceUsage);
  58. }
  59. void getResourceUsage(struct rusage *RU) {
  60. std::lock_guard<std::mutex> Lock(ResourceUsageMutex);
  61. memcpy(RU, &ResourceUsage, sizeof(struct rusage));
  62. }
  63. private:
  64. std::pair<Dimension *, Duration<double>> findDimensionToTrain(const TimePoint &NowTP);
  65. void trainDimension(Dimension *D, const TimePoint &NowTP);
  66. struct rusage ResourceUsage{};
  67. std::mutex ResourceUsageMutex;
  68. };
  69. class DetectableHost : public TrainableHost {
  70. public:
  71. DetectableHost(RRDHOST *RH) : TrainableHost(RH) {}
  72. void startAnomalyDetectionThreads();
  73. void stopAnomalyDetectionThreads();
  74. template<typename ...ArgTypes>
  75. bool getAnomalyInfo(ArgTypes&&... Args) {
  76. return DB.getAnomalyInfo(Args...);
  77. }
  78. template<typename ...ArgTypes>
  79. bool getAnomaliesInRange(ArgTypes&&... Args) {
  80. return DB.getAnomaliesInRange(Args...);
  81. }
  82. void getDetectionInfoAsJson(nlohmann::json &Json) const;
  83. private:
  84. void detect();
  85. void detectOnce();
  86. private:
  87. std::thread TrainingThread;
  88. std::thread DetectionThread;
  89. BitRateWindow BRW{
  90. static_cast<size_t>(Cfg.ADMinWindowSize),
  91. static_cast<size_t>(Cfg.ADMaxWindowSize),
  92. static_cast<size_t>(Cfg.ADIdleWindowSize),
  93. static_cast<size_t>(Cfg.ADMinWindowSize * Cfg.ADWindowRateThreshold)
  94. };
  95. CalculatedNumber WindowAnomalyRate{0.0};
  96. size_t NumAnomalousDimensions{0};
  97. size_t NumNormalDimensions{0};
  98. size_t NumTrainedDimensions{0};
  99. size_t NumActiveDimensions{0};
  100. unsigned AnomalyRateTimer{0};
  101. Database DB{Cfg.AnomalyDBPath};
  102. };
  103. using Host = DetectableHost;
  104. } // namespace ml
  105. #endif /* ML_HOST_H */