1 // Copyright (C) 2019 The Android Open Source Project
2 //
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 #include <random>
16 #include <set>
17 #include <unordered_set>
18
19 #include <benchmark/benchmark.h>
20
21 #include "perfetto/base/logging.h"
22 #include "perfetto/ext/base/utils.h"
23 #include "src/base/test/utils.h"
24 #include "src/kallsyms/kernel_symbol_map.h"
25
26 namespace {
27
IsBenchmarkFunctionalOnly()28 bool IsBenchmarkFunctionalOnly() {
29 return getenv("BENCHMARK_FUNCTIONAL_TEST_ONLY") != nullptr;
30 }
31
BenchmarkArgs(benchmark::internal::Benchmark * b)32 void BenchmarkArgs(benchmark::internal::Benchmark* b) {
33 if (IsBenchmarkFunctionalOnly()) {
34 b->Ranges({{16, 16}, {16, 16}});
35 } else {
36 b->RangeMultiplier(2)->Ranges({{4, 512}, {4, 512}});
37 }
38 }
39
40 struct ExpectedSym {
41 uint64_t addr;
42 const char* name;
43 };
44
45 // This set of symbols has been chosen by randomly picking 40 random symbols
46 // from the original kallsyms.
47 ExpectedSym kExpectedSyms[] = {
48 {0xffffff8f79c0d978, "__map_memblock"},
49 {0xffffff8f78fddbb8, "smack_inode_getsecid"},
50 {0xffffff8f78fe43b4, "msm_smmu_set_attribute"},
51 {0xffffff8f79d23e20, "__initcall_41_dm_verity_init6"},
52 {0xffffff8f74206c5c, "sme_update_fast_transition_enabled"},
53 {0xffffff8f74878c8c, "tavil_hph_idle_detect_put"},
54 {0xffffff8f78fd7db0, "privileged_wrt_inode_uidgid"},
55 {0xffffff8f78ffe030, "__hrtimer_tasklet_trampoline"},
56 {0xffffff8f78fd86b0, "store_enable"},
57 {0xffffff8f78ffbcb8, "raw6_exit_net"},
58 {0xffffff8f78ffa6ec, "idProduct_show"},
59 {0xffffff8f78fd99c0, "perf_tp_event"},
60 {0xffffff8f78fe1468, "rpmh_tx_done"},
61 {0xffffff8f78fda274, "page_unlock_anon_vma_read"},
62 {0xffffff8f78ffedfc, "vmstat_period_ms_operations_open"},
63 {0xffffff8f78fe0148, "devm_gpio_request"},
64 {0xffffff8f77915028, "ctx_sched_out"},
65 {0xffffff8f77ccdc2c, "gcm_hash_crypt_remain_continue"},
66 {0xffffff8f790022ec, "loop_init"},
67 {0xffffff8f78ff0004, "pcim_release"},
68 {0xffffff8f78fe1d8c, "uart_close"},
69 {0xffffff8f78fda9d4, "pipe_lock"},
70 {0xffffff8f78e62c68, "local_bh_enable.117091"},
71 {0xffffff8f78fd918c, "fork_idle"},
72 {0xffffff8f78fe24c4, "drm_dp_downstream_debug"},
73 {0xffffff8f78ff41d0, "inet_addr_onlink"},
74 {0xffffff8f78fdf2d4, "idr_alloc"},
75 {0xffffff8f78ff073c, "fts_remove"},
76 {0xffffff8f78ffe294, "xfrm4_local_error"},
77 {0xffffff8f79001994, "cpu_feature_match_PMULL_init"},
78 {0xffffff8f78ff4740, "xfrm_state_find"},
79 {0xffffff8f78ff58b0, "inet_del_offload"},
80 {0xffffff8f742041ac, "csr_is_conn_state_connected_infra"},
81 {0xffffff8f78fe1fd4, "diag_add_client"},
82 {0xffffff8f78ffc000, "trace_raw_output_mm_vmscan_kswapd_sleep"},
83 {0xffffff8f78fe6388, "scsi_queue_insert"},
84 {0xffffff8f78fdd480, "selinux_sb_clone_mnt_opts"},
85 {0xffffff8f78fe0e9c, "clk_fixed_rate_recalc_rate"},
86 {0xffffff8f78fedaec, "cap_inode_killpriv"},
87 {0xffffff8f79002b64, "audio_amrwb_init"},
88 };
89
90 } // namespace
91
BM_KallSyms(benchmark::State & state)92 static void BM_KallSyms(benchmark::State& state) {
93 perfetto::KernelSymbolMap::kTokenIndexSampling =
94 static_cast<size_t>(state.range(0));
95 perfetto::KernelSymbolMap::kSymIndexSampling =
96 static_cast<size_t>(state.range(1));
97 perfetto::KernelSymbolMap kallsyms;
98
99 // Don't run the benchmark on the CI as it requires pushing all test data,
100 // which slows down significantly the CI.
101 const bool skip = IsBenchmarkFunctionalOnly();
102 if (!skip) {
103 kallsyms.Parse(perfetto::base::GetTestDataPath("test/data/kallsyms.txt"));
104 }
105
106 for (auto _ : state) {
107 for (size_t i = 0; i < perfetto::base::ArraySize(kExpectedSyms); i++) {
108 const auto& exp = kExpectedSyms[i];
109 PERFETTO_CHECK(skip || kallsyms.Lookup(exp.addr) == exp.name);
110 }
111 }
112
113 state.counters["mem"] = static_cast<double>(kallsyms.size_bytes());
114 }
115
116 BENCHMARK(BM_KallSyms)->Apply(BenchmarkArgs);
117