From b4e560ed7c10d533939c0c0bb6190d17fba554d6 Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Thu, 26 Oct 2023 17:00:00 -0700 Subject: Add android_mallopt M_GET_DECAY_TIME_ENABLED. The bionic benchmarks set the decay time in various ways, but don't necessarily restore it properly. Add a new method for getting the current decay time and then a way to restore it. Right now the assumption is that the decay time defaults to zero, but in the near future that assumption might be incorrect. Therefore using this method will future proof the code. Bug: 302212507 Test: Unit tests pass for both static and dynamic executables. Test: Ran bionic benchmarks that were modified. Change-Id: Ia77ff9ffee3081c5c1c02cb4309880f33b284e82 --- benchmarks/Android.bp | 1 + benchmarks/ScopedDecayTimeRestorer.h | 40 ++++++++++++++++++++++++++++++++++++ benchmarks/malloc_benchmark.cpp | 4 +++- benchmarks/malloc_sql_benchmark.cpp | 7 +++++-- benchmarks/stdlib_benchmark.cpp | 22 +++++++++++++------- 5 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 benchmarks/ScopedDecayTimeRestorer.h (limited to 'benchmarks') diff --git a/benchmarks/Android.bp b/benchmarks/Android.bp index ffb592108..75e607cd1 100644 --- a/benchmarks/Android.bp +++ b/benchmarks/Android.bp @@ -72,6 +72,7 @@ cc_defaults { target: { android: { + header_libs: ["bionic_libc_platform_headers"], static_libs: [ "libmeminfo", "libprocinfo", diff --git a/benchmarks/ScopedDecayTimeRestorer.h b/benchmarks/ScopedDecayTimeRestorer.h new file mode 100644 index 000000000..5835b4316 --- /dev/null +++ b/benchmarks/ScopedDecayTimeRestorer.h @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2023 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. + */ + +#pragma once + +#include + +#if defined(__BIONIC__) + +#include "platform/bionic/malloc.h" + +class ScopedDecayTimeRestorer { + public: + ScopedDecayTimeRestorer() { + bool value; + if (android_mallopt(M_GET_DECAY_TIME_ENABLED, &value, sizeof(value))) { + saved_value_ = value ? 1 : 0; + } + } + + virtual ~ScopedDecayTimeRestorer() { mallopt(M_DECAY_TIME, saved_value_); } + + private: + int saved_value_ = 0; +}; + +#endif diff --git a/benchmarks/malloc_benchmark.cpp b/benchmarks/malloc_benchmark.cpp index 258343fca..8f467d2ab 100644 --- a/benchmarks/malloc_benchmark.cpp +++ b/benchmarks/malloc_benchmark.cpp @@ -36,11 +36,14 @@ #include #include +#include "ScopedDecayTimeRestorer.h" #include "util.h" #if defined(__BIONIC__) static void RunMalloptPurge(benchmark::State& state, int purge_value) { + ScopedDecayTimeRestorer restorer; + static size_t sizes[] = {8, 16, 32, 64, 128, 1024, 4096, 16384, 65536, 131072, 1048576}; static int pagesize = getpagesize(); mallopt(M_DECAY_TIME, 1); @@ -69,7 +72,6 @@ static void RunMalloptPurge(benchmark::State& state, int purge_value) { mallopt(purge_value, 0); } - mallopt(M_DECAY_TIME, 0); } static void RunThreadsThroughput(benchmark::State& state, size_t size, size_t num_threads) { diff --git a/benchmarks/malloc_sql_benchmark.cpp b/benchmarks/malloc_sql_benchmark.cpp index 383325ccb..d5b17f677 100644 --- a/benchmarks/malloc_sql_benchmark.cpp +++ b/benchmarks/malloc_sql_benchmark.cpp @@ -31,6 +31,7 @@ #include #include +#include "ScopedDecayTimeRestorer.h" #include "util.h" #if defined(__BIONIC__) @@ -104,6 +105,8 @@ void BenchmarkMalloc(MallocEntry entries[], size_t total_entries, size_t max_all #include "malloc_sql.h" static void BM_malloc_sql_trace_default(benchmark::State& state) { + ScopedDecayTimeRestorer restorer; + // The default is expected to be a zero decay time. mallopt(M_DECAY_TIME, 0); @@ -115,14 +118,14 @@ static void BM_malloc_sql_trace_default(benchmark::State& state) { BIONIC_BENCHMARK(BM_malloc_sql_trace_default); static void BM_malloc_sql_trace_decay1(benchmark::State& state) { + ScopedDecayTimeRestorer restorer; + mallopt(M_DECAY_TIME, 1); for (auto _ : state) { BenchmarkMalloc(g_sql_entries, sizeof(g_sql_entries) / sizeof(MallocEntry), kMaxSqlAllocSlots); } - - mallopt(M_DECAY_TIME, 0); } BIONIC_BENCHMARK(BM_malloc_sql_trace_decay1); diff --git a/benchmarks/stdlib_benchmark.cpp b/benchmarks/stdlib_benchmark.cpp index 14b380a45..9be72e7aa 100644 --- a/benchmarks/stdlib_benchmark.cpp +++ b/benchmarks/stdlib_benchmark.cpp @@ -22,6 +22,7 @@ #include #include +#include "ScopedDecayTimeRestorer.h" #include "util.h" static void MallocFree(benchmark::State& state) { @@ -40,6 +41,8 @@ static void MallocFree(benchmark::State& state) { static void BM_stdlib_malloc_free_default(benchmark::State& state) { #if defined(__BIONIC__) + ScopedDecayTimeRestorer restorer; + // The default is expected to be a zero decay time. mallopt(M_DECAY_TIME, 0); #endif @@ -50,11 +53,11 @@ BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_free_default, "AT_COMMON_SIZES"); #if defined(__BIONIC__) static void BM_stdlib_malloc_free_decay1(benchmark::State& state) { + ScopedDecayTimeRestorer restorer; + mallopt(M_DECAY_TIME, 1); MallocFree(state); - - mallopt(M_DECAY_TIME, 0); } BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_free_decay1, "AT_COMMON_SIZES"); #endif @@ -75,6 +78,8 @@ static void CallocFree(benchmark::State& state) { static void BM_stdlib_calloc_free_default(benchmark::State& state) { #if defined(__BIONIC__) + ScopedDecayTimeRestorer restorer; + // The default is expected to be a zero decay time. mallopt(M_DECAY_TIME, 0); #endif @@ -113,8 +118,9 @@ static void MallocMultiple(benchmark::State& state, size_t nbytes, size_t numAll } void BM_stdlib_malloc_forty_default(benchmark::State& state) { - #if defined(__BIONIC__) + ScopedDecayTimeRestorer restorer; + // The default is expected to be a zero decay time. mallopt(M_DECAY_TIME, 0); #endif @@ -125,17 +131,19 @@ BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_forty_default, "AT_COMMON_SIZES"); #if defined(__BIONIC__) void BM_stdlib_malloc_forty_decay1(benchmark::State& state) { + ScopedDecayTimeRestorer restorer; + mallopt(M_DECAY_TIME, 1); MallocMultiple(state, state.range(0), 40); - - mallopt(M_DECAY_TIME, 0); } BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_forty_decay1, "AT_COMMON_SIZES"); #endif void BM_stdlib_malloc_multiple_8192_allocs_default(benchmark::State& state) { #if defined(__BIONIC__) + ScopedDecayTimeRestorer restorer; + // The default is expected to be a zero decay time. mallopt(M_DECAY_TIME, 0); #endif @@ -146,11 +154,11 @@ BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_multiple_8192_allocs_default, "AT_SMA #if defined(__BIONIC__) void BM_stdlib_malloc_multiple_8192_allocs_decay1(benchmark::State& state) { + ScopedDecayTimeRestorer restorer; + mallopt(M_DECAY_TIME, 1); MallocMultiple(state, 8192, state.range(0)); - - mallopt(M_DECAY_TIME, 0); } BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_multiple_8192_allocs_decay1, "AT_SMALL_SIZES"); #endif -- cgit v1.2.3