1 #ifndef DATA_DEDUPLICATION_SERVICE_CLOCKARRAY_H
2 #define DATA_DEDUPLICATION_SERVICE_CLOCKARRAY_H
10 #include <source_location>
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);
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);
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++) {
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>
62 = std::source_location::current());
73 template<
typename to_dur = std::milli,
typename doubleType>
75 std::chrono::duration<doubleType, to_dur> ret =
76 std::chrono::duration_cast<std::chrono::duration<doubleType, to_dur>>(curr - prev);
84 template<
typename chrono_duration_type=std::milli>
86 &std::chrono::high_resolution_clock::now,
88 &doubleCastChrono<chrono_duration_type>>;
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>
127 void tik(
const std::source_location &location
128 = std::source_location::current());
135 void tak(
const std::source_location &location
136 = std::source_location::current());
153 = std::source_location::current()) {
155 return sourceTypeConverter(location);
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));
167 decltype(
auto) begin()
const {
168 return timers.begin();
171 decltype(
auto) end()
const {
176 return timers.cbegin();
180 return timers.cend();
183 friend std::ostream &
186 out <<
"Function name\tThread\tLine\tTime\n";
188 out << val.first[0] <<
'\t' << val.first[4] <<
'\t'
189 << val.first[3] <<
":" << val.first[1]
190 <<
'\t' << val.second <<
'\n';
200 return timers.contains(loc);
204 std::map<locationType, timeStore, cmpArr> timers;
205 std::map<locationType, inType, cmpArr> startIngTimers;
206 std::stack<locationType> toTak;
208 static inline std::mutex s_mutex;
209 using guardType = std::lock_guard<std::mutex>;
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) {
219 for (; i < sz - 1; ++i) {
220 out << arr[i] <<
'\t';
222 out << arr[i] <<
'\n';
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);
241 id[1] = toTak.top()[1];
242 id[2] = toTak.top()[2];
244 auto res = timeConverter((*timeGetter)(), startIngTimers[
id]);
245 if (!timers.contains(
id)) {
246 timers[id] = {res, 1};
248 timers[id].time += res;
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();
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();
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