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