1 // Copyright 2024 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_allocator/metrics.h"
16
17 #include <cstdint>
18 #include <string_view>
19
20 #include "examples/named_u32.h"
21 #include "pw_allocator/testing.h"
22 #include "pw_allocator/tracking_allocator.h"
23 #include "pw_tokenizer/tokenize.h"
24
25 namespace examples {
26
27 // DOCSTAG: [pw_allocator-examples-metrics-custom_metrics1]
28 struct CustomMetrics {
29 PW_ALLOCATOR_METRICS_ENABLE(allocated_bytes);
30 PW_ALLOCATOR_METRICS_ENABLE(peak_allocated_bytes);
31 PW_ALLOCATOR_METRICS_ENABLE(num_failures);
32 };
33 // DOCSTAG: [pw_allocator-examples-metrics-custom_metrics1]
34
CollectCustomMetrics(pw::Allocator & allocator)35 void CollectCustomMetrics(pw::Allocator& allocator) {
36 // DOCSTAG: [pw_allocator-examples-metrics-custom_metrics2]
37 constexpr pw::metric::Token kToken = PW_TOKENIZE_STRING("CustomMetrics");
38 pw::allocator::TrackingAllocatorImpl<CustomMetrics> tracker(kToken,
39 allocator);
40 // DOCSTAG: [pw_allocator-examples-metrics-custom_metrics2]
41
42 // Keep one UniquePtr in scope, and let the other deallocate immediately.
43 auto ptr = tracker.MakeUnique<NamedU32>("test", 111);
44 ptr = tracker.MakeUnique<NamedU32>("test", 222);
45
46 // DOCSTAG: [pw_allocator-examples-metrics-dump]
47 tracker.metric_group().Dump();
48 // DOCSTAG: [pw_allocator-examples-metrics-dump]
49 }
50
CollectMultipleTrackers(pw::Allocator & allocator)51 void CollectMultipleTrackers(pw::Allocator& allocator) {
52 // DOCSTAG: [pw_allocator-examples-metrics-multiple_trackers]
53 using MyTrackingAllocator =
54 pw::allocator::TrackingAllocatorImpl<pw::allocator::internal::AllMetrics>;
55
56 constexpr pw::metric::Token kToken0 = PW_TOKENIZE_STRING("Combined");
57 MyTrackingAllocator combined(kToken0, allocator);
58
59 constexpr pw::metric::Token kToken1 = PW_TOKENIZE_STRING("Tracker1");
60 MyTrackingAllocator tracker1(kToken1, combined);
61
62 constexpr pw::metric::Token kToken2 = PW_TOKENIZE_STRING("Tracker2");
63 MyTrackingAllocator tracker2(kToken2, combined);
64
65 combined.metric_group().Add(tracker1.metric_group());
66 combined.metric_group().Add(tracker2.metric_group());
67 // DOCSTAG: [pw_allocator-examples-metrics-multiple_trackers]
68
69 // Keep one UniquePtr in scope, and let others deallocate immediately.
70 auto ptr = tracker1.MakeUnique<NamedU32>("test", 111);
71 ptr = tracker1.MakeUnique<NamedU32>("test", 222);
72 ptr = tracker2.MakeUnique<NamedU32>("test", 222);
73
74 combined.metric_group().Dump();
75 }
76
77 } // namespace examples
78
79 namespace pw::allocator {
80
81 using AllocatorForTest = ::pw::allocator::test::AllocatorForTest<256>;
82
83 class MetricsExampleTest : public ::testing::Test {
84 protected:
85 AllocatorForTest allocator_;
86 };
87
88 // TODO: b/328076428 - Use pwrev/193642.
TEST_F(MetricsExampleTest,CollectCustomMetrics)89 TEST_F(MetricsExampleTest, CollectCustomMetrics) {
90 // log_basic::test::LogCache<32> logs;
91 examples::CollectCustomMetrics(allocator_);
92
93 // std::array<std::atring_view, 1> kExpectedLines{
94 // "todo",
95 // };
96 // for (const &auto line : kExpectedLines) {
97 // ASSERT_TRUE(logs.HasNext());
98 // log_basic::test::LogMessage log = logs.Next();
99 // EXPECT_EQ(log.level, PW_LOG_LEVEL_INFO);
100 // EXPECT_STREQ(log.message.c_str(), line.data());
101 // }
102 }
103
104 // TODO: b/328076428 - Use pwrev/193642.
TEST_F(MetricsExampleTest,CollectMultipleTrackers)105 TEST_F(MetricsExampleTest, CollectMultipleTrackers) {
106 // log_basic::test::LogCache<32> logs;
107 examples::CollectMultipleTrackers(allocator_);
108
109 // std::array<std::atring_view, 1> kExpectedLines{
110 // "todo",
111 // };
112 // for (const &auto line : kExpectedLines) {
113 // ASSERT_TRUE(logs.HasNext());
114 // log_basic::test::LogMessage log = logs.Next();
115 // EXPECT_EQ(log.level, PW_LOG_LEVEL_INFO);
116 // EXPECT_STREQ(log.message.c_str(), line.data());
117 // }
118 }
119
120 } // namespace pw::allocator
121