19#ifndef CYBER_STATISTICS_STATISTICS_H_
20#define CYBER_STATISTICS_STATISTICS_H_
28#include <unordered_map>
32#include "cyber/proto/role_attributes.pb.h"
35#include "third_party/var/bvar/bvar.h"
53static const std::string TIMER_COMPONENT_CHAN_NAME =
"_timer_component";
60 inline bool CreateSpan(std::string name, uint64_t min_ns = 0);
62 inline bool EndSpan(std::string name);
65 disable_chan_var_ =
true;
68 template <
typename SampleT>
71 std::string expose_name =
73 return std::make_shared<::bvar::Adder<SampleT>>(expose_name);
76 template <
typename SampleT>
79 if (disable_chan_var_) {
82 auto var_ptr = GetChanProcVar(role_attr);
83 if (var_ptr !=
nullptr) {
91 template <
typename SampleT>
94 if (disable_chan_var_) {
97 if (role_attr.
channel_name() == TIMER_COMPONENT_CHAN_NAME) {
100 auto var_ptr = GetChanTranVar(role_attr);
101 if (var_ptr !=
nullptr) {
102 (*var_ptr) << sample;
109 template <
typename SampleT>
112 if (disable_chan_var_) {
115 if (role_attr.
channel_name() == TIMER_COMPONENT_CHAN_NAME) {
118 auto var_ptr = GetChanCyberVar(role_attr);
119 if (var_ptr !=
nullptr) {
120 (*var_ptr) << sample;
128 if (disable_chan_var_) {
131 if (role_attr.
channel_name() == TIMER_COMPONENT_CHAN_NAME) {
134 auto var_ptr = GetProcStatusVar(role_attr);
135 if (var_ptr !=
nullptr) {
136 var_ptr->set_value(val);
144 if (disable_chan_var_) {
148 if (role_attr.
channel_name() == TIMER_COMPONENT_CHAN_NAME) {
151 auto var_ptr = GetProcStatusVar(role_attr);
152 if (var_ptr !=
nullptr) {
153 *val = var_ptr->get_value();
161 if (disable_chan_var_) {
164 if (role_attr.
channel_name() == TIMER_COMPONENT_CHAN_NAME) {
167 auto var_ptr = GetTotalMsgsStatusVar(role_attr);
168 if (var_ptr !=
nullptr) {
169 var_ptr->set_value(val);
177 if (disable_chan_var_) {
180 if (role_attr.
channel_name() == TIMER_COMPONENT_CHAN_NAME) {
184 auto var_ptr = GetAdderVar(role_attr);
185 if (var_ptr ==
nullptr) {
190 (*var_ptr) << total_msg_val;
213 inline uint64_t GetMicroTimeNow() const noexcept;
215 inline const
std::
string GetProcLatencyKey(
217 return role_attr.node_name() +
"-" + role_attr.channel_name() +
"proc";
220 inline const std::string GetTranLatencyKey(
225 inline const std::string GetCyberLatencyKey(
226 const proto::RoleAttributes& role_attr) {
227 return role_attr.node_name() +
"-" + role_attr.channel_name() +
"cyber";
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";
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";
242 inline const std::string GetTotalRecvStatusKey(
243 const proto::RoleAttributes& role_attr) {
244 return role_attr.node_name() +
"-" + role_attr.channel_name() +
"recv-msgs";
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_;
251 std::unordered_map<std::string, std::shared_ptr<SpanHandler>> span_handlers_;
253 bool first_recv_ =
true;
254 bool disable_chan_var_ =
false;
259inline uint64_t Statistics::GetMicroTimeNow() const noexcept {
264 if (
cyber_unlikely(span_handlers_.find(name) != span_handlers_.end())) {
265 AERROR <<
"span handler " << name <<
"has been created!";
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;
274 span_handlers_[name] = std::move(handler);
279 auto it = span_handlers_.find(name);
281 AERROR <<
"span handler " << name <<
"not found!";
284 it->second->start_time = std::move(GetMicroTimeNow());
289 auto it = span_handlers_.find(name);
291 AERROR <<
"span handler " << name <<
"not found!";
294 it->second->end_time = std::move(GetMicroTimeNow());
295 auto handler = it->second;
296 auto diff = handler->end_time - handler->start_time;
298 AWARN <<
"Time span less than preset value: " \
299 << diff <<
" vs " << handler->min_ns;
303 AWARN <<
"Time span is larger than INT32_MAX: " << diff <<
", drop it...";
306 *(handler->span_trace_node) << diff;
uint64_t ToMicrosecond() const
convert time to microsecond (us).
static Time Now()
get the current time.
bool SamplingTranLatency(const proto::RoleAttributes &role_attr, SampleT sample)
bool SamplingCyberLatency(const proto::RoleAttributes &role_attr, SampleT sample)
bool AddRecvCount(const proto::RoleAttributes &role_attr, int total_msg_val)
bool EndSpan(std::string name)
bool SetTotalMsgsStatus(const proto::RoleAttributes &role_attr, int32_t val)
bool CreateSpan(std::string name, uint64_t min_ns=0)
bool SamplingProcLatency(const proto::RoleAttributes &role_attr, SampleT sample)
bool RegisterChanVar(const proto::RoleAttributes &role_attr)
bool GetProcStatus(const proto::RoleAttributes &role_attr, uint64_t *val)
std::shared_ptr<::bvar::Adder< SampleT > > CreateAdder(const proto::RoleAttributes &role_attr)
bool StartSpan(std::string name)
bool SetProcStatus(const proto::RoleAttributes &role_attr, uint64_t val)
#define cyber_unlikely(x)
#define DECLARE_SINGLETON(classname)
std::shared_ptr<::bvar::LatencyRecorder > LatencyVarPtr
std::shared_ptr<::bvar::Adder< int32_t > > AdderVarPtr
std::shared_ptr<::bvar::Status< uint64_t > > StatusVarPtr
optional string node_name
optional string channel_name
std::shared_ptr<::bvar::LatencyRecorder > span_trace_node