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