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

Side by Side Diff: third_party/tcmalloc/chromium/src/common.cc

Issue 9320005: [NOT TO COMMIT!] Replace third_party/tcmalloc/chromium with tcmalloc r136 (the latest). (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Created 8 years, 10 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2008, Google Inc. 1 // Copyright (c) 2008, Google Inc.
2 // All rights reserved. 2 // All rights reserved.
3 // 3 //
4 // Redistribution and use in source and binary forms, with or without 4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are 5 // modification, are permitted provided that the following conditions are
6 // met: 6 // met:
7 // 7 //
8 // * Redistributions of source code must retain the above copyright 8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer. 9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above 10 // * Redistributions in binary form must reproduce the above
(...skipping 16 matching lines...) Expand all
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 29
30 // --- 30 // ---
31 // Author: Sanjay Ghemawat <opensource@google.com> 31 // Author: Sanjay Ghemawat <opensource@google.com>
32 32
33 #include "config.h" 33 #include "config.h"
34 #include "common.h" 34 #include "common.h"
35 #include "system-alloc.h" 35 #include "system-alloc.h"
36 36
37 #if defined(HAVE_UNISTD_H) && defined(HAVE_GETPAGESIZE)
38 #include <unistd.h> // for getpagesize
39 #endif
40
41 namespace tcmalloc { 37 namespace tcmalloc {
42 38
43 // Note: the following only works for "n"s that fit in 32-bits, but 39 // Note: the following only works for "n"s that fit in 32-bits, but
44 // that is fine since we only use it for small sizes. 40 // that is fine since we only use it for small sizes.
45 static inline int LgFloor(size_t n) { 41 static inline int LgFloor(size_t n) {
46 int log = 0; 42 int log = 0;
47 for (int i = 4; i >= 0; --i) { 43 for (int i = 4; i >= 0; --i) {
48 int shift = (1 << i); 44 int shift = (1 << i);
49 size_t x = n >> shift; 45 size_t x = n >> shift;
50 if (x != 0) { 46 if (x != 0) {
51 n = x; 47 n = x;
52 log += shift; 48 log += shift;
53 } 49 }
54 } 50 }
55 ASSERT(n == 1); 51 ASSERT(n == 1);
56 return log; 52 return log;
57 } 53 }
58 54
59 int AlignmentForSize(size_t size) { 55 int AlignmentForSize(size_t size) {
60 int alignment = kAlignment; 56 int alignment = kAlignment;
61 if (size >= 2048) { 57 if (size > kMaxSize) {
62 // Cap alignment at 256 for large sizes. 58 // Cap alignment at kPageSize for large sizes.
63 alignment = 256; 59 alignment = kPageSize;
64 } else if (size >= 128) { 60 } else if (size >= 128) {
65 // Space wasted due to alignment is at most 1/8, i.e., 12.5%. 61 // Space wasted due to alignment is at most 1/8, i.e., 12.5%.
66 alignment = (1 << LgFloor(size)) / 8; 62 alignment = (1 << LgFloor(size)) / 8;
67 } else if (size >= 16) { 63 } else if (size >= 16) {
68 // We need an alignment of at least 16 bytes to satisfy 64 // We need an alignment of at least 16 bytes to satisfy
69 // requirements for some SSE types. 65 // requirements for some SSE types.
70 alignment = 16; 66 alignment = 16;
71 } 67 }
68 // Maximum alignment allowed is page size alignment.
69 if (alignment > kPageSize) {
70 alignment = kPageSize;
71 }
72 CHECK_CONDITION(size < 16 || alignment >= 16); 72 CHECK_CONDITION(size < 16 || alignment >= 16);
73 CHECK_CONDITION((alignment & (alignment - 1)) == 0); 73 CHECK_CONDITION((alignment & (alignment - 1)) == 0);
74 return alignment; 74 return alignment;
75 } 75 }
76 76
77 int SizeMap::NumMoveSize(size_t size) { 77 int SizeMap::NumMoveSize(size_t size) {
78 if (size == 0) return 0; 78 if (size == 0) return 0;
79 // Use approx 64k transfers between thread and central caches. 79 // Use approx 64k transfers between thread and central caches.
80 int num = static_cast<int>(64.0 * 1024.0 / size); 80 int num = static_cast<int>(64.0 * 1024.0 / size);
81 if (num < 2) num = 2; 81 if (num < 2) num = 2;
(...skipping 10 matching lines...) Expand all
92 // This value strikes a balance between the constraints above. 92 // This value strikes a balance between the constraints above.
93 if (num > 32) num = 32; 93 if (num > 32) num = 32;
94 94
95 return num; 95 return num;
96 } 96 }
97 97
98 // Initialize the mapping arrays 98 // Initialize the mapping arrays
99 void SizeMap::Init() { 99 void SizeMap::Init() {
100 // Do some sanity checking on add_amount[]/shift_amount[]/class_array[] 100 // Do some sanity checking on add_amount[]/shift_amount[]/class_array[]
101 if (ClassIndex(0) < 0) { 101 if (ClassIndex(0) < 0) {
102 CRASH("Invalid class index %d for size 0\n", ClassIndex(0)); 102 Log(kCrash, __FILE__, __LINE__,
103 "Invalid class index for size 0", ClassIndex(0));
103 } 104 }
104 if (ClassIndex(kMaxSize) >= sizeof(class_array_)) { 105 if (ClassIndex(kMaxSize) >= sizeof(class_array_)) {
105 CRASH("Invalid class index %d for kMaxSize\n", ClassIndex(kMaxSize)); 106 Log(kCrash, __FILE__, __LINE__,
107 "Invalid class index for kMaxSize", ClassIndex(kMaxSize));
106 } 108 }
107 109
108 // Compute the size classes we want to use 110 // Compute the size classes we want to use
109 int sc = 1; // Next size class to assign 111 int sc = 1; // Next size class to assign
110 int alignment = kAlignment; 112 int alignment = kAlignment;
111 CHECK_CONDITION(kAlignment <= 16); 113 CHECK_CONDITION(kAlignment <= 16);
112 int last_lg = -1; 114 for (size_t size = kAlignment; size <= kMaxSize; size += alignment) {
113 for (size_t size = kMinClassSize; size <= kMaxSize; size += alignment) { 115 alignment = AlignmentForSize(size);
114 int lg = LgFloor(size);
115 if (lg > last_lg) {
116 // Increase alignment every so often to reduce number of size classes.
117 alignment = AlignmentForSize(size);
118 last_lg = lg;
119 }
120 CHECK_CONDITION((size % alignment) == 0); 116 CHECK_CONDITION((size % alignment) == 0);
121 117
122 // Allocate enough pages so leftover is less than 1/8 of total. 118 int blocks_to_move = NumMoveSize(size) / 4;
123 // This bounds wasted space to at most 12.5%. 119 size_t psize = 0;
124 size_t psize = kPageSize; 120 do {
125 while ((psize % size) > (psize >> 3)) {
126 psize += kPageSize; 121 psize += kPageSize;
127 } 122 // Allocate enough pages so leftover is less than 1/8 of total.
123 // This bounds wasted space to at most 12.5%.
124 while ((psize % size) > (psize >> 3)) {
125 psize += kPageSize;
126 }
127 // Continue to add pages until there are at least as many objects in
128 // the span as are needed when moving objects from the central
129 // freelists and spans to the thread caches.
130 } while ((psize / size) < (blocks_to_move));
128 const size_t my_pages = psize >> kPageShift; 131 const size_t my_pages = psize >> kPageShift;
129 132
130 if (sc > 1 && my_pages == class_to_pages_[sc-1]) { 133 if (sc > 1 && my_pages == class_to_pages_[sc-1]) {
131 // See if we can merge this into the previous class without 134 // See if we can merge this into the previous class without
132 // increasing the fragmentation of the previous class. 135 // increasing the fragmentation of the previous class.
133 const size_t my_objects = (my_pages << kPageShift) / size; 136 const size_t my_objects = (my_pages << kPageShift) / size;
134 const size_t prev_objects = (class_to_pages_[sc-1] << kPageShift) 137 const size_t prev_objects = (class_to_pages_[sc-1] << kPageShift)
135 / class_to_size_[sc-1]; 138 / class_to_size_[sc-1];
136 if (my_objects == prev_objects) { 139 if (my_objects == prev_objects) {
137 // Adjust last class to include this size 140 // Adjust last class to include this size
138 class_to_size_[sc-1] = size; 141 class_to_size_[sc-1] = size;
139 continue; 142 continue;
140 } 143 }
141 } 144 }
142 145
143 // Add new class 146 // Add new class
144 class_to_pages_[sc] = my_pages; 147 class_to_pages_[sc] = my_pages;
145 class_to_size_[sc] = size; 148 class_to_size_[sc] = size;
146 sc++; 149 sc++;
147 } 150 }
148 if (sc != kNumClasses) { 151 if (sc != kNumClasses) {
149 CRASH("wrong number of size classes: found %d instead of %d\n", 152 Log(kCrash, __FILE__, __LINE__,
150 sc, int(kNumClasses)); 153 "wrong number of size classes: (found vs. expected )", sc, kNumClasses);
151 } 154 }
152 155
153 // Initialize the mapping arrays 156 // Initialize the mapping arrays
154 int next_size = 0; 157 int next_size = 0;
155 for (int c = 1; c < kNumClasses; c++) { 158 for (int c = 1; c < kNumClasses; c++) {
156 const int max_size_in_class = class_to_size_[c]; 159 const int max_size_in_class = class_to_size_[c];
157 for (int s = next_size; s <= max_size_in_class; s += kAlignment) { 160 for (int s = next_size; s <= max_size_in_class; s += kAlignment) {
158 class_array_[ClassIndex(s)] = c; 161 class_array_[ClassIndex(s)] = c;
159 } 162 }
160 next_size = max_size_in_class + kAlignment; 163 next_size = max_size_in_class + kAlignment;
161 } 164 }
162 165
163 // Double-check sizes just to be safe 166 // Double-check sizes just to be safe
164 for (size_t size = 0; size <= kMaxSize; size++) { 167 for (size_t size = 0; size <= kMaxSize; size++) {
165 const int sc = SizeClass(size); 168 const int sc = SizeClass(size);
166 if (sc <= 0 || sc >= kNumClasses) { 169 if (sc <= 0 || sc >= kNumClasses) {
167 CRASH("Bad size class %d for %" PRIuS "\n", sc, size); 170 Log(kCrash, __FILE__, __LINE__,
171 "Bad size class (class, size)", sc, size);
168 } 172 }
169 if (sc > 1 && size <= class_to_size_[sc-1]) { 173 if (sc > 1 && size <= class_to_size_[sc-1]) {
170 CRASH("Allocating unnecessarily large class %d for %" PRIuS 174 Log(kCrash, __FILE__, __LINE__,
171 "\n", sc, size); 175 "Allocating unnecessarily large class (class, size)", sc, size);
172 } 176 }
173 const size_t s = class_to_size_[sc]; 177 const size_t s = class_to_size_[sc];
174 if (size > s) { 178 if (size > s || s == 0) {
175 CRASH("Bad size %" PRIuS " for %" PRIuS " (sc = %d)\n", s, size, sc); 179 Log(kCrash, __FILE__, __LINE__,
176 } 180 "Bad (class, size, requested)", sc, s, size);
177 if (s == 0) {
178 CRASH("Bad size %" PRIuS " for %" PRIuS " (sc = %d)\n", s, size, sc);
179 } 181 }
180 } 182 }
181 183
182 // Initialize the num_objects_to_move array. 184 // Initialize the num_objects_to_move array.
183 for (size_t cl = 1; cl < kNumClasses; ++cl) { 185 for (size_t cl = 1; cl < kNumClasses; ++cl) {
184 num_objects_to_move_[cl] = NumMoveSize(ByteSizeForClass(cl)); 186 num_objects_to_move_[cl] = NumMoveSize(ByteSizeForClass(cl));
185 } 187 }
186 } 188 }
187 189
188 void SizeMap::Dump(TCMalloc_Printer* out) {
189 // Dump class sizes and maximum external wastage per size class
190 for (size_t cl = 1; cl < kNumClasses; ++cl) {
191 const int alloc_size = class_to_pages_[cl] << kPageShift;
192 const int alloc_objs = alloc_size / class_to_size_[cl];
193 const int min_used = (class_to_size_[cl-1] + 1) * alloc_objs;
194 const int max_waste = alloc_size - min_used;
195 out->printf("SC %3d [ %8d .. %8d ] from %8d ; %2.0f%% maxwaste\n",
196 int(cl),
197 int(class_to_size_[cl-1] + 1),
198 int(class_to_size_[cl]),
199 int(class_to_pages_[cl] << kPageShift),
200 max_waste * 100.0 / alloc_size
201 );
202 }
203 }
204
205 // Metadata allocator -- keeps stats about how many bytes allocated. 190 // Metadata allocator -- keeps stats about how many bytes allocated.
206 static uint64_t metadata_system_bytes_ = 0; 191 static uint64_t metadata_system_bytes_ = 0;
207 void* MetaDataAlloc(size_t bytes) { 192 void* MetaDataAlloc(size_t bytes) {
208 static size_t pagesize; 193 void* result = TCMalloc_SystemAlloc(bytes, NULL);
209 #ifdef HAVE_GETPAGESIZE
210 if (pagesize == 0)
211 pagesize = getpagesize();
212 #endif
213
214 void* result = TCMalloc_SystemAlloc(bytes, NULL, pagesize);
215 if (result != NULL) { 194 if (result != NULL) {
216 metadata_system_bytes_ += bytes; 195 metadata_system_bytes_ += bytes;
217 } 196 }
218 return result; 197 return result;
219 } 198 }
220 199
221 uint64_t metadata_system_bytes() { return metadata_system_bytes_; } 200 uint64_t metadata_system_bytes() { return metadata_system_bytes_; }
222 201
223 void increment_metadata_system_bytes(size_t bytes) {
224 metadata_system_bytes_ += bytes;
225 }
226
227 } // namespace tcmalloc 202 } // namespace tcmalloc
OLDNEW
« no previous file with comments | « third_party/tcmalloc/chromium/src/common.h ('k') | third_party/tcmalloc/chromium/src/config.h.in » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698