Copilot commented on code in PR #13218:
URL: https://github.com/apache/trafficserver/pull/13218#discussion_r3336609722


##########
src/proxy/hdrs/unit_tests/test_HdrHeap.cc:
##########
@@ -130,3 +141,87 @@ TEST_CASE("HdrHeap", "[proxy][hdrheap]")
   // Clean up
   heap->destroy();
 }
+
+#if TS_USE_ALLOCATOR_METRICS
+
+extern Allocator hdrHeapAllocator;
+extern Allocator strHeapAllocator;
+
+namespace
+{
+// Mirror the per-thread-freelist branch of the THREAD_FREE macro: push the 
block
+// onto the freelist and account the free with the same metered call the macro
+// uses (UPDATE_FREE_METRICS).
+void
+freelist_push(Allocator &a, ProxyAllocator &l, void *p)
+{
+  *reinterpret_cast<char **>(p) = reinterpret_cast<char *>(l.freelist);
+  l.freelist                    = p;
+  l.allocated++;
+  a.increment_for_free();
+}
+} // namespace
+
+// Regression test for the hdrHeap/hdrStrHeap allocator inuse underflow. These 
two
+// allocators are plain Allocator globals whose objects are allocated with no
+// constructor arguments, so THREAD_ALLOC resolves to the non-templated
+// thread_alloc(Allocator &, ProxyAllocator &). That overload used to skip the
+// metric increment when reusing an object from the per-thread freelist, while
+// THREAD_FREE always decremented it on the way in. The counted frees therefore
+// outran the counted allocs and proxy.process.allocator.inuse.* marched 
negative,
+// wrapping around as a huge uint64 value.
+TEST_CASE("allocator inuse stays balanced across freelist reuse", 
"[proxy][hdrheap][metrics]")
+{
+  struct AllocCase {
+    const char *metric;
+    Allocator  *allocator;
+  };
+
+  AllocCase const cases[] = {
+    {"proxy.process.allocator.inuse.hdrHeap",    &hdrHeapAllocator},
+    {"proxy.process.allocator.inuse.hdrStrHeap", &strHeapAllocator},
+  };
+
+  // The unit test harness disables per-thread freelists; enable them so that
+  // thread_alloc() exercises the freelist-reuse path that regressed.
+  bool const saved_disable = cmd_disable_pfreelist;
+  cmd_disable_pfreelist    = false;
+

Review Comment:
   cmd_disable_pfreelist is a global used by the unit-test harness to keep 
per-thread freelists disabled. This test flips it to false but only restores it 
at the end of the test body; if any REQUIRE() fails, the early-exit will skip 
the restore and can contaminate subsequent tests in the same process. Please 
restore via an RAII guard (and preserve the original int value).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to