Apollo  v5.5.0
Open source self driving car software
perf.h
Go to the documentation of this file.
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 
21 
22 // Usage:
23 // Firstly, you can define PERCEPTION_DISABLE_PERF to disable all perf infos
24 // in compiling time.
25 //
26 // Using PERCEPTION_PERF_FUNCTION to perf the entire function time elapsed.
27 // as following code:
28 // void MyFunc() {
29 // PERCEPTION_PERF_FUNCION();
30 // // do somethings.
31 // }
32 // log would output:
33 // >>>>>>>>>>>>>>>>>>
34 // I0615 15:49:30.756429 12748 timer.cpp:31] TIMER MyFunc elapsed time: 100 ms
35 // >>>>>>>>>>>>>>>>>>
36 //
37 // Using PERCEPTION_PERF_BLOCK to perf code block time elapsed.
38 // as following code:
39 // void MyFunc() {
40 // // xxx1
41 // PERCEPTION_PERF_BLOCK_START();
42 //
43 // // do xx2
44 //
45 // PERCEPTION_PERF_BLOCK_END("xx2");
46 //
47 // // do xx3
48 //
49 // PERCEPTION_PERF_BLOCK_END("xx3");
50 // }
51 //
52 // log would output:
53 // >>>>>>>>>>>>>>>
54 // I0615 15:49:30.756429 12748 timer.cpp:31] TIMER xx2 elapsed time: 100 ms
55 // I0615 15:49:30.756429 12748 timer.cpp:31] TIMER xx3 elapsed time: 200 ms
56 // >>>>>>>>>>>>>>>
57 
58 namespace apollo {
59 namespace perception {
60 namespace lib {
61 
62 inline std::string get_full_name(const std::string& full_name) {
63  size_t end = full_name.find("(");
64 
65  if (end == std::string::npos) {
66  return full_name;
67  }
68 
69  std::string new_str = full_name.substr(0, end);
70  size_t start = new_str.rfind(" ");
71 
72  if (start == std::string::npos) {
73  return full_name;
74  }
75 
76  return new_str.substr(start + 1);
77 }
78 
79 inline std::string get_full_name(const std::string& full_name,
80  const std::string& indicator) {
81  return indicator + "_" + get_full_name(full_name);
82 }
83 
84 } // namespace lib
85 } // namespace perception
86 } // namespace apollo
87 
88 #ifdef PERCEPTION_DISABLE_PERF
89 
90 // disable macros.
91 #define PERCEPTION_PERF_FUNCTION()
92 
93 #define PERCEPTION_PERF_FUNCTION_WITH_INDICATOR(indicator)
94 
95 #define PERCEPTION_PERF_BLOCK_START()
96 
97 #define PERCEPTION_PERF_BLOCK_END(msg)
98 
99 #define PERCEPTION_PERF_BLOCK_END_WITH_INDICATOR(indicator, msg)
100 
101 #else
102 
103 #define PERCEPTION_PERF_FUNCTION() \
104  apollo::perception::lib::TimerWrapper _timer_wrapper_( \
105  apollo::perception::lib::get_full_name(__PRETTY_FUNCTION__))
106 
107 #define PERCEPTION_PERF_FUNCTION_WITH_INDICATOR(indicator) \
108  apollo::perception::lib::TimerWrapper _timer_wrapper_( \
109  apollo::perception::lib::get_full_name(__PRETTY_FUNCTION__, indicator))
110 
111 #define PERCEPTION_PERF_BLOCK_START() \
112  apollo::perception::lib::Timer _timer_; \
113  _timer_.Start()
114 
115 #define PERCEPTION_PERF_BLOCK_END(msg) _timer_.End(msg)
116 
117 #define PERCEPTION_PERF_BLOCK_END_WITH_INDICATOR(indicator, msg) \
118  _timer_.End(indicator + "_" + msg)
119 
120 #endif // PERCEPTION_DISABLE_PERF
Definition: blob.h:72
std::string get_full_name(const std::string &full_name)
Definition: perf.h:62