Apollo 10.0
自动驾驶开放平台
statistics.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2024 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
17#pragma once
18
19#ifndef CYBER_STATISTICS_STATISTICS_H_
20#define CYBER_STATISTICS_STATISTICS_H_
21
22#include <limits.h>
23
24#include <memory>
25#include <mutex>
26#include <utility>
27#include <string>
28#include <unordered_map>
29
30#include "cyber/base/macros.h"
31#include "cyber/common/log.h"
32#include "cyber/proto/role_attributes.pb.h"
33#include "cyber/common/macros.h"
34#include "cyber/time/time.h"
35#include "third_party/var/bvar/bvar.h"
36
37namespace apollo {
38namespace cyber {
39namespace statistics {
40
41using LatencyVarPtr = std::shared_ptr<::bvar::LatencyRecorder>;
42using StatusVarPtr = std::shared_ptr<::bvar::Status<uint64_t>>;
43using AdderVarPtr = std::shared_ptr<::bvar::Adder<int32_t>>;
44
46 std::string name;
47 uint64_t start_time;
48 uint64_t end_time;
49 std::shared_ptr<::bvar::LatencyRecorder> span_trace_node;
50 uint64_t min_ns = 0;
51};
52
53static const std::string TIMER_COMPONENT_CHAN_NAME = "_timer_component"; // NOLINT
54
56 public:
58 bool RegisterChanVar(const proto::RoleAttributes& role_attr);
59
60 inline bool CreateSpan(std::string name, uint64_t min_ns = 0);
61 inline bool StartSpan(std::string name);
62 inline bool EndSpan(std::string name);
63
64 inline void DisableChanVar() {
65 disable_chan_var_ = true;
66 }
67
68 template <typename SampleT>
69 std::shared_ptr<::bvar::Adder<SampleT>> CreateAdder(
70 const proto::RoleAttributes& role_attr) {
71 std::string expose_name =
72 role_attr.node_name() + "-" + role_attr.channel_name();
73 return std::make_shared<::bvar::Adder<SampleT>>(expose_name);
74 }
75
76 template <typename SampleT>
78 const proto::RoleAttributes& role_attr, SampleT sample) {
79 if (disable_chan_var_) {
80 return true;
81 }
82 auto var_ptr = GetChanProcVar(role_attr);
83 if (var_ptr != nullptr) {
84 (*var_ptr) << sample;
85 } else {
86 return false;
87 }
88 return true;
89 }
90
91 template <typename SampleT>
93 const proto::RoleAttributes& role_attr, SampleT sample) {
94 if (disable_chan_var_) {
95 return true;
96 }
97 if (role_attr.channel_name() == TIMER_COMPONENT_CHAN_NAME) {
98 return true;
99 }
100 auto var_ptr = GetChanTranVar(role_attr);
101 if (var_ptr != nullptr) {
102 (*var_ptr) << sample;
103 } else {
104 return false;
105 }
106 return true;
107 }
108
109 template <typename SampleT>
111 const proto::RoleAttributes& role_attr, SampleT sample) {
112 if (disable_chan_var_) {
113 return true;
114 }
115 if (role_attr.channel_name() == TIMER_COMPONENT_CHAN_NAME) {
116 return true;
117 }
118 auto var_ptr = GetChanCyberVar(role_attr);
119 if (var_ptr != nullptr) {
120 (*var_ptr) << sample;
121 } else {
122 return false;
123 }
124 return true;
125 }
126
127 bool SetProcStatus(const proto::RoleAttributes& role_attr, uint64_t val) {
128 if (disable_chan_var_) {
129 return true;
130 }
131 if (role_attr.channel_name() == TIMER_COMPONENT_CHAN_NAME) {
132 return true;
133 }
134 auto var_ptr = GetProcStatusVar(role_attr);
135 if (var_ptr != nullptr) {
136 var_ptr->set_value(val);
137 } else {
138 return false;
139 }
140 return true;
141 }
142
143 bool GetProcStatus(const proto::RoleAttributes& role_attr, uint64_t* val) {
144 if (disable_chan_var_) {
145 *val = 0;
146 return true;
147 }
148 if (role_attr.channel_name() == TIMER_COMPONENT_CHAN_NAME) {
149 return false;
150 }
151 auto var_ptr = GetProcStatusVar(role_attr);
152 if (var_ptr != nullptr) {
153 *val = var_ptr->get_value();
154 } else {
155 return false;
156 }
157 return true;
158 }
159
160 bool SetTotalMsgsStatus(const proto::RoleAttributes& role_attr, int32_t val) {
161 if (disable_chan_var_) {
162 return true;
163 }
164 if (role_attr.channel_name() == TIMER_COMPONENT_CHAN_NAME) {
165 return true;
166 }
167 auto var_ptr = GetTotalMsgsStatusVar(role_attr);
168 if (var_ptr != nullptr) {
169 var_ptr->set_value(val);
170 } else {
171 return false;
172 }
173 return true;
174 }
175
176 bool AddRecvCount(const proto::RoleAttributes& role_attr, int total_msg_val) {
177 if (disable_chan_var_) {
178 return true;
179 }
180 if (role_attr.channel_name() == TIMER_COMPONENT_CHAN_NAME) {
181 return true;
182 }
183
184 auto var_ptr = GetAdderVar(role_attr);
185 if (var_ptr == nullptr) {
186 return true;
187 }
188
189 if cyber_unlikely(first_recv_) {
190 (*var_ptr) << total_msg_val;
191 first_recv_ = false;
192 return true;
193 }
194
195 (*var_ptr) << 1;
196
197 return true;
198 }
199
200 private:
201 LatencyVarPtr GetChanProcVar(const proto::RoleAttributes& role_attr);
202
203 LatencyVarPtr GetChanTranVar(const proto::RoleAttributes& role_attr);
204
205 LatencyVarPtr GetChanCyberVar(const proto::RoleAttributes& role_attr);
206
207 StatusVarPtr GetProcStatusVar(const proto::RoleAttributes& role_attr);
208
209 AdderVarPtr GetAdderVar(const proto::RoleAttributes& role_attr);
210
211 StatusVarPtr GetTotalMsgsStatusVar(const proto::RoleAttributes& role_attr);
212
213 inline uint64_t GetMicroTimeNow() const noexcept;
214
215 inline const std::string GetProcLatencyKey(
216 const proto::RoleAttributes& role_attr) {
217 return role_attr.node_name() + "-" + role_attr.channel_name() + "proc";
218 }
219
220 inline const std::string GetTranLatencyKey(
221 const proto::RoleAttributes& role_attr) {
222 return role_attr.node_name() + "-" + role_attr.channel_name() + "tran";
223 }
224
225 inline const std::string GetCyberLatencyKey(
226 const proto::RoleAttributes& role_attr) {
227 return role_attr.node_name() + "-" + role_attr.channel_name() + "cyber";
228 }
229
230 inline const std::string GetStartProcessStatusKey(
231 const proto::RoleAttributes& role_attr) {
232 return role_attr.node_name() + "-" + \
233 role_attr.channel_name() + "process-status";
234 }
235
236 inline const std::string GetTotalMsgsStatusKey(
237 const proto::RoleAttributes& role_attr) {
238 return role_attr.node_name() + "-" + \
239 role_attr.channel_name() + "total-sended-msgs";
240 }
241
242 inline const std::string GetTotalRecvStatusKey(
243 const proto::RoleAttributes& role_attr) {
244 return role_attr.node_name() + "-" + role_attr.channel_name() + "recv-msgs";
245 }
246
247 std::unordered_map<std::string, LatencyVarPtr> latency_map_;
248 std::unordered_map<std::string, StatusVarPtr> status_map_;
249 std::unordered_map<std::string, AdderVarPtr> adder_map_;
250
251 std::unordered_map<std::string, std::shared_ptr<SpanHandler>> span_handlers_;
252
253 bool first_recv_ = true;
254 bool disable_chan_var_ = false;
255
256 DECLARE_SINGLETON(Statistics)
257};
258
259inline uint64_t Statistics::GetMicroTimeNow() const noexcept {
260 return Time::Now().ToMicrosecond();
261}
262
263inline bool Statistics::CreateSpan(std::string name, uint64_t min_ns) {
264 if (cyber_unlikely(span_handlers_.find(name) != span_handlers_.end())) {
265 AERROR << "span handler " << name << "has been created!";
266 return false;
267 }
268 auto handler = std::make_shared<SpanHandler>();
269 handler->name = name;
270 handler->span_trace_node = std::move(
271 std::make_shared<::bvar::LatencyRecorder>(name, "user", 1200));
272 handler->min_ns = min_ns;
273
274 span_handlers_[name] = std::move(handler);
275 return true;
276}
277
278inline bool Statistics::StartSpan(std::string name) {
279 auto it = span_handlers_.find(name);
280 if (cyber_unlikely(it == span_handlers_.end())) {
281 AERROR << "span handler " << name << "not found!";
282 return false;
283 }
284 it->second->start_time = std::move(GetMicroTimeNow());
285 return true;
286}
287
288inline bool Statistics::EndSpan(std::string name) {
289 auto it = span_handlers_.find(name);
290 if (cyber_unlikely(it == span_handlers_.end())) {
291 AERROR << "span handler " << name << "not found!";
292 return false;
293 }
294 it->second->end_time = std::move(GetMicroTimeNow());
295 auto handler = it->second;
296 auto diff = handler->end_time - handler->start_time;
297 if (cyber_unlikely(diff < handler->min_ns)) {
298 AWARN << "Time span less than preset value: " \
299 << diff << " vs " << handler->min_ns;
300 return false;
301 }
302 if (cyber_unlikely(diff > INT32_MAX)) {
303 AWARN << "Time span is larger than INT32_MAX: " << diff << ", drop it...";
304 return false;
305 }
306 *(handler->span_trace_node) << diff;
307 return true;
308}
309
310} // namespace statistics
311} // namespace cyber
312} // namespace apollo
313
314#endif // CYBER_STATISTICS_STATISTICS_H_
uint64_t ToMicrosecond() const
convert time to microsecond (us).
Definition time.cc:85
static Time Now()
get the current time.
Definition time.cc:57
bool SamplingTranLatency(const proto::RoleAttributes &role_attr, SampleT sample)
Definition statistics.h:92
bool SamplingCyberLatency(const proto::RoleAttributes &role_attr, SampleT sample)
Definition statistics.h:110
bool AddRecvCount(const proto::RoleAttributes &role_attr, int total_msg_val)
Definition statistics.h:176
bool SetTotalMsgsStatus(const proto::RoleAttributes &role_attr, int32_t val)
Definition statistics.h:160
bool CreateSpan(std::string name, uint64_t min_ns=0)
Definition statistics.h:263
bool SamplingProcLatency(const proto::RoleAttributes &role_attr, SampleT sample)
Definition statistics.h:77
bool RegisterChanVar(const proto::RoleAttributes &role_attr)
Definition statistics.cc:25
bool GetProcStatus(const proto::RoleAttributes &role_attr, uint64_t *val)
Definition statistics.h:143
std::shared_ptr<::bvar::Adder< SampleT > > CreateAdder(const proto::RoleAttributes &role_attr)
Definition statistics.h:69
bool StartSpan(std::string name)
Definition statistics.h:278
bool SetProcStatus(const proto::RoleAttributes &role_attr, uint64_t val)
Definition statistics.h:127
#define cyber_unlikely(x)
Definition macros.h:30
#define DECLARE_SINGLETON(classname)
Definition macros.h:52
#define AERROR
Definition log.h:44
#define AWARN
Definition log.h:43
std::shared_ptr<::bvar::LatencyRecorder > LatencyVarPtr
Definition statistics.h:41
std::shared_ptr<::bvar::Adder< int32_t > > AdderVarPtr
Definition statistics.h:43
std::shared_ptr<::bvar::Status< uint64_t > > StatusVarPtr
Definition statistics.h:42
class register implement
Definition arena_queue.h:37
Definition future.h:29
std::shared_ptr<::bvar::LatencyRecorder > span_trace_node
Definition statistics.h:49