2013-01-29 02:19:43 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2014-05-01 22:00:32 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2013-02-13 23:41:48 +01:00
|
|
|
#include <unistd.h>
|
2013-01-29 02:19:43 +01:00
|
|
|
|
2015-02-18 04:58:53 +01:00
|
|
|
#include <string>
|
2017-11-07 22:47:37 +01:00
|
|
|
#include <vector>
|
2015-02-18 04:58:53 +01:00
|
|
|
|
2018-11-15 00:19:53 +01:00
|
|
|
#include <android-base/file.h>
|
2017-12-12 08:31:33 +01:00
|
|
|
|
|
|
|
using namespace std::literals;
|
|
|
|
|
2015-02-18 04:58:53 +01:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
|
2013-01-29 02:19:43 +01:00
|
|
|
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
|
|
|
|
#include <sys/_system_properties.h>
|
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
#include <benchmark/benchmark.h>
|
2017-12-12 08:31:33 +01:00
|
|
|
#include <system_properties/system_properties.h>
|
2017-07-25 05:01:13 +02:00
|
|
|
#include "util.h"
|
2013-01-29 02:19:43 +01:00
|
|
|
|
|
|
|
struct LocalPropertyTestState {
|
2017-12-12 08:31:33 +01:00
|
|
|
explicit LocalPropertyTestState(int nprops)
|
|
|
|
: nprops(nprops), valid(false), system_properties_(false) {
|
2015-02-18 04:58:53 +01:00
|
|
|
static const char prop_name_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_.";
|
2013-02-13 23:41:48 +01:00
|
|
|
|
2017-12-12 08:31:33 +01:00
|
|
|
valid = system_properties_.AreaInit(dir_.path, nullptr);
|
|
|
|
if (!valid) {
|
2015-02-18 04:58:53 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
names = new char* [nprops];
|
|
|
|
name_lens = new int[nprops];
|
|
|
|
values = new char* [nprops];
|
|
|
|
value_lens = new int[nprops];
|
|
|
|
|
|
|
|
srandom(nprops);
|
|
|
|
|
|
|
|
for (int i = 0; i < nprops; i++) {
|
|
|
|
// Make sure the name has at least 10 characters to make
|
|
|
|
// it very unlikely to generate the same random name.
|
|
|
|
name_lens[i] = (random() % (PROP_NAME_MAX - 10)) + 10;
|
|
|
|
names[i] = new char[PROP_NAME_MAX + 1];
|
|
|
|
size_t prop_name_len = sizeof(prop_name_chars) - 1;
|
|
|
|
for (int j = 0; j < name_lens[i]; j++) {
|
|
|
|
if (j == 0 || names[i][j-1] == '.' || j == name_lens[i] - 1) {
|
|
|
|
// Certain values are not allowed:
|
|
|
|
// - Don't start name with '.'
|
|
|
|
// - Don't allow '.' to appear twice in a row
|
|
|
|
// - Don't allow the name to end with '.'
|
|
|
|
// This assumes that '.' is the last character in the
|
|
|
|
// array so that decrementing the length by one removes
|
|
|
|
// the value from the possible values.
|
|
|
|
prop_name_len--;
|
2013-01-29 02:19:43 +01:00
|
|
|
}
|
2015-02-18 04:58:53 +01:00
|
|
|
names[i][j] = prop_name_chars[random() % prop_name_len];
|
|
|
|
}
|
|
|
|
names[i][name_lens[i]] = 0;
|
|
|
|
|
|
|
|
// Make sure the value contains at least 1 character.
|
|
|
|
value_lens[i] = (random() % (PROP_VALUE_MAX - 1)) + 1;
|
|
|
|
values[i] = new char[PROP_VALUE_MAX];
|
|
|
|
for (int j = 0; j < value_lens[i]; j++) {
|
|
|
|
values[i][j] = prop_name_chars[random() % (sizeof(prop_name_chars) - 1)];
|
|
|
|
}
|
|
|
|
|
2017-12-12 08:31:33 +01:00
|
|
|
if (system_properties_.Add(names[i], name_lens[i], values[i], value_lens[i]) < 0) {
|
2015-02-18 04:58:53 +01:00
|
|
|
printf("Failed to add a property, terminating...\n");
|
|
|
|
printf("%s = %.*s\n", names[i], value_lens[i], values[i]);
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-01-29 02:19:43 +01:00
|
|
|
}
|
|
|
|
|
2015-02-18 04:58:53 +01:00
|
|
|
valid = true;
|
|
|
|
}
|
2013-02-13 23:41:48 +01:00
|
|
|
|
2017-12-12 08:31:33 +01:00
|
|
|
SystemProperties& system_properties() {
|
|
|
|
return system_properties_;
|
|
|
|
}
|
|
|
|
|
2015-02-18 04:58:53 +01:00
|
|
|
~LocalPropertyTestState() {
|
2017-12-12 08:31:33 +01:00
|
|
|
if (!valid) {
|
2015-02-18 04:58:53 +01:00
|
|
|
return;
|
2017-12-12 08:31:33 +01:00
|
|
|
}
|
2013-02-13 23:41:48 +01:00
|
|
|
|
2018-02-22 00:01:22 +01:00
|
|
|
system_properties_.contexts_->FreeAndUnmap();
|
2015-02-18 04:58:53 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < nprops; i++) {
|
|
|
|
delete names[i];
|
|
|
|
delete values[i];
|
2013-01-29 02:19:43 +01:00
|
|
|
}
|
2015-02-18 04:58:53 +01:00
|
|
|
delete[] names;
|
|
|
|
delete[] name_lens;
|
|
|
|
delete[] values;
|
|
|
|
delete[] value_lens;
|
|
|
|
}
|
2016-02-17 19:23:52 +01:00
|
|
|
|
|
|
|
public:
|
2015-02-18 04:58:53 +01:00
|
|
|
const int nprops;
|
|
|
|
char** names;
|
|
|
|
int* name_lens;
|
|
|
|
char** values;
|
|
|
|
int* value_lens;
|
|
|
|
bool valid;
|
2013-01-29 02:19:43 +01:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
private:
|
2017-12-12 08:31:33 +01:00
|
|
|
SystemProperties system_properties_;
|
|
|
|
TemporaryDir dir_;
|
2013-01-29 02:19:43 +01:00
|
|
|
};
|
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
static void BM_property_get(benchmark::State& state) {
|
2016-11-14 14:16:08 +01:00
|
|
|
const size_t nprops = state.range(0);
|
2013-01-29 02:19:43 +01:00
|
|
|
|
2015-02-18 04:58:53 +01:00
|
|
|
LocalPropertyTestState pa(nprops);
|
2016-02-17 19:23:52 +01:00
|
|
|
if (!pa.valid) return;
|
2013-06-18 21:47:38 +02:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
while (state.KeepRunning()) {
|
|
|
|
char value[PROP_VALUE_MAX];
|
2017-12-12 08:31:33 +01:00
|
|
|
pa.system_properties().Get(pa.names[random() % nprops], value);
|
2015-02-18 04:58:53 +01:00
|
|
|
}
|
2013-01-29 02:19:43 +01:00
|
|
|
}
|
2017-11-30 17:53:15 +01:00
|
|
|
BIONIC_BENCHMARK_WITH_ARG(BM_property_get, "NUM_PROPS");
|
2013-01-29 02:19:43 +01:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
static void BM_property_find(benchmark::State& state) {
|
2016-11-14 14:16:08 +01:00
|
|
|
const size_t nprops = state.range(0);
|
2013-01-29 02:19:43 +01:00
|
|
|
|
2015-02-18 04:58:53 +01:00
|
|
|
LocalPropertyTestState pa(nprops);
|
2016-02-17 19:23:52 +01:00
|
|
|
if (!pa.valid) return;
|
2013-01-29 02:19:43 +01:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
while (state.KeepRunning()) {
|
2017-12-12 08:31:33 +01:00
|
|
|
pa.system_properties().Find(pa.names[random() % nprops]);
|
2015-02-18 04:58:53 +01:00
|
|
|
}
|
2013-01-29 02:19:43 +01:00
|
|
|
}
|
2017-11-30 17:53:15 +01:00
|
|
|
BIONIC_BENCHMARK_WITH_ARG(BM_property_find, "NUM_PROPS");
|
2014-07-09 19:26:17 +02:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
static void BM_property_read(benchmark::State& state) {
|
2016-11-14 14:16:08 +01:00
|
|
|
const size_t nprops = state.range(0);
|
2014-07-09 19:26:17 +02:00
|
|
|
|
2015-02-18 04:58:53 +01:00
|
|
|
LocalPropertyTestState pa(nprops);
|
2016-02-17 19:23:52 +01:00
|
|
|
if (!pa.valid) return;
|
2014-07-09 19:26:17 +02:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
const prop_info** pinfo = new const prop_info*[nprops];
|
2015-02-18 04:58:53 +01:00
|
|
|
char propvalue[PROP_VALUE_MAX];
|
2014-07-09 19:26:17 +02:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
for (size_t i = 0; i < nprops; ++i) {
|
2017-12-12 08:31:33 +01:00
|
|
|
pinfo[i] = pa.system_properties().Find(pa.names[random() % nprops]);
|
2015-02-18 04:58:53 +01:00
|
|
|
}
|
2014-07-09 19:26:17 +02:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
size_t i = 0;
|
|
|
|
while (state.KeepRunning()) {
|
2018-08-03 02:31:13 +02:00
|
|
|
pa.system_properties().Read(pinfo[i], nullptr, propvalue);
|
2016-02-17 19:23:52 +01:00
|
|
|
i = (i + 1) % nprops;
|
2015-02-18 04:58:53 +01:00
|
|
|
}
|
2014-07-09 19:26:17 +02:00
|
|
|
|
2015-02-18 04:58:53 +01:00
|
|
|
delete[] pinfo;
|
2014-07-09 19:26:17 +02:00
|
|
|
}
|
2017-11-30 17:53:15 +01:00
|
|
|
BIONIC_BENCHMARK_WITH_ARG(BM_property_read, "NUM_PROPS");
|
2014-07-10 00:48:37 +02:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
static void BM_property_serial(benchmark::State& state) {
|
2016-11-14 14:16:08 +01:00
|
|
|
const size_t nprops = state.range(0);
|
2014-07-10 00:48:37 +02:00
|
|
|
|
2015-02-18 04:58:53 +01:00
|
|
|
LocalPropertyTestState pa(nprops);
|
2016-02-17 19:23:52 +01:00
|
|
|
if (!pa.valid) return;
|
2014-07-10 00:48:37 +02:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
const prop_info** pinfo = new const prop_info*[nprops];
|
|
|
|
for (size_t i = 0; i < nprops; ++i) {
|
2017-12-12 08:31:33 +01:00
|
|
|
pinfo[i] = pa.system_properties().Find(pa.names[random() % nprops]);
|
2015-02-18 04:58:53 +01:00
|
|
|
}
|
2014-07-10 00:48:37 +02:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
size_t i = 0;
|
|
|
|
while (state.KeepRunning()) {
|
2019-11-12 21:41:55 +01:00
|
|
|
__system_property_serial(pinfo[i]);
|
2016-02-17 19:23:52 +01:00
|
|
|
i = (i + 1) % nprops;
|
2015-02-18 04:58:53 +01:00
|
|
|
}
|
2014-07-10 00:48:37 +02:00
|
|
|
|
2015-02-18 04:58:53 +01:00
|
|
|
delete[] pinfo;
|
2014-07-10 00:48:37 +02:00
|
|
|
}
|
2017-11-30 17:53:15 +01:00
|
|
|
BIONIC_BENCHMARK_WITH_ARG(BM_property_serial, "NUM_PROPS");
|
2015-02-18 04:58:53 +01:00
|
|
|
|
2017-11-07 22:47:37 +01:00
|
|
|
// This benchmarks find the actual properties currently set on the system and accessible by the
|
|
|
|
// user that runs this benchmark (aka this is best run as root). It is not comparable between
|
|
|
|
// devices, nor even boots, but is useful to understand the the real end-to-end speed, including
|
|
|
|
// costs to find the correct property file within /dev/__properties__.
|
|
|
|
static void BM_property_find_real(benchmark::State& state) {
|
|
|
|
std::vector<std::string> properties;
|
|
|
|
__system_property_foreach(
|
|
|
|
[](const prop_info* pi, void* cookie) {
|
|
|
|
__system_property_read_callback(pi,
|
|
|
|
[](void* cookie, const char* name, const char*, unsigned) {
|
|
|
|
auto properties =
|
|
|
|
reinterpret_cast<std::vector<std::string>*>(cookie);
|
|
|
|
properties->emplace_back(name);
|
|
|
|
},
|
|
|
|
cookie);
|
|
|
|
},
|
|
|
|
&properties);
|
|
|
|
|
|
|
|
while (state.KeepRunning()) {
|
|
|
|
for (const auto& property : properties) {
|
|
|
|
__system_property_find(property.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BIONIC_BENCHMARK(BM_property_find_real);
|
|
|
|
|
2015-02-18 04:58:53 +01:00
|
|
|
#endif // __BIONIC__
|