blob: 944cd68493d48a83f91244d148af2347040a11d2 [file] [log] [blame]
Colin Crossb27e2002013-01-28 17:19:43 -08001/*
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 Ferris8b1ade52014-05-01 13:00:32 -070017#include <errno.h>
18#include <stdio.h>
19#include <stdlib.h>
Greg Hackmanncb215a72013-02-13 14:41:48 -080020#include <unistd.h>
Colin Crossb27e2002013-01-28 17:19:43 -080021
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080022#include <string>
23
24#if defined(__BIONIC__)
25
Colin Crossb27e2002013-01-28 17:19:43 -080026#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
27#include <sys/_system_properties.h>
28
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080029#include <benchmark/Benchmark.h>
Colin Crossb27e2002013-01-28 17:19:43 -080030
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080031extern void* __system_property_area__;
Colin Crossb27e2002013-01-28 17:19:43 -080032
Christopher Ferris53531cc2014-07-15 19:09:07 -070033// Do not exceed 512, that is about the largest number of properties
34// that can be created with the current property area size.
Colin Crossb27e2002013-01-28 17:19:43 -080035#define TEST_NUM_PROPS \
Christopher Ferris53531cc2014-07-15 19:09:07 -070036 Arg(1)->Arg(4)->Arg(16)->Arg(64)->Arg(128)->Arg(256)->Arg(512)
Colin Crossb27e2002013-01-28 17:19:43 -080037
38struct LocalPropertyTestState {
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080039 LocalPropertyTestState(int nprops) : nprops(nprops), valid(false) {
40 static const char prop_name_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_.";
Greg Hackmanncb215a72013-02-13 14:41:48 -080041
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080042 const char* android_data = getenv("ANDROID_DATA");
43 if (android_data == NULL) {
44 printf("ANDROID_DATA environment variable not set\n");
45 return;
46 }
47 char dir_template[PATH_MAX];
48 snprintf(dir_template, sizeof(dir_template), "%s/local/tmp/prop-XXXXXX", android_data);
49 char* dirname = mkdtemp(dir_template);
50 if (!dirname) {
51 printf("making temp file for test state failed (is %s/local/tmp writable?): %s\n",
52 android_data, strerror(errno));
53 return;
Colin Crossb27e2002013-01-28 17:19:43 -080054 }
55
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080056 old_pa = __system_property_area__;
57 __system_property_area__ = NULL;
Greg Hackmanncb215a72013-02-13 14:41:48 -080058
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080059 pa_dirname = dirname;
60 pa_filename = pa_dirname + "/__properties__";
Greg Hackmanncb215a72013-02-13 14:41:48 -080061
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080062 __system_property_set_filename(pa_filename.c_str());
63 __system_property_area_init();
Greg Hackmanncb215a72013-02-13 14:41:48 -080064
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080065 names = new char* [nprops];
66 name_lens = new int[nprops];
67 values = new char* [nprops];
68 value_lens = new int[nprops];
69
70 srandom(nprops);
71
72 for (int i = 0; i < nprops; i++) {
73 // Make sure the name has at least 10 characters to make
74 // it very unlikely to generate the same random name.
75 name_lens[i] = (random() % (PROP_NAME_MAX - 10)) + 10;
76 names[i] = new char[PROP_NAME_MAX + 1];
77 size_t prop_name_len = sizeof(prop_name_chars) - 1;
78 for (int j = 0; j < name_lens[i]; j++) {
79 if (j == 0 || names[i][j-1] == '.' || j == name_lens[i] - 1) {
80 // Certain values are not allowed:
81 // - Don't start name with '.'
82 // - Don't allow '.' to appear twice in a row
83 // - Don't allow the name to end with '.'
84 // This assumes that '.' is the last character in the
85 // array so that decrementing the length by one removes
86 // the value from the possible values.
87 prop_name_len--;
Colin Crossb27e2002013-01-28 17:19:43 -080088 }
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080089 names[i][j] = prop_name_chars[random() % prop_name_len];
90 }
91 names[i][name_lens[i]] = 0;
92
93 // Make sure the value contains at least 1 character.
94 value_lens[i] = (random() % (PROP_VALUE_MAX - 1)) + 1;
95 values[i] = new char[PROP_VALUE_MAX];
96 for (int j = 0; j < value_lens[i]; j++) {
97 values[i][j] = prop_name_chars[random() % (sizeof(prop_name_chars) - 1)];
98 }
99
100 if (__system_property_add(names[i], name_lens[i], values[i], value_lens[i]) < 0) {
101 printf("Failed to add a property, terminating...\n");
102 printf("%s = %.*s\n", names[i], value_lens[i], values[i]);
103 exit(1);
104 }
Colin Crossb27e2002013-01-28 17:19:43 -0800105 }
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800106
107 valid = true;
108 }
109
110 ~LocalPropertyTestState() {
111 if (!valid)
112 return;
113
114 __system_property_area__ = old_pa;
115
116 __system_property_set_filename(PROP_FILENAME);
117 unlink(pa_filename.c_str());
118 rmdir(pa_dirname.c_str());
119
120 for (int i = 0; i < nprops; i++) {
121 delete names[i];
122 delete values[i];
123 }
124 delete[] names;
125 delete[] name_lens;
126 delete[] values;
127 delete[] value_lens;
128 }
Colin Crossb27e2002013-01-28 17:19:43 -0800129public:
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800130 const int nprops;
131 char** names;
132 int* name_lens;
133 char** values;
134 int* value_lens;
135 bool valid;
Colin Crossb27e2002013-01-28 17:19:43 -0800136
137private:
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800138 std::string pa_dirname;
139 std::string pa_filename;
140 void* old_pa;
Colin Crossb27e2002013-01-28 17:19:43 -0800141};
142
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800143BENCHMARK_WITH_ARG(BM_property_get, int)->TEST_NUM_PROPS;
144void BM_property_get::Run(int iters, int nprops) {
145 StopBenchmarkTiming();
Colin Crossb27e2002013-01-28 17:19:43 -0800146
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800147 LocalPropertyTestState pa(nprops);
148 char value[PROP_VALUE_MAX];
Colin Crossb27e2002013-01-28 17:19:43 -0800149
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800150 if (!pa.valid)
151 return;
Greg Hackmanncb215a72013-02-13 14:41:48 -0800152
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800153 srandom(iters * nprops);
Colin Cross7d90cfa2013-06-18 12:47:38 -0700154
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800155 StartBenchmarkTiming();
Colin Crossb27e2002013-01-28 17:19:43 -0800156
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800157 for (int i = 0; i < iters; i++) {
158 __system_property_get(pa.names[random() % nprops], value);
159 }
160 StopBenchmarkTiming();
Colin Crossb27e2002013-01-28 17:19:43 -0800161}
Colin Crossb27e2002013-01-28 17:19:43 -0800162
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800163BENCHMARK_WITH_ARG(BM_property_find, int)->TEST_NUM_PROPS;
164void BM_property_find::Run(int iters, int nprops) {
165 StopBenchmarkTiming();
Colin Crossb27e2002013-01-28 17:19:43 -0800166
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800167 LocalPropertyTestState pa(nprops);
Colin Crossb27e2002013-01-28 17:19:43 -0800168
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800169 if (!pa.valid)
170 return;
Greg Hackmanncb215a72013-02-13 14:41:48 -0800171
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800172 srandom(iters * nprops);
Colin Cross7d90cfa2013-06-18 12:47:38 -0700173
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800174 StartBenchmarkTiming();
Colin Crossb27e2002013-01-28 17:19:43 -0800175
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800176 for (int i = 0; i < iters; i++) {
177 __system_property_find(pa.names[random() % nprops]);
178 }
179 StopBenchmarkTiming();
Colin Crossb27e2002013-01-28 17:19:43 -0800180}
Brigid Smitha3044762014-07-09 10:26:17 -0700181
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800182BENCHMARK_WITH_ARG(BM_property_read, int)->TEST_NUM_PROPS;
183void BM_property_read::Run(int iters, int nprops) {
184 StopBenchmarkTiming();
Brigid Smitha3044762014-07-09 10:26:17 -0700185
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800186 LocalPropertyTestState pa(nprops);
Brigid Smitha3044762014-07-09 10:26:17 -0700187
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800188 if (!pa.valid)
189 return;
Brigid Smitha3044762014-07-09 10:26:17 -0700190
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800191 srandom(iters * nprops);
192 const prop_info** pinfo = new const prop_info*[iters];
193 char propvalue[PROP_VALUE_MAX];
Brigid Smitha3044762014-07-09 10:26:17 -0700194
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800195 for (int i = 0; i < iters; i++) {
196 pinfo[i] = __system_property_find(pa.names[random() % nprops]);
197 }
Brigid Smitha3044762014-07-09 10:26:17 -0700198
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800199 StartBenchmarkTiming();
200 for (int i = 0; i < iters; i++) {
201 __system_property_read(pinfo[i], 0, propvalue);
202 }
203 StopBenchmarkTiming();
Brigid Smitha3044762014-07-09 10:26:17 -0700204
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800205 delete[] pinfo;
Brigid Smitha3044762014-07-09 10:26:17 -0700206}
Brigid Smith28417e62014-07-09 15:48:37 -0700207
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800208BENCHMARK_WITH_ARG(BM_property_serial, int)->TEST_NUM_PROPS;
209void BM_property_serial::Run(int iters, int nprops) {
210 StopBenchmarkTiming();
Brigid Smith28417e62014-07-09 15:48:37 -0700211
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800212 LocalPropertyTestState pa(nprops);
Brigid Smith28417e62014-07-09 15:48:37 -0700213
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800214 if (!pa.valid)
215 return;
Brigid Smith28417e62014-07-09 15:48:37 -0700216
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800217 srandom(iters * nprops);
218 const prop_info** pinfo = new const prop_info*[iters];
Brigid Smith28417e62014-07-09 15:48:37 -0700219
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800220 for (int i = 0; i < iters; i++) {
221 pinfo[i] = __system_property_find(pa.names[random() % nprops]);
222 }
Brigid Smith28417e62014-07-09 15:48:37 -0700223
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800224 StartBenchmarkTiming();
225 for (int i = 0; i < iters; i++) {
226 __system_property_serial(pinfo[i]);
227 }
228 StopBenchmarkTiming();
Brigid Smith28417e62014-07-09 15:48:37 -0700229
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800230 delete[] pinfo;
Brigid Smith28417e62014-07-09 15:48:37 -0700231}
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800232
233#endif // __BIONIC__