blob: 7266bd08a5d9a263d1f16d35b895199d6dfc4284 [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"
Greg Hackmanncb215a72013-02-13 14:41:48 -080018#include <unistd.h>
Colin Crossb27e2002013-01-28 17:19:43 -080019
20#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
21#include <sys/_system_properties.h>
22
23#include <vector>
Greg Hackmanncb215a72013-02-13 14:41:48 -080024#include <string>
Colin Crossb27e2002013-01-28 17:19:43 -080025
Greg Hackmanncb215a72013-02-13 14:41:48 -080026extern void *__system_property_regions__[PA_REGION_COUNT];
Colin Crossb27e2002013-01-28 17:19:43 -080027
28#define TEST_NUM_PROPS \
Greg Hackmanncb215a72013-02-13 14:41:48 -080029 Arg(1)->Arg(4)->Arg(16)->Arg(64)->Arg(128)->Arg(256)->Arg(512)->Arg(1024)
Colin Crossb27e2002013-01-28 17:19:43 -080030
31struct LocalPropertyTestState {
Greg Hackmanncb215a72013-02-13 14:41:48 -080032 LocalPropertyTestState(int nprops) : nprops(nprops), valid(false) {
Colin Crossb27e2002013-01-28 17:19:43 -080033 static const char prop_name_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_";
Greg Hackmanncb215a72013-02-13 14:41:48 -080034
35 char dir_template[] = "/data/nativetest/prop-XXXXXX";
36 char *dirname = mkdtemp(dir_template);
37 if (!dirname) {
38 perror("making temp file for test state failed (is /data/nativetest writable?)");
39 return;
40 }
41
42 for (size_t i = 0; i < PA_REGION_COUNT; i++) {
43 old_pa[i] = __system_property_regions__[i];
44 __system_property_regions__[i] = NULL;
45 }
46
47 pa_dirname = dirname;
48 pa_filename = pa_dirname + "/__properties__";
49
50 __system_property_set_filename(pa_filename.c_str());
51 __system_property_area_init();
Colin Crossb27e2002013-01-28 17:19:43 -080052
53 names = new char* [nprops];
54 name_lens = new int[nprops];
55 values = new char* [nprops];
56 value_lens = new int[nprops];
57
58 srandom(nprops);
59
60 for (int i = 0; i < nprops; i++) {
61 name_lens[i] = random() % PROP_NAME_MAX;
62 names[i] = new char[PROP_NAME_MAX + 1];
63 for (int j = 0; j < name_lens[i]; j++) {
64 names[i][j] = prop_name_chars[random() % (sizeof(prop_name_chars) - 1)];
65 }
66 names[i][name_lens[i]] = 0;
67 value_lens[i] = random() % PROP_VALUE_MAX;
68 values[i] = new char[PROP_VALUE_MAX];
69 for (int j = 0; j < value_lens[i]; j++) {
70 values[i][j] = prop_name_chars[random() % (sizeof(prop_name_chars) - 1)];
71 }
72 __system_property_add(names[i], name_lens[i], values[i], value_lens[i]);
73 }
Greg Hackmanncb215a72013-02-13 14:41:48 -080074
75 valid = true;
Colin Crossb27e2002013-01-28 17:19:43 -080076 }
77
78 ~LocalPropertyTestState() {
Greg Hackmanncb215a72013-02-13 14:41:48 -080079 if (!valid)
80 return;
81
82 for (size_t i = 0; i < PA_REGION_COUNT; i++) {
83 __system_property_regions__[i] = old_pa[i];
84 }
85
86 __system_property_set_filename(PROP_FILENAME);
87 unlink(pa_filename.c_str());
88 rmdir(pa_dirname.c_str());
89
Colin Crossb27e2002013-01-28 17:19:43 -080090 for (int i = 0; i < nprops; i++) {
91 delete names[i];
92 delete values[i];
93 }
Colin Cross7d068132013-06-18 13:08:28 -070094 delete[] names;
95 delete[] name_lens;
96 delete[] values;
97 delete[] value_lens;
Colin Crossb27e2002013-01-28 17:19:43 -080098 }
99public:
100 const int nprops;
101 char **names;
102 int *name_lens;
103 char **values;
104 int *value_lens;
Greg Hackmanncb215a72013-02-13 14:41:48 -0800105 bool valid;
Colin Crossb27e2002013-01-28 17:19:43 -0800106
107private:
Greg Hackmanncb215a72013-02-13 14:41:48 -0800108 std::string pa_dirname;
109 std::string pa_filename;
110 void *old_pa[PA_REGION_COUNT];
Colin Crossb27e2002013-01-28 17:19:43 -0800111};
112
113static void BM_property_get(int iters, int nprops)
114{
115 StopBenchmarkTiming();
116
117 LocalPropertyTestState pa(nprops);
118 char value[PROP_VALUE_MAX];
119
Greg Hackmanncb215a72013-02-13 14:41:48 -0800120 if (!pa.valid)
121 return;
122
Colin Cross7d90cfa2013-06-18 12:47:38 -0700123 srandom(iters * nprops);
124
Colin Crossb27e2002013-01-28 17:19:43 -0800125 StartBenchmarkTiming();
126
127 for (int i = 0; i < iters; i++) {
Colin Cross7d90cfa2013-06-18 12:47:38 -0700128 __system_property_get(pa.names[random() % nprops], value);
Colin Crossb27e2002013-01-28 17:19:43 -0800129 }
130 StopBenchmarkTiming();
131}
132BENCHMARK(BM_property_get)->TEST_NUM_PROPS;
133
134static void BM_property_find(int iters, int nprops)
135{
136 StopBenchmarkTiming();
137
138 LocalPropertyTestState pa(nprops);
139
Greg Hackmanncb215a72013-02-13 14:41:48 -0800140 if (!pa.valid)
141 return;
142
Colin Cross7d90cfa2013-06-18 12:47:38 -0700143 srandom(iters * nprops);
144
Colin Crossb27e2002013-01-28 17:19:43 -0800145 StartBenchmarkTiming();
146
147 for (int i = 0; i < iters; i++) {
Colin Cross7d90cfa2013-06-18 12:47:38 -0700148 __system_property_find(pa.names[random() % nprops]);
Colin Crossb27e2002013-01-28 17:19:43 -0800149 }
150 StopBenchmarkTiming();
151}
152BENCHMARK(BM_property_find)->TEST_NUM_PROPS;