Home | History | Annotate | Download | only in benchmarks
      1 /*
      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"
     18 #include <errno.h>
     19 #include <stdio.h>
     20 #include <stdlib.h>
     21 #include <unistd.h>
     22 
     23 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
     24 #include <sys/_system_properties.h>
     25 
     26 #include <vector>
     27 #include <string>
     28 
     29 extern void *__system_property_area__;
     30 
     31 // Do not exceed 512, that is about the largest number of properties
     32 // that can be created with the current property area size.
     33 #define TEST_NUM_PROPS \
     34     Arg(1)->Arg(4)->Arg(16)->Arg(64)->Arg(128)->Arg(256)->Arg(512)
     35 
     36 struct LocalPropertyTestState {
     37     LocalPropertyTestState(int nprops) : nprops(nprops), valid(false) {
     38         static const char prop_name_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_.";
     39 
     40         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);
     47         char *dirname = mkdtemp(dir_template);
     48         if (!dirname) {
     49             printf("making temp file for test state failed (is %s/local/tmp writable?): %s\n",
     50                    android_data, strerror(errno));
     51             return;
     52         }
     53 
     54         old_pa = __system_property_area__;
     55         __system_property_area__ = NULL;
     56 
     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();
     62 
     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++) {
     71             // 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;
     74             names[i] = new char[PROP_NAME_MAX + 1];
     75             size_t prop_name_len = sizeof(prop_name_chars) - 1;
     76             for (int j = 0; j < name_lens[i]; j++) {
     77                 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];
     88             }
     89             names[i][name_lens[i]] = 0;
     90 
     91             // Make sure the value contains at least 1 character.
     92             value_lens[i] = (random() % (PROP_VALUE_MAX - 1)) + 1;
     93             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             }
     97 
     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             }
    103         }
    104 
    105         valid = true;
    106     }
    107 
    108     ~LocalPropertyTestState() {
    109         if (!valid)
    110             return;
    111 
    112         __system_property_area__ = old_pa;
    113 
    114         __system_property_set_filename(PROP_FILENAME);
    115         unlink(pa_filename.c_str());
    116         rmdir(pa_dirname.c_str());
    117 
    118         for (int i = 0; i < nprops; i++) {
    119             delete names[i];
    120             delete values[i];
    121         }
    122         delete[] names;
    123         delete[] name_lens;
    124         delete[] values;
    125         delete[] value_lens;
    126     }
    127 public:
    128     const int nprops;
    129     char **names;
    130     int *name_lens;
    131     char **values;
    132     int *value_lens;
    133     bool valid;
    134 
    135 private:
    136     std::string pa_dirname;
    137     std::string pa_filename;
    138     void *old_pa;
    139 };
    140 
    141 static void BM_property_get(int iters, int nprops)
    142 {
    143     StopBenchmarkTiming();
    144 
    145     LocalPropertyTestState pa(nprops);
    146     char value[PROP_VALUE_MAX];
    147 
    148     if (!pa.valid)
    149         return;
    150 
    151     srandom(iters * nprops);
    152 
    153     StartBenchmarkTiming();
    154 
    155     for (int i = 0; i < iters; i++) {
    156         __system_property_get(pa.names[random() % nprops], value);
    157     }
    158     StopBenchmarkTiming();
    159 }
    160 BENCHMARK(BM_property_get)->TEST_NUM_PROPS;
    161 
    162 static void BM_property_find(int iters, int nprops)
    163 {
    164     StopBenchmarkTiming();
    165 
    166     LocalPropertyTestState pa(nprops);
    167 
    168     if (!pa.valid)
    169         return;
    170 
    171     srandom(iters * nprops);
    172 
    173     StartBenchmarkTiming();
    174 
    175     for (int i = 0; i < iters; i++) {
    176         __system_property_find(pa.names[random() % nprops]);
    177     }
    178     StopBenchmarkTiming();
    179 }
    180 BENCHMARK(BM_property_find)->TEST_NUM_PROPS;
    181 
    182 static 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 }
    207 BENCHMARK(BM_property_read)->TEST_NUM_PROPS;
    208 
    209 static 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 }
    233 BENCHMARK(BM_property_serial)->TEST_NUM_PROPS;
    234