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