1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18
19#ifndef BRPC_PROFILER_LINKER_H
20#define BRPC_PROFILER_LINKER_H
21
22#if defined(BRPC_ENABLE_CPU_PROFILER) || defined(BAIDU_RPC_ENABLE_CPU_PROFILER)
23#include "butil/gperftools_profiler.h"
24#endif
25
26namespace brpc {
27
28// defined in src/brpc/builtin/index_service.cpp
29extern bool cpu_profiler_enabled;
30
31// defined in src/brpc/controller.cpp
32extern int PROFILER_LINKER_DUMMY;
33
34struct ProfilerLinker {
35 // [ Must be inlined ]
36 // This function is included by user's compilation unit to force
37 // linking of ProfilerStart()/ProfilerStop()
38 // etc when corresponding macros are defined.
39 inline ProfilerLinker() {
40
41#if defined(BRPC_ENABLE_CPU_PROFILER) || defined(BAIDU_RPC_ENABLE_CPU_PROFILER)
42 cpu_profiler_enabled = true;
43 // compiler has no way to tell if PROFILER_LINKER_DUMMY is 0 or not,
44 // so it has to link the function inside the branch.
45 if (PROFILER_LINKER_DUMMY != 0/*must be false*/) {
46 ProfilerStart("this_function_should_never_run");
47 }
48#endif
49 }
50};
51
52} // namespace brpc
53
54
55#endif // BRPC_PROFILER_LINKER_H
56