summaryrefslogtreecommitdiffstats
path: root/compiler-rt/lib/scudo
diff options
context:
space:
mode:
authorPeter Collingbourne <peter@pcc.me.uk>2019-12-05 16:44:07 -0800
committerPeter Collingbourne <peter@pcc.me.uk>2019-12-09 10:10:19 -0800
commit29f0a65671e2be3a5529b7429f6d6b5c5988b79e (patch)
tree3af866418b9ed6210b107f10da556ad410259c7b /compiler-rt/lib/scudo
parentbab9849963eb9b9f1fa03900e8c48a7c7d6fc305 (diff)
downloadbcm5719-llvm-29f0a65671e2be3a5529b7429f6d6b5c5988b79e.tar.gz
bcm5719-llvm-29f0a65671e2be3a5529b7429f6d6b5c5988b79e.zip
scudo: Add a basic malloc/free benchmark.
Differential Revision: https://reviews.llvm.org/D71104
Diffstat (limited to 'compiler-rt/lib/scudo')
-rw-r--r--compiler-rt/lib/scudo/standalone/CMakeLists.txt1
-rw-r--r--compiler-rt/lib/scudo/standalone/allocator_config.h2
-rw-r--r--compiler-rt/lib/scudo/standalone/benchmarks/CMakeLists.txt21
-rw-r--r--compiler-rt/lib/scudo/standalone/benchmarks/malloc_benchmark.cpp101
-rw-r--r--compiler-rt/lib/scudo/standalone/tsd_shared.h1
5 files changed, 126 insertions, 0 deletions
diff --git a/compiler-rt/lib/scudo/standalone/CMakeLists.txt b/compiler-rt/lib/scudo/standalone/CMakeLists.txt
index bd1e3e8cd66..08a093ce69f 100644
--- a/compiler-rt/lib/scudo/standalone/CMakeLists.txt
+++ b/compiler-rt/lib/scudo/standalone/CMakeLists.txt
@@ -133,6 +133,7 @@ if(COMPILER_RT_HAS_SCUDO_STANDALONE)
CFLAGS ${SCUDO_CFLAGS}
PARENT_TARGET scudo_standalone)
+ add_subdirectory(benchmarks)
if(COMPILER_RT_INCLUDE_TESTS)
add_subdirectory(tests)
endif()
diff --git a/compiler-rt/lib/scudo/standalone/allocator_config.h b/compiler-rt/lib/scudo/standalone/allocator_config.h
index 1d00a5d76d0..3a5aaae7367 100644
--- a/compiler-rt/lib/scudo/standalone/allocator_config.h
+++ b/compiler-rt/lib/scudo/standalone/allocator_config.h
@@ -64,6 +64,7 @@ struct AndroidSvelteConfig {
using TSDRegistryT = TSDRegistrySharedT<A, 1U>; // Shared, only 1 TSD.
};
+#if SCUDO_CAN_USE_PRIMARY64
struct FuchsiaConfig {
// 1GB Regions
typedef SizeClassAllocator64<DefaultSizeClassMap, 30U> Primary;
@@ -71,6 +72,7 @@ struct FuchsiaConfig {
template <class A>
using TSDRegistryT = TSDRegistrySharedT<A, 8U>; // Shared, max 8 TSDs.
};
+#endif
#if SCUDO_ANDROID
typedef AndroidConfig Config;
diff --git a/compiler-rt/lib/scudo/standalone/benchmarks/CMakeLists.txt b/compiler-rt/lib/scudo/standalone/benchmarks/CMakeLists.txt
new file mode 100644
index 00000000000..6eb0bd6a1e3
--- /dev/null
+++ b/compiler-rt/lib/scudo/standalone/benchmarks/CMakeLists.txt
@@ -0,0 +1,21 @@
+# To build these benchmarks, build the target "ScudoBenchmarks.$ARCH", where
+# $ARCH is the name of the target architecture. For example,
+# ScudoBenchmarks.x86_64 for 64-bit x86. The benchmark executable is then
+# available under projects/compiler-rt/lib/scudo/standalone/benchmarks/ in the
+# build directory.
+
+include(AddLLVM)
+
+set(SCUDO_BENCHMARK_CFLAGS -I${COMPILER_RT_SOURCE_DIR}/lib/scudo/standalone)
+if(ANDROID)
+ list(APPEND SCUDO_BENCHMARK_CFLAGS -fno-emulated-tls)
+endif()
+string(REPLACE ";" " " SCUDO_BENCHMARK_CFLAGS " ${SCUDO_BENCHMARK_CFLAGS}")
+
+foreach(arch ${SCUDO_STANDALONE_SUPPORTED_ARCH})
+ add_benchmark(ScudoBenchmarks.${arch}
+ malloc_benchmark.cpp
+ $<TARGET_OBJECTS:RTScudoStandalone.${arch}>)
+ set_property(TARGET ScudoBenchmarks.${arch} APPEND_STRING PROPERTY
+ COMPILE_FLAGS "${SCUDO_BENCHMARK_CFLAGS}")
+endforeach()
diff --git a/compiler-rt/lib/scudo/standalone/benchmarks/malloc_benchmark.cpp b/compiler-rt/lib/scudo/standalone/benchmarks/malloc_benchmark.cpp
new file mode 100644
index 00000000000..713820437b0
--- /dev/null
+++ b/compiler-rt/lib/scudo/standalone/benchmarks/malloc_benchmark.cpp
@@ -0,0 +1,101 @@
+//===-- malloc_benchmark.cpp ------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "allocator_config.h"
+#include "combined.h"
+#include "common.h"
+
+#include "benchmark/benchmark.h"
+
+#include <memory>
+
+template <typename Config> static void BM_malloc_free(benchmark::State &State) {
+ using AllocatorT = scudo::Allocator<Config>;
+ auto Deleter = [](AllocatorT *A) {
+ A->unmapTestOnly();
+ delete A;
+ };
+ std::unique_ptr<AllocatorT, decltype(Deleter)> Allocator(new AllocatorT,
+ Deleter);
+ Allocator->reset();
+
+ const size_t NBytes = State.range(0);
+ size_t PageSize = scudo::getPageSizeCached();
+
+ for (auto _ : State) {
+ void *Ptr = Allocator->allocate(NBytes, scudo::Chunk::Origin::Malloc);
+ auto *Data = reinterpret_cast<uint8_t *>(Ptr);
+ for (size_t I = 0; I < NBytes; I += PageSize)
+ Data[I] = 1;
+ benchmark::DoNotOptimize(Ptr);
+ Allocator->deallocate(Ptr, scudo::Chunk::Origin::Malloc);
+ }
+
+ State.SetBytesProcessed(uint64_t(State.iterations()) * uint64_t(NBytes));
+}
+
+static const size_t MinSize = 8;
+static const size_t MaxSize = 128 * 1024;
+
+// FIXME: Add DefaultConfig here once we can tear down the exclusive TSD
+// cleanly.
+BENCHMARK_TEMPLATE(BM_malloc_free, scudo::AndroidConfig)
+ ->Range(MinSize, MaxSize);
+BENCHMARK_TEMPLATE(BM_malloc_free, scudo::AndroidSvelteConfig)
+ ->Range(MinSize, MaxSize);
+#if SCUDO_CAN_USE_PRIMARY64
+BENCHMARK_TEMPLATE(BM_malloc_free, scudo::FuchsiaConfig)
+ ->Range(MinSize, MaxSize);
+#endif
+
+template <typename Config>
+static void BM_malloc_free_loop(benchmark::State &State) {
+ using AllocatorT = scudo::Allocator<Config>;
+ auto Deleter = [](AllocatorT *A) {
+ A->unmapTestOnly();
+ delete A;
+ };
+ std::unique_ptr<AllocatorT, decltype(Deleter)> Allocator(new AllocatorT,
+ Deleter);
+ Allocator->reset();
+
+ const size_t NumIters = State.range(0);
+ size_t PageSize = scudo::getPageSizeCached();
+ void *Ptrs[NumIters];
+
+ for (auto _ : State) {
+ for (void *&Ptr : Ptrs) {
+ Ptr = Allocator->allocate(8192, scudo::Chunk::Origin::Malloc);
+ auto *Data = reinterpret_cast<uint8_t *>(Ptr);
+ for (size_t I = 0; I < 8192; I += PageSize)
+ Data[I] = 1;
+ benchmark::DoNotOptimize(Ptr);
+ }
+ for (void *&Ptr : Ptrs)
+ Allocator->deallocate(Ptr, scudo::Chunk::Origin::Malloc);
+ }
+
+ State.SetBytesProcessed(uint64_t(State.iterations()) * uint64_t(NumIters) *
+ 8192);
+}
+
+static const size_t MinIters = 8;
+static const size_t MaxIters = 32 * 1024;
+
+// FIXME: Add DefaultConfig here once we can tear down the exclusive TSD
+// cleanly.
+BENCHMARK_TEMPLATE(BM_malloc_free_loop, scudo::AndroidConfig)
+ ->Range(MinIters, MaxIters);
+BENCHMARK_TEMPLATE(BM_malloc_free_loop, scudo::AndroidSvelteConfig)
+ ->Range(MinIters, MaxIters);
+#if SCUDO_CAN_USE_PRIMARY64
+BENCHMARK_TEMPLATE(BM_malloc_free_loop, scudo::FuchsiaConfig)
+ ->Range(MinIters, MaxIters);
+#endif
+
+BENCHMARK_MAIN();
diff --git a/compiler-rt/lib/scudo/standalone/tsd_shared.h b/compiler-rt/lib/scudo/standalone/tsd_shared.h
index 5f58068edf7..05b367ef07c 100644
--- a/compiler-rt/lib/scudo/standalone/tsd_shared.h
+++ b/compiler-rt/lib/scudo/standalone/tsd_shared.h
@@ -51,6 +51,7 @@ template <class Allocator, u32 MaxTSDCount> struct TSDRegistrySharedT {
unmap(reinterpret_cast<void *>(TSDs),
sizeof(TSD<Allocator>) * NumberOfTSDs);
setCurrentTSD(nullptr);
+ pthread_key_delete(PThreadKey);
}
ALWAYS_INLINE void initThreadMaybe(Allocator *Instance,
OpenPOWER on IntegriCloud