P4C
The P4 Compiler
Loading...
Searching...
No Matches
event_logger.h
1
19#ifndef _BACKENDS_TOFINO_BF_P4C_LOGGING_EVENT_LOGGER_H_
20#define _BACKENDS_TOFINO_BF_P4C_LOGGING_EVENT_LOGGER_H_
21
22#include <ctime>
23#include <functional>
24#include <map>
25#include <string>
26#include <vector>
27
28#include "lib/error_message.h"
29#include "lib/log.h"
30
31namespace P4 {
32
33namespace IR {
34class Node; // Forward declare IR::Node for debug hook
35}
36
37namespace Util {
38class SourceInfo; // Forward declare Util::SourceInfo for errors/warnings
39}
40
41// Copy typedef of DebugHook so we don't have to depend on ir/ir.h
42typedef std::function<void(const char *, unsigned, const char *, const IR::Node *)> DebugHook;
43
44} // namespace P4
45
46using namespace P4;
47
57 protected:
58 const time_t BEGIN_TIME = std::time(nullptr);
59
60 bool enabled = false;
61 bool initialized = false;
62
63 // Used for deduplicating passChange messages
64 std::string lastManager = "";
65 std::string lastPass = "";
66 unsigned lastSeq = 0;
67
68 // Used to form JSON mapping of names to ids and back
69 // Used from pass change event
70 int managerId = 0;
71 std::map<std::string, int> managerNameToIds;
72 std::vector<std::string> managerNames;
73
74 // Used to form JSON mapping of filenames to ids and back
75 // Used from debug and decision events
76 int fileId = 0;
77 std::map<std::string, int> fileNameToIds;
78 std::vector<std::string> fileNames;
79
81 EventLogger(EventLogger &&other) = delete;
82 EventLogger(const EventLogger &other) = delete;
83 EventLogger &operator=(const EventLogger &other) = delete;
85
86 void nullInit();
87
94 int getManagerId(const std::string &name);
95
102 int getFileNameId(const std::string &name);
103
108 virtual int getTimeDifference() const;
109
114 virtual std::string getStartTimestamp() const;
115
120 virtual std::ostream &getDebugStream(unsigned level, const std::string &file) const;
121
127 template <typename T>
128 void logSink(const T *obj);
129
134 void logProperties();
135
139 void passChange(const std::string &manager, const std::string &pass, unsigned seq);
140
141 public:
142 enum class AllocPhase { PhvAllocation, TablePlacement };
143
144 static EventLogger &get();
145
154 void init(const std::string &outdir, const std::string &filename);
155
163 void enable() { enabled = true; }
164
168 void deinit();
169
174 void parserError(const P4::ParserErrorMessage &msg);
175
180 void error(const P4::ErrorMessage &msg);
181
186 void warning(const P4::ErrorMessage &msg);
187
191 void debug(unsigned level, const std::string &file, const std::string &message);
192
196 void decision(unsigned level, const std::string &file, const std::string &description,
197 const std::string &what, const std::string &why);
198
203 void iterationChange(unsigned iteration, AllocPhase phase);
204
208 void pipeChange(int pipeId);
209
214 return [](const char *m, unsigned s, const char *p, const IR::Node *) {
215 get().passChange(m, p, s);
216 };
217 }
218};
219
220#define TAB1 " "_cs
221#define TAB2 TAB1 TAB1
222#define TAB3 TAB1 TAB2
223#define TAB4 TAB2 TAB2
224#define TAB5 TAB1 TAB4
225#define TAB6 TAB3 TAB3
226#define TAB7 TAB1 TAB6
227
236#define LOG_DEBUG(level, message) \
237 if (LOGGING(level)) { \
238 std::stringstream out; \
239 out << message; \
240 EventLogger::get().debug(level, __FILE__, out.str()); \
241 }
242#define LOG_DEBUG1(message) LOG_DEBUG(1, message)
243#define LOG_DEBUG2(message) LOG_DEBUG(2, message)
244#define LOG_DEBUG3(message) LOG_DEBUG(3, message)
245#define LOG_DEBUG4(message) LOG_DEBUG(4, message)
246#define LOG_DEBUG5(message) LOG_DEBUG(5, message)
247#define LOG_DEBUG6(message) LOG_DEBUG(6, message)
248#define LOG_DEBUG7(message) LOG_DEBUG(7, message)
249#define LOG_DEBUG8(message) LOG_DEBUG(8, message)
250#define LOG_DEBUG9(message) LOG_DEBUG(9, message)
251
256#define LOG_DECISION(level, description, chosen, why) \
257 EventLogger::get().decision(level, __FILE__, description, chosen, why);
258#define LOG_DECISION1(description, chosen, why) LOG_DECISION(1, description, chosen, why);
259#define LOG_DECISION2(description, chosen, why) LOG_DECISION(2, description, chosen, why);
260#define LOG_DECISION3(description, chosen, why) LOG_DECISION(3, description, chosen, why);
261#define LOG_DECISION4(description, chosen, why) LOG_DECISION(4, description, chosen, why);
262#define LOG_DECISION5(description, chosen, why) LOG_DECISION(5, description, chosen, why);
263#define LOG_DECISION6(description, chosen, why) LOG_DECISION(6, description, chosen, why);
264#define LOG_DECISION7(description, chosen, why) LOG_DECISION(7, description, chosen, why);
265#define LOG_DECISION8(description, chosen, why) LOG_DECISION(8, description, chosen, why);
266#define LOG_DECISION9(description, chosen, why) LOG_DECISION(9, description, chosen, why);
267
268#endif /* _BACKENDS_TOFINO_BF_P4C_LOGGING_EVENT_LOGGER_H_ */
Definition event_logger.h:56
void decision(unsigned level, const std::string &file, const std::string &description, const std::string &what, const std::string &why)
Definition logging/event_logger.cpp:276
void logSink(const T *obj)
Definition logging/event_logger.cpp:135
void debug(unsigned level, const std::string &file, const std::string &message)
Definition logging/event_logger.cpp:265
void parserError(const P4::ParserErrorMessage &msg)
Definition logging/event_logger.cpp:233
int getManagerId(const std::string &name)
Definition logging/event_logger.cpp:117
void iterationChange(unsigned iteration, AllocPhase phase)
Definition logging/event_logger.cpp:291
void init(const std::string &outdir, const std::string &filename)
Definition logging/event_logger.cpp:151
int getFileNameId(const std::string &name)
Definition logging/event_logger.cpp:121
virtual std::ostream & getDebugStream(unsigned level, const std::string &file) const
Definition logging/event_logger.cpp:129
void pipeChange(int pipeId)
Definition logging/event_logger.cpp:300
void passChange(const std::string &manager, const std::string &pass, unsigned seq)
Definition logging/event_logger.cpp:218
void logProperties()
Definition logging/event_logger.cpp:182
void error(const P4::ErrorMessage &msg)
Definition logging/event_logger.cpp:243
void enable()
Definition event_logger.h:163
void deinit()
Definition logging/event_logger.cpp:194
void warning(const P4::ErrorMessage &msg)
Definition logging/event_logger.cpp:254
virtual int getTimeDifference() const
Definition logging/event_logger.cpp:125
static P4::DebugHook getDebugHook()
Definition event_logger.h:213
virtual std::string getStartTimestamp() const
Definition logging/event_logger.cpp:127
Definition node.h:95
Definition table_placement.h:51
The namespace encapsulating IR node classes.
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
std::function< void(const char *manager, unsigned seqNo, const char *pass, const IR::Node *node)> DebugHook
Definition ir/pass_manager.h:38
Definition error_message.h:38
Definition error_message.h:70