1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker * Copyright (C) 2020 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker * All rights reserved.
4*8d67ca89SAndroid Build Coastguard Worker *
5*8d67ca89SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
6*8d67ca89SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions
7*8d67ca89SAndroid Build Coastguard Worker * are met:
8*8d67ca89SAndroid Build Coastguard Worker * * Redistributions of source code must retain the above copyright
9*8d67ca89SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
10*8d67ca89SAndroid Build Coastguard Worker * * Redistributions in binary form must reproduce the above copyright
11*8d67ca89SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in
12*8d67ca89SAndroid Build Coastguard Worker * the documentation and/or other materials provided with the
13*8d67ca89SAndroid Build Coastguard Worker * distribution.
14*8d67ca89SAndroid Build Coastguard Worker *
15*8d67ca89SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16*8d67ca89SAndroid Build Coastguard Worker * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17*8d67ca89SAndroid Build Coastguard Worker * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18*8d67ca89SAndroid Build Coastguard Worker * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19*8d67ca89SAndroid Build Coastguard Worker * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20*8d67ca89SAndroid Build Coastguard Worker * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*8d67ca89SAndroid Build Coastguard Worker * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22*8d67ca89SAndroid Build Coastguard Worker * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*8d67ca89SAndroid Build Coastguard Worker * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*8d67ca89SAndroid Build Coastguard Worker * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25*8d67ca89SAndroid Build Coastguard Worker * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8d67ca89SAndroid Build Coastguard Worker * SUCH DAMAGE.
27*8d67ca89SAndroid Build Coastguard Worker */
28*8d67ca89SAndroid Build Coastguard Worker
29*8d67ca89SAndroid Build Coastguard Worker #include <ctype.h>
30*8d67ca89SAndroid Build Coastguard Worker #include <stdarg.h>
31*8d67ca89SAndroid Build Coastguard Worker #include <stdlib.h>
32*8d67ca89SAndroid Build Coastguard Worker #include <string.h>
33*8d67ca89SAndroid Build Coastguard Worker #include <unistd.h>
34*8d67ca89SAndroid Build Coastguard Worker
35*8d67ca89SAndroid Build Coastguard Worker #include <string>
36*8d67ca89SAndroid Build Coastguard Worker #include <vector>
37*8d67ca89SAndroid Build Coastguard Worker
38*8d67ca89SAndroid Build Coastguard Worker #include <android-base/file.h>
39*8d67ca89SAndroid Build Coastguard Worker #include <android-base/logging.h>
40*8d67ca89SAndroid Build Coastguard Worker #include <android-base/strings.h>
41*8d67ca89SAndroid Build Coastguard Worker #include <benchmark/benchmark.h>
42*8d67ca89SAndroid Build Coastguard Worker #include <property_info_parser/property_info_parser.h>
43*8d67ca89SAndroid Build Coastguard Worker #include <property_info_serializer/property_info_serializer.h>
44*8d67ca89SAndroid Build Coastguard Worker #include <system_properties/contexts_split.h>
45*8d67ca89SAndroid Build Coastguard Worker
46*8d67ca89SAndroid Build Coastguard Worker #include "context_lookup_benchmark_data.h"
47*8d67ca89SAndroid Build Coastguard Worker
48*8d67ca89SAndroid Build Coastguard Worker using android::base::Split;
49*8d67ca89SAndroid Build Coastguard Worker using android::base::WriteStringToFd;
50*8d67ca89SAndroid Build Coastguard Worker using android::properties::BuildTrie;
51*8d67ca89SAndroid Build Coastguard Worker using android::properties::ParsePropertyInfoFile;
52*8d67ca89SAndroid Build Coastguard Worker using android::properties::PropertyInfoArea;
53*8d67ca89SAndroid Build Coastguard Worker using android::properties::PropertyInfoEntry;
54*8d67ca89SAndroid Build Coastguard Worker
55*8d67ca89SAndroid Build Coastguard Worker BENCHMARK_MAIN();
56*8d67ca89SAndroid Build Coastguard Worker
57*8d67ca89SAndroid Build Coastguard Worker class LegacyPropertyMapping : public ContextsSplit {
58*8d67ca89SAndroid Build Coastguard Worker public:
LegacyPropertyMapping(const char * property_contexts)59*8d67ca89SAndroid Build Coastguard Worker LegacyPropertyMapping(const char* property_contexts) {
60*8d67ca89SAndroid Build Coastguard Worker TemporaryFile file;
61*8d67ca89SAndroid Build Coastguard Worker if (!WriteStringToFd(property_contexts, file.fd)) {
62*8d67ca89SAndroid Build Coastguard Worker PLOG(FATAL) << "Could not write to temporary file";
63*8d67ca89SAndroid Build Coastguard Worker }
64*8d67ca89SAndroid Build Coastguard Worker
65*8d67ca89SAndroid Build Coastguard Worker if (!InitializePropertiesFromFile(file.path)) {
66*8d67ca89SAndroid Build Coastguard Worker LOG(FATAL) << "Could not initialize properties";
67*8d67ca89SAndroid Build Coastguard Worker }
68*8d67ca89SAndroid Build Coastguard Worker }
69*8d67ca89SAndroid Build Coastguard Worker };
70*8d67ca89SAndroid Build Coastguard Worker
PropertiesToLookup()71*8d67ca89SAndroid Build Coastguard Worker static std::vector<std::string> PropertiesToLookup() {
72*8d67ca89SAndroid Build Coastguard Worker std::vector<std::string> properties;
73*8d67ca89SAndroid Build Coastguard Worker auto property_lines = Split(aosp_s_property_contexts, "\n");
74*8d67ca89SAndroid Build Coastguard Worker for (const auto& line : property_lines) {
75*8d67ca89SAndroid Build Coastguard Worker if (line.empty() || line[0] == '#') {
76*8d67ca89SAndroid Build Coastguard Worker continue;
77*8d67ca89SAndroid Build Coastguard Worker }
78*8d67ca89SAndroid Build Coastguard Worker
79*8d67ca89SAndroid Build Coastguard Worker auto property = Split(line, " ")[0];
80*8d67ca89SAndroid Build Coastguard Worker properties.push_back(property);
81*8d67ca89SAndroid Build Coastguard Worker properties.push_back(property + "0");
82*8d67ca89SAndroid Build Coastguard Worker properties.push_back(property + "A");
83*8d67ca89SAndroid Build Coastguard Worker }
84*8d67ca89SAndroid Build Coastguard Worker return properties;
85*8d67ca89SAndroid Build Coastguard Worker }
86*8d67ca89SAndroid Build Coastguard Worker
LegacyLookupOreo(benchmark::State & state)87*8d67ca89SAndroid Build Coastguard Worker static void LegacyLookupOreo(benchmark::State& state) {
88*8d67ca89SAndroid Build Coastguard Worker LegacyPropertyMapping mapping(oreo_property_contexts);
89*8d67ca89SAndroid Build Coastguard Worker auto properties = PropertiesToLookup();
90*8d67ca89SAndroid Build Coastguard Worker for (auto _ : state) {
91*8d67ca89SAndroid Build Coastguard Worker for (const auto& property : properties) {
92*8d67ca89SAndroid Build Coastguard Worker benchmark::DoNotOptimize(mapping.GetPrefixNodeForName(property.c_str()));
93*8d67ca89SAndroid Build Coastguard Worker }
94*8d67ca89SAndroid Build Coastguard Worker }
95*8d67ca89SAndroid Build Coastguard Worker }
96*8d67ca89SAndroid Build Coastguard Worker BENCHMARK(LegacyLookupOreo);
97*8d67ca89SAndroid Build Coastguard Worker
LegacyLookupS(benchmark::State & state)98*8d67ca89SAndroid Build Coastguard Worker static void LegacyLookupS(benchmark::State& state) {
99*8d67ca89SAndroid Build Coastguard Worker LegacyPropertyMapping mapping(aosp_s_property_contexts);
100*8d67ca89SAndroid Build Coastguard Worker auto properties = PropertiesToLookup();
101*8d67ca89SAndroid Build Coastguard Worker for (auto _ : state) {
102*8d67ca89SAndroid Build Coastguard Worker for (const auto& property : properties) {
103*8d67ca89SAndroid Build Coastguard Worker benchmark::DoNotOptimize(mapping.GetPrefixNodeForName(property.c_str()));
104*8d67ca89SAndroid Build Coastguard Worker }
105*8d67ca89SAndroid Build Coastguard Worker }
106*8d67ca89SAndroid Build Coastguard Worker }
107*8d67ca89SAndroid Build Coastguard Worker BENCHMARK(LegacyLookupS);
108*8d67ca89SAndroid Build Coastguard Worker
CreateSerializedTrie(const char * input_file)109*8d67ca89SAndroid Build Coastguard Worker static std::string CreateSerializedTrie(const char* input_file) {
110*8d67ca89SAndroid Build Coastguard Worker std::vector<std::string> errors;
111*8d67ca89SAndroid Build Coastguard Worker std::vector<PropertyInfoEntry> property_infos;
112*8d67ca89SAndroid Build Coastguard Worker ParsePropertyInfoFile(input_file, false, &property_infos, &errors);
113*8d67ca89SAndroid Build Coastguard Worker
114*8d67ca89SAndroid Build Coastguard Worker std::string serialized_trie;
115*8d67ca89SAndroid Build Coastguard Worker std::string error;
116*8d67ca89SAndroid Build Coastguard Worker if (!BuildTrie(property_infos, "u:object_r:default_prop:s0", "string", &serialized_trie,
117*8d67ca89SAndroid Build Coastguard Worker &error)) {
118*8d67ca89SAndroid Build Coastguard Worker LOG(FATAL) << "Could not build trie: " << error;
119*8d67ca89SAndroid Build Coastguard Worker }
120*8d67ca89SAndroid Build Coastguard Worker return serialized_trie;
121*8d67ca89SAndroid Build Coastguard Worker }
122*8d67ca89SAndroid Build Coastguard Worker
TrieLookupOreo(benchmark::State & state)123*8d67ca89SAndroid Build Coastguard Worker static void TrieLookupOreo(benchmark::State& state) {
124*8d67ca89SAndroid Build Coastguard Worker std::string serialized_trie = CreateSerializedTrie(oreo_property_contexts);
125*8d67ca89SAndroid Build Coastguard Worker PropertyInfoArea* trie = reinterpret_cast<PropertyInfoArea*>(serialized_trie.data());
126*8d67ca89SAndroid Build Coastguard Worker auto properties = PropertiesToLookup();
127*8d67ca89SAndroid Build Coastguard Worker for (auto _ : state) {
128*8d67ca89SAndroid Build Coastguard Worker for (const auto& property : properties) {
129*8d67ca89SAndroid Build Coastguard Worker trie->GetPropertyInfo(property.c_str(), nullptr, nullptr);
130*8d67ca89SAndroid Build Coastguard Worker }
131*8d67ca89SAndroid Build Coastguard Worker }
132*8d67ca89SAndroid Build Coastguard Worker }
133*8d67ca89SAndroid Build Coastguard Worker BENCHMARK(TrieLookupOreo);
134*8d67ca89SAndroid Build Coastguard Worker
TrieLookupS(benchmark::State & state)135*8d67ca89SAndroid Build Coastguard Worker static void TrieLookupS(benchmark::State& state) {
136*8d67ca89SAndroid Build Coastguard Worker std::string serialized_trie = CreateSerializedTrie(aosp_s_property_contexts);
137*8d67ca89SAndroid Build Coastguard Worker PropertyInfoArea* trie = reinterpret_cast<PropertyInfoArea*>(serialized_trie.data());
138*8d67ca89SAndroid Build Coastguard Worker auto properties = PropertiesToLookup();
139*8d67ca89SAndroid Build Coastguard Worker for (auto _ : state) {
140*8d67ca89SAndroid Build Coastguard Worker for (const auto& property : properties) {
141*8d67ca89SAndroid Build Coastguard Worker trie->GetPropertyInfo(property.c_str(), nullptr, nullptr);
142*8d67ca89SAndroid Build Coastguard Worker }
143*8d67ca89SAndroid Build Coastguard Worker }
144*8d67ca89SAndroid Build Coastguard Worker }
145*8d67ca89SAndroid Build Coastguard Worker BENCHMARK(TrieLookupS);
146