blob: 77814bf8ad2ed0264086cc5629c05ad812d0828b [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>
23
Mark Salyzynba1a7232018-11-14 15:19:53 -080024#include <android-base/file.h>
Tom Cherrye275d6d2017-12-11 23:31:33 -080025
26using namespace std::literals;
27
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080028#if defined(__BIONIC__)
29
Colin Crossb27e2002013-01-28 17:19:43 -080030#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
31#include <sys/_system_properties.h>
32
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
Tom Cherrye275d6d2017-12-11 23:31:33 -080042 valid = system_properties_.AreaInit(dir_.path, nullptr);
43 if (!valid) {
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080044 return;
45 }
Greg Hackmanncb215a72013-02-13 14:41:48 -080046
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080047 names = new char* [nprops];
48 name_lens = new int[nprops];
49 values = new char* [nprops];
50 value_lens = new int[nprops];
51
52 srandom(nprops);
53
54 for (int i = 0; i < nprops; i++) {
55 // Make sure the name has at least 10 characters to make
56 // it very unlikely to generate the same random name.
57 name_lens[i] = (random() % (PROP_NAME_MAX - 10)) + 10;
58 names[i] = new char[PROP_NAME_MAX + 1];
59 size_t prop_name_len = sizeof(prop_name_chars) - 1;
60 for (int j = 0; j < name_lens[i]; j++) {
61 if (j == 0 || names[i][j-1] == '.' || j == name_lens[i] - 1) {
62 // Certain values are not allowed:
63 // - Don't start name with '.'
64 // - Don't allow '.' to appear twice in a row
65 // - Don't allow the name to end with '.'
66 // This assumes that '.' is the last character in the
67 // array so that decrementing the length by one removes
68 // the value from the possible values.
69 prop_name_len--;
Colin Crossb27e2002013-01-28 17:19:43 -080070 }
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080071 names[i][j] = prop_name_chars[random() % prop_name_len];
72 }
73 names[i][name_lens[i]] = 0;
74
75 // Make sure the value contains at least 1 character.
76 value_lens[i] = (random() % (PROP_VALUE_MAX - 1)) + 1;
77 values[i] = new char[PROP_VALUE_MAX];
78 for (int j = 0; j < value_lens[i]; j++) {
79 values[i][j] = prop_name_chars[random() % (sizeof(prop_name_chars) - 1)];
80 }
81
Tom Cherrye275d6d2017-12-11 23:31:33 -080082 if (system_properties_.Add(names[i], name_lens[i], values[i], value_lens[i]) < 0) {
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080083 printf("Failed to add a property, terminating...\n");
84 printf("%s = %.*s\n", names[i], value_lens[i], values[i]);
85 exit(1);
86 }
Colin Crossb27e2002013-01-28 17:19:43 -080087 }
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080088
89 valid = true;
90 }
91
Tom Cherrye275d6d2017-12-11 23:31:33 -080092 SystemProperties& system_properties() {
93 return system_properties_;
94 }
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080095
Tom Cherrye275d6d2017-12-11 23:31:33 -080096 ~LocalPropertyTestState() {
97 if (!valid) {
98 return;
99 }
100
Tom Cherryee8e3dd2018-02-21 15:01:22 -0800101 system_properties_.contexts_->FreeAndUnmap();
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800102
103 for (int i = 0; i < nprops; i++) {
104 delete names[i];
105 delete values[i];
106 }
107 delete[] names;
108 delete[] name_lens;
109 delete[] values;
110 delete[] value_lens;
111 }
Elliott Hughes281e06b2016-02-17 10:23:52 -0800112
113 public:
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800114 const int nprops;
115 char** names;
116 int* name_lens;
117 char** values;
118 int* value_lens;
119 bool valid;
Colin Crossb27e2002013-01-28 17:19:43 -0800120
Elliott Hughes281e06b2016-02-17 10:23:52 -0800121 private:
Tom Cherrye275d6d2017-12-11 23:31:33 -0800122 SystemProperties system_properties_;
123 TemporaryDir dir_;
Colin Crossb27e2002013-01-28 17:19:43 -0800124};
125
Elliott Hughes281e06b2016-02-17 10:23:52 -0800126static void BM_property_get(benchmark::State& state) {
Martijn Coenenbe763d82016-11-14 14:16:08 +0100127 const size_t nprops = state.range(0);
Colin Crossb27e2002013-01-28 17:19:43 -0800128
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800129 LocalPropertyTestState pa(nprops);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800130 if (!pa.valid) return;
Colin Crossb27e2002013-01-28 17:19:43 -0800131
Elliott Hughes281e06b2016-02-17 10:23:52 -0800132 while (state.KeepRunning()) {
133 char value[PROP_VALUE_MAX];
Tom Cherrye275d6d2017-12-11 23:31:33 -0800134 pa.system_properties().Get(pa.names[random() % nprops], value);
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800135 }
Colin Crossb27e2002013-01-28 17:19:43 -0800136}
Christopher Ferris858e3362017-11-30 08:53:15 -0800137BIONIC_BENCHMARK_WITH_ARG(BM_property_get, "NUM_PROPS");
Colin Crossb27e2002013-01-28 17:19:43 -0800138
Elliott Hughes281e06b2016-02-17 10:23:52 -0800139static void BM_property_find(benchmark::State& state) {
Martijn Coenenbe763d82016-11-14 14:16:08 +0100140 const size_t nprops = state.range(0);
Colin Crossb27e2002013-01-28 17:19:43 -0800141
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800142 LocalPropertyTestState pa(nprops);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800143 if (!pa.valid) return;
Colin Crossb27e2002013-01-28 17:19:43 -0800144
Elliott Hughes281e06b2016-02-17 10:23:52 -0800145 while (state.KeepRunning()) {
Tom Cherrye275d6d2017-12-11 23:31:33 -0800146 pa.system_properties().Find(pa.names[random() % nprops]);
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800147 }
Colin Crossb27e2002013-01-28 17:19:43 -0800148}
Christopher Ferris858e3362017-11-30 08:53:15 -0800149BIONIC_BENCHMARK_WITH_ARG(BM_property_find, "NUM_PROPS");
Brigid Smitha3044762014-07-09 10:26:17 -0700150
Elliott Hughes281e06b2016-02-17 10:23:52 -0800151static void BM_property_read(benchmark::State& state) {
Martijn Coenenbe763d82016-11-14 14:16:08 +0100152 const size_t nprops = state.range(0);
Brigid Smitha3044762014-07-09 10:26:17 -0700153
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800154 LocalPropertyTestState pa(nprops);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800155 if (!pa.valid) return;
Brigid Smitha3044762014-07-09 10:26:17 -0700156
Elliott Hughes281e06b2016-02-17 10:23:52 -0800157 const prop_info** pinfo = new const prop_info*[nprops];
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800158 char propvalue[PROP_VALUE_MAX];
Brigid Smitha3044762014-07-09 10:26:17 -0700159
Elliott Hughes281e06b2016-02-17 10:23:52 -0800160 for (size_t i = 0; i < nprops; ++i) {
Tom Cherrye275d6d2017-12-11 23:31:33 -0800161 pinfo[i] = pa.system_properties().Find(pa.names[random() % nprops]);
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800162 }
Brigid Smitha3044762014-07-09 10:26:17 -0700163
Elliott Hughes281e06b2016-02-17 10:23:52 -0800164 size_t i = 0;
165 while (state.KeepRunning()) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700166 pa.system_properties().Read(pinfo[i], nullptr, propvalue);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800167 i = (i + 1) % nprops;
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800168 }
Brigid Smitha3044762014-07-09 10:26:17 -0700169
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800170 delete[] pinfo;
Brigid Smitha3044762014-07-09 10:26:17 -0700171}
Christopher Ferris858e3362017-11-30 08:53:15 -0800172BIONIC_BENCHMARK_WITH_ARG(BM_property_read, "NUM_PROPS");
Brigid Smith28417e62014-07-09 15:48:37 -0700173
Elliott Hughes281e06b2016-02-17 10:23:52 -0800174static void BM_property_serial(benchmark::State& state) {
Martijn Coenenbe763d82016-11-14 14:16:08 +0100175 const size_t nprops = state.range(0);
Brigid Smith28417e62014-07-09 15:48:37 -0700176
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800177 LocalPropertyTestState pa(nprops);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800178 if (!pa.valid) return;
Brigid Smith28417e62014-07-09 15:48:37 -0700179
Elliott Hughes281e06b2016-02-17 10:23:52 -0800180 const prop_info** pinfo = new const prop_info*[nprops];
181 for (size_t i = 0; i < nprops; ++i) {
Tom Cherrye275d6d2017-12-11 23:31:33 -0800182 pinfo[i] = pa.system_properties().Find(pa.names[random() % nprops]);
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800183 }
Brigid Smith28417e62014-07-09 15:48:37 -0700184
Elliott Hughes281e06b2016-02-17 10:23:52 -0800185 size_t i = 0;
186 while (state.KeepRunning()) {
Tom Cherrye275d6d2017-12-11 23:31:33 -0800187 pa.system_properties().Serial(pinfo[i]);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800188 i = (i + 1) % nprops;
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800189 }
Brigid Smith28417e62014-07-09 15:48:37 -0700190
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800191 delete[] pinfo;
Brigid Smith28417e62014-07-09 15:48:37 -0700192}
Christopher Ferris858e3362017-11-30 08:53:15 -0800193BIONIC_BENCHMARK_WITH_ARG(BM_property_serial, "NUM_PROPS");
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800194
195#endif // __BIONIC__