blob: c108a2aa72de229e4f805d45ec03d44b30789220 [file] [log] [blame]
David Srbeckya17c2b62020-04-14 16:59:27 +01001/*
2 * Copyright (C) 2020 The Android Open Source Project
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 <err.h>
18#include <malloc.h>
19#include <stdint.h>
20
21#include <string>
22
23#include <benchmark/benchmark.h>
24
25#include <unwindstack/Elf.h>
26#include <unwindstack/Memory.h>
27
28#include "Utils.h"
29
30static void BenchmarkElfCreate(benchmark::State& state, const std::string& elf_file) {
31#if defined(__BIONIC__)
32 uint64_t rss_bytes = 0;
33#endif
34 uint64_t alloc_bytes = 0;
35 for (auto _ : state) {
36 state.PauseTiming();
37#if defined(__BIONIC__)
38 mallopt(M_PURGE, 0);
39 uint64_t rss_bytes_before = 0;
40 GatherRss(&rss_bytes_before);
41#endif
42 uint64_t alloc_bytes_before = mallinfo().uordblks;
43 auto file_memory = unwindstack::Memory::CreateFileMemory(elf_file, 0);
44 state.ResumeTiming();
45
46 unwindstack::Elf elf(file_memory.release());
47 if (!elf.Init() || !elf.valid()) {
48 errx(1, "Internal Error: Cannot open elf.");
49 }
50
51 state.PauseTiming();
52#if defined(__BIONIC__)
53 mallopt(M_PURGE, 0);
54#endif
55 alloc_bytes += mallinfo().uordblks - alloc_bytes_before;
56#if defined(__BIONIC__)
57 GatherRss(&rss_bytes);
58 rss_bytes -= rss_bytes_before;
59#endif
60 state.ResumeTiming();
61 }
62
63#if defined(__BIONIC__)
64 state.counters["RSS_BYTES"] = rss_bytes / static_cast<double>(state.iterations());
65#endif
66 state.counters["ALLOCATED_BYTES"] = alloc_bytes / static_cast<double>(state.iterations());
67}
68
69void BM_elf_create(benchmark::State& state) {
70 BenchmarkElfCreate(state, GetElfFile());
71}
72BENCHMARK(BM_elf_create);
73
74void BM_elf_create_compressed(benchmark::State& state) {
75 BenchmarkElfCreate(state, GetCompressedElfFile());
76}
77BENCHMARK(BM_elf_create_compressed);