30using common::GlobalData;
34PerfEventCache::PerfEventCache() {
35 auto& global_conf = GlobalData::Instance()->Config();
36 if (global_conf.has_perf_conf()) {
37 perf_conf_.CopyFrom(global_conf.perf_conf());
38 enable_ = perf_conf_.
enable();
42 if (!event_queue_.
Init(kEventQueueSize)) {
43 AERROR <<
"Event queue init failed.";
44 throw std::runtime_error(
"Event queue init failed.");
59 if (io_thread_.joinable()) {
69 const uint64_t cr_id,
const int proc_id,
75 if (perf_conf_.
type() != PerfType::SCHED &&
76 perf_conf_.
type() != PerfType::ALL) {
81 e->set_eid(
static_cast<int>(event_id));
83 e->set_cr_state(cr_state);
85 e->set_proc_id(proc_id);
91 const uint64_t channel_id,
92 const uint64_t msg_seq,
94 const std::string& adder) {
99 if (perf_conf_.
type() != PerfType::TRANSPORT &&
100 perf_conf_.
type() != PerfType::ALL) {
105 e->set_eid(
static_cast<int>(event_id));
106 e->set_channel_id(channel_id);
107 e->set_msg_seq(msg_seq);
118void PerfEventCache::Run() {
123 of_ <<
event->SerializeToString() << std::endl;
125 if (buf_size >= kFlushSize) {
133void PerfEventCache::Start() {
135 std::string perf_file =
"cyber_perf_" + now.ToString() +
".data";
136 std::replace(perf_file.begin(), perf_file.end(),
' ',
'_');
137 std::replace(perf_file.begin(), perf_file.end(),
':',
'-');
138 of_.open(perf_file, std::ios::trunc);
139 perf_file_ = perf_file;
141 io_thread_ = std::thread(&PerfEventCache::Run,
this);
uint64_t ToNanosecond() const
convert time to nanosecond.
static Time Now()
get the current time.
bool Enqueue(const T &element)
bool WaitDequeue(T *element)
void AddSchedEvent(const SchedPerf event_id, const uint64_t cr_id, const int proc_id, const int cr_state=-1)
std::shared_ptr< EventBase > EventBasePtr
void AddTransportEvent(const TransPerf event_id, const uint64_t channel_id, const uint64_t msg_seq, const uint64_t stamp=0, const std::string &adder="-")