Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(817)

Side by Side Diff: base/trace_event/memory_profiler_allocation_register.h

Issue 1371053002: [Tracing] Add allocation register for heap profiling (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@backtrace
Patch Set: _linux -> _posix Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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 BASE_TRACE_EVENT_MEMORY_PROFILER_ALLOCATION_REGISTER_H_
6 #define BASE_TRACE_EVENT_MEMORY_PROFILER_ALLOCATION_REGISTER_H_
7
8 #include <stdint.h>
9
10 #include "base/logging.h"
11 #include "base/trace_event/memory_profiler_allocation_context.h"
12
13 namespace base {
14 namespace trace_event {
15
16 // The allocation register keeps track of all allocations that have not been
17 // freed. It is an mmap-backed hash table that stores size and context indexed
Primiano Tucci (use gerrit) 2015/10/06 13:34:44 nit: s/an mmap/a mmap/
Ruud van Asseldonk 2015/10/06 15:44:35 Changed to “a memory map-backed” to avoid all ambi
18 // by address. The hash table is tailored specifically for this use case. The
19 // common case is that an entry is inserted and removed after a while, lookup
20 // without modifying the table is not an intended use case. The hash table is
21 // implemented as an array of linked lists. The size of this array is fixed,
22 // but it does not limit the amount of entries that can be stored.
23 //
24 // Replaying a recording of Chrome's allocations and frees against this hash
25 // table takes about 15% of the time that it takes to replay them against
26 // |std::map|.
27 class BASE_EXPORT AllocationRegister {
28 public:
29 // An entry in the hash table; contains the details about an allocation.
Primiano Tucci (use gerrit) 2015/10/06 13:34:44 Hmm not really (w.r.t. comment). The real entry he
Ruud van Asseldonk 2015/10/06 15:44:35 To me, an entry is the thing you put in / get out,
30 struct Allocation {
31 void* address;
32 size_t size;
33 AllocationContext context;
34 };
35
36 // An iterator that iterates entries in the hash table efficiently, but in no
37 // particular order. It can do this by iterating the cells and ignoring the
38 // linked lists altogether. Instead of checking whether a cell is in the free
39 // list to see if it should be skipped, a null address is used to indicate
40 // that a cell is free.
41 class ConstIterator {
42 public:
43 void operator++();
44 bool operator!=(const ConstIterator& other) const;
45 const Allocation& operator*() const;
46
47 private:
48 friend class AllocationRegister;
49 using CellIndex = uint32_t;
50
51 ConstIterator(const AllocationRegister& alloc_register, CellIndex index);
52
53 const AllocationRegister& register_;
54 CellIndex index_;
55 };
56
57 AllocationRegister();
58 ~AllocationRegister();
59
60 // Inserts the address, size and context into the table. If the address was
Primiano Tucci (use gerrit) 2015/10/06 13:34:44 I'd just say "Inserts an allocation and its metada
Ruud van Asseldonk 2015/10/06 15:44:35 Done.
61 // present already, the size and context are updated. |address| must not be
62 // null. (This is because null is used to mark free cells, to allow efficient
63 // iteration of the hash table.)
64 void Insert(void* address, size_t size, AllocationContext context);
65
66 // Removes the address from the table if it is present. It is ok to call this
67 // with a null pointer.
68 void Remove(void* address);
69
70 ConstIterator begin() const;
71 ConstIterator end() const;
72
73 private:
74 friend class AllocationRegisterTest;
75 using CellIndex = uint32_t;
76
77 // A cell can store allocation details (size and context) by address. Cells
78 // are part of a linked list via the |next| member. This list is either the
79 // list for a particular hash, or the free list. All cells are contiguous in
80 // memory in one big array. Therefore, on 64-bit systems, space can be saved
81 // by storing 32-bit indices instead of pointers as links. Index 0 is used as
82 // the list terminator. Address, size and context are not stored directly but
Primiano Tucci (use gerrit) 2015/10/06 13:34:44 I'd remove the latter sentence, in the beginning i
Ruud van Asseldonk 2015/10/06 15:44:35 Done.
83 // wrapped in |Allocation| to avoid exposing implementation details via the
84 // iterator.
85 struct Cell {
86 CellIndex next;
87 Allocation allocation;
88 };
89
90 // Returns a value in the range [0, kNumBuckets - 1] (inclusive).
91 uint32_t Hash(void* address);
Primiano Tucci (use gerrit) 2015/10/06 13:34:44 shouldn't this be a static method?
Ruud van Asseldonk 2015/10/06 15:44:35 It can be, yes.
92
93 // Returns a pointer to the variable that contains or should contain the
94 // index of the cell that stores the entry for |address|. The pointer may
95 // point at an element of |buckets_| or at the |next| member of an element of
96 // |cells_|. If the value pointed at is 0, |address| is not in the table.
97 CellIndex* Lookup(void* address);
98
99 // Returns the index of a cell that is not being used to store an entry.
Primiano Tucci (use gerrit) 2015/10/06 13:34:44 Probably want to make this comment a bit stronger
Ruud van Asseldonk 2015/10/06 15:44:35 Done.
100 CellIndex GetFreeCell();
101
102 // Allocates a region of virtual address space of |min_size| rounded up to the
103 // system page size. The memory is zeroed by the system. A guard page is added
104 // after the end.
105 static void* AllocateVirtualMemory(size_t min_size);
Primiano Tucci (use gerrit) 2015/10/06 13:34:44 plz move static methods before other non-static me
Primiano Tucci (use gerrit) 2015/10/06 13:34:44 I'd just call the 2nd method size, just because th
Ruud van Asseldonk 2015/10/06 15:44:35 Done. At least I grouped all the static methods to
106
107 // Frees a region of virtual address space allocated by a call to
108 // |AllocateVirtualMemory|.
109 static void FreeVirtualMemory(void* address, size_t allocated_min_size);
110
111 // The number of buckets, 2^18, approximately 260 000, has been tuned for
112 // Chrome's typical number of outstanding allocations. (This number varies
113 // between processes. Most processes have a sustained load of ~30k unfreed
114 // allocations, but some processes have peeks around 100k-400k allocations.)
115 // Because of the size of the table, it is likely that every |buckets_|
116 // access and every |cells_| access will incur a cache miss. Microbenchmarks
117 // suggest that it is worthwile to use more memory for the table to avoid
118 // chasing down the linked list, until the size is 2^18. The number of buckets
119 // is a power of two so modular indexing can be done with bitwise and.
120 const uint32_t kNumBuckets = 0x40000;
Primiano Tucci (use gerrit) 2015/10/06 13:34:44 move these constants up to line 93. From https://g
Ruud van Asseldonk 2015/10/06 15:44:35 Done.
121 const uint32_t kNumBucketsMask = kNumBuckets - 1;
122
123 // Reserve address space to store at most this number of entries. High
124 // capacity does not imply high memory usage due to the access pattern. The
125 // only constraint on the number of cells is that on 32-bit systems address
126 // space is scarce (i.e. reserving 2GiB of address space for the entries is
127 // not an option). A value of ~3M entries is large enough to handle spikes in
128 // the number of allocations, and modest enough to require no more than a few
129 // dozens of MiB of address space.
130 const uint32_t kNumCells = kNumBuckets * 10;
131
132 // The array of cells. This array is backed by mmapped memory. Lower indices
133 // are accessed first, higher indices are only accessed when required. In
134 // this way, even if a huge amount of address space has been mmapped, only
135 // the cells that are actually used will be backed by physical memory.
136 Cell* const cells_;
137
138 // The array of indices into |cells_|. |buckets_[Hash(address)]| will contain
139 // the index of the head of the linked list for |Hash(key)|. A value of 0
140 // indicates an empty list. This array is backed by mmapped memory.
141 CellIndex* const buckets_;
142
143 // The head of the free list. This is the index of the cell. A value of 0
144 // means that the free list is empty.
145 CellIndex free_list_;
146
147 // The index of the first element of |cells_| that has not been used before.
148 // If the free list is empty and a new cell is needed, the cell at this index
149 // is used. This is the high water mark for the number of entries stored.
150 CellIndex next_unused_cell_;
151 };
152
153 } // namespace trace_event
154 } // namespace base
155
156 #endif // BASE_TRACE_EVENT_MEMORY_PROFILER_ALLOCATION_REGISTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698