preCICE v3.2.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
18namespace precice::profiling {
19
21enum struct Mode {
22 All,
24 Off
25};
26
27enum struct EventClass : bool {
28 Normal = false,
29 Fundamental = true
30};
31
32inline EventClass toEventClass(bool isFundamental)
33{
34 return static_cast<EventClass>(isFundamental);
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(EventClass ec) const
130 {
132 }
133
135 inline bool parallel() const
136 {
137 return _size > 1;
138 }
139
141
142private:
145
147
150
152 int _rank = 0;
153
155 int _size = 1;
156
158 bool _firstwrite = true;
159
162
164 EventRegistry() = default;
165
167
170
172
173 bool _initialized = false;
174
175 bool _finalized = false;
176
177 bool _isBackendRunning = false;
178
180 Event::Clock::time_point _initClock;
181
183 std::chrono::system_clock::time_point _initTime;
184
186 void stopBackend();
187
189};
190
191} // namespace precice::profiling
std::string name
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.
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
bool _firstwrite
Indicator for the first record to be written.
void put(PendingEntry pe)
Records an event.
std::map< std::string, int, std::less<> > _nameDict
EventRegistry(EventRegistry &&)=delete
EventRegistry & operator=(EventRegistry &&)=delete
bool accepting(EventClass ec) const
Should an event of this class be forwarded to the registry?
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.
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 FundamentalTag Fundamental
Convenience instance of the FundamentalTag.
Definition Event.hpp:19
EventClass toEventClass(bool isFundamental)
Mode
The Mode of the Event utility.
DataEntry(int eid, Event::Clock::time_point c, int did, int dv)
static constexpr char type
static constexpr char type
static constexpr char type
static constexpr char type
An event that has been recorded and it waiting to be written to file.
TimedEntry(int eid, Event::Clock::time_point c)
Event::Clock::time_point clock