123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- #ifndef CNSTREAM_FRAMEWORK_CORE_INCLUDE_PROFILER_TRACE_HPP_
- #define CNSTREAM_FRAMEWORK_CORE_INCLUDE_PROFILER_TRACE_HPP_
- #include <chrono>
- #include <string>
- #include <unordered_map>
- #include <utility>
- #include <vector>
- namespace cnstream {
- using Clock = std::chrono::steady_clock;
- using Duration = std::chrono::duration<double, std::milli>;
- using Time = Clock::time_point;
- using RecordKey = std::pair<std::string, int64_t>;
- class TraceEvent {
- public:
- RecordKey key;
- std::string module_name;
- std::string process_name;
- Time time;
-
- enum class Level {
- PIPELINE = 0,
- MODULE
- } level = Level::PIPELINE;
-
- enum class Type {
- START = 1 << 0,
- END = 1 << 1
- } type = Type::START;
-
- TraceEvent() = default;
-
- explicit TraceEvent(const RecordKey& key);
-
- explicit TraceEvent(RecordKey&& key);
-
- TraceEvent(const TraceEvent& other) = default;
-
- TraceEvent& operator=(const TraceEvent& other) = default;
-
- TraceEvent(TraceEvent&& other);
-
- TraceEvent& operator=(TraceEvent&& other);
-
- TraceEvent& SetKey(const RecordKey& key);
-
- TraceEvent& SetKey(RecordKey&& key);
-
- TraceEvent& SetModuleName(const std::string& module_name);
-
- TraceEvent& SetModuleName(std::string&& module_name);
-
- TraceEvent& SetProcessName(const std::string& process_name);
-
- TraceEvent& SetProcessName(std::string&& process_name);
-
- TraceEvent& SetTime(const Time& time);
-
- TraceEvent& SetTime(Time&& time);
-
- TraceEvent& SetLevel(const Level& level);
-
- TraceEvent& SetType(const Type& type);
- };
- struct TraceElem {
- RecordKey key;
- Time time;
- TraceEvent::Type type;
-
- TraceElem() = default;
-
- TraceElem(const TraceElem& other) = default;
-
- TraceElem& operator=(const TraceElem& other) = default;
-
- TraceElem(TraceElem&& other);
-
- TraceElem& operator=(TraceElem&& other);
-
- explicit TraceElem(const TraceEvent& event);
-
- explicit TraceElem(TraceEvent&& event);
- };
- using ProcessTrace = std::vector<TraceElem>;
- using ModuleTrace = std::unordered_map<std::string, ProcessTrace>;
- struct PipelineTrace {
- std::unordered_map<std::string, ProcessTrace> process_traces;
- std::unordered_map<std::string, ModuleTrace> module_traces;
-
- PipelineTrace() = default;
-
- PipelineTrace(const PipelineTrace& other) = default;
-
- PipelineTrace& operator=(const PipelineTrace& other) = default;
-
- PipelineTrace(PipelineTrace&& other);
-
- PipelineTrace& operator=(PipelineTrace&& other);
- };
- inline TraceEvent::TraceEvent(const RecordKey& key) : key(key) {}
- inline TraceEvent::TraceEvent(RecordKey&& key) : key(std::forward<RecordKey>(key)) {}
- inline TraceEvent::TraceEvent(TraceEvent&& other) { *this = std::forward<TraceEvent>(other); }
- inline TraceEvent& TraceEvent::operator=(TraceEvent&& other) {
- key = std::move(other.key);
- module_name = std::move(other.module_name);
- process_name = std::move(other.process_name);
- time = std::move(other.time);
- level = other.level;
- type = other.type;
- return *this;
- }
- inline TraceEvent& TraceEvent::SetKey(const RecordKey& key) {
- this->key = key;
- return *this;
- }
- inline TraceEvent& TraceEvent::SetKey(RecordKey&& key) {
- this->key = std::forward<RecordKey>(key);
- return *this;
- }
- inline TraceEvent& TraceEvent::SetModuleName(const std::string& module_name) {
- this->module_name = module_name;
- return *this;
- }
- inline TraceEvent& TraceEvent::SetModuleName(std::string&& module_name) {
- this->module_name = std::forward<std::string>(module_name);
- return *this;
- }
- inline TraceEvent& TraceEvent::SetProcessName(const std::string& process_name) {
- this->process_name = process_name;
- return *this;
- }
- inline TraceEvent& TraceEvent::SetProcessName(std::string&& process_name) {
- this->process_name = std::forward<std::string>(process_name);
- return *this;
- }
- inline TraceEvent& TraceEvent::SetTime(const Time& time) {
- this->time = time;
- return *this;
- }
- inline TraceEvent& TraceEvent::SetTime(Time&& time) {
- this->time = std::forward<Time>(time);
- return *this;
- }
- inline TraceEvent& TraceEvent::SetLevel(const Level& level) {
- this->level = level;
- return *this;
- }
- inline TraceEvent& TraceEvent::SetType(const Type& type) {
- this->type = type;
- return *this;
- }
- inline TraceElem::TraceElem(TraceElem&& other) { *this = std::forward<TraceElem>(other); }
- inline TraceElem& TraceElem::operator=(TraceElem&& other) {
- key = std::move(other.key);
- time = std::move(other.time);
- type = other.type;
- return *this;
- }
- inline TraceElem::TraceElem(const TraceEvent& event) {
- key = event.key;
- time = event.time;
- type = event.type;
- }
- inline TraceElem::TraceElem(TraceEvent&& event) {
- key = std::move(event.key);
- time = std::move(event.time);
- type = event.type;
- }
- inline PipelineTrace::PipelineTrace(PipelineTrace&& other) { *this = std::forward<PipelineTrace>(other); }
- inline PipelineTrace& PipelineTrace::operator=(PipelineTrace&& other) {
- process_traces = std::move(other.process_traces);
- module_traces = std::move(other.module_traces);
- return *this;
- }
- }
- #endif
|