| 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> | 
| Tom Cherry | 64d03a2 | 2017-11-07 13:47:37 -0800 | [diff] [blame] | 23 | #include <vector> | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 24 |  | 
| Mark Salyzyn | ba1a723 | 2018-11-14 15:19:53 -0800 | [diff] [blame] | 25 | #include <android-base/file.h> | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 26 |  | 
 | 27 | using namespace std::literals; | 
 | 28 |  | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 29 | #if defined(__BIONIC__) | 
 | 30 |  | 
| Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 31 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ | 
 | 32 | #include <sys/_system_properties.h> | 
 | 33 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 34 | #include <benchmark/benchmark.h> | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 35 | #include <system_properties/system_properties.h> | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 36 | #include "util.h" | 
| Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 37 |  | 
 | 38 | struct LocalPropertyTestState { | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 39 |   explicit LocalPropertyTestState(int nprops) | 
 | 40 |       : nprops(nprops), valid(false), system_properties_(false) { | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 41 |     static const char prop_name_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_."; | 
| Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 42 |  | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 43 |     valid = system_properties_.AreaInit(dir_.path, nullptr); | 
 | 44 |     if (!valid) { | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 45 |       return; | 
 | 46 |     } | 
| Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 47 |  | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 48 |     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 Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 71 |         } | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 72 |         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 Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 83 |       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] | 84 |         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 Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 88 |     } | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 89 |  | 
 | 90 |     valid = true; | 
 | 91 |   } | 
 | 92 |  | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 93 |   SystemProperties& system_properties() { | 
 | 94 |     return system_properties_; | 
 | 95 |   } | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 96 |  | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 97 |   ~LocalPropertyTestState() { | 
 | 98 |     if (!valid) { | 
 | 99 |       return; | 
 | 100 |     } | 
 | 101 |  | 
| Tom Cherry | ee8e3dd | 2018-02-21 15:01:22 -0800 | [diff] [blame] | 102 |     system_properties_.contexts_->FreeAndUnmap(); | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 103 |  | 
 | 104 |     for (int i = 0; i < nprops; i++) { | 
 | 105 |       delete names[i]; | 
 | 106 |       delete values[i]; | 
 | 107 |     } | 
 | 108 |     delete[] names; | 
 | 109 |     delete[] name_lens; | 
 | 110 |     delete[] values; | 
 | 111 |     delete[] value_lens; | 
 | 112 |   } | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 113 |  | 
 | 114 |  public: | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 115 |   const int nprops; | 
 | 116 |   char** names; | 
 | 117 |   int* name_lens; | 
 | 118 |   char** values; | 
 | 119 |   int* value_lens; | 
 | 120 |   bool valid; | 
| Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 121 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 122 |  private: | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 123 |   SystemProperties system_properties_; | 
 | 124 |   TemporaryDir dir_; | 
| Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 125 | }; | 
 | 126 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 127 | static void BM_property_get(benchmark::State& state) { | 
| Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 128 |   const size_t nprops = state.range(0); | 
| Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 129 |  | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 130 |   LocalPropertyTestState pa(nprops); | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 131 |   if (!pa.valid) return; | 
| Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 132 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 133 |   while (state.KeepRunning()) { | 
 | 134 |     char value[PROP_VALUE_MAX]; | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 135 |     pa.system_properties().Get(pa.names[random() % nprops], value); | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 136 |   } | 
| Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 137 | } | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 138 | BIONIC_BENCHMARK_WITH_ARG(BM_property_get, "NUM_PROPS"); | 
| Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 139 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 140 | static void BM_property_find(benchmark::State& state) { | 
| Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 141 |   const size_t nprops = state.range(0); | 
| Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 142 |  | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 143 |   LocalPropertyTestState pa(nprops); | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 144 |   if (!pa.valid) return; | 
| Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 145 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 146 |   while (state.KeepRunning()) { | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 147 |     pa.system_properties().Find(pa.names[random() % nprops]); | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 148 |   } | 
| Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 149 | } | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 150 | BIONIC_BENCHMARK_WITH_ARG(BM_property_find, "NUM_PROPS"); | 
| Brigid Smith | a304476 | 2014-07-09 10:26:17 -0700 | [diff] [blame] | 151 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 152 | static void BM_property_read(benchmark::State& state) { | 
| Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 153 |   const size_t nprops = state.range(0); | 
| Brigid Smith | a304476 | 2014-07-09 10:26:17 -0700 | [diff] [blame] | 154 |  | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 155 |   LocalPropertyTestState pa(nprops); | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 156 |   if (!pa.valid) return; | 
| Brigid Smith | a304476 | 2014-07-09 10:26:17 -0700 | [diff] [blame] | 157 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 158 |   const prop_info** pinfo = new const prop_info*[nprops]; | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 159 |   char propvalue[PROP_VALUE_MAX]; | 
| Brigid Smith | a304476 | 2014-07-09 10:26:17 -0700 | [diff] [blame] | 160 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 161 |   for (size_t i = 0; i < nprops; ++i) { | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 162 |     pinfo[i] = pa.system_properties().Find(pa.names[random() % nprops]); | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 163 |   } | 
| Brigid Smith | a304476 | 2014-07-09 10:26:17 -0700 | [diff] [blame] | 164 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 165 |   size_t i = 0; | 
 | 166 |   while (state.KeepRunning()) { | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 167 |     pa.system_properties().Read(pinfo[i], nullptr, propvalue); | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 168 |     i = (i + 1) % nprops; | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 169 |   } | 
| Brigid Smith | a304476 | 2014-07-09 10:26:17 -0700 | [diff] [blame] | 170 |  | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 171 |   delete[] pinfo; | 
| Brigid Smith | a304476 | 2014-07-09 10:26:17 -0700 | [diff] [blame] | 172 | } | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 173 | BIONIC_BENCHMARK_WITH_ARG(BM_property_read, "NUM_PROPS"); | 
| Brigid Smith | 28417e6 | 2014-07-09 15:48:37 -0700 | [diff] [blame] | 174 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 175 | static void BM_property_serial(benchmark::State& state) { | 
| Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 176 |   const size_t nprops = state.range(0); | 
| Brigid Smith | 28417e6 | 2014-07-09 15:48:37 -0700 | [diff] [blame] | 177 |  | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 178 |   LocalPropertyTestState pa(nprops); | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 179 |   if (!pa.valid) return; | 
| Brigid Smith | 28417e6 | 2014-07-09 15:48:37 -0700 | [diff] [blame] | 180 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 181 |   const prop_info** pinfo = new const prop_info*[nprops]; | 
 | 182 |   for (size_t i = 0; i < nprops; ++i) { | 
| Tom Cherry | e275d6d | 2017-12-11 23:31:33 -0800 | [diff] [blame] | 183 |     pinfo[i] = pa.system_properties().Find(pa.names[random() % nprops]); | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 184 |   } | 
| Brigid Smith | 28417e6 | 2014-07-09 15:48:37 -0700 | [diff] [blame] | 185 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 186 |   size_t i = 0; | 
 | 187 |   while (state.KeepRunning()) { | 
| Raman Tenneti | b481a2e | 2019-11-12 20:41:55 +0000 | [diff] [blame] | 188 |     __system_property_serial(pinfo[i]); | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 189 |     i = (i + 1) % nprops; | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 190 |   } | 
| Brigid Smith | 28417e6 | 2014-07-09 15:48:37 -0700 | [diff] [blame] | 191 |  | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 192 |   delete[] pinfo; | 
| Brigid Smith | 28417e6 | 2014-07-09 15:48:37 -0700 | [diff] [blame] | 193 | } | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 194 | BIONIC_BENCHMARK_WITH_ARG(BM_property_serial, "NUM_PROPS"); | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 195 |  | 
| Tom Cherry | 64d03a2 | 2017-11-07 13:47:37 -0800 | [diff] [blame] | 196 | // This benchmarks find the actual properties currently set on the system and accessible by the | 
 | 197 | // user that runs this benchmark (aka this is best run as root).  It is not comparable between | 
 | 198 | // devices, nor even boots, but is useful to understand the the real end-to-end speed, including | 
 | 199 | // costs to find the correct property file within /dev/__properties__. | 
 | 200 | static void BM_property_find_real(benchmark::State& state) { | 
 | 201 |   std::vector<std::string> properties; | 
 | 202 |   __system_property_foreach( | 
 | 203 |       [](const prop_info* pi, void* cookie) { | 
 | 204 |         __system_property_read_callback(pi, | 
 | 205 |                                         [](void* cookie, const char* name, const char*, unsigned) { | 
 | 206 |                                           auto properties = | 
 | 207 |                                               reinterpret_cast<std::vector<std::string>*>(cookie); | 
 | 208 |                                           properties->emplace_back(name); | 
 | 209 |                                         }, | 
 | 210 |                                         cookie); | 
 | 211 |       }, | 
 | 212 |       &properties); | 
 | 213 |  | 
 | 214 |   while (state.KeepRunning()) { | 
 | 215 |     for (const auto& property : properties) { | 
 | 216 |       __system_property_find(property.c_str()); | 
 | 217 |     } | 
 | 218 |   } | 
 | 219 | } | 
 | 220 | BIONIC_BENCHMARK(BM_property_find_real); | 
 | 221 |  | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 222 | #endif  // __BIONIC__ |