stack_trace_table_test.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright 2009 Google Inc. All Rights Reserved.
  3. // Author: fikes@google.com (Andrew Fikes)
  4. //
  5. // Use of this source code is governed by a BSD-style license that can
  6. // be found in the LICENSE file.
  7. #include "config_for_unittests.h"
  8. #include <stdio.h> // for puts()
  9. #include "stack_trace_table.h"
  10. #include "base/logging.h"
  11. #include "base/spinlock.h"
  12. #include "static_vars.h"
  13. #undef ARRAYSIZE // may be defined on, eg, windows
  14. #define ARRAYSIZE(a) ( sizeof(a) / sizeof(*(a)) )
  15. static void CheckTracesAndReset(tcmalloc::StackTraceTable* table,
  16. const uintptr_t* expected, int len) {
  17. void** entries = table->ReadStackTracesAndClear();
  18. for (int i = 0; i < len; ++i) {
  19. CHECK_EQ(reinterpret_cast<uintptr_t>(entries[i]), expected[i]);
  20. }
  21. delete[] entries;
  22. }
  23. static void AddTrace(tcmalloc::StackTraceTable* table,
  24. const tcmalloc::StackTrace& t) {
  25. // Normally we'd need this lock, but since the test is single-threaded
  26. // we don't. I comment it out on windows because the DLL-decl thing
  27. // is really annoying in this case.
  28. #ifndef _MSC_VER
  29. SpinLockHolder h(tcmalloc::Static::pageheap_lock());
  30. #endif
  31. table->AddTrace(t);
  32. }
  33. int main(int argc, char **argv) {
  34. tcmalloc::StackTraceTable table;
  35. // Empty table
  36. CHECK_EQ(table.depth_total(), 0);
  37. CHECK_EQ(table.bucket_total(), 0);
  38. static const uintptr_t k1[] = {0};
  39. CheckTracesAndReset(&table, k1, ARRAYSIZE(k1));
  40. tcmalloc::StackTrace t1;
  41. t1.size = static_cast<uintptr_t>(1024);
  42. t1.depth = static_cast<uintptr_t>(2);
  43. t1.stack[0] = reinterpret_cast<void*>(1);
  44. t1.stack[1] = reinterpret_cast<void*>(2);
  45. tcmalloc::StackTrace t2;
  46. t2.size = static_cast<uintptr_t>(512);
  47. t2.depth = static_cast<uintptr_t>(2);
  48. t2.stack[0] = reinterpret_cast<void*>(2);
  49. t2.stack[1] = reinterpret_cast<void*>(1);
  50. // Table w/ just t1
  51. AddTrace(&table, t1);
  52. CHECK_EQ(table.depth_total(), 2);
  53. CHECK_EQ(table.bucket_total(), 1);
  54. static const uintptr_t k2[] = {1, 1024, 2, 1, 2, 0};
  55. CheckTracesAndReset(&table, k2, ARRAYSIZE(k2));
  56. // Table w/ t1, t2
  57. AddTrace(&table, t1);
  58. AddTrace(&table, t2);
  59. CHECK_EQ(table.depth_total(), 4);
  60. CHECK_EQ(table.bucket_total(), 2);
  61. static const uintptr_t k3[] = {1, 1024, 2, 1, 2, 1, 512, 2, 2, 1, 0};
  62. CheckTracesAndReset(&table, k3, ARRAYSIZE(k3));
  63. // Table w/ 2 x t1, 1 x t2
  64. AddTrace(&table, t1);
  65. AddTrace(&table, t2);
  66. AddTrace(&table, t1);
  67. CHECK_EQ(table.depth_total(), 4);
  68. CHECK_EQ(table.bucket_total(), 2);
  69. static const uintptr_t k4[] = {2, 2048, 2, 1, 2, 1, 512, 2, 2, 1, 0};
  70. CheckTracesAndReset(&table, k4, ARRAYSIZE(k4));
  71. // Same stack as t1, but w/ different size
  72. tcmalloc::StackTrace t3;
  73. t3.size = static_cast<uintptr_t>(2);
  74. t3.depth = static_cast<uintptr_t>(2);
  75. t3.stack[0] = reinterpret_cast<void*>(1);
  76. t3.stack[1] = reinterpret_cast<void*>(2);
  77. // Table w/ t1, t3
  78. AddTrace(&table, t1);
  79. AddTrace(&table, t3);
  80. CHECK_EQ(table.depth_total(), 2);
  81. CHECK_EQ(table.bucket_total(), 1);
  82. static const uintptr_t k5[] = {2, 1026, 2, 1, 2, 0};
  83. CheckTracesAndReset(&table, k5, ARRAYSIZE(k5));
  84. puts("PASS");
  85. return 0;
  86. }