Fledge
An open source edge computing platform for industrial users
profile.h
1 #ifndef _PROFILE_H
2 #define _PROFILE_H
3 /*
4  * Fledge storage service.
5  *
6  * Copyright (c) 2018 OSisoft, LLC
7  *
8  * Released under the Apache 2.0 Licence
9  *
10  * Author: Mark Riddoch
11  */
12 #include <string>
13 #include <vector>
14 #include <sys/time.h>
15 #include <logger.h>
16 
17 #define TIME_BUCKETS 20
18 #define BUCKET_SIZE 5
20 {
21  public:
22  ProfileItem(const std::string& reference) : m_reference(reference)
23  { gettimeofday(&m_tvStart, NULL);
24  auto timenow = chrono::system_clock::to_time_t(chrono::system_clock::now());
25  m_ts = std::string(ctime(&timenow));
26  m_ts.back() = '\0'; };
27  ~ProfileItem() {};
28  void complete()
29  {
30  struct timeval tv;
31 
32  gettimeofday(&tv, NULL);
33  m_duration = (tv.tv_sec - m_tvStart.tv_sec) * 1000 +
34  (tv.tv_usec - m_tvStart.tv_usec) / 1000;
35  };
36  unsigned long getDuration() { return m_duration; };
37  const std::string& getReference() const { return m_reference; };
38  const std::string& getTs() const { return m_ts; };
39  private:
40  std::string m_reference;
41  struct timeval m_tvStart;
42  unsigned long m_duration;
43  std::string m_ts;
44 };
45 
47 {
48  public:
49  QueryProfile(int samples) : m_samples(samples) { time(&m_lastReport); };
50  void insert(ProfileItem *item)
51  {
52  int b = item->getDuration() / BUCKET_SIZE;
53  if (b >= TIME_BUCKETS)
54  b = TIME_BUCKETS - 1;
55  m_buckets[b]++;
56  if (m_items.size() == m_samples)
57  {
58  int minIndex = 0;
59  unsigned long minDuration = m_items[0]->getDuration();
60  for (int i = 1; i < m_items.size(); i++)
61  {
62  if (m_items[i]->getDuration() < minDuration)
63  {
64  minDuration = m_items[i]->getDuration();
65  minIndex = i;
66  }
67  }
68  if (item->getDuration() > minDuration)
69  {
70  delete m_items[minIndex];
71  m_items[minIndex] = item;
72  }
73  else
74  {
75  delete item;
76  }
77  }
78  else
79  {
80  m_items.push_back(item);
81  }
82  if (time(0) - m_lastReport > 600)
83  {
84  report();
85  }
86  };
87  private:
88  int m_samples;
89  std::vector<ProfileItem *> m_items;
90  time_t m_lastReport;
91  unsigned int m_buckets[TIME_BUCKETS];
92  void report()
93  {
94  Logger *logger = Logger::getLogger();
95  logger->info("Storage profile report");
96  logger->info(" < %3d mS %d", BUCKET_SIZE, m_buckets[0]);
97  for (int j = 1; j < TIME_BUCKETS - 1; j++)
98  {
99  logger->info("%3d-%3d mS %d",
100  j * BUCKET_SIZE, (j + 1) * BUCKET_SIZE,
101  m_buckets[j]);
102  }
103  logger->info(" > %3d mS %d", BUCKET_SIZE * TIME_BUCKETS, m_buckets[TIME_BUCKETS-1]);
104  for (int i = 0; i < m_items.size(); i++)
105  {
106  logger->info("%ld mS, %s, %s\n",
107  m_items[i]->getDuration(),
108  m_items[i]->getTs().c_str(),
109  m_items[i]->getReference().c_str());
110  }
111  time(&m_lastReport);
112  };
113 };
114 #endif
Fledge Logger class used to log to syslog.
Definition: logger.h:26
Definition: profile.h:46
Definition: profile.h:19