Skip to content

Commit

Permalink
pw_allocator: Rename AllocatorMetricProxy to TrackingAllocator
Browse files Browse the repository at this point in the history
Change-Id: I25fe88891b0262c683d273f2bc32a06f9d104323
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/190230
Presubmit-Verified: CQ Bot Account <[email protected]>
Reviewed-by: Alexei Frolov <[email protected]>
Commit-Queue: Aaron Green <[email protected]>
  • Loading branch information
nopsledder authored and CQ Bot Account committed Feb 2, 2024
1 parent 6f4be3a commit 66a9be9
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 141 deletions.
58 changes: 29 additions & 29 deletions pw_allocator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,6 @@ cc_library(
],
)

cc_library(
name = "allocator_metric_proxy",
hdrs = [
"public/pw_allocator/allocator_metric_proxy.h",
],
includes = ["public"],
deps = [
":allocator",
":metrics",
"//pw_assert",
"//pw_metric:metric",
"//pw_status",
],
)

cc_library(
name = "block",
srcs = ["block.cc"],
Expand Down Expand Up @@ -87,8 +72,8 @@ cc_library(
includes = ["public"],
deps = [
":allocator",
":allocator_metric_proxy",
":metrics",
":tracking_allocator",
"//pw_metric:metric",
"//pw_status",
"//pw_tokenizer",
Expand Down Expand Up @@ -229,6 +214,21 @@ cc_library(
],
)

cc_library(
name = "tracking_allocator",
hdrs = [
"public/pw_allocator/tracking_allocator.h",
],
includes = ["public"],
deps = [
":allocator",
":metrics",
"//pw_assert",
"//pw_metric:metric",
"//pw_status",
],
)

cc_library(
name = "allocator_testing",
testonly = True,
Expand All @@ -241,10 +241,10 @@ cc_library(
includes = ["public"],
deps = [
":allocator",
":allocator_metric_proxy",
":block",
":buffer",
":simple_allocator",
":tracking_allocator",
"//pw_assert",
"//pw_bytes",
"//pw_status",
Expand Down Expand Up @@ -300,18 +300,6 @@ pw_cc_test(
],
)

pw_cc_test(
name = "allocator_metric_proxy_test",
srcs = [
"allocator_metric_proxy_test.cc",
],
deps = [
":allocator_metric_proxy",
":allocator_testing",
"//pw_unit_test",
],
)

pw_cc_test(
name = "block_test",
srcs = [
Expand Down Expand Up @@ -444,6 +432,18 @@ pw_cc_test(
],
)

pw_cc_test(
name = "tracking_allocator_test",
srcs = [
"tracking_allocator_test.cc",
],
deps = [
":allocator_testing",
":tracking_allocator",
"//pw_unit_test",
],
)

pw_cc_test(
name = "unique_ptr_test",
srcs = [
Expand Down
40 changes: 20 additions & 20 deletions pw_allocator/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,6 @@ pw_source_set("allocator") {
sources = [ "allocator.cc" ]
}

pw_source_set("allocator_metric_proxy") {
public_configs = [ ":default_config" ]
public = [ "public/pw_allocator/allocator_metric_proxy.h" ]
public_deps = [
":allocator",
":metrics",
dir_pw_metric,
dir_pw_status,
]
deps = [ dir_pw_assert ]
}

pw_source_set("block") {
public_configs = [
":default_config",
Expand Down Expand Up @@ -98,8 +86,8 @@ pw_source_set("fallback_allocator") {
public = [ "public/pw_allocator/fallback_allocator.h" ]
public_deps = [
":allocator",
":allocator_metric_proxy",
":metrics",
":tracking_allocator",
dir_pw_metric,
dir_pw_status,
dir_pw_tokenizer,
Expand Down Expand Up @@ -207,6 +195,18 @@ pw_source_set("synchronized_allocator") {
]
}

pw_source_set("tracking_allocator") {
public_configs = [ ":default_config" ]
public = [ "public/pw_allocator/tracking_allocator.h" ]
public_deps = [
":allocator",
":metrics",
dir_pw_metric,
dir_pw_status,
]
deps = [ dir_pw_assert ]
}

pw_size_diff("allocator_size_report") {
title = "Sizes of various pw_allocator implementations"
binaries = [
Expand All @@ -231,7 +231,6 @@ pw_size_diff("allocator_size_report") {
pw_test_group("tests") {
tests = [
":allocator_test",
":allocator_metric_proxy_test",
":synchronized_allocator_test",
":block_test",
":fallback_allocator_test",
Expand All @@ -242,6 +241,7 @@ pw_test_group("tests") {
":null_allocator_test",
":simple_allocator_test",
":split_free_list_allocator_test",
":tracking_allocator_test",
":unique_ptr_test",
]
}
Expand All @@ -250,11 +250,11 @@ pw_source_set("allocator_testing") {
public = [ "public/pw_allocator/allocator_testing.h" ]
public_deps = [
":allocator",
":allocator_metric_proxy",
":block",
":buffer",
":metrics",
":simple_allocator",
":tracking_allocator",
dir_pw_bytes,
dir_pw_status,
dir_pw_unit_test,
Expand Down Expand Up @@ -295,11 +295,6 @@ pw_test("allocator_test") {
sources = [ "allocator_test.cc" ]
}

pw_test("allocator_metric_proxy_test") {
deps = [ ":allocator_testing" ]
sources = [ "allocator_metric_proxy_test.cc" ]
}

pw_test("block_test") {
configs = [ ":enable_heap_poison" ]
deps = [
Expand Down Expand Up @@ -395,6 +390,11 @@ pw_test("synchronized_allocator_test") {
sources = [ "synchronized_allocator_test.cc" ]
}

pw_test("tracking_allocator_test") {
deps = [ ":allocator_testing" ]
sources = [ "tracking_allocator_test.cc" ]
}

pw_test("unique_ptr_test") {
deps = [ ":allocator_testing" ]
sources = [ "unique_ptr_test.cc" ]
Expand Down
48 changes: 24 additions & 24 deletions pw_allocator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@ pw_add_library(pw_allocator.allocator STATIC
allocator.cc
)

pw_add_library(pw_allocator.allocator_metric_proxy INTERFACE
HEADERS
public/pw_allocator/allocator_metric_proxy.h
PUBLIC_INCLUDES
public
PUBLIC_DEPS
pw_allocator.allocator
pw_allocator.metrics
pw_metric
pw_status
)

pw_add_library(pw_allocator.block STATIC
PUBLIC_DEFINES
${enable_heap_poison}
Expand Down Expand Up @@ -82,8 +70,8 @@ pw_add_library(pw_allocator.fallback_allocator INTERFACE
public
PUBLIC_DEPS
pw_allocator.allocator
pw_allocator.allocator_metric_proxy
pw_allocator.metrics
pw_allocator.tracking_allocator
pw_metric
pw_status
pw_tokenizer
Expand Down Expand Up @@ -206,17 +194,29 @@ pw_add_library(pw_allocator.synchronized_allocator INTERFACE
pw_sync.borrow
)

pw_add_library(pw_allocator.tracking_allocator INTERFACE
HEADERS
public/pw_allocator/tracking_allocator.h
PUBLIC_INCLUDES
public
PUBLIC_DEPS
pw_allocator.allocator
pw_allocator.metrics
pw_metric
pw_status
)

pw_add_library(pw_allocator.allocator_testing STATIC
HEADERS
public/pw_allocator/allocator_testing.h
PUBLIC_INCLUDES
public
PUBLIC_DEPS
pw_allocator.allocator
pw_allocator.allocator_metric_proxy
pw_allocator.block
pw_allocator.buffer
pw_allocator.simple_allocator
pw_allocator.tracking_allocator
pw_bytes
pw_status
pw_unit_test
Expand Down Expand Up @@ -266,16 +266,6 @@ pw_add_test(pw_allocator.allocator_test
pw_allocator
)

pw_add_test(pw_allocator.allocator_metric_proxy_test
SOURCES
allocator_metric_proxy_test.cc
PRIVATE_DEPS
pw_allocator.allocator_testing
GROUPS
modules
pw_allocator
)

pw_add_test(pw_allocator.block_test
SOURCES
block_test.cc
Expand Down Expand Up @@ -399,6 +389,16 @@ pw_add_test(pw_allocator.synchronized_allocator_test
pw_allocator
)

pw_add_test(pw_allocator.tracking_allocator_test
SOURCES
tracking_allocator_test.cc
PRIVATE_DEPS
pw_allocator.allocator_testing
GROUPS
modules
pw_allocator
)

pw_add_test(pw_allocator.unique_ptr_test
SOURCES
unique_ptr_test.cc
Expand Down
12 changes: 6 additions & 6 deletions pw_allocator/allocator_testing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ Status AllocatorForTestImpl::Init(ByteSpan bytes) {
if (auto status = allocator_.Init(bytes); !status.ok()) {
return status;
}
proxy_.Init(allocator_);
tracker_.Init(allocator_);
return OkStatus();
}

void AllocatorForTestImpl::Exhaust() {
for (auto* block : allocator_.blocks()) {
block->MarkUsed();
proxy_.metric_group().Update(0, block->InnerSize());
tracker_.metric_group().Update(0, block->InnerSize());
}
}

Expand All @@ -57,25 +57,25 @@ void AllocatorForTestImpl::Reset() {
}

Status AllocatorForTestImpl::DoQuery(const void* ptr, Layout layout) const {
return proxy_.Query(ptr, layout);
return tracker_.Query(ptr, layout);
}

void* AllocatorForTestImpl::DoAllocate(Layout layout) {
allocate_size_ = layout.size();
return proxy_.Allocate(layout);
return tracker_.Allocate(layout);
}

void AllocatorForTestImpl::DoDeallocate(void* ptr, Layout layout) {
deallocate_ptr_ = ptr;
deallocate_size_ = layout.size();
return proxy_.Deallocate(ptr, layout);
return tracker_.Deallocate(ptr, layout);
}

bool AllocatorForTestImpl::DoResize(void* ptr, Layout layout, size_t new_size) {
resize_ptr_ = ptr;
resize_old_size_ = layout.size();
resize_new_size_ = new_size;
return proxy_.Resize(ptr, layout, new_size);
return tracker_.Resize(ptr, layout, new_size);
}

} // namespace internal
Expand Down
18 changes: 7 additions & 11 deletions pw_allocator/docs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ Other Implemetations
--------------------
Provided implementations of the ``Allocator`` interface include:

- ``AllocatorMetricProxy``: Wraps another allocator and records its usage.
- ``AllocatorSyncProxy``: Synchronizes access to another allocator, allowing it
to be used by multiple threads.
- ``FallbackAllocator``: Dispatches first to a primary allocator, and, if that
fails, to a secondary alloator.
- ``LibCAllocator``: Uses ``malloc``, ``realloc``, and ``free``. This should
Expand All @@ -58,6 +55,9 @@ Provided implementations of the ``Allocator`` interface include:
- ``SplitFreeListAllocator``: Tracks memory using ``Block``, and splits large
and small allocations between the front and back, respectively, of it memory
region in order to reduce fragmentation.
- ``SynchronizedAllocator``: Synchronizes access to another allocator, allowing it
to be used by multiple threads.
- ``TrackingAllocator``: Wraps another allocator and records its usage.

UniquePtr
=========
Expand Down Expand Up @@ -187,21 +187,17 @@ interface, whos costs are shown below.

Metric collection
=================
Consumers can use an ``AllocatorMetricProxy`` to wrap an allocator and collect
Consumers can use an ``TrackingAllocator`` to wrap an allocator and collect
usage statistics. These statistics are implemented using
:ref:`module-pw_metric`.

.. code-block:: cpp
MyAllocator allocator;
AllocatorMetricProxy proxy(PW_TOKENIZE_STRING_EXPR("my allocator"));
proxy.Init(allocator);
TrackingAllocator tracker(PW_TOKENIZE_STRING_EXPR("my allocator"));
tracker.Init(allocator);
// ...Perform various allocations and deallocations...
proxy.Dump();
tracker.Dump();
Metric collection is controlled using the ``pw_allocator_COLLECT_METRICS`` build
argument. If this is unset or ``false``, metric collection is skipped.

To force metric collection regardless of the build argument, tests may include
the ``"pw_allocator/allocator_metric_proxy_for_tests.h"`` header file and depend
on the ``//pw_allocator:allocator_metric_proxy_for_tests`` target.
Loading

0 comments on commit 66a9be9

Please sign in to comment.