DASH  0.3.0
Trace.h
1 #ifndef DASH__UTIL__TRACE_H__
2 #define DASH__UTIL__TRACE_H__
3 
4 #include <dash/Init.h>
5 #include <dash/util/Timer.h>
6 #include <iostream>
7 #include <fstream>
8 #include <sstream>
9 #include <iomanip>
10 #include <string>
11 #include <utility>
12 #include <vector>
13 
14 namespace dash {
15 namespace util {
16 
18 {
19 public:
20  typedef std::string
21  state_t;
23  timer_t;
24  typedef typename timer_t::timestamp_t
25  timestamp_t;
26  typedef struct {
27  timestamp_t start{};
28  timestamp_t end{};
29  state_t state;
31  typedef std::vector<state_timespan_t>
32  trace_events_t;
33 
34 public:
41  static bool on();
42 
46  static void off();
47 
51  static bool enabled();
52 
56  static void clear();
57 
61  static void clear(const std::string & context);
62 
66  static void add_context(const std::string & context);
67 
71  static trace_events_t & context_trace(const std::string & context);
72 
76  static void write(std::ostream & out, bool printHeader = true);
77 
81  static void write(
82  const std::string & filename,
83  const std::string & path = "");
84 
85 private:
86  static std::map<std::string, trace_events_t> _traces;
87  static bool _trace_enabled;
88 };
89 
90 class Trace
91 {
92 private:
93  typedef typename TraceStore::timestamp_t
94  timestamp_t;
95  typedef typename TraceStore::state_t
96  state_t;
97  typedef typename TraceStore::timer_t
98  timer_t;
99  typedef typename TraceStore::state_timespan_t
101  typedef typename TraceStore::trace_events_t
102  trace_events_t;
103 
104 private:
105  std::string _context;
106  timestamp_t _ts_start;
107 
108 public:
109  Trace() : Trace("global")
110  { }
111 
112  Trace(std::string context)
113  : _context(std::move(context))
114  {
115  if (!TraceStore::enabled()) {
116  return;
117  }
118  TraceStore::add_context(_context);
119  timer_t::Calibrate(0);
120  dash::barrier();
121  _ts_start = timer_t::Now();
122  }
123 
124  inline void enter_state(const state_t & state)
125  {
126  if (!TraceStore::enabled()) {
127  return;
128  }
129  state_timespan_t state_timespan;
130  timestamp_t ts_event = timer_t::Now() - _ts_start;
131  state_timespan.start = ts_event;
132  state_timespan.end = ts_event;
133  state_timespan.state = state;
134  TraceStore::context_trace(_context).push_back(state_timespan);
135  }
136 
137  inline void exit_state(const state_t &)
138  {
139  if (!TraceStore::enabled()) {
140  return;
141  }
142  timestamp_t ts_event = timer_t::Now() - _ts_start;
143  TraceStore::context_trace(_context).back().end = ts_event;
144  }
145 
146 };
147 
148 } // namspace util
149 } // namespace dash
150 
151 #endif // DASH__UTIL__TRACE_H__
constexpr auto end(RangeType &&range) -> decltype(std::forward< RangeType >(range).end())
Definition: Range.h:98
This class is a simple memory pool which holds allocates elements of size ValueType.
Definition: AllOf.h:8
static void off()
Disable trace storage.
static void add_context(const std::string &context)
Register a new trace context.
static bool enabled()
Whether trace storage is enabled.
static void write(std::ostream &out, bool printHeader=true)
Write trace data to given output stream.
static void clear()
Clear trace data.
static bool on()
Enable trace storage if environment variable DASH_ENABLE_TRACE is set to &#39;on&#39;.
static trace_events_t & context_trace(const std::string &context)
Return reference to traces list for given context.
void barrier()
A global barrier involving all units.