blob: ff3618e5ef8cde615fc7ebe75e610a8253d3e973 [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
Colin Crossb27e2002013-01-28 17:19:43 -080031#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
32#include <sys/_system_properties.h>
33
Elliott Hughes281e06b2016-02-17 10:23:52 -080034#include <benchmark/benchmark.h>
Tom Cherrye275d6d2017-12-11 23:31:33 -080035#include <system_properties/system_properties.h>
Anders Lewisa7b0f882017-07-24 20:01:13 -070036#include "util.h"
Colin Crossb27e2002013-01-28 17:19:43 -080037
38struct LocalPropertyTestState {
Tom Cherrye275d6d2017-12-11 23:31:33 -080039 explicit LocalPropertyTestState(int nprops)
40 : nprops(nprops), valid(false), system_properties_(false) {
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080041 static const char prop_name_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_.";
Greg Hackmanncb215a72013-02-13 14:41:48 -080042
Nate Myren58df4df2023-11-07 15:20:49 -080043 valid = system_properties_.AreaInit(dir_.path, nullptr, true);
Tom Cherrye275d6d2017-12-11 23:31:33 -080044 if (!valid) {
Nate Myren58df4df2023-11-07 15:20:49 -080045 printf("Failed to initialize properties, terminating...\n");
46 exit(1);
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080047 }
Greg Hackmanncb215a72013-02-13 14:41:48 -080048
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080049 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--;
Colin Crossb27e2002013-01-28 17:19:43 -080072 }
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080073 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
Tom Cherrye275d6d2017-12-11 23:31:33 -080084 if (system_properties_.Add(names[i], name_lens[i], values[i], value_lens[i]) < 0) {
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080085 printf("Failed to add a property, terminating...\n");
86 printf("%s = %.*s\n", names[i], value_lens[i], values[i]);
87 exit(1);
88 }
Colin Crossb27e2002013-01-28 17:19:43 -080089 }
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080090
91 valid = true;
92 }
93
Tom Cherrye275d6d2017-12-11 23:31:33 -080094 SystemProperties& system_properties() {
95 return system_properties_;
96 }
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080097
Tom Cherrye275d6d2017-12-11 23:31:33 -080098 ~LocalPropertyTestState() {
99 if (!valid) {
100 return;
101 }
102
Tom Cherryee8e3dd2018-02-21 15:01:22 -0800103 system_properties_.contexts_->FreeAndUnmap();
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800104
105 for (int i = 0; i < nprops; i++) {
106 delete names[i];
107 delete values[i];
108 }
109 delete[] names;
110 delete[] name_lens;
111 delete[] values;
112 delete[] value_lens;
113 }
Elliott Hughes281e06b2016-02-17 10:23:52 -0800114
115 public:
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800116 const int nprops;
117 char** names;
118 int* name_lens;
119 char** values;
120 int* value_lens;
121 bool valid;
Colin Crossb27e2002013-01-28 17:19:43 -0800122
Elliott Hughes281e06b2016-02-17 10:23:52 -0800123 private:
Tom Cherrye275d6d2017-12-11 23:31:33 -0800124 SystemProperties system_properties_;
125 TemporaryDir dir_;
Colin Crossb27e2002013-01-28 17:19:43 -0800126};
127
Elliott Hughes281e06b2016-02-17 10:23:52 -0800128static void BM_property_get(benchmark::State& state) {
Martijn Coenenbe763d82016-11-14 14:16:08 +0100129 const size_t nprops = state.range(0);
Colin Crossb27e2002013-01-28 17:19:43 -0800130
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800131 LocalPropertyTestState pa(nprops);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800132 if (!pa.valid) return;
Colin Crossb27e2002013-01-28 17:19:43 -0800133
Elliott Hughes281e06b2016-02-17 10:23:52 -0800134 while (state.KeepRunning()) {
135 char value[PROP_VALUE_MAX];
Tom Cherrye275d6d2017-12-11 23:31:33 -0800136 pa.system_properties().Get(pa.names[random() % nprops], value);
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800137 }
Colin Crossb27e2002013-01-28 17:19:43 -0800138}
Christopher Ferris858e3362017-11-30 08:53:15 -0800139BIONIC_BENCHMARK_WITH_ARG(BM_property_get, "NUM_PROPS");
Colin Crossb27e2002013-01-28 17:19:43 -0800140
Elliott Hughes281e06b2016-02-17 10:23:52 -0800141static void BM_property_find(benchmark::State& state) {
Martijn Coenenbe763d82016-11-14 14:16:08 +0100142 const size_t nprops = state.range(0);
Colin Crossb27e2002013-01-28 17:19:43 -0800143
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800144 LocalPropertyTestState pa(nprops);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800145 if (!pa.valid) return;
Colin Crossb27e2002013-01-28 17:19:43 -0800146
Elliott Hughes281e06b2016-02-17 10:23:52 -0800147 while (state.KeepRunning()) {
Tom Cherrye275d6d2017-12-11 23:31:33 -0800148 pa.system_properties().Find(pa.names[random() % nprops]);
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800149 }
Colin Crossb27e2002013-01-28 17:19:43 -0800150}
Christopher Ferris858e3362017-11-30 08:53:15 -0800151BIONIC_BENCHMARK_WITH_ARG(BM_property_find, "NUM_PROPS");
Brigid Smitha3044762014-07-09 10:26:17 -0700152
Elliott Hughes281e06b2016-02-17 10:23:52 -0800153static void BM_property_read(benchmark::State& state) {
Martijn Coenenbe763d82016-11-14 14:16:08 +0100154 const size_t nprops = state.range(0);
Brigid Smitha3044762014-07-09 10:26:17 -0700155
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800156 LocalPropertyTestState pa(nprops);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800157 if (!pa.valid) return;
Brigid Smitha3044762014-07-09 10:26:17 -0700158
Elliott Hughes281e06b2016-02-17 10:23:52 -0800159 const prop_info** pinfo = new const prop_info*[nprops];
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800160 char propvalue[PROP_VALUE_MAX];
Brigid Smitha3044762014-07-09 10:26:17 -0700161
Elliott Hughes281e06b2016-02-17 10:23:52 -0800162 for (size_t i = 0; i < nprops; ++i) {
Tom Cherrye275d6d2017-12-11 23:31:33 -0800163 pinfo[i] = pa.system_properties().Find(pa.names[random() % nprops]);
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800164 }
Brigid Smitha3044762014-07-09 10:26:17 -0700165
Elliott Hughes281e06b2016-02-17 10:23:52 -0800166 size_t i = 0;
167 while (state.KeepRunning()) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700168 pa.system_properties().Read(pinfo[i], nullptr, propvalue);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800169 i = (i + 1) % nprops;
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800170 }
Brigid Smitha3044762014-07-09 10:26:17 -0700171
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800172 delete[] pinfo;
Brigid Smitha3044762014-07-09 10:26:17 -0700173}
Christopher Ferris858e3362017-11-30 08:53:15 -0800174BIONIC_BENCHMARK_WITH_ARG(BM_property_read, "NUM_PROPS");
Brigid Smith28417e62014-07-09 15:48:37 -0700175
Elliott Hughes281e06b2016-02-17 10:23:52 -0800176static void BM_property_serial(benchmark::State& state) {
Martijn Coenenbe763d82016-11-14 14:16:08 +0100177 const size_t nprops = state.range(0);
Brigid Smith28417e62014-07-09 15:48:37 -0700178
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800179 LocalPropertyTestState pa(nprops);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800180 if (!pa.valid) return;
Brigid Smith28417e62014-07-09 15:48:37 -0700181
Elliott Hughes281e06b2016-02-17 10:23:52 -0800182 const prop_info** pinfo = new const prop_info*[nprops];
183 for (size_t i = 0; i < nprops; ++i) {
Tom Cherrye275d6d2017-12-11 23:31:33 -0800184 pinfo[i] = pa.system_properties().Find(pa.names[random() % nprops]);
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800185 }
Brigid Smith28417e62014-07-09 15:48:37 -0700186
Elliott Hughes281e06b2016-02-17 10:23:52 -0800187 size_t i = 0;
188 while (state.KeepRunning()) {
Raman Tennetib481a2e2019-11-12 20:41:55 +0000189 __system_property_serial(pinfo[i]);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800190 i = (i + 1) % nprops;
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800191 }
Brigid Smith28417e62014-07-09 15:48:37 -0700192
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800193 delete[] pinfo;
Brigid Smith28417e62014-07-09 15:48:37 -0700194}
Christopher Ferris858e3362017-11-30 08:53:15 -0800195BIONIC_BENCHMARK_WITH_ARG(BM_property_serial, "NUM_PROPS");
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800196
Tom Cherry64d03a22017-11-07 13:47:37 -0800197// This benchmarks find the actual properties currently set on the system and accessible by the
198// user that runs this benchmark (aka this is best run as root). It is not comparable between
199// devices, nor even boots, but is useful to understand the the real end-to-end speed, including
200// costs to find the correct property file within /dev/__properties__.
201static void BM_property_find_real(benchmark::State& state) {
202 std::vector<std::string> properties;
203 __system_property_foreach(
204 [](const prop_info* pi, void* cookie) {
205 __system_property_read_callback(pi,
206 [](void* cookie, const char* name, const char*, unsigned) {
207 auto properties =
208 reinterpret_cast<std::vector<std::string>*>(cookie);
209 properties->emplace_back(name);
210 },
211 cookie);
212 },
213 &properties);
214
215 while (state.KeepRunning()) {
216 for (const auto& property : properties) {
217 __system_property_find(property.c_str());
218 }
219 }
220}
221BIONIC_BENCHMARK(BM_property_find_real);
222
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800223#endif // __BIONIC__