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 if (map->offset == 0 && map->GetElf()->GetBuildId() != "") {
39 *buildIdMap = map.get();
40 break;
41 }
42 }
43
44 if (*buildIdMap == nullptr) {
45 state.SkipWithError("Failed to find a map with a BuildId.");
46 }
47 }
48
49 /**
50 * @tc.name: BenchmarkElfGetBuildIdFromObj
51 * @tc.desc: Elf Get BuildId From obj
52 * @tc.type: FUNC
53 */
BenchmarkElfGetBuildIdFromObj(benchmark::State & state)54 static void BenchmarkElfGetBuildIdFromObj(benchmark::State& state)
55 {
56 auto dfxMaps = DfxMaps::Create();
57 DfxMap* buildIdMap;
58 InitializeBuildId(state, dfxMaps.get(), &buildIdMap);
59
60 auto elf = buildIdMap->GetElf();
61 if (!elf->IsValid()) {
62 state.SkipWithError("Cannot get valid elf from map.");
63 }
64
65 for (const auto& _ : state) {
66 state.PauseTiming();
67 elf->SetBuildId("");
68 state.ResumeTiming();
69 benchmark::DoNotOptimize(elf->GetBuildId());
70 }
71 }
72 BENCHMARK(BenchmarkElfGetBuildIdFromObj);
73