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