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 | |
| 24 | #if defined(__BIONIC__) |
| 25 | |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 26 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ |
| 27 | #include <sys/_system_properties.h> |
| 28 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 29 | #include <benchmark/benchmark.h> |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 30 | |
Christopher Ferris | 53531cc | 2014-07-15 19:09:07 -0700 | [diff] [blame] | 31 | // Do not exceed 512, that is about the largest number of properties |
| 32 | // that can be created with the current property area size. |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 33 | #define TEST_NUM_PROPS \ |
Christopher Ferris | 53531cc | 2014-07-15 19:09:07 -0700 | [diff] [blame] | 34 | Arg(1)->Arg(4)->Arg(16)->Arg(64)->Arg(128)->Arg(256)->Arg(512) |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 35 | |
| 36 | struct LocalPropertyTestState { |
Chih-Hung Hsieh | 956df72 | 2016-04-22 10:25:10 -0700 | [diff] [blame] | 37 | explicit LocalPropertyTestState(int nprops) : nprops(nprops), valid(false) { |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 38 | static const char prop_name_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_."; |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 39 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 40 | const char* android_data = getenv("ANDROID_DATA"); |
| 41 | if (android_data == NULL) { |
| 42 | printf("ANDROID_DATA environment variable not set\n"); |
| 43 | return; |
| 44 | } |
| 45 | char dir_template[PATH_MAX]; |
| 46 | snprintf(dir_template, sizeof(dir_template), "%s/local/tmp/prop-XXXXXX", android_data); |
| 47 | char* dirname = mkdtemp(dir_template); |
| 48 | if (!dirname) { |
| 49 | printf("making temp file for test state failed (is %s/local/tmp writable?): %s\n", |
| 50 | android_data, strerror(errno)); |
| 51 | return; |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 54 | pa_dirname = dirname; |
| 55 | pa_filename = pa_dirname + "/__properties__"; |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 56 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 57 | __system_property_set_filename(pa_filename.c_str()); |
| 58 | __system_property_area_init(); |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 59 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 60 | names = new char* [nprops]; |
| 61 | name_lens = new int[nprops]; |
| 62 | values = new char* [nprops]; |
| 63 | value_lens = new int[nprops]; |
| 64 | |
| 65 | srandom(nprops); |
| 66 | |
| 67 | for (int i = 0; i < nprops; i++) { |
| 68 | // Make sure the name has at least 10 characters to make |
| 69 | // it very unlikely to generate the same random name. |
| 70 | name_lens[i] = (random() % (PROP_NAME_MAX - 10)) + 10; |
| 71 | names[i] = new char[PROP_NAME_MAX + 1]; |
| 72 | size_t prop_name_len = sizeof(prop_name_chars) - 1; |
| 73 | for (int j = 0; j < name_lens[i]; j++) { |
| 74 | if (j == 0 || names[i][j-1] == '.' || j == name_lens[i] - 1) { |
| 75 | // Certain values are not allowed: |
| 76 | // - Don't start name with '.' |
| 77 | // - Don't allow '.' to appear twice in a row |
| 78 | // - Don't allow the name to end with '.' |
| 79 | // This assumes that '.' is the last character in the |
| 80 | // array so that decrementing the length by one removes |
| 81 | // the value from the possible values. |
| 82 | prop_name_len--; |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 83 | } |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 84 | names[i][j] = prop_name_chars[random() % prop_name_len]; |
| 85 | } |
| 86 | names[i][name_lens[i]] = 0; |
| 87 | |
| 88 | // Make sure the value contains at least 1 character. |
| 89 | value_lens[i] = (random() % (PROP_VALUE_MAX - 1)) + 1; |
| 90 | values[i] = new char[PROP_VALUE_MAX]; |
| 91 | for (int j = 0; j < value_lens[i]; j++) { |
| 92 | values[i][j] = prop_name_chars[random() % (sizeof(prop_name_chars) - 1)]; |
| 93 | } |
| 94 | |
| 95 | if (__system_property_add(names[i], name_lens[i], values[i], value_lens[i]) < 0) { |
| 96 | printf("Failed to add a property, terminating...\n"); |
| 97 | printf("%s = %.*s\n", names[i], value_lens[i], values[i]); |
| 98 | exit(1); |
| 99 | } |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 100 | } |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 101 | |
| 102 | valid = true; |
| 103 | } |
| 104 | |
| 105 | ~LocalPropertyTestState() { |
| 106 | if (!valid) |
| 107 | return; |
| 108 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 109 | __system_property_set_filename(PROP_FILENAME); |
Victor Khimenko | 4a92ffd | 2017-03-01 20:36:13 +0100 | [diff] [blame^] | 110 | __system_property_area_init(); |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 111 | unlink(pa_filename.c_str()); |
| 112 | rmdir(pa_dirname.c_str()); |
| 113 | |
| 114 | for (int i = 0; i < nprops; i++) { |
| 115 | delete names[i]; |
| 116 | delete values[i]; |
| 117 | } |
| 118 | delete[] names; |
| 119 | delete[] name_lens; |
| 120 | delete[] values; |
| 121 | delete[] value_lens; |
| 122 | } |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 123 | |
| 124 | public: |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 125 | const int nprops; |
| 126 | char** names; |
| 127 | int* name_lens; |
| 128 | char** values; |
| 129 | int* value_lens; |
| 130 | bool valid; |
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 | private: |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 133 | std::string pa_dirname; |
| 134 | std::string pa_filename; |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 135 | }; |
| 136 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 137 | static void BM_property_get(benchmark::State& state) { |
Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 138 | const size_t nprops = state.range(0); |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 139 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 140 | LocalPropertyTestState pa(nprops); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 141 | if (!pa.valid) return; |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 142 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 143 | while (state.KeepRunning()) { |
| 144 | char value[PROP_VALUE_MAX]; |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 145 | __system_property_get(pa.names[random() % nprops], value); |
| 146 | } |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 147 | } |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 148 | BENCHMARK(BM_property_get)->TEST_NUM_PROPS; |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 149 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 150 | static void BM_property_find(benchmark::State& state) { |
Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 151 | const size_t nprops = state.range(0); |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 152 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 153 | LocalPropertyTestState pa(nprops); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 154 | if (!pa.valid) return; |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 155 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 156 | while (state.KeepRunning()) { |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 157 | __system_property_find(pa.names[random() % nprops]); |
| 158 | } |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 159 | } |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 160 | BENCHMARK(BM_property_find)->TEST_NUM_PROPS; |
Brigid Smith | a304476 | 2014-07-09 10:26:17 -0700 | [diff] [blame] | 161 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 162 | static void BM_property_read(benchmark::State& state) { |
Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 163 | const size_t nprops = state.range(0); |
Brigid Smith | a304476 | 2014-07-09 10:26:17 -0700 | [diff] [blame] | 164 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 165 | LocalPropertyTestState pa(nprops); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 166 | if (!pa.valid) return; |
Brigid Smith | a304476 | 2014-07-09 10:26:17 -0700 | [diff] [blame] | 167 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 168 | const prop_info** pinfo = new const prop_info*[nprops]; |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 169 | char propvalue[PROP_VALUE_MAX]; |
Brigid Smith | a304476 | 2014-07-09 10:26:17 -0700 | [diff] [blame] | 170 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 171 | for (size_t i = 0; i < nprops; ++i) { |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 172 | pinfo[i] = __system_property_find(pa.names[random() % nprops]); |
| 173 | } |
Brigid Smith | a304476 | 2014-07-09 10:26:17 -0700 | [diff] [blame] | 174 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 175 | size_t i = 0; |
| 176 | while (state.KeepRunning()) { |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 177 | __system_property_read(pinfo[i], 0, propvalue); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 178 | i = (i + 1) % nprops; |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 179 | } |
Brigid Smith | a304476 | 2014-07-09 10:26:17 -0700 | [diff] [blame] | 180 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 181 | delete[] pinfo; |
Brigid Smith | a304476 | 2014-07-09 10:26:17 -0700 | [diff] [blame] | 182 | } |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 183 | BENCHMARK(BM_property_read)->TEST_NUM_PROPS; |
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 | static void BM_property_serial(benchmark::State& state) { |
Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 186 | const size_t nprops = state.range(0); |
Brigid Smith | 28417e6 | 2014-07-09 15:48:37 -0700 | [diff] [blame] | 187 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 188 | LocalPropertyTestState pa(nprops); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 189 | if (!pa.valid) return; |
Brigid Smith | 28417e6 | 2014-07-09 15:48:37 -0700 | [diff] [blame] | 190 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 191 | const prop_info** pinfo = new const prop_info*[nprops]; |
| 192 | for (size_t i = 0; i < nprops; ++i) { |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 193 | pinfo[i] = __system_property_find(pa.names[random() % nprops]); |
| 194 | } |
Brigid Smith | 28417e6 | 2014-07-09 15:48:37 -0700 | [diff] [blame] | 195 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 196 | size_t i = 0; |
| 197 | while (state.KeepRunning()) { |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 198 | __system_property_serial(pinfo[i]); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 199 | i = (i + 1) % nprops; |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 200 | } |
Brigid Smith | 28417e6 | 2014-07-09 15:48:37 -0700 | [diff] [blame] | 201 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 202 | delete[] pinfo; |
Brigid Smith | 28417e6 | 2014-07-09 15:48:37 -0700 | [diff] [blame] | 203 | } |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 204 | BENCHMARK(BM_property_serial)->TEST_NUM_PROPS; |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 205 | |
| 206 | #endif // __BIONIC__ |