1// Copyright (c) 2009, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8//     * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10//     * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14//     * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30// ---
31// Author: Andrew Fikes
32//
33// Utility class for coalescing sampled stack traces.  Not thread-safe.
34
35#ifndef TCMALLOC_STACK_TRACE_TABLE_H_
36#define TCMALLOC_STACK_TRACE_TABLE_H_
37
38#include <config.h>
39#ifdef HAVE_STDINT_H
40#include <stdint.h>                     // for uintptr_t
41#endif
42#include "common.h"
43
44namespace tcmalloc {
45
46class PERFTOOLS_DLL_DECL StackTraceTable {
47 public:
48  // REQUIRES: L < pageheap_lock
49  StackTraceTable();
50  ~StackTraceTable();
51
52  // Adds stack trace "t" to table.
53  //
54  // REQUIRES: L >= pageheap_lock
55  void AddTrace(const StackTrace& t);
56
57  // Returns stack traces formatted per MallocExtension guidelines.
58  // May return NULL on error.  Clears state before returning.
59  //
60  // REQUIRES: L < pageheap_lock
61  void** ReadStackTracesAndClear();
62
63  // Exposed for PageHeapAllocator
64  struct Bucket {
65    // Key
66    uintptr_t hash;
67    StackTrace trace;
68
69    // Payload
70    int count;
71    Bucket* next;
72
73    bool KeyEqual(uintptr_t h, const StackTrace& t) const;
74  };
75
76  // For testing
77  int depth_total() const { return depth_total_; }
78  int bucket_total() const { return bucket_total_; }
79
80 private:
81  static const int kHashTableSize = 1 << 14; // => table_ is 128k
82
83  bool error_;
84  int depth_total_;
85  int bucket_total_;
86  Bucket** table_;
87};
88
89}  // namespace tcmalloc
90
91#endif  // TCMALLOC_STACK_TRACE_TABLE_H_
92