stack_trace_table.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright (c) 2009, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // ---
  31. // Author: Andrew Fikes
  32. #include <config.h>
  33. #include "stack_trace_table.h"
  34. #include <string.h> // for NULL, memset
  35. #include "base/spinlock.h" // for SpinLockHolder
  36. #include "common.h" // for StackTrace
  37. #include "internal_logging.h" // for ASSERT, Log
  38. #include "page_heap_allocator.h" // for PageHeapAllocator
  39. #include "static_vars.h" // for Static
  40. namespace tcmalloc {
  41. bool StackTraceTable::Bucket::KeyEqual(uintptr_t h,
  42. const StackTrace& t) const {
  43. const bool eq = (this->hash == h && this->trace.depth == t.depth);
  44. for (int i = 0; eq && i < t.depth; ++i) {
  45. if (this->trace.stack[i] != t.stack[i]) {
  46. return false;
  47. }
  48. }
  49. return eq;
  50. }
  51. StackTraceTable::StackTraceTable()
  52. : error_(false),
  53. depth_total_(0),
  54. bucket_total_(0),
  55. table_(new Bucket*[kHashTableSize]()) {
  56. memset(table_, 0, kHashTableSize * sizeof(Bucket*));
  57. }
  58. StackTraceTable::~StackTraceTable() {
  59. delete[] table_;
  60. }
  61. void StackTraceTable::AddTrace(const StackTrace& t) {
  62. if (error_) {
  63. return;
  64. }
  65. // Hash function borrowed from base/heap-profile-table.cc
  66. uintptr_t h = 0;
  67. for (int i = 0; i < t.depth; ++i) {
  68. h += reinterpret_cast<uintptr_t>(t.stack[i]);
  69. h += h << 10;
  70. h ^= h >> 6;
  71. }
  72. h += h << 3;
  73. h ^= h >> 11;
  74. const int idx = h % kHashTableSize;
  75. Bucket* b = table_[idx];
  76. while (b != NULL && !b->KeyEqual(h, t)) {
  77. b = b->next;
  78. }
  79. if (b != NULL) {
  80. b->count++;
  81. b->trace.size += t.size; // keep cumulative size
  82. } else {
  83. depth_total_ += t.depth;
  84. bucket_total_++;
  85. b = Static::bucket_allocator()->New();
  86. if (b == NULL) {
  87. Log(kLog, __FILE__, __LINE__,
  88. "tcmalloc: could not allocate bucket", sizeof(*b));
  89. error_ = true;
  90. } else {
  91. b->hash = h;
  92. b->trace = t;
  93. b->count = 1;
  94. b->next = table_[idx];
  95. table_[idx] = b;
  96. }
  97. }
  98. }
  99. void** StackTraceTable::ReadStackTracesAndClear() {
  100. if (error_) {
  101. return NULL;
  102. }
  103. // Allocate output array
  104. const int out_len = bucket_total_ * 3 + depth_total_ + 1;
  105. void** out = new void*[out_len];
  106. if (out == NULL) {
  107. Log(kLog, __FILE__, __LINE__,
  108. "tcmalloc: allocation failed for stack traces",
  109. out_len * sizeof(*out));
  110. return NULL;
  111. }
  112. // Fill output array
  113. int idx = 0;
  114. for (int i = 0; i < kHashTableSize; ++i) {
  115. Bucket* b = table_[i];
  116. while (b != NULL) {
  117. out[idx++] = reinterpret_cast<void*>(static_cast<uintptr_t>(b->count));
  118. out[idx++] = reinterpret_cast<void*>(b->trace.size); // cumulative size
  119. out[idx++] = reinterpret_cast<void*>(b->trace.depth);
  120. for (int d = 0; d < b->trace.depth; ++d) {
  121. out[idx++] = b->trace.stack[d];
  122. }
  123. b = b->next;
  124. }
  125. }
  126. out[idx++] = NULL;
  127. ASSERT(idx == out_len);
  128. // Clear state
  129. error_ = false;
  130. depth_total_ = 0;
  131. bucket_total_ = 0;
  132. SpinLockHolder h(Static::pageheap_lock());
  133. for (int i = 0; i < kHashTableSize; ++i) {
  134. Bucket* b = table_[i];
  135. while (b != NULL) {
  136. Bucket* next = b->next;
  137. Static::bucket_allocator()->Delete(b);
  138. b = next;
  139. }
  140. table_[i] = NULL;
  141. }
  142. return out;
  143. }
  144. } // namespace tcmalloc