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