Apollo 10.0
自动驾驶开放平台
perf_util.h
浏览该文件的文档.
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#pragma once
17
18#include <string>
19
20#include "absl/strings/str_cat.h"
21
22#include "cyber/common/macros.h"
23#include "cyber/time/time.h"
24
25#if defined(__GNUC__) || defined(__GNUG__)
26#define AFUNC __PRETTY_FUNCTION__
27#elif defined(__clang__)
28#define AFUNC __PRETTY_FUNCTION__
29#else
30#define AFUNC __func__
31#endif
32
33// How to Use:
34// 1) Use PERF_FUNCTION to compute time cost of function execution as follows:
35// void MyFunc() {
36// PERF_FUNCION();
37// // do somethings.
38// }
39// Console log:
40// >>>>>>>>>>>>>>>>>>
41// I0615 15:49:30.756429 12748 timer.cpp:31] TIMER MyFunc elapsed time: 100 ms
42// >>>>>>>>>>>>>>>>>>
43//
44// 2) Use PERF_BLOCK_START/END to compute time cost of block execution.
45// void MyFunc() {
46// // xxx1
47// PERF_BLOCK_START();
48//
49// // do xx2
50//
51// PERF_BLOCK_END("xx2");
52//
53// // do xx3
54//
55// PERF_BLOCK_END("xx3");
56// }
57//
58// Console log:
59// >>>>>>>>>>>>>>>
60// I0615 15:49:30.756429 12748 timer.cpp:31] TIMER xx2 elapsed time: 100 ms
61// I0615 15:49:30.756429 12748 timer.cpp:31] TIMER xx3 elapsed time: 200 ms
62// >>>>>>>>>>>>>>>
63namespace apollo {
64namespace common {
65namespace util {
66
67std::string function_signature(const std::string& func_name,
68 const std::string& indicator = "");
69
70class Timer {
71 public:
72 Timer() = default;
73
74 // no-thread safe.
75 void Start();
76
77 // return the elapsed time,
78 // also output msg and time in glog.
79 // automatically start a new timer.
80 // no-thread safe.
81 int64_t End(const std::string& msg);
82
83 private:
84 apollo::cyber::Time start_time_;
85 apollo::cyber::Time end_time_;
86
88};
89
91 public:
92 explicit TimerWrapper(const std::string& msg) : msg_(msg) { timer_.Start(); }
93
94 ~TimerWrapper() { timer_.End(msg_); }
95
96 private:
97 Timer timer_;
98 std::string msg_;
99
101};
102
103} // namespace util
104} // namespace common
105} // namespace apollo
106
107#if defined(ENABLE_PERF)
108#define PERF_FUNCTION() \
109 apollo::common::util::TimerWrapper _timer_wrapper_( \
110 apollo::common::util::function_signature(AFUNC))
111#define PERF_FUNCTION_WITH_NAME(func_name) \
112 apollo::common::util::TimerWrapper _timer_wrapper_(func_name)
113#define PERF_FUNCTION_WITH_INDICATOR(indicator) \
114 apollo::common::util::TimerWrapper _timer_wrapper_( \
115 apollo::common::util::function_signature(AFUNC, indicator))
116#define PERF_BLOCK_START() \
117 apollo::common::util::Timer _timer_; \
118 _timer_.Start()
119#define PERF_BLOCK_END(msg) _timer_.End(msg)
120#define PERF_BLOCK_END_WITH_INDICATOR(indicator, msg) \
121 _timer_.End(absl::StrCat(indicator, "_", msg))
122#else
123#define PERF_FUNCTION()
124#define PERF_FUNCTION_WITH_NAME(func_name) UNUSED(func_name);
125#define PERF_FUNCTION_WITH_INDICATOR(indicator) UNUSED(indicator);
126#define PERF_BLOCK_START()
127#define PERF_BLOCK_END(msg) UNUSED(msg);
128#define PERF_BLOCK_END_WITH_INDICATOR(indicator, msg) \
129 { \
130 UNUSED(indicator); \
131 UNUSED(msg); \
132 }
133#endif // ENABLE_PERF
TimerWrapper(const std::string &msg)
Definition perf_util.h:92
int64_t End(const std::string &msg)
Definition perf_util.cc:55
Cyber has builtin time type Time.
Definition time.h:31
#define DISALLOW_COPY_AND_ASSIGN(classname)
Definition macros.h:48
std::string function_signature(const std::string &func_name, const std::string &indicator)
Definition perf_util.cc:44
class register implement
Definition arena_queue.h:37