preCICE v3.3.0
Loading...
Searching...
No Matches
EventUtils.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4#include <cstddef>
5#include <fstream>
6#include <iosfwd>
7#include <optional>
8#include <string>
9#include <string_view>
10#include <utility>
11#include <variant>
12#include <vector>
13
14#include "logging/Logger.hpp"
15#include "profiling/Event.hpp"
16#include "utils/assertion.hpp"
17
18#ifdef PRECICE_COMPRESSION
19#include <lzma.h>
20#endif
21
22namespace precice::profiling {
23
25enum struct Mode {
30};
31
32enum struct EventClass : bool {
33 Normal = false,
35};
36
38struct TimedEntry {
39 TimedEntry(int eid, Event::Clock::time_point c)
40 : eid(eid), clock(c) {}
41
42 int eid;
43 Event::Clock::time_point clock;
44};
45
48 static constexpr char type = 'b';
49};
50
53 static constexpr char type = 'e';
54};
55
57 DataEntry(int eid, Event::Clock::time_point c, int did, int dv)
58 : TimedEntry(eid, c), did(did), dvalue(dv) {}
59
60 static constexpr char type = 'd';
61 int did;
62 int dvalue;
63};
64
65struct NameEntry {
66 static constexpr char type = 'n';
68 int id;
69};
70
72
81public:
83
85 EventRegistry(EventRegistry const &) = delete;
89
91 static EventRegistry &instance();
92
94
99 void initialize(std::string_view applicationName, int rank = 0, int size = 1);
100
102 void setWriteQueueMax(std::size_t size);
103
105 void setDirectory(std::string_view directory);
106
108 void setMode(Mode mode);
109
111 void startBackend();
112
114 void finalize();
115
117 void clear();
118
120 void put(PendingEntry pe);
121
123 void putCritical(PendingEntry pe);
124
126 void flush();
127
129 inline bool accepting(Group g) const
130 {
131 switch (_mode) {
132 case Mode::Off:
133 return false;
134 case Mode::All:
135 return true;
137 return g == Group::Fundamental;
138 case Mode::API:
139 return (g == Group::Fundamental) || (g == Group::API);
140 }
141 PRECICE_UNREACHABLE("Unknown mode");
142 }
143
145 inline bool parallel() const
146 {
147 return _size > 1;
148 }
149
150 int nameToID(std::string_view name);
151
152private:
155
157
160
162 int _rank = 0;
163
165 int _size = 1;
166
167#ifdef PRECICE_COMPRESSION
168 lzma_stream _strm;
170#endif
171
174
177
179 EventRegistry() = default;
180
182
185
187
188 bool _initialized = false;
189
190 bool _finalized = false;
191
192 bool _isBackendRunning = false;
193
195 Event::Clock::time_point _initClock;
196
198 std::chrono::system_clock::time_point _initTime;
199
201 void stopBackend();
202
204};
205
206} // namespace precice::profiling
#define PRECICE_UNREACHABLE(...)
Definition assertion.hpp:93
This class provides a lightweight logger.
Definition Logger.hpp:17
static EventRegistry & instance()
Returns the only instance (singleton) of the EventRegistry class.
void startBackend()
Create the file and starts the filestream if profiling is turned on.
void initialize(std::string_view applicationName, int rank=0, int size=1)
Sets the global start time.
EventRegistry & operator=(EventRegistry const &)=delete
void clear()
Clears the registry.
void setWriteQueueMax(std::size_t size)
Sets the maximum size of the writequeue before calling flush(). Use 0 to flush on destruction.
void setDirectory(std::string_view directory)
Sets the directory where to write the event files to.
int nameToID(std::string_view name)
int _size
The amount of parallel instances of the current program.
std::vector< char > _inbuf
Buffer for the text to be written to file.
void finalize()
Sets the global end time and flushes buffers.
void setMode(Mode mode)
Sets the operational mode of the registry.
void stopBackend()
Stops the global event, flushes the buffers and closes the filestream.
Mode _mode
The operational mode of the registry.
std::vector< PendingEntry > _writeQueue
void put(PendingEntry pe)
Records an event.
std::map< std::string, int, std::less<> > _nameDict
EventRegistry(EventRegistry &&)=delete
int _rank
The rank/number of parallel instance of the current program.
EventRegistry & operator=(EventRegistry &&)=delete
Event::Clock::time_point _initClock
The initial time clock, used to take runtime measurements.
std::optional< int > _globalId
The id of the global event.
EventRegistry()=default
Private, empty constructor for singleton pattern.
bool accepting(Group g) const
Should an event of this class be forwarded to the registry?
std::string _applicationName
The name of the current participant.
void putCritical(PendingEntry pe)
Records an event without flushing events.
EventRegistry(EventRegistry const &)=delete
Deleted copy and move SMFs for singleton pattern.
void flush()
Writes all recorded events to file and flushes the buffer.
bool parallel() const
Is the solver running in parallel?
std::chrono::system_clock::time_point _initTime
The initial time, used to describe when the run started.
contains profiling utilities.
static constexpr Group API
Convenience instance of the Cat::API.
Definition Event.hpp:25
Mode
The Mode of the Event utility.
std::variant< StartEntry, StopEntry, DataEntry, NameEntry > PendingEntry
Group
Tag to annotate event group.
Definition Event.hpp:15
static constexpr Group Fundamental
Convenience instance of the Cat::Fundamental.
Definition Event.hpp:22
DataEntry(int eid, Event::Clock::time_point c, int did, int dv)
static constexpr char type
static constexpr char type
TimedEntry(int eid, Event::Clock::time_point c)
static constexpr char type
TimedEntry(int eid, Event::Clock::time_point c)
static constexpr char type
TimedEntry(int eid, Event::Clock::time_point c)
Event::Clock::time_point clock