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 |
| 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 #endif |
| 29 |
| 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. |
| 33 |
| 34 // Total number of bytes allocated through this code path. It is equal to the |
| 35 // sum of VMA instances' length which .bt == this. |
| 36 size_t alloc_bytes; |
| 37 |
| 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; |
| 42 } StacktraceEntry; |
| 43 |
| 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 VMA { |
| 47 RB_ENTRY(VMA) rb_node; // Anchor for the RB-tree; |
| 48 uintptr_t start; |
| 49 uintptr_t end; |
| 50 uint32_t flags; // See HEAP_PROFILER_FLAGS_*. |
| 51 StacktraceEntry* st; // NULL == free entry. |
| 52 struct VMA* next_free; |
| 53 } VMA; |
| 54 |
| 55 typedef struct { |
| 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 VMA* allocs; // Start of the the VMA pool. |
| 63 StacktraceEntry* stack_traces; // Start of the StacktraceEntry pool. |
| 64 } HeapStats; |
| 65 |
| 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); |
| 69 |
| 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, |
| 74 size_t size, |
| 75 uintptr_t* frames, |
| 76 uint32_t depth, |
| 77 uint32_t flags); |
| 78 |
| 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 VMAs. |
| 81 void heap_profiler_free(void* addr, size_t size, uint32_t* old_flags); |
| 82 |
| 83 // Cleans up the HeapStats and all the internal data structures. |
| 84 void heap_profiler_cleanup(void); |
| 85 |
| 86 #ifdef __cplusplus |
| 87 } |
| 88 #endif |
| 89 |
| 90 #endif // TOOLS_ANDROID_HEAP_PROFILER_HEAP_PROFILER_H_ |
OLD | NEW |