1 /*
2 * Copyright (C) 2012 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 <errno.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21
22 #include <string>
23 #include <vector>
24
25 #include <android-base/file.h>
26
27 using namespace std::literals;
28
29 #if defined(__BIONIC__)
30
31 #include <sys/system_properties.h>
32
33 #include <benchmark/benchmark.h>
34 #include <system_properties/system_properties.h>
35 #include "util.h"
36
37 struct LocalPropertyTestState {
LocalPropertyTestStateLocalPropertyTestState38 explicit LocalPropertyTestState(int nprops)
39 : nprops(nprops), valid(false), system_properties_(false) {
40 static const char prop_name_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_.";
41
42 valid = system_properties_.AreaInit(dir_.path, nullptr, true);
43 if (!valid) {
44 printf("Failed to initialize properties, terminating...\n");
45 exit(1);
46 }
47
48 names = new char* [nprops];
49 name_lens = new int[nprops];
50 values = new char* [nprops];
51 value_lens = new int[nprops];
52
53 srandom(nprops);
54
55 for (int i = 0; i < nprops; i++) {
56 // Make sure the name has at least 10 characters to make
57 // it very unlikely to generate the same random name.
58 name_lens[i] = (random() % (PROP_NAME_MAX - 10)) + 10;
59 names[i] = new char[PROP_NAME_MAX + 1];
60 size_t prop_name_len = sizeof(prop_name_chars) - 1;
61 for (int j = 0; j < name_lens[i]; j++) {
62 if (j == 0 || names[i][j-1] == '.' || j == name_lens[i] - 1) {
63 // Certain values are not allowed:
64 // - Don't start name with '.'
65 // - Don't allow '.' to appear twice in a row
66 // - Don't allow the name to end with '.'
67 // This assumes that '.' is the last character in the
68 // array so that decrementing the length by one removes
69 // the value from the possible values.
70 prop_name_len--;
71 }
72 names[i][j] = prop_name_chars[random() % prop_name_len];
73 }
74 names[i][name_lens[i]] = 0;
75
76 // Make sure the value contains at least 1 character.
77 value_lens[i] = (random() % (PROP_VALUE_MAX - 1)) + 1;
78 values[i] = new char[PROP_VALUE_MAX];
79 for (int j = 0; j < value_lens[i]; j++) {
80 values[i][j] = prop_name_chars[random() % (sizeof(prop_name_chars) - 1)];
81 }
82
83 if (system_properties_.Add(names[i], name_lens[i], values[i], value_lens[i]) < 0) {
84 printf("Failed to add a property, terminating...\n");
85 printf("%s = %.*s\n", names[i], value_lens[i], values[i]);
86 exit(1);
87 }
88 }
89
90 valid = true;
91 }
92
system_propertiesLocalPropertyTestState93 SystemProperties& system_properties() {
94 return system_properties_;
95 }
96
~LocalPropertyTestStateLocalPropertyTestState97 ~LocalPropertyTestState() {
98 if (!valid) {
99 return;
100 }
101
102 system_properties_.contexts_->FreeAndUnmap();
103 if (system_properties_.appcompat_override_contexts_) {
104 system_properties_.appcompat_override_contexts_->FreeAndUnmap();
105 }
106
107 for (int i = 0; i < nprops; i++) {
108 delete names[i];
109 delete values[i];
110 }
111 delete[] names;
112 delete[] name_lens;
113 delete[] values;
114 delete[] value_lens;
115 }
116
117 public:
118 const int nprops;
119 char** names;
120 int* name_lens;
121 char** values;
122 int* value_lens;
123 bool valid;
124
125 private:
126 SystemProperties system_properties_;
127 TemporaryDir dir_;
128 };
129
BM_property_get(benchmark::State & state)130 static void BM_property_get(benchmark::State& state) {
131 const size_t nprops = state.range(0);
132
133 LocalPropertyTestState pa(nprops);
134 if (!pa.valid) return;
135
136 while (state.KeepRunning()) {
137 char value[PROP_VALUE_MAX];
138 pa.system_properties().Get(pa.names[random() % nprops], value);
139 }
140 }
141 BIONIC_BENCHMARK_WITH_ARG(BM_property_get, "NUM_PROPS");
142
BM_property_find(benchmark::State & state)143 static void BM_property_find(benchmark::State& state) {
144 const size_t nprops = state.range(0);
145
146 LocalPropertyTestState pa(nprops);
147 if (!pa.valid) return;
148
149 while (state.KeepRunning()) {
150 pa.system_properties().Find(pa.names[random() % nprops]);
151 }
152 }
153 BIONIC_BENCHMARK_WITH_ARG(BM_property_find, "NUM_PROPS");
154
BM_property_read(benchmark::State & state)155 static void BM_property_read(benchmark::State& state) {
156 const size_t nprops = state.range(0);
157
158 LocalPropertyTestState pa(nprops);
159 if (!pa.valid) return;
160
161 const prop_info** pinfo = new const prop_info*[nprops];
162 char propvalue[PROP_VALUE_MAX];
163
164 for (size_t i = 0; i < nprops; ++i) {
165 pinfo[i] = pa.system_properties().Find(pa.names[random() % nprops]);
166 }
167
168 size_t i = 0;
169 while (state.KeepRunning()) {
170 pa.system_properties().Read(pinfo[i], nullptr, propvalue);
171 i = (i + 1) % nprops;
172 }
173
174 delete[] pinfo;
175 }
176 BIONIC_BENCHMARK_WITH_ARG(BM_property_read, "NUM_PROPS");
177
BM_property_serial(benchmark::State & state)178 static void BM_property_serial(benchmark::State& state) {
179 const size_t nprops = state.range(0);
180
181 LocalPropertyTestState pa(nprops);
182 if (!pa.valid) return;
183
184 const prop_info** pinfo = new const prop_info*[nprops];
185 for (size_t i = 0; i < nprops; ++i) {
186 pinfo[i] = pa.system_properties().Find(pa.names[random() % nprops]);
187 }
188
189 size_t i = 0;
190 while (state.KeepRunning()) {
191 __system_property_serial(pinfo[i]);
192 i = (i + 1) % nprops;
193 }
194
195 delete[] pinfo;
196 }
197 BIONIC_BENCHMARK_WITH_ARG(BM_property_serial, "NUM_PROPS");
198
199 // This benchmarks find the actual properties currently set on the system and accessible by the
200 // user that runs this benchmark (aka this is best run as root). It is not comparable between
201 // devices, nor even boots, but is useful to understand the the real end-to-end speed, including
202 // costs to find the correct property file within /dev/__properties__.
BM_property_find_real(benchmark::State & state)203 static void BM_property_find_real(benchmark::State& state) {
204 std::vector<std::string> properties;
205 __system_property_foreach(
206 [](const prop_info* pi, void* cookie) {
207 __system_property_read_callback(pi,
208 [](void* cookie, const char* name, const char*, unsigned) {
209 auto properties =
210 reinterpret_cast<std::vector<std::string>*>(cookie);
211 properties->emplace_back(name);
212 },
213 cookie);
214 },
215 &properties);
216
217 while (state.KeepRunning()) {
218 for (const auto& property : properties) {
219 __system_property_find(property.c_str());
220 }
221 }
222 }
223 BIONIC_BENCHMARK(BM_property_find_real);
224
225 #endif // __BIONIC__
226