Fledge
An open source edge computing platform for industrial users
perfmonitors.h
1 #ifndef _PERFMONITOR_H
2 #define _PERFMONITOR_H
3 /*
4  * Fledge performance monitor
5  *
6  * Copyright (c) 2023 Dianomic Systems
7  *
8  * Released under the Apache 2.0 Licence
9  *
10  * Author: Mark Riddoch, Massimiliano Pinto
11  */
12 #include <thread>
13 #include <storage_client.h>
14 #include <unordered_map>
15 #include <insert.h>
16 #include <mutex>
17 #include <condition_variable>
18 
19 class PerfMon {
20  public:
21  PerfMon(const std::string& name);
22  void addValue(long value);
23  int getValues(InsertValues& values);
24  private:
25  std::string m_name;
26  long m_average;
27  long m_min;
28  long m_max;
29  int m_samples;
30  std::mutex m_mutex;
31 };
36  public:
37  PerformanceMonitor(const std::string& service, StorageClient *storage);
38  // Write data to storage
39  virtual void writeData(const std::string& table, const InsertValues& values)
40  {
41  // Write data via storage client
42  if (m_storage != NULL)
43  {
44  m_storage->insertTable(table, values);
45  }
46  else
47  {
48  Logger::getLogger()->error("Failed to save performace monitor data: "\
49  "storage client is null for servide '%s'",
50  m_service.c_str());
51  }
52  };
53  virtual ~PerformanceMonitor();
60  inline void collect(const std::string& name, long value)
61  {
62  if (m_collecting)
63  {
64  doCollection(name, value);
65  }
66  };
67  void setCollecting(bool state);
68  void writeThread();
69  bool isCollecting() { return m_collecting; };
70  private:
71  void doCollection(const std::string& name, long value);
72  private:
73  std::string m_service;
74  StorageClient *m_storage;
75  std::thread *m_thread;
76  bool m_collecting;
77  std::unordered_map<std::string, PerfMon *>
78  m_monitors;
79  std::condition_variable m_cv;
80  std::mutex m_mutex;
81 };
82 #endif
int getValues(InsertValues &values)
Return the performance values to insert.
Definition: perfmonitor.cpp:54
void addValue(long value)
Collect a new value for the performance monitor.
Definition: perfmonitor.cpp:29
Class to handle the performance monitors.
Definition: perfmonitors.h:35
PerfMon(const std::string &name)
Constructor for an individual performance monitor.
Definition: perfmonitor.cpp:20
Client for accessing the storage service.
Definition: storage_client.h:43
void collect(const std::string &name, long value)
Collect a performance monitor.
Definition: perfmonitors.h:60
Definition: perfmonitors.h:19
Definition: insert.h:145