blob: 0802b4c3068a48e0399fb693083dfdc525d0719f [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
17#include "benchmark.h"
Christopher Ferris8b1ade52014-05-01 13:00:32 -070018#include <errno.h>
19#include <stdio.h>
20#include <stdlib.h>
Greg Hackmanncb215a72013-02-13 14:41:48 -080021#include <unistd.h>
Colin Crossb27e2002013-01-28 17:19:43 -080022
23#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
24#include <sys/_system_properties.h>
25
26#include <vector>
Greg Hackmanncb215a72013-02-13 14:41:48 -080027#include <string>
Colin Crossb27e2002013-01-28 17:19:43 -080028
Greg Hackmann1540f602013-06-19 13:31:21 -070029extern void *__system_property_area__;
Colin Crossb27e2002013-01-28 17:19:43 -080030
Christopher Ferrisb0815ae2014-07-15 19:09:07 -070031// Do not exceed 512, that is about the largest number of properties
32// that can be created with the current property area size.
Colin Crossb27e2002013-01-28 17:19:43 -080033#define TEST_NUM_PROPS \
Christopher Ferrisb0815ae2014-07-15 19:09:07 -070034 Arg(1)->Arg(4)->Arg(16)->Arg(64)->Arg(128)->Arg(256)->Arg(512)
Colin Crossb27e2002013-01-28 17:19:43 -080035
36struct LocalPropertyTestState {
Greg Hackmanncb215a72013-02-13 14:41:48 -080037 LocalPropertyTestState(int nprops) : nprops(nprops), valid(false) {
Christopher Ferrisb0815ae2014-07-15 19:09:07 -070038 static const char prop_name_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_.";
Greg Hackmanncb215a72013-02-13 14:41:48 -080039
Christopher Ferris8b1ade52014-05-01 13:00:32 -070040 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);
Greg Hackmanncb215a72013-02-13 14:41:48 -080047 char *dirname = mkdtemp(dir_template);
48 if (!dirname) {
Christopher Ferris8b1ade52014-05-01 13:00:32 -070049 printf("making temp file for test state failed (is %s/local/tmp writable?): %s\n",
50 android_data, strerror(errno));
Greg Hackmanncb215a72013-02-13 14:41:48 -080051 return;
52 }
53
Greg Hackmann1540f602013-06-19 13:31:21 -070054 old_pa = __system_property_area__;
55 __system_property_area__ = NULL;
Greg Hackmanncb215a72013-02-13 14:41:48 -080056
57 pa_dirname = dirname;
58 pa_filename = pa_dirname + "/__properties__";
59
60 __system_property_set_filename(pa_filename.c_str());
61 __system_property_area_init();
Colin Crossb27e2002013-01-28 17:19:43 -080062
63 names = new char* [nprops];
64 name_lens = new int[nprops];
65 values = new char* [nprops];
66 value_lens = new int[nprops];
67
68 srandom(nprops);
69
70 for (int i = 0; i < nprops; i++) {
Christopher Ferrisb0815ae2014-07-15 19:09:07 -070071 // Make sure the name has at least 10 characters to make
72 // it very unlikely to generate the same random name.
73 name_lens[i] = (random() % (PROP_NAME_MAX - 10)) + 10;
Colin Crossb27e2002013-01-28 17:19:43 -080074 names[i] = new char[PROP_NAME_MAX + 1];
Christopher Ferrisb0815ae2014-07-15 19:09:07 -070075 size_t prop_name_len = sizeof(prop_name_chars) - 1;
Colin Crossb27e2002013-01-28 17:19:43 -080076 for (int j = 0; j < name_lens[i]; j++) {
Christopher Ferrisb0815ae2014-07-15 19:09:07 -070077 if (j == 0 || names[i][j-1] == '.' || j == name_lens[i] - 1) {
78 // Certain values are not allowed:
79 // - Don't start name with '.'
80 // - Don't allow '.' to appear twice in a row
81 // - Don't allow the name to end with '.'
82 // This assumes that '.' is the last character in the
83 // array so that decrementing the length by one removes
84 // the value from the possible values.
85 prop_name_len--;
86 }
87 names[i][j] = prop_name_chars[random() % prop_name_len];
Colin Crossb27e2002013-01-28 17:19:43 -080088 }
89 names[i][name_lens[i]] = 0;
Christopher Ferrisb0815ae2014-07-15 19:09:07 -070090
91 // Make sure the value contains at least 1 character.
92 value_lens[i] = (random() % (PROP_VALUE_MAX - 1)) + 1;
Colin Crossb27e2002013-01-28 17:19:43 -080093 values[i] = new char[PROP_VALUE_MAX];
94 for (int j = 0; j < value_lens[i]; j++) {
95 values[i][j] = prop_name_chars[random() % (sizeof(prop_name_chars) - 1)];
96 }
Christopher Ferrisb0815ae2014-07-15 19:09:07 -070097
98 if (__system_property_add(names[i], name_lens[i], values[i], value_lens[i]) < 0) {
99 printf("Failed to add a property, terminating...\n");
100 printf("%s = %.*s\n", names[i], value_lens[i], values[i]);
101 exit(1);
102 }
Colin Crossb27e2002013-01-28 17:19:43 -0800103 }
Greg Hackmanncb215a72013-02-13 14:41:48 -0800104
105 valid = true;
Colin Crossb27e2002013-01-28 17:19:43 -0800106 }
107
108 ~LocalPropertyTestState() {
Greg Hackmanncb215a72013-02-13 14:41:48 -0800109 if (!valid)
110 return;
111
Greg Hackmann1540f602013-06-19 13:31:21 -0700112 __system_property_area__ = old_pa;
Greg Hackmanncb215a72013-02-13 14:41:48 -0800113
114 __system_property_set_filename(PROP_FILENAME);
115 unlink(pa_filename.c_str());
116 rmdir(pa_dirname.c_str());
117
Colin Crossb27e2002013-01-28 17:19:43 -0800118 for (int i = 0; i < nprops; i++) {
119 delete names[i];
120 delete values[i];
121 }
Colin Cross7d068132013-06-18 13:08:28 -0700122 delete[] names;
123 delete[] name_lens;
124 delete[] values;
125 delete[] value_lens;
Colin Crossb27e2002013-01-28 17:19:43 -0800126 }
127public:
128 const int nprops;
129 char **names;
130 int *name_lens;
131 char **values;
132 int *value_lens;
Greg Hackmanncb215a72013-02-13 14:41:48 -0800133 bool valid;
Colin Crossb27e2002013-01-28 17:19:43 -0800134
135private:
Greg Hackmanncb215a72013-02-13 14:41:48 -0800136 std::string pa_dirname;
137 std::string pa_filename;
Greg Hackmann1540f602013-06-19 13:31:21 -0700138 void *old_pa;
Colin Crossb27e2002013-01-28 17:19:43 -0800139};
140
141static void BM_property_get(int iters, int nprops)
142{
143 StopBenchmarkTiming();
144
145 LocalPropertyTestState pa(nprops);
146 char value[PROP_VALUE_MAX];
147
Greg Hackmanncb215a72013-02-13 14:41:48 -0800148 if (!pa.valid)
149 return;
150
Colin Cross7d90cfa2013-06-18 12:47:38 -0700151 srandom(iters * nprops);
152
Colin Crossb27e2002013-01-28 17:19:43 -0800153 StartBenchmarkTiming();
154
155 for (int i = 0; i < iters; i++) {
Colin Cross7d90cfa2013-06-18 12:47:38 -0700156 __system_property_get(pa.names[random() % nprops], value);
Colin Crossb27e2002013-01-28 17:19:43 -0800157 }
158 StopBenchmarkTiming();
159}
160BENCHMARK(BM_property_get)->TEST_NUM_PROPS;
161
162static void BM_property_find(int iters, int nprops)
163{
164 StopBenchmarkTiming();
165
166 LocalPropertyTestState pa(nprops);
167
Greg Hackmanncb215a72013-02-13 14:41:48 -0800168 if (!pa.valid)
169 return;
170
Colin Cross7d90cfa2013-06-18 12:47:38 -0700171 srandom(iters * nprops);
172
Colin Crossb27e2002013-01-28 17:19:43 -0800173 StartBenchmarkTiming();
174
175 for (int i = 0; i < iters; i++) {
Colin Cross7d90cfa2013-06-18 12:47:38 -0700176 __system_property_find(pa.names[random() % nprops]);
Colin Crossb27e2002013-01-28 17:19:43 -0800177 }
178 StopBenchmarkTiming();
179}
180BENCHMARK(BM_property_find)->TEST_NUM_PROPS;
Brigid Smitha3044762014-07-09 10:26:17 -0700181
182static void BM_property_read(int iters, int nprops)
183{
184 StopBenchmarkTiming();
185
186 LocalPropertyTestState pa(nprops);
187
188 if (!pa.valid)
189 return;
190
191 srandom(iters * nprops);
192 const prop_info** pinfo = new const prop_info*[iters];
193 char propvalue[PROP_VALUE_MAX];
194
195 for (int i = 0; i < iters; i++) {
196 pinfo[i] = __system_property_find(pa.names[random() % nprops]);
197 }
198
199 StartBenchmarkTiming();
200 for (int i = 0; i < iters; i++) {
201 __system_property_read(pinfo[i], 0, propvalue);
202 }
203 StopBenchmarkTiming();
204
205 delete[] pinfo;
206}
207BENCHMARK(BM_property_read)->TEST_NUM_PROPS;
Brigid Smith28417e62014-07-09 15:48:37 -0700208
209static void BM_property_serial(int iters, int nprops)
210{
211 StopBenchmarkTiming();
212
213 LocalPropertyTestState pa(nprops);
214
215 if (!pa.valid)
216 return;
217
218 srandom(iters * nprops);
219 const prop_info** pinfo = new const prop_info*[iters];
220
221 for (int i = 0; i < iters; i++) {
222 pinfo[i] = __system_property_find(pa.names[random() % nprops]);
223 }
224
225 StartBenchmarkTiming();
226 for (int i = 0; i < iters; i++) {
227 __system_property_serial(pinfo[i]);
228 }
229 StopBenchmarkTiming();
230
231 delete[] pinfo;
232}
233BENCHMARK(BM_property_serial)->TEST_NUM_PROPS;