Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 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 | |
Christopher Ferris | 8b1ade5 | 2014-05-01 13:00:32 -0700 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 20 | #include <unistd.h> |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 21 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 22 | #include <string> |
| 23 | |
Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 24 | #include <android-base/test_utils.h> |
| 25 | |
| 26 | using namespace std::literals; |
| 27 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 28 | #if defined(__BIONIC__) |
| 29 | |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 30 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ |
| 31 | #include <sys/_system_properties.h> |
| 32 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 33 | #include <benchmark/benchmark.h> |
Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 34 | #include <system_properties/system_properties.h> |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 35 | #include "util.h" |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 36 | |
| 37 | struct LocalPropertyTestState { |
Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 38 | explicit LocalPropertyTestState(int nprops) |
| 39 | : nprops(nprops), valid(false), system_properties_(false) { |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 40 | static const char prop_name_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_."; |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 41 | |
Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 42 | valid = system_properties_.AreaInit(dir_.path, nullptr); |
| 43 | if (!valid) { |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 44 | return; |
| 45 | } |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 46 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 47 | 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 Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 70 | } |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 71 | 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 Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 82 | if (system_properties_.Add(names[i], name_lens[i], values[i], value_lens[i]) < 0) { |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 83 | 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 Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 87 | } |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 88 | |
| 89 | valid = true; |
| 90 | } |
| 91 | |
Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 92 | SystemProperties& system_properties() { |
| 93 | return system_properties_; |
| 94 | } |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 95 | |
Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 96 | ~LocalPropertyTestState() { |
| 97 | if (!valid) { |
| 98 | return; |
| 99 | } |
| 100 | |
Tom Cherry | ee8e3dd | 2018-02-21 15:01:22 -0800 | [diff] [blame^] | 101 | system_properties_.contexts_->FreeAndUnmap(); |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 102 | |
| 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 Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 112 | |
| 113 | public: |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 114 | const int nprops; |
| 115 | char** names; |
| 116 | int* name_lens; |
| 117 | char** values; |
| 118 | int* value_lens; |
| 119 | bool valid; |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 120 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 121 | private: |
Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 122 | SystemProperties system_properties_; |
| 123 | TemporaryDir dir_; |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 124 | }; |
| 125 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 126 | static void BM_property_get(benchmark::State& state) { |
Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 127 | const size_t nprops = state.range(0); |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 128 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 129 | LocalPropertyTestState pa(nprops); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 130 | if (!pa.valid) return; |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 131 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 132 | while (state.KeepRunning()) { |
| 133 | char value[PROP_VALUE_MAX]; |
Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 134 | pa.system_properties().Get(pa.names[random() % nprops], value); |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 135 | } |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 136 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 137 | BIONIC_BENCHMARK_WITH_ARG(BM_property_get, "NUM_PROPS"); |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 138 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 139 | static void BM_property_find(benchmark::State& state) { |
Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 140 | const size_t nprops = state.range(0); |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 141 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 142 | LocalPropertyTestState pa(nprops); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 143 | if (!pa.valid) return; |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 144 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 145 | while (state.KeepRunning()) { |
Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 146 | pa.system_properties().Find(pa.names[random() % nprops]); |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 147 | } |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 148 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 149 | BIONIC_BENCHMARK_WITH_ARG(BM_property_find, "NUM_PROPS"); |
Brigid Smith | a304476 | 2014-07-09 10:26:17 -0700 | [diff] [blame] | 150 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 151 | static void BM_property_read(benchmark::State& state) { |
Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 152 | const size_t nprops = state.range(0); |
Brigid Smith | a304476 | 2014-07-09 10:26:17 -0700 | [diff] [blame] | 153 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 154 | LocalPropertyTestState pa(nprops); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 155 | if (!pa.valid) return; |
Brigid Smith | a304476 | 2014-07-09 10:26:17 -0700 | [diff] [blame] | 156 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 157 | const prop_info** pinfo = new const prop_info*[nprops]; |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 158 | char propvalue[PROP_VALUE_MAX]; |
Brigid Smith | a304476 | 2014-07-09 10:26:17 -0700 | [diff] [blame] | 159 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 160 | for (size_t i = 0; i < nprops; ++i) { |
Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 161 | pinfo[i] = pa.system_properties().Find(pa.names[random() % nprops]); |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 162 | } |
Brigid Smith | a304476 | 2014-07-09 10:26:17 -0700 | [diff] [blame] | 163 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 164 | size_t i = 0; |
| 165 | while (state.KeepRunning()) { |
Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 166 | pa.system_properties().Read(pinfo[i], 0, propvalue); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 167 | i = (i + 1) % nprops; |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 168 | } |
Brigid Smith | a304476 | 2014-07-09 10:26:17 -0700 | [diff] [blame] | 169 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 170 | delete[] pinfo; |
Brigid Smith | a304476 | 2014-07-09 10:26:17 -0700 | [diff] [blame] | 171 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 172 | BIONIC_BENCHMARK_WITH_ARG(BM_property_read, "NUM_PROPS"); |
Brigid Smith | 28417e6 | 2014-07-09 15:48:37 -0700 | [diff] [blame] | 173 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 174 | static void BM_property_serial(benchmark::State& state) { |
Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 175 | const size_t nprops = state.range(0); |
Brigid Smith | 28417e6 | 2014-07-09 15:48:37 -0700 | [diff] [blame] | 176 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 177 | LocalPropertyTestState pa(nprops); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 178 | if (!pa.valid) return; |
Brigid Smith | 28417e6 | 2014-07-09 15:48:37 -0700 | [diff] [blame] | 179 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 180 | const prop_info** pinfo = new const prop_info*[nprops]; |
| 181 | for (size_t i = 0; i < nprops; ++i) { |
Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 182 | pinfo[i] = pa.system_properties().Find(pa.names[random() % nprops]); |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 183 | } |
Brigid Smith | 28417e6 | 2014-07-09 15:48:37 -0700 | [diff] [blame] | 184 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 185 | size_t i = 0; |
| 186 | while (state.KeepRunning()) { |
Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 187 | pa.system_properties().Serial(pinfo[i]); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 188 | i = (i + 1) % nprops; |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 189 | } |
Brigid Smith | 28417e6 | 2014-07-09 15:48:37 -0700 | [diff] [blame] | 190 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 191 | delete[] pinfo; |
Brigid Smith | 28417e6 | 2014-07-09 15:48:37 -0700 | [diff] [blame] | 192 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 193 | BIONIC_BENCHMARK_WITH_ARG(BM_property_serial, "NUM_PROPS"); |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 194 | |
| 195 | #endif // __BIONIC__ |