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