1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <benchmark/benchmark.h>
17 #include <string>
18 #include <vector>
19 #include "dfx_log.h"
20 #include "dfx_elf.h"
21 #include "dfx_map.h"
22 #include "dfx_maps.h"
23
24 using namespace OHOS::HiviewDFX;
25 using namespace std;
26
InitializeBuildId(benchmark::State & state,DfxMaps * dfxMaps,DfxMap ** buildIdMap)27 static void InitializeBuildId(benchmark::State& state, DfxMaps* dfxMaps, DfxMap** buildIdMap)
28 {
29 auto maps = dfxMaps->GetMaps();
30 if (maps.size() == 0) {
31 state.SkipWithError("Failed to get local maps.");
32 return;
33 }
34
35 // Find the libc.so share library and use that for benchmark purposes.
36 *buildIdMap = nullptr;
37 for (auto& map : maps) {
38 auto elf = map->GetElf();
39 if (elf == nullptr) {
40 continue;
41 }
42 if (map->offset == 0 && elf->GetBuildId() != "") {
43 *buildIdMap = map.get();
44 break;
45 }
46 }
47
48 if (*buildIdMap == nullptr) {
49 state.SkipWithError("Failed to find a map with a BuildId.");
50 }
51 }
52
53 /**
54 * @tc.name: BenchmarkElfGetBuildIdFromObj
55 * @tc.desc: Elf Get BuildId From obj
56 * @tc.type: FUNC
57 */
BenchmarkElfGetBuildIdFromObj(benchmark::State & state)58 static void BenchmarkElfGetBuildIdFromObj(benchmark::State& state)
59 {
60 auto dfxMaps = DfxMaps::Create();
61 DfxMap* buildIdMap;
62 InitializeBuildId(state, dfxMaps.get(), &buildIdMap);
63
64 auto elf = buildIdMap->GetElf();
65 if (elf == nullptr) {
66 return;
67 }
68 if (!elf->IsValid()) {
69 state.SkipWithError("Cannot get valid elf from map.");
70 }
71
72 for (const auto& _ : state) {
73 state.PauseTiming();
74 elf->SetBuildId("");
75 state.ResumeTiming();
76 benchmark::DoNotOptimize(elf->GetBuildId());
77 }
78 }
79 BENCHMARK(BenchmarkElfGetBuildIdFromObj);
80