blob: b356ab7349a1524d2431f85ccc901a626a62d040 [file] [log] [blame]
Colin Crossb27e2002013-01-28 17:19:43 -08001/*
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
Christopher Ferris8b1ade52014-05-01 13:00:32 -070017#include <errno.h>
18#include <stdio.h>
19#include <stdlib.h>
Greg Hackmanncb215a72013-02-13 14:41:48 -080020#include <unistd.h>
Colin Crossb27e2002013-01-28 17:19:43 -080021
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080022#include <string>
Tom Cherry64d03a22017-11-07 13:47:37 -080023#include <vector>
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080024
Mark Salyzynba1a7232018-11-14 15:19:53 -080025#include <android-base/file.h>
Tom Cherrye275d6d2017-12-11 23:31:33 -080026
27using namespace std::literals;
28
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080029#if defined(__BIONIC__)
30
Elliott Hughes620eec12024-08-07 17:59:53 +000031#include <sys/system_properties.h>
Colin Crossb27e2002013-01-28 17:19:43 -080032
Elliott Hughes281e06b2016-02-17 10:23:52 -080033#include <benchmark/benchmark.h>
Tom Cherrye275d6d2017-12-11 23:31:33 -080034#include <system_properties/system_properties.h>
Anders Lewisa7b0f882017-07-24 20:01:13 -070035#include "util.h"
Colin Crossb27e2002013-01-28 17:19:43 -080036
37struct LocalPropertyTestState {
Tom Cherrye275d6d2017-12-11 23:31:33 -080038 explicit LocalPropertyTestState(int nprops)
39 : nprops(nprops), valid(false), system_properties_(false) {
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080040 static const char prop_name_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_.";
Greg Hackmanncb215a72013-02-13 14:41:48 -080041
Nate Myren58df4df2023-11-07 15:20:49 -080042 valid = system_properties_.AreaInit(dir_.path, nullptr, true);
Tom Cherrye275d6d2017-12-11 23:31:33 -080043 if (!valid) {
Nate Myren58df4df2023-11-07 15:20:49 -080044 printf("Failed to initialize properties, terminating...\n");
45 exit(1);
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080046 }
Greg Hackmanncb215a72013-02-13 14:41:48 -080047
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080048 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--;
Colin Crossb27e2002013-01-28 17:19:43 -080071 }
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080072 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
Tom Cherrye275d6d2017-12-11 23:31:33 -080083 if (system_properties_.Add(names[i], name_lens[i], values[i], value_lens[i]) < 0) {
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080084 printf("Failed to add a property, terminating...\n");
85 printf("%s = %.*s\n", names[i], value_lens[i], values[i]);
86 exit(1);
87 }
Colin Crossb27e2002013-01-28 17:19:43 -080088 }
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080089
90 valid = true;
91 }
92
Tom Cherrye275d6d2017-12-11 23:31:33 -080093 SystemProperties& system_properties() {
94 return system_properties_;
95 }
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080096
Tom Cherrye275d6d2017-12-11 23:31:33 -080097 ~LocalPropertyTestState() {
98 if (!valid) {
99 return;
100 }
101
Tom Cherryee8e3dd2018-02-21 15:01:22 -0800102 system_properties_.contexts_->FreeAndUnmap();
Nate Myren08b0a682023-11-10 11:09:46 -0800103 if (system_properties_.appcompat_override_contexts_) {
104 system_properties_.appcompat_override_contexts_->FreeAndUnmap();
105 }
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800106
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 }
Elliott Hughes281e06b2016-02-17 10:23:52 -0800116
117 public:
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800118 const int nprops;
119 char** names;
120 int* name_lens;
121 char** values;
122 int* value_lens;
123 bool valid;
Colin Crossb27e2002013-01-28 17:19:43 -0800124
Elliott Hughes281e06b2016-02-17 10:23:52 -0800125 private:
Tom Cherrye275d6d2017-12-11 23:31:33 -0800126 SystemProperties system_properties_;
127 TemporaryDir dir_;
Colin Crossb27e2002013-01-28 17:19:43 -0800128};
129
Elliott Hughes281e06b2016-02-17 10:23:52 -0800130static void BM_property_get(benchmark::State& state) {
Martijn Coenenbe763d82016-11-14 14:16:08 +0100131 const size_t nprops = state.range(0);
Colin Crossb27e2002013-01-28 17:19:43 -0800132
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800133 LocalPropertyTestState pa(nprops);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800134 if (!pa.valid) return;
Colin Crossb27e2002013-01-28 17:19:43 -0800135
Elliott Hughes281e06b2016-02-17 10:23:52 -0800136 while (state.KeepRunning()) {
137 char value[PROP_VALUE_MAX];
Tom Cherrye275d6d2017-12-11 23:31:33 -0800138 pa.system_properties().Get(pa.names[random() % nprops], value);
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800139 }
Colin Crossb27e2002013-01-28 17:19:43 -0800140}
Christopher Ferris858e3362017-11-30 08:53:15 -0800141BIONIC_BENCHMARK_WITH_ARG(BM_property_get, "NUM_PROPS");
Colin Crossb27e2002013-01-28 17:19:43 -0800142
Elliott Hughes281e06b2016-02-17 10:23:52 -0800143static void BM_property_find(benchmark::State& state) {
Martijn Coenenbe763d82016-11-14 14:16:08 +0100144 const size_t nprops = state.range(0);
Colin Crossb27e2002013-01-28 17:19:43 -0800145
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800146 LocalPropertyTestState pa(nprops);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800147 if (!pa.valid) return;
Colin Crossb27e2002013-01-28 17:19:43 -0800148
Elliott Hughes281e06b2016-02-17 10:23:52 -0800149 while (state.KeepRunning()) {
Tom Cherrye275d6d2017-12-11 23:31:33 -0800150 pa.system_properties().Find(pa.names[random() % nprops]);
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800151 }
Colin Crossb27e2002013-01-28 17:19:43 -0800152}
Christopher Ferris858e3362017-11-30 08:53:15 -0800153BIONIC_BENCHMARK_WITH_ARG(BM_property_find, "NUM_PROPS");
Brigid Smitha3044762014-07-09 10:26:17 -0700154
Elliott Hughes281e06b2016-02-17 10:23:52 -0800155static void BM_property_read(benchmark::State& state) {
Martijn Coenenbe763d82016-11-14 14:16:08 +0100156 const size_t nprops = state.range(0);
Brigid Smitha3044762014-07-09 10:26:17 -0700157
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800158 LocalPropertyTestState pa(nprops);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800159 if (!pa.valid) return;
Brigid Smitha3044762014-07-09 10:26:17 -0700160
Elliott Hughes281e06b2016-02-17 10:23:52 -0800161 const prop_info** pinfo = new const prop_info*[nprops];
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800162 char propvalue[PROP_VALUE_MAX];
Brigid Smitha3044762014-07-09 10:26:17 -0700163
Elliott Hughes281e06b2016-02-17 10:23:52 -0800164 for (size_t i = 0; i < nprops; ++i) {
Tom Cherrye275d6d2017-12-11 23:31:33 -0800165 pinfo[i] = pa.system_properties().Find(pa.names[random() % nprops]);
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800166 }
Brigid Smitha3044762014-07-09 10:26:17 -0700167
Elliott Hughes281e06b2016-02-17 10:23:52 -0800168 size_t i = 0;
169 while (state.KeepRunning()) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700170 pa.system_properties().Read(pinfo[i], nullptr, propvalue);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800171 i = (i + 1) % nprops;
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800172 }
Brigid Smitha3044762014-07-09 10:26:17 -0700173
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800174 delete[] pinfo;
Brigid Smitha3044762014-07-09 10:26:17 -0700175}
Christopher Ferris858e3362017-11-30 08:53:15 -0800176BIONIC_BENCHMARK_WITH_ARG(BM_property_read, "NUM_PROPS");
Brigid Smith28417e62014-07-09 15:48:37 -0700177
Elliott Hughes281e06b2016-02-17 10:23:52 -0800178static void BM_property_serial(benchmark::State& state) {
Martijn Coenenbe763d82016-11-14 14:16:08 +0100179 const size_t nprops = state.range(0);
Brigid Smith28417e62014-07-09 15:48:37 -0700180
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800181 LocalPropertyTestState pa(nprops);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800182 if (!pa.valid) return;
Brigid Smith28417e62014-07-09 15:48:37 -0700183
Elliott Hughes281e06b2016-02-17 10:23:52 -0800184 const prop_info** pinfo = new const prop_info*[nprops];
185 for (size_t i = 0; i < nprops; ++i) {
Tom Cherrye275d6d2017-12-11 23:31:33 -0800186 pinfo[i] = pa.system_properties().Find(pa.names[random() % nprops]);
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800187 }
Brigid Smith28417e62014-07-09 15:48:37 -0700188
Elliott Hughes281e06b2016-02-17 10:23:52 -0800189 size_t i = 0;
190 while (state.KeepRunning()) {
Raman Tennetib481a2e2019-11-12 20:41:55 +0000191 __system_property_serial(pinfo[i]);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800192 i = (i + 1) % nprops;
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800193 }
Brigid Smith28417e62014-07-09 15:48:37 -0700194
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800195 delete[] pinfo;
Brigid Smith28417e62014-07-09 15:48:37 -0700196}
Christopher Ferris858e3362017-11-30 08:53:15 -0800197BIONIC_BENCHMARK_WITH_ARG(BM_property_serial, "NUM_PROPS");
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800198
Tom Cherry64d03a22017-11-07 13:47:37 -0800199// 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__.
203static 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}
223BIONIC_BENCHMARK(BM_property_find_real);
224
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800225#endif // __BIONIC__