blob: 1862124af3cf0b1c32ce054061c9621041f65052 [file] [log] [blame]
Austin Schuh745610d2015-09-06 18:19:50 -07001// -*- 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// ---
32// Author: Andrew Fikes
33
34#include <config.h>
35#include "stack_trace_table.h"
36#include <string.h> // for NULL, memset
37#include "base/spinlock.h" // for SpinLockHolder
38#include "common.h" // for StackTrace
39#include "internal_logging.h" // for ASSERT, Log
40#include "page_heap_allocator.h" // for PageHeapAllocator
41#include "static_vars.h" // for Static
42
43namespace tcmalloc {
44
45bool StackTraceTable::Bucket::KeyEqual(uintptr_t h,
46 const StackTrace& t) const {
47 const bool eq = (this->hash == h && this->trace.depth == t.depth);
48 for (int i = 0; eq && i < t.depth; ++i) {
49 if (this->trace.stack[i] != t.stack[i]) {
50 return false;
51 }
52 }
53 return eq;
54}
55
56StackTraceTable::StackTraceTable()
57 : error_(false),
58 depth_total_(0),
59 bucket_total_(0),
60 table_(new Bucket*[kHashTableSize]()) {
61 memset(table_, 0, kHashTableSize * sizeof(Bucket*));
62}
63
64StackTraceTable::~StackTraceTable() {
65 delete[] table_;
66}
67
68void StackTraceTable::AddTrace(const StackTrace& t) {
69 if (error_) {
70 return;
71 }
72
73 // Hash function borrowed from base/heap-profile-table.cc
74 uintptr_t h = 0;
75 for (int i = 0; i < t.depth; ++i) {
76 h += reinterpret_cast<uintptr_t>(t.stack[i]);
77 h += h << 10;
78 h ^= h >> 6;
79 }
80 h += h << 3;
81 h ^= h >> 11;
82
83 const int idx = h % kHashTableSize;
84
85 Bucket* b = table_[idx];
86 while (b != NULL && !b->KeyEqual(h, t)) {
87 b = b->next;
88 }
89 if (b != NULL) {
90 b->count++;
91 b->trace.size += t.size; // keep cumulative size
92 } else {
93 depth_total_ += t.depth;
94 bucket_total_++;
95 b = Static::bucket_allocator()->New();
96 if (b == NULL) {
97 Log(kLog, __FILE__, __LINE__,
98 "tcmalloc: could not allocate bucket", sizeof(*b));
99 error_ = true;
100 } else {
101 b->hash = h;
102 b->trace = t;
103 b->count = 1;
104 b->next = table_[idx];
105 table_[idx] = b;
106 }
107 }
108}
109
110void** StackTraceTable::ReadStackTracesAndClear() {
111 if (error_) {
112 return NULL;
113 }
114
115 // Allocate output array
116 const int out_len = bucket_total_ * 3 + depth_total_ + 1;
117 void** out = new void*[out_len];
118 if (out == NULL) {
119 Log(kLog, __FILE__, __LINE__,
120 "tcmalloc: allocation failed for stack traces",
121 out_len * sizeof(*out));
122 return NULL;
123 }
124
125 // Fill output array
126 int idx = 0;
127 for (int i = 0; i < kHashTableSize; ++i) {
128 Bucket* b = table_[i];
129 while (b != NULL) {
130 out[idx++] = reinterpret_cast<void*>(static_cast<uintptr_t>(b->count));
131 out[idx++] = reinterpret_cast<void*>(b->trace.size); // cumulative size
132 out[idx++] = reinterpret_cast<void*>(b->trace.depth);
133 for (int d = 0; d < b->trace.depth; ++d) {
134 out[idx++] = b->trace.stack[d];
135 }
136 b = b->next;
137 }
138 }
139 out[idx++] = NULL;
140 ASSERT(idx == out_len);
141
142 // Clear state
143 error_ = false;
144 depth_total_ = 0;
145 bucket_total_ = 0;
146 SpinLockHolder h(Static::pageheap_lock());
147 for (int i = 0; i < kHashTableSize; ++i) {
148 Bucket* b = table_[i];
149 while (b != NULL) {
150 Bucket* next = b->next;
151 Static::bucket_allocator()->Delete(b);
152 b = next;
153 }
154 table_[i] = NULL;
155 }
156
157 return out;
158}
159
160} // namespace tcmalloc