blob: 6d17ec75af1b3e6dd65267369b5c43e1cc709ed9 [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
31#define TEST_NUM_PROPS \
Greg Hackmanncb215a72013-02-13 14:41:48 -080032 Arg(1)->Arg(4)->Arg(16)->Arg(64)->Arg(128)->Arg(256)->Arg(512)->Arg(1024)
Colin Crossb27e2002013-01-28 17:19:43 -080033
34struct LocalPropertyTestState {
Greg Hackmanncb215a72013-02-13 14:41:48 -080035 LocalPropertyTestState(int nprops) : nprops(nprops), valid(false) {
Colin Crossb27e2002013-01-28 17:19:43 -080036 static const char prop_name_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_";
Greg Hackmanncb215a72013-02-13 14:41:48 -080037
Christopher Ferris8b1ade52014-05-01 13:00:32 -070038 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 Hackmanncb215a72013-02-13 14:41:48 -080045 char *dirname = mkdtemp(dir_template);
46 if (!dirname) {
Christopher Ferris8b1ade52014-05-01 13:00:32 -070047 printf("making temp file for test state failed (is %s/local/tmp writable?): %s\n",
48 android_data, strerror(errno));
Greg Hackmanncb215a72013-02-13 14:41:48 -080049 return;
50 }
51
Greg Hackmann1540f602013-06-19 13:31:21 -070052 old_pa = __system_property_area__;
53 __system_property_area__ = NULL;
Greg Hackmanncb215a72013-02-13 14:41:48 -080054
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 Crossb27e2002013-01-28 17:19:43 -080060
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 Hackmanncb215a72013-02-13 14:41:48 -080082
83 valid = true;
Colin Crossb27e2002013-01-28 17:19:43 -080084 }
85
86 ~LocalPropertyTestState() {
Greg Hackmanncb215a72013-02-13 14:41:48 -080087 if (!valid)
88 return;
89
Greg Hackmann1540f602013-06-19 13:31:21 -070090 __system_property_area__ = old_pa;
Greg Hackmanncb215a72013-02-13 14:41:48 -080091
92 __system_property_set_filename(PROP_FILENAME);
93 unlink(pa_filename.c_str());
94 rmdir(pa_dirname.c_str());
95
Colin Crossb27e2002013-01-28 17:19:43 -080096 for (int i = 0; i < nprops; i++) {
97 delete names[i];
98 delete values[i];
99 }
Colin Cross7d068132013-06-18 13:08:28 -0700100 delete[] names;
101 delete[] name_lens;
102 delete[] values;
103 delete[] value_lens;
Colin Crossb27e2002013-01-28 17:19:43 -0800104 }
105public:
106 const int nprops;
107 char **names;
108 int *name_lens;
109 char **values;
110 int *value_lens;
Greg Hackmanncb215a72013-02-13 14:41:48 -0800111 bool valid;
Colin Crossb27e2002013-01-28 17:19:43 -0800112
113private:
Greg Hackmanncb215a72013-02-13 14:41:48 -0800114 std::string pa_dirname;
115 std::string pa_filename;
Greg Hackmann1540f602013-06-19 13:31:21 -0700116 void *old_pa;
Colin Crossb27e2002013-01-28 17:19:43 -0800117};
118
119static void BM_property_get(int iters, int nprops)
120{
121 StopBenchmarkTiming();
122
123 LocalPropertyTestState pa(nprops);
124 char value[PROP_VALUE_MAX];
125
Greg Hackmanncb215a72013-02-13 14:41:48 -0800126 if (!pa.valid)
127 return;
128
Colin Cross7d90cfa2013-06-18 12:47:38 -0700129 srandom(iters * nprops);
130
Colin Crossb27e2002013-01-28 17:19:43 -0800131 StartBenchmarkTiming();
132
133 for (int i = 0; i < iters; i++) {
Colin Cross7d90cfa2013-06-18 12:47:38 -0700134 __system_property_get(pa.names[random() % nprops], value);
Colin Crossb27e2002013-01-28 17:19:43 -0800135 }
136 StopBenchmarkTiming();
137}
138BENCHMARK(BM_property_get)->TEST_NUM_PROPS;
139
140static void BM_property_find(int iters, int nprops)
141{
142 StopBenchmarkTiming();
143
144 LocalPropertyTestState pa(nprops);
145
Greg Hackmanncb215a72013-02-13 14:41:48 -0800146 if (!pa.valid)
147 return;
148
Colin Cross7d90cfa2013-06-18 12:47:38 -0700149 srandom(iters * nprops);
150
Colin Crossb27e2002013-01-28 17:19:43 -0800151 StartBenchmarkTiming();
152
153 for (int i = 0; i < iters; i++) {
Colin Cross7d90cfa2013-06-18 12:47:38 -0700154 __system_property_find(pa.names[random() % nprops]);
Colin Crossb27e2002013-01-28 17:19:43 -0800155 }
156 StopBenchmarkTiming();
157}
158BENCHMARK(BM_property_find)->TEST_NUM_PROPS;