1 // Copyright 2014 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.
5 #ifndef TOOLS_ANDROID_HEAP_PROFILER_HEAP_PROFILER_H_
6 #define TOOLS_ANDROID_HEAP_PROFILER_HEAP_PROFILER_H_
9 #include "third_party/bsdtrees/tree.h"
11 #define HEAP_PROFILER_MAGIC_MARKER 0x42beef42L
12 #define HEAP_PROFILER_MAX_DEPTH 12
14 // The allocation is a result of a system malloc() invocation.
15 #define HEAP_PROFILER_FLAGS_MALLOC 1
17 // The allocation is a result of a mmap() invocation.
18 #define HEAP_PROFILER_FLAGS_MMAP 2 // Allocation performed through mmap.
20 // Only in the case of FLAGS_MMAP: The mmap is not anonymous (i.e. file backed).
21 #define HEAP_PROFILER_FLAGS_MMAP_FILE 4
23 // Android only: allocation made by the Zygote (before forking).
24 #define HEAP_PROFILER_FLAGS_IN_ZYGOTE 8
30 typedef struct StacktraceEntry
{
31 uintptr_t frames
[HEAP_PROFILER_MAX_DEPTH
]; // Absolute addrs of stack frames.
32 uint32_t hash
; // H(frames), used to keep these entries in a hashtable.
34 // Total number of bytes allocated through this code path. It is equal to the
35 // sum of Alloc instances' length which .bt == this.
38 // |next| has a dual purpose. When the entry is used (hence in the hashtable),
39 // this is a ptr to the next item in the same bucket. When the entry is free,
40 // this is a ptr to the next entry in the freelist.
41 struct StacktraceEntry
* next
;
44 // Represents a contiguous range of virtual memory which has been allocated by
45 // a give code path (identified by the corresponding StacktraceEntry).
46 typedef struct Alloc
{
47 RB_ENTRY(Alloc
) rb_node
; // Anchor for the RB-tree;
50 uint32_t flags
; // See HEAP_PROFILER_FLAGS_*.
51 StacktraceEntry
* st
; // NULL == free entry.
52 struct Alloc
* next_free
;
56 uint32_t magic_start
; // The magic marker used to locate the stats mmap.
57 uint32_t num_allocs
; // The total number of allocation entries present.
58 uint32_t max_allocs
; // The max number of items in |allocs|.
59 uint32_t num_stack_traces
; // The total number of stack traces present.
60 uint32_t max_stack_traces
; // The max number of items in |stack_traces|.
61 size_t total_alloc_bytes
; // Total allocation bytes tracked.
62 Alloc
* allocs
; // Start of the the Alloc pool.
63 StacktraceEntry
* stack_traces
; // Start of the StacktraceEntry pool.
66 // Initialize the heap_profiler. The caller has to allocate the HeapStats
67 // "superblock", since the way it is mapped is platform-specific.
68 void heap_profiler_init(HeapStats
* heap_stats
);
70 // Records and allocation. The caller must unwind the stack and pass the
71 // frames array. Flags are optionals and don't affect the behavior of the
72 // library (they're just kept along and dumped).
73 void heap_profiler_alloc(void* addr
,
79 // Frees any allocation (even partial) overlapping with the given range.
80 // If old_flags != NULL, it will be filled with the flags of the deleted allocs.
81 void heap_profiler_free(void* addr
, size_t size
, uint32_t* old_flags
);
83 // Cleans up the HeapStats and all the internal data structures.
84 void heap_profiler_cleanup(void);
90 #endif // TOOLS_ANDROID_HEAP_PROFILER_HEAP_PROFILER_H_