Galois
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ThreadTimer.h
Go to the documentation of this file.
1 #ifndef GALOIS_RUNTIME_THREADTIMER_H
2 #define GALOIS_RUNTIME_THREADTIMER_H
3 
4 #include <ctime>
5 
6 #include "galois/config.h"
8 
9 namespace galois::runtime {
10 
11 class ThreadTimer {
12  timespec start_;
13  timespec stop_;
14  uint64_t nsec_{0};
15 
16 public:
17  ThreadTimer() = default;
18 
19  void start() { clock_gettime(CLOCK_THREAD_CPUTIME_ID, &start_); }
20 
21  void stop() {
22  clock_gettime(CLOCK_THREAD_CPUTIME_ID, &stop_);
23  nsec_ += (stop_.tv_nsec - start_.tv_nsec);
24  nsec_ += ((stop_.tv_sec - start_.tv_sec) * 1000000000);
25  }
26 
27  uint64_t get_nsec() const { return nsec_; }
28 
29  uint64_t get_sec() const { return (nsec_ / 1000000000); }
30 
31  uint64_t get_msec() const { return (nsec_ / 1000000); }
32 };
33 
34 class ThreadTimers {
35 protected:
37 
38  void reportTimes(const char* category, const char* region);
39 };
40 
41 template <bool enabled>
42 class PerThreadTimer : private ThreadTimers {
43  const char* const region_;
44  const char* const category_;
45 
46  void reportTimes() { reportTimes(category_, region_); }
47 
48 public:
49  PerThreadTimer(const char* const region, const char* const category)
50  : region_(region), category_(category) {}
51 
52  PerThreadTimer(const PerThreadTimer&) = delete;
53  PerThreadTimer(PerThreadTimer&&) = delete;
54  PerThreadTimer& operator=(const PerThreadTimer&) = delete;
56 
57  ~PerThreadTimer() { reportTimes(); }
58 
59  void start() { timers_.getLocal()->start(); }
60 
61  void stop() { timers_.getLocal()->stop(); }
62 };
63 
64 template <>
65 class PerThreadTimer<false> {
66 
67 public:
68  PerThreadTimer(const char* const, const char* const) {}
69 
70  PerThreadTimer(const PerThreadTimer&) = delete;
71  PerThreadTimer(PerThreadTimer&&) = delete;
72  PerThreadTimer& operator=(const PerThreadTimer&) = delete;
74 
75  ~PerThreadTimer() = default;
76 
77  void start() const {}
78 
79  void stop() const {}
80 };
81 
82 } // end namespace galois::runtime
83 
84 #endif
substrate::PerThreadStorage< ThreadTimer > timers_
Definition: ThreadTimer.h:36
uint64_t get_msec() const
Definition: ThreadTimer.h:31
Definition: ThreadTimer.h:11
void stop()
Definition: ThreadTimer.h:61
void reportTimes(const char *category, const char *region)
Definition: ThreadTimer.cpp:8
Definition: ThreadTimer.h:42
void start()
Definition: ThreadTimer.h:19
void stop() const
Definition: ThreadTimer.h:79
uint64_t get_sec() const
Definition: ThreadTimer.h:29
void start()
Definition: ThreadTimer.h:59
uint64_t get_nsec() const
Definition: ThreadTimer.h:27
PerThreadTimer(const char *const region, const char *const category)
Definition: ThreadTimer.h:49
PerThreadTimer(const char *const, const char *const)
Definition: ThreadTimer.h:68
Definition: PerThreadStorage.h:88
void start() const
Definition: ThreadTimer.h:77
~PerThreadTimer()
Definition: ThreadTimer.h:57
Definition: ThreadTimer.h:34
void stop()
Definition: ThreadTimer.h:21
PerThreadTimer & operator=(const PerThreadTimer &)=delete