123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- #ifndef CNSTREAM_FRAMEWORK_CORE_INCLUDE_PROFILER_PROCESS_PROFILER_HPP_
- #define CNSTREAM_FRAMEWORK_CORE_INCLUDE_PROFILER_PROCESS_PROFILER_HPP_
- #include <algorithm>
- #include <string>
- #include <unordered_map>
- #include <vector>
- #include "cnstream_common.hpp"
- #include "cnstream_config.hpp"
- #include "profiler/pipeline_tracer.hpp"
- #include "profiler/profile.hpp"
- #include "profiler/stream_profiler.hpp"
- #include "profiler/trace.hpp"
- namespace cnstream {
- class RecordPolicy;
- class ProcessProfiler : private NonCopyable {
- public:
-
- explicit ProcessProfiler(const ProfilerConfig& config,
- const std::string& process_name,
- PipelineTracer* tracer);
-
- ~ProcessProfiler();
-
- ProcessProfiler& SetModuleName(const std::string& module_name);
-
- ProcessProfiler& SetTraceLevel(const TraceEvent::Level& level);
-
- void RecordStart(const RecordKey& key);
-
- void RecordEnd(const RecordKey& key);
-
- std::string GetName() const;
-
- ProcessProfile GetProfile();
-
- ProcessProfile GetProfile(const ProcessTrace& trace) const;
-
- void OnStreamEos(const std::string& stream_name);
- private:
-
- void RecordStart(const RecordKey& key, const Time& time);
-
- void RecordEnd(const RecordKey& key, const Time& time);
-
- void AddPhysicalTime(const Time& now);
-
- void AddLatency(const std::string& stream_name, const Duration& latency);
-
- void AddDropped(const std::string& stream_name, uint64_t dropped);
-
-
-
- void OnStreamStart(const std::string& stream_name);
-
- std::vector<StreamProfiler> GetStreamProfilers();
-
- void Tracing(const RecordKey& key, const Time& time, const TraceEvent::Type& type);
- private:
- ProfilerConfig config_;
- std::mutex lk_;
-
-
- uint64_t ongoing_ = 0;
-
- uint64_t dropped_ = 0;
-
- uint64_t completed_ = 0;
-
-
- uint64_t latency_add_times_ = 0;
-
- Time last_record_time_ = Time::min();
- Duration total_latency_ = Duration::zero();
- Duration maximum_latency_ = Duration::zero();
- Duration minimum_latency_ = Duration::max();
-
- Duration total_phy_time_ = Duration::zero();
- std::string module_name_ = "";
- std::string process_name_ = "";
- PipelineTracer* tracer_ = nullptr;
-
- RecordPolicy* record_policy_ = nullptr;
- TraceEvent::Level trace_level_;
-
- std::unordered_map<std::string, StreamProfiler> stream_profilers_;
- };
- inline ProcessProfiler& ProcessProfiler::SetModuleName(const std::string& module_name) {
- module_name_ = module_name;
- return *this;
- }
- inline ProcessProfiler& ProcessProfiler::SetTraceLevel(const TraceEvent::Level& level) {
- trace_level_ = level;
- return *this;
- }
- inline std::string ProcessProfiler::GetName() const {
- return process_name_;
- }
- inline void ProcessProfiler::AddLatency(const std::string& stream_name, const Duration& latency) {
- total_latency_ += latency;
- maximum_latency_ = std::max(latency, maximum_latency_);
- minimum_latency_ = std::min(latency, minimum_latency_);
- latency_add_times_++;
- stream_profilers_.find(stream_name)->second.AddLatency(latency);
- }
- inline void ProcessProfiler::AddDropped(const std::string& stream_name, uint64_t dropped) {
- dropped_ += dropped;
- stream_profilers_.find(stream_name)->second.AddDropped(dropped);
- }
- inline void ProcessProfiler::Tracing(const RecordKey& key, const Time& time, const TraceEvent::Type& type) {
- tracer_->RecordEvent(TraceEvent(key).SetModuleName(module_name_)
- .SetProcessName(process_name_)
- .SetLevel(trace_level_)
- .SetTime(time)
- .SetType(type));
- }
- }
- #endif
|