Debian Codegraph @

src/atlas/runtime/trace/TraceT.h

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #pragma once
12
13 #include <iosfwd>
14 #include <string>
15 #include <vector>
16
17 #include "atlas/parallel/omp/omp.h"
18 #include "atlas/runtime/trace/CallStack.h"
19 #include "atlas/runtime/trace/CodeLocation.h"
20 #include "atlas/runtime/trace/Nesting.h"
21 #include "atlas/runtime/trace/StopWatch.h"
22 #include "atlas/runtime/trace/Timings.h"
23
24 //-----------------------------------------------------------------------------------------------------------
25
26 namespace eckit {
27 class Configuration;
28 } // namespace eckit
29
30 //-----------------------------------------------------------------------------------------------------------
31
32 namespace atlas {
33 namespace runtime {
34 namespace trace {
35
36 //-----------------------------------------------------------------------------------------------------------
37
38 template <typename TraceTraits>
39 class TraceT {
40 public:
41 using Traits = TraceTraits;
42 using Barriers = typename Traits::Barriers;
43 using Tracing = typename Traits::Tracing;
44 using Labels = std::vector<std::string>;
45
46 public: // static methods
47 static std::string report();
48 static std::string report(const eckit::Configuration& config);
49
50 public:
51 TraceT(const CodeLocation&);
52 TraceT(const CodeLocation&, const std::string& title);
53 TraceT(const CodeLocation&, const std::string& title, const Labels&);
54
55 ~TraceT();
56
57 bool running() const;
58
59 void start();
60
61 void stop();
62
63 void pause();
64
65 void resume();
66
67 double elapsed() const;
68
69 private: // types
70 using Identifier = Timings::Identifier;
71
72 private: // member functions
73 void barrier() const;
74
75 void updateTimings() const;
76
77 void registerTimer();
78
79 static std::string formatTitle(const std::string&);
80
81 private: // member data
82 bool running_{false};
83 StopWatch stopwatch_;
84 CodeLocation loc_;
85 std::string title_;
86 Identifier id_;
87 CallStack callstack_;
88 Labels labels_;
89 };
90
91 //-----------------------------------------------------------------------------------------------------------
92 // Definitions
93
94 template <typename TraceTraits>
95 inline std::string TraceT<TraceTraits>::formatTitle(const std::string& _title) {
96 std::string title =
97 _title + (Barriers::state() ? " [b]" : "") +
98 (atlas_omp_get_num_threads() > 1 ? " @thread[" + std::to_string(atlas_omp_get_thread_num()) + "]" : "");
99 return title;
100 }
101
102 template <typename TraceTraits>
103 inline TraceT<TraceTraits>::TraceT(const CodeLocation& loc, const std::string& title):
104 loc_(loc), title_(formatTitle(title)) {
105 start();
106 }
107
108 template <typename TraceTraits>
109 inline TraceT<TraceTraits>::TraceT(const CodeLocation& loc): loc_(loc), title_(loc_ ? loc_.func() : "") {
110 start();
111 }
112
113 template <typename TraceTraits>
114 inline TraceT<TraceTraits>::TraceT(const CodeLocation& loc, const std::string& title, const Labels& labels):
115 loc_(loc), title_(title), labels_(labels) {
116 start();
117 }
118
119 template <typename TraceTraits>
120 inline TraceT<TraceTraits>::~TraceT() {
121 stop();
122 }
123
124 template <typename TraceTraits>
125 inline void TraceT<TraceTraits>::barrier() const {
126 Barriers::execute();
127 }
128
129 template <typename TraceTraits>
130 inline void TraceT<TraceTraits>::registerTimer() {
131 std::string title =
132 title_ + (Barriers::state() ? " [b]" : "") +
133 (atlas_omp_get_num_threads() > 1 ? " @thread[" + std::to_string(atlas_omp_get_thread_num()) + "]" : "");
134 id_ = Timings::add(loc_, callstack_, title, labels_);
135 }
136
137 template <typename TraceTraits>
138 inline void TraceT<TraceTraits>::updateTimings() const {
139 Timings::update(id_, stopwatch_.elapsed());
140 }
141
142 template <typename TraceTraits>
143 inline bool TraceT<TraceTraits>::running() const {
144 return running_;
145 }
146
147 template <typename TraceTraits>
148 inline void TraceT<TraceTraits>::start() {
149 if (Control::enabled()) {
150 running_ = true;
151 if (not callstack_) {
152 callstack_ = CurrentCallStack::instance().push(loc_, title_);
153 }
154 registerTimer();
155 Tracing::start(title_);
156 barrier();
157 stopwatch_.start();
158 }
159 }
160
161 template <typename TraceTraits>
162 inline void TraceT<TraceTraits>::stop() {
163 if (running_) {
164 barrier();
165 stopwatch_.stop();
166 CurrentCallStack::instance().pop();
167 updateTimings();
168 Tracing::stop(title_, stopwatch_.elapsed());
169 running_ = false;
170 }
171 }
172
173 template <typename TraceTraits>
174 inline void TraceT<TraceTraits>::pause() {
175 if (running_) {
176 barrier();
177 stopwatch_.stop();
178 CurrentCallStack::instance().pop();
179 }
180 }
181
182 template <typename TraceTraits>
183 inline void TraceT<TraceTraits>::resume() {
184 if (running_) {
185 barrier();
186 CurrentCallStack::instance().push(loc_, title_);
187 stopwatch_.start();
188 }
189 }
190
191 template <typename TraceTraits>
192 inline double TraceT<TraceTraits>::elapsed() const {
193 return stopwatch_.elapsed();
194 }
195
196 template <typename TraceTraits>
197 inline std::string TraceT<TraceTraits>::report() {
198 return Timings::report() + Barriers::report();
199 }
200
201 template <typename TraceTraits>
202 inline std::string TraceT<TraceTraits>::report(const eckit::Configuration& config) {
203 return Timings::report(config) + Barriers::report();
204 }
205
206 //-----------------------------------------------------------------------------------------------------------
207
208 } // namespace trace
209 } // namespace runtime
210 } // namespace atlas