1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
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 #include <folly/Benchmark.h>
18 #include <folly/Range.h>
19 #include <folly/experimental/symbolizer/Dwarf.h>
20 #include <folly/experimental/symbolizer/SymbolizedFrame.h>
21 #include <folly/experimental/symbolizer/Symbolizer.h>
22 #include <folly/experimental/symbolizer/test/SymbolizerTestUtils.h>
23 #include <folly/portability/GFlags.h>
24 
25 #if FOLLY_HAVE_ELF && FOLLY_HAVE_DWARF
26 
27 namespace {
28 
29 using namespace folly::symbolizer;
30 using namespace folly::symbolizer::test;
31 
lexicalBlockBar()32 FOLLY_NOINLINE void lexicalBlockBar() try {
33   size_t unused = 0;
34   unused++;
35   inlineB_inlineA_lfind();
36 } catch (...) {
37   folly::assume_unreachable();
38 }
39 
run(LocationInfoMode mode,size_t n)40 void run(LocationInfoMode mode, size_t n) {
41   folly::BenchmarkSuspender suspender;
42   Symbolizer symbolizer(nullptr, LocationInfoMode::FULL_WITH_INLINE, 0);
43   FrameArray<100> frames;
44   gComparatorGetStackTraceArg = &frames;
45   gComparatorGetStackTrace = (bool (*)(void*))getStackTrace<100>;
46   lexicalBlockBar();
47   symbolizer.symbolize(frames);
48   // The address of the line where lexicalBlockBar calls inlineB_inlineA_lfind.
49   uintptr_t address = frames.frames[7].addr;
50 
51   ElfFile elf("/proc/self/exe");
52   Dwarf dwarf(&elf);
53   auto inlineFrames = std::array<SymbolizedFrame, 10>();
54   suspender.dismiss();
55 
56   for (size_t i = 0; i < n; i++) {
57     LocationInfo info;
58     dwarf.findAddress(address, mode, info, folly::range(inlineFrames));
59   }
60 }
61 
62 } // namespace
63 
BENCHMARK(DwarfFindAddressFast,n)64 BENCHMARK(DwarfFindAddressFast, n) {
65   run(folly::symbolizer::LocationInfoMode::FAST, n);
66 }
67 
BENCHMARK(DwarfFindAddressFull,n)68 BENCHMARK(DwarfFindAddressFull, n) {
69   run(folly::symbolizer::LocationInfoMode::FULL, n);
70 }
71 
BENCHMARK(DwarfFindAddressFullWithInline,n)72 BENCHMARK(DwarfFindAddressFullWithInline, n) {
73   run(folly::symbolizer::LocationInfoMode::FULL_WITH_INLINE, n);
74 }
75 
76 #endif // FOLLY_HAVE_ELF && FOLLY_HAVE_DWARF
77 
main(int argc,char * argv[])78 int main(int argc, char* argv[]) {
79   gflags::ParseCommandLineFlags(&argc, &argv, true);
80   google::InitGoogleLogging(argv[0]);
81   folly::runBenchmarksOnFlag();
82   return 0;
83 }
84