heap_profiler_allocation_context_tracker_unittest.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <stddef.h>
  5. #include <iterator>
  6. #include "base/memory/ref_counted.h"
  7. #include "base/pending_task.h"
  8. #include "base/strings/string_piece.h"
  9. #include "base/trace_event/heap_profiler.h"
  10. #include "base/trace_event/heap_profiler_allocation_context.h"
  11. #include "base/trace_event/heap_profiler_allocation_context_tracker.h"
  12. #include "base/trace_event/memory_dump_manager.h"
  13. #include "base/trace_event/trace_event.h"
  14. #include "testing/gtest/include/gtest/gtest.h"
  15. namespace base {
  16. namespace trace_event {
  17. const char kThreadName[] = "TestThread";
  18. // Asserts that the fixed-size array |expected_backtrace| matches the backtrace
  19. // in |AllocationContextTracker::GetContextSnapshot|.
  20. template <size_t N>
  21. void AssertBacktraceEquals(const StackFrame(&expected_backtrace)[N]) {
  22. AllocationContext ctx;
  23. ASSERT_TRUE(AllocationContextTracker::GetInstanceForCurrentThread()
  24. ->GetContextSnapshot(&ctx));
  25. auto* actual = std::begin(ctx.backtrace.frames);
  26. auto* actual_bottom = actual + ctx.backtrace.frame_count;
  27. auto expected = std::begin(expected_backtrace);
  28. auto expected_bottom = std::end(expected_backtrace);
  29. // Note that this requires the pointers to be equal, this is not doing a deep
  30. // string comparison.
  31. for (; actual != actual_bottom && expected != expected_bottom;
  32. actual++, expected++)
  33. ASSERT_EQ(*expected, *actual);
  34. // Ensure that the height of the stacks is the same.
  35. ASSERT_EQ(actual, actual_bottom);
  36. ASSERT_EQ(expected, expected_bottom);
  37. }
  38. void AssertBacktraceContainsOnlyThreadName() {
  39. StackFrame t = StackFrame::FromThreadName(kThreadName);
  40. AllocationContext ctx;
  41. ASSERT_TRUE(AllocationContextTracker::GetInstanceForCurrentThread()
  42. ->GetContextSnapshot(&ctx));
  43. ASSERT_GE(ctx.backtrace.frame_count, 1u);
  44. ASSERT_EQ(t, ctx.backtrace.frames[0]);
  45. }
  46. class AllocationContextTrackerTest : public testing::Test {
  47. public:
  48. void SetUp() override {
  49. AllocationContextTracker::SetCaptureMode(
  50. AllocationContextTracker::CaptureMode::NATIVE_STACK);
  51. AllocationContextTracker::SetCurrentThreadName(kThreadName);
  52. }
  53. void TearDown() override {
  54. AllocationContextTracker::SetCaptureMode(
  55. AllocationContextTracker::CaptureMode::DISABLED);
  56. }
  57. };
  58. TEST_F(AllocationContextTrackerTest, StackContainsThreadName) {
  59. AssertBacktraceContainsOnlyThreadName();
  60. }
  61. TEST_F(AllocationContextTrackerTest, IgnoreAllocationTest) {
  62. HEAP_PROFILER_SCOPED_IGNORE;
  63. AllocationContext ctx;
  64. ASSERT_FALSE(AllocationContextTracker::GetInstanceForCurrentThread()
  65. ->GetContextSnapshot(&ctx));
  66. }
  67. } // namespace trace_event
  68. } // namespace base