Apollo 10.0
自动驾驶开放平台
perf_event_cache.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include <string>
20
22#include "cyber/common/log.h"
23#include "cyber/state.h"
24#include "cyber/time/time.h"
25
26namespace apollo {
27namespace cyber {
28namespace event {
29
30using common::GlobalData;
31using proto::PerfConf;
32using proto::PerfType;
33
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();
39 }
40
41 if (enable_) {
42 if (!event_queue_.Init(kEventQueueSize)) {
43 AERROR << "Event queue init failed.";
44 throw std::runtime_error("Event queue init failed.");
45 }
46 Start();
47 }
48}
49
51
53 if (!enable_) {
54 return;
55 }
56
57 shutdown_ = true;
58 event_queue_.BreakAllWait();
59 if (io_thread_.joinable()) {
60 io_thread_.join();
61 }
62
63 of_ << cyber::Time::Now().ToNanosecond() << std::endl;
64 of_.flush();
65 of_.close();
66}
67
69 const uint64_t cr_id, const int proc_id,
70 const int cr_state) {
71 if (!enable_) {
72 return;
73 }
74
75 if (perf_conf_.type() != PerfType::SCHED &&
76 perf_conf_.type() != PerfType::ALL) {
77 return;
78 }
79
80 EventBasePtr e = std::make_shared<SchedEvent>();
81 e->set_eid(static_cast<int>(event_id));
82 e->set_stamp(Time::Now().ToNanosecond());
83 e->set_cr_state(cr_state);
84 e->set_cr_id(cr_id);
85 e->set_proc_id(proc_id);
86
87 event_queue_.Enqueue(e);
88}
89
91 const uint64_t channel_id,
92 const uint64_t msg_seq,
93 const uint64_t stamp,
94 const std::string& adder) {
95 if (!enable_) {
96 return;
97 }
98
99 if (perf_conf_.type() != PerfType::TRANSPORT &&
100 perf_conf_.type() != PerfType::ALL) {
101 return;
102 }
103
104 EventBasePtr e = std::make_shared<TransportEvent>();
105 e->set_eid(static_cast<int>(event_id));
106 e->set_channel_id(channel_id);
107 e->set_msg_seq(msg_seq);
108 e->set_adder(adder);
109 auto t = stamp;
110 if (stamp == 0) {
111 t = Time::Now().ToNanosecond();
112 }
113 e->set_stamp(t);
114
115 event_queue_.Enqueue(e);
116}
117
118void PerfEventCache::Run() {
119 EventBasePtr event;
120 int buf_size = 0;
121 while (!shutdown_ && !apollo::cyber::IsShutdown()) {
122 if (event_queue_.WaitDequeue(&event)) {
123 of_ << event->SerializeToString() << std::endl;
124 buf_size++;
125 if (buf_size >= kFlushSize) {
126 of_.flush();
127 buf_size = 0;
128 }
129 }
130 }
131}
132
133void PerfEventCache::Start() {
134 auto now = Time::Now();
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;
140 of_ << Time::Now().ToNanosecond() << std::endl;
141 io_thread_ = std::thread(&PerfEventCache::Run, this);
142}
143
144} // namespace event
145} // namespace cyber
146} // namespace apollo
uint64_t ToNanosecond() const
convert time to nanosecond.
Definition time.cc:83
static Time Now()
get the current time.
Definition time.cc:57
bool Enqueue(const 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="-")
#define AERROR
Definition log.h:44
bool IsShutdown()
Definition state.h:46
class register implement
Definition arena_queue.h:37