Data_deduplication_service
Service that will use hashes to deduplicate files
clockArray.h
Go to the documentation of this file.
1 #ifndef DATA_DEDUPLICATION_SERVICE_CLOCKARRAY_H
2 #define DATA_DEDUPLICATION_SERVICE_CLOCKARRAY_H
3 
4 #include <array>
5 #include <numeric>
6 #include <iostream>
7 #include <chrono>
8 #include <map>
9 #include <stack>
10 #include <source_location>
11 #include <cassert>
12 #include <utility>
13 #include <thread>
14 #include <sstream>
15 
16 template<typename T, size_t sz>
17 requires std::is_floating_point_v<T> or std::is_integral_v<T>
18 std::ostream &operator<<(std::ostream &out, std::array<T, sz> &arr);
19 
20 
21 template<typename T, size_t sz>
22 bool operator==(const std::array<T, sz> &arr1, const std::array<T, sz> &arr2) {
23  return std::equal(arr1.begin(), arr1.end(), arr2);
24 }
26 namespace timing {
27  using timepointType = std::chrono::system_clock::time_point;
28  using locationType = std::array<std::string, 5>;
29 
33  struct cmpArr {
34  /* Comparator for arrays
35  * @tparam N
36  * @param a
37  * @param b
38  * @return
39  */
40  template<size_t N>
41  bool operator()(const std::array<std::string, N> &a, const std::array<std::string, N> &b) const {
42  for (int i = 0; i < N; i++) {
43  if (a[i] != b[i])
44  return b[i] > a[i];
45  }
46  return false;
47  }
48  };
49 
50  template<typename OutType, typename inType, inType(*timeGetter)(), locationType (*sourceTypeConverter)(
51  std::source_location location),
52  OutType(*timeConverter)(inType curr, inType prev)> requires std::is_floating_point_v<OutType>
53  or std::is_integral_v<OutType>
54  class clockArray;
55 
56 
61  locationType getFileState(std::source_location location
62  = std::source_location::current());
63 
64 
73  template<typename to_dur = std::milli, typename doubleType>
74  doubleType doubleCastChrono(timepointType curr, timepointType prev) {
75  std::chrono::duration<doubleType, to_dur> ret =
76  std::chrono::duration_cast<std::chrono::duration<doubleType, to_dur>>(curr - prev);
77  return ret.count();
78  }
79 
80 
84  template<typename chrono_duration_type=std::milli>
86  &std::chrono::high_resolution_clock::now,
87  &getFileState,
88  &doubleCastChrono<chrono_duration_type>>;
89 
90 
99  template<typename OutType, typename inType, inType(*timeGetter)(), locationType (*sourceTypeConverter)(
100  std::source_location location),
101  OutType(*timeConverter)(inType curr, inType prev)> requires std::is_floating_point_v<OutType>
102  or std::is_integral_v<OutType>
103  class clockArray {
104  public:
105  struct timeStore {
106  OutType time;
107  size_t count;
108 
109  friend std::ostream &operator<<(std::ostream &out, const timeStore &ts) {
110  out << ts.time;
111  return out;
112  }
113  };
114 
118  void reset();
119 
120 
127  void tik(const std::source_location &location
128  = std::source_location::current());
129 
130 
135  void tak(const std::source_location &location
136  = std::source_location::current());
137 
152  locationType tikLoc(const std::source_location &location
153  = std::source_location::current()) {
154  tik(location);
155  return sourceTypeConverter(location);
156  }
157 
162  std::pair<std::source_location, locationType> tikPair(const std::source_location &location
163  = std::source_location::current()) {
164  return std::make_pair(location, tikLoc(location));
165  }
166 
167  decltype(auto) begin() const {
168  return timers.begin();
169  }
170 
171  decltype(auto) end() const {
172  return timers.end();
173  }
174 
175  auto cbegin() const {
176  return timers.cbegin();
177  }
178 
179  auto cend() const {
180  return timers.cend();
181  }
182 
183  friend std::ostream &
184  operator<<(std::ostream &out,
186  out << "Function name\tThread\tLine\tTime\n";
187  for (auto &val: ts)
188  out << val.first[0] << '\t' << val.first[4] << '\t'
189  << val.first[3] << ":" << val.first[1]
190  << '\t' << val.second << '\n';
191  return out;
192  }
193 
194 
195  auto &operator[](const locationType &loc) {
196  return timers[loc];
197  }
198 
199  [[nodiscard]] bool contains(const locationType &loc) const {
200  return timers.contains(loc);
201  }
202 
203  private:
204  std::map<locationType, timeStore, cmpArr> timers;
205  std::map<locationType, inType, cmpArr> startIngTimers;
206  std::stack<locationType> toTak;
207 
208  static inline std::mutex s_mutex;
209  using guardType = std::lock_guard<std::mutex>;
210  };
211 
212 }
213 
214 
215 template<typename T, size_t sz>
216 requires std::is_convertible_v<T, std::string>
217 std::ostream &operator<<(std::ostream &out, std::array<T, sz> &arr) {
218  int i = 0;
219  for (; i < sz - 1; ++i) {
220  out << arr[i] << '\t';
221  }
222  out << arr[i] << '\n';
223  return out;
224 }
225 
226 
227 namespace timing {
228  template<typename OutType, typename inType, inType (*timeGetter)(), locationType (*sourceTypeConverter)(
229  std::source_location), OutType (*timeConverter)(inType, inType)>
230  requires std::is_floating_point_v<OutType> or std::is_integral_v<OutType>void
232  const std::source_location &location) {
233  const guardType guard{s_mutex};
234  auto id = (*sourceTypeConverter)(location);
235  if (toTak.empty() || toTak.top()[0] != id[0]) {
236  std::string msg = "No paired tik statement found in queue\t"
237  "Tak values" + id[3] + ":" + id[1] + "\t"
238  + id[0] + '\t' + id[2];
239  throw std::logic_error(msg);
240  }
241  id[1] = toTak.top()[1];
242  id[2] = toTak.top()[2];
243  toTak.pop();
244  auto res = timeConverter((*timeGetter)(), startIngTimers[id]);
245  if (!timers.contains(id)) {
246  timers[id] = {res, 1};
247  } else {
248  timers[id].time += res;
249  timers[id].count++;
250  }
251  }
252 
253  template<typename OutType, typename inType, inType (*timeGetter)(), locationType (*sourceTypeConverter)(
254  std::source_location), OutType (*timeConverter)(inType, inType)>
255  requires std::is_floating_point_v<OutType> or std::is_integral_v<OutType>void
257  const std::source_location &location) {
258  const guardType guard{s_mutex};
259  auto id = sourceTypeConverter(location);
260  startIngTimers[id] = timeGetter();
261 
262  toTak.push(id);
263  }
264 
265  template<typename OutType, typename inType, inType (*timeGetter)(), locationType (*sourceTypeConverter)(
266  std::source_location), OutType (*timeConverter)(inType, inType)>
267  requires std::is_floating_point_v<OutType> or std::is_integral_v<OutType>void
269  assert(toTak.empty());
270  this->timers.clear();
271  this->startIngTimers.clear();
272  }
273 }
274 
275 
276 #endif // DATA_DEDUPLICATION_SERVICE_CLOCKARRAY_H
Clock array template class.
Definition: clockArray.h:103
auto cbegin() const
Definition: clockArray.h:175
auto cend() const
Definition: clockArray.h:179
locationType tikLoc(const std::source_location &location=std::source_location::current())
Definition: clockArray.h:152
bool contains(const locationType &loc) const
Definition: clockArray.h:199
std::pair< std::source_location, locationType > tikPair(const std::source_location &location=std::source_location::current())
Definition: clockArray.h:162
auto & operator[](const locationType &loc)
Definition: clockArray.h:195
bool operator==(const std::array< T, sz > &arr1, const std::array< T, sz > &arr2)
Definition: clockArray.h:22
requires std::is_floating_point_v< T > or std::is_integral_v< T > std::ostream & operator<<(std::ostream &out, std::array< T, sz > &arr)
Definition: clockArray.h:217
timing namespace
Definition: clockArray.h:26
std::array< std::string, 5 > locationType
Definition: clockArray.h:28
locationType getFileState(std::source_location location=std::source_location::current())
Definition: clockArray.cpp:5
std::chrono::system_clock::time_point timepointType
Definition: clockArray.h:27
doubleType doubleCastChrono(timepointType curr, timepointType prev)
Definition: clockArray.h:74
Definition: clockArray.h:105
friend std::ostream & operator<<(std::ostream &out, const timeStore &ts)
Definition: clockArray.h:109
size_t count
Definition: clockArray.h:107
OutType time
Definition: clockArray.h:106
Comparator functor arrays.
Definition: clockArray.h:33
bool operator()(const std::array< std::string, N > &a, const std::array< std::string, N > &b) const
Definition: clockArray.h:41