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 | |
| 17 | #include "benchmark.h" |
Christopher Ferris | 8b1ade5 | 2014-05-01 13:00:32 -0700 | [diff] [blame] | 18 | #include <errno.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 21 | #include <unistd.h> |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 22 | |
| 23 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ |
| 24 | #include <sys/_system_properties.h> |
| 25 | |
| 26 | #include <vector> |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 27 | #include <string> |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 28 | |
Greg Hackmann | 1540f60 | 2013-06-19 13:31:21 -0700 | [diff] [blame] | 29 | extern void *__system_property_area__; |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 30 | |
| 31 | #define TEST_NUM_PROPS \ |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 32 | Arg(1)->Arg(4)->Arg(16)->Arg(64)->Arg(128)->Arg(256)->Arg(512)->Arg(1024) |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 33 | |
| 34 | struct LocalPropertyTestState { |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 35 | LocalPropertyTestState(int nprops) : nprops(nprops), valid(false) { |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 36 | static const char prop_name_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_"; |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 37 | |
Christopher Ferris | 8b1ade5 | 2014-05-01 13:00:32 -0700 | [diff] [blame] | 38 | const char* android_data = getenv("ANDROID_DATA"); |
| 39 | if (android_data == NULL) { |
| 40 | printf("ANDROID_DATA environment variable not set\n"); |
| 41 | return; |
| 42 | } |
| 43 | char dir_template[PATH_MAX]; |
| 44 | snprintf(dir_template, sizeof(dir_template), "%s/local/tmp/prop-XXXXXX", android_data); |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 45 | char *dirname = mkdtemp(dir_template); |
| 46 | if (!dirname) { |
Christopher Ferris | 8b1ade5 | 2014-05-01 13:00:32 -0700 | [diff] [blame] | 47 | printf("making temp file for test state failed (is %s/local/tmp writable?): %s\n", |
| 48 | android_data, strerror(errno)); |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 49 | return; |
| 50 | } |
| 51 | |
Greg Hackmann | 1540f60 | 2013-06-19 13:31:21 -0700 | [diff] [blame] | 52 | old_pa = __system_property_area__; |
| 53 | __system_property_area__ = NULL; |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 54 | |
| 55 | pa_dirname = dirname; |
| 56 | pa_filename = pa_dirname + "/__properties__"; |
| 57 | |
| 58 | __system_property_set_filename(pa_filename.c_str()); |
| 59 | __system_property_area_init(); |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 60 | |
| 61 | names = new char* [nprops]; |
| 62 | name_lens = new int[nprops]; |
| 63 | values = new char* [nprops]; |
| 64 | value_lens = new int[nprops]; |
| 65 | |
| 66 | srandom(nprops); |
| 67 | |
| 68 | for (int i = 0; i < nprops; i++) { |
| 69 | name_lens[i] = random() % PROP_NAME_MAX; |
| 70 | names[i] = new char[PROP_NAME_MAX + 1]; |
| 71 | for (int j = 0; j < name_lens[i]; j++) { |
| 72 | names[i][j] = prop_name_chars[random() % (sizeof(prop_name_chars) - 1)]; |
| 73 | } |
| 74 | names[i][name_lens[i]] = 0; |
| 75 | value_lens[i] = random() % PROP_VALUE_MAX; |
| 76 | values[i] = new char[PROP_VALUE_MAX]; |
| 77 | for (int j = 0; j < value_lens[i]; j++) { |
| 78 | values[i][j] = prop_name_chars[random() % (sizeof(prop_name_chars) - 1)]; |
| 79 | } |
| 80 | __system_property_add(names[i], name_lens[i], values[i], value_lens[i]); |
| 81 | } |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 82 | |
| 83 | valid = true; |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | ~LocalPropertyTestState() { |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 87 | if (!valid) |
| 88 | return; |
| 89 | |
Greg Hackmann | 1540f60 | 2013-06-19 13:31:21 -0700 | [diff] [blame] | 90 | __system_property_area__ = old_pa; |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 91 | |
| 92 | __system_property_set_filename(PROP_FILENAME); |
| 93 | unlink(pa_filename.c_str()); |
| 94 | rmdir(pa_dirname.c_str()); |
| 95 | |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 96 | for (int i = 0; i < nprops; i++) { |
| 97 | delete names[i]; |
| 98 | delete values[i]; |
| 99 | } |
Colin Cross | 7d06813 | 2013-06-18 13:08:28 -0700 | [diff] [blame] | 100 | delete[] names; |
| 101 | delete[] name_lens; |
| 102 | delete[] values; |
| 103 | delete[] value_lens; |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 104 | } |
| 105 | public: |
| 106 | const int nprops; |
| 107 | char **names; |
| 108 | int *name_lens; |
| 109 | char **values; |
| 110 | int *value_lens; |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 111 | bool valid; |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 112 | |
| 113 | private: |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 114 | std::string pa_dirname; |
| 115 | std::string pa_filename; |
Greg Hackmann | 1540f60 | 2013-06-19 13:31:21 -0700 | [diff] [blame] | 116 | void *old_pa; |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | static void BM_property_get(int iters, int nprops) |
| 120 | { |
| 121 | StopBenchmarkTiming(); |
| 122 | |
| 123 | LocalPropertyTestState pa(nprops); |
| 124 | char value[PROP_VALUE_MAX]; |
| 125 | |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 126 | if (!pa.valid) |
| 127 | return; |
| 128 | |
Colin Cross | 7d90cfa | 2013-06-18 12:47:38 -0700 | [diff] [blame] | 129 | srandom(iters * nprops); |
| 130 | |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 131 | StartBenchmarkTiming(); |
| 132 | |
| 133 | for (int i = 0; i < iters; i++) { |
Colin Cross | 7d90cfa | 2013-06-18 12:47:38 -0700 | [diff] [blame] | 134 | __system_property_get(pa.names[random() % nprops], value); |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 135 | } |
| 136 | StopBenchmarkTiming(); |
| 137 | } |
| 138 | BENCHMARK(BM_property_get)->TEST_NUM_PROPS; |
| 139 | |
| 140 | static void BM_property_find(int iters, int nprops) |
| 141 | { |
| 142 | StopBenchmarkTiming(); |
| 143 | |
| 144 | LocalPropertyTestState pa(nprops); |
| 145 | |
Greg Hackmann | cb215a7 | 2013-02-13 14:41:48 -0800 | [diff] [blame] | 146 | if (!pa.valid) |
| 147 | return; |
| 148 | |
Colin Cross | 7d90cfa | 2013-06-18 12:47:38 -0700 | [diff] [blame] | 149 | srandom(iters * nprops); |
| 150 | |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 151 | StartBenchmarkTiming(); |
| 152 | |
| 153 | for (int i = 0; i < iters; i++) { |
Colin Cross | 7d90cfa | 2013-06-18 12:47:38 -0700 | [diff] [blame] | 154 | __system_property_find(pa.names[random() % nprops]); |
Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame] | 155 | } |
| 156 | StopBenchmarkTiming(); |
| 157 | } |
| 158 | BENCHMARK(BM_property_find)->TEST_NUM_PROPS; |