OLD | NEW |
1 // Copyright (c) 2005, Google Inc. | 1 // Copyright (c) 2005, 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 // TODO: Bias reclamation to larger addresses | 80 // TODO: Bias reclamation to larger addresses |
81 // TODO: implement mallinfo/mallopt | 81 // TODO: implement mallinfo/mallopt |
82 // TODO: Better testing | 82 // TODO: Better testing |
83 // | 83 // |
84 // 9/28/2003 (new page-level allocator replaces ptmalloc2): | 84 // 9/28/2003 (new page-level allocator replaces ptmalloc2): |
85 // * malloc/free of small objects goes from ~300 ns to ~50 ns. | 85 // * malloc/free of small objects goes from ~300 ns to ~50 ns. |
86 // * allocation of a reasonably complicated struct | 86 // * allocation of a reasonably complicated struct |
87 // goes from about 1100 ns to about 300 ns. | 87 // goes from about 1100 ns to about 300 ns. |
88 | 88 |
89 #include "config.h" | 89 #include "config.h" |
90 #include <google/tcmalloc.h> | 90 #include <gperftools/tcmalloc.h> |
91 | 91 |
92 #include <errno.h> // for ENOMEM, EINVAL, errno | 92 #include <errno.h> // for ENOMEM, EINVAL, errno |
93 #ifdef HAVE_SYS_CDEFS_H | 93 #ifdef HAVE_SYS_CDEFS_H |
94 #include <sys/cdefs.h> // for __THROW | 94 #include <sys/cdefs.h> // for __THROW |
95 #endif | 95 #endif |
96 #if defined HAVE_STDINT_H | 96 #if defined HAVE_STDINT_H |
97 #include <stdint.h> | 97 #include <stdint.h> |
98 #elif defined HAVE_INTTYPES_H | 98 #elif defined HAVE_INTTYPES_H |
99 #include <inttypes.h> | 99 #include <inttypes.h> |
100 #else | 100 #else |
101 #include <sys/types.h> | 101 #include <sys/types.h> |
102 #endif | 102 #endif |
103 #include <stddef.h> // for size_t, NULL | 103 #include <stddef.h> // for size_t, NULL |
104 #include <stdlib.h> // for getenv | 104 #include <stdlib.h> // for getenv |
105 #include <string.h> // for strcmp, memset, strlen, etc | 105 #include <string.h> // for strcmp, memset, strlen, etc |
106 #ifdef HAVE_UNISTD_H | 106 #ifdef HAVE_UNISTD_H |
107 #include <unistd.h> // for getpagesize, write, etc | 107 #include <unistd.h> // for getpagesize, write, etc |
108 #endif | 108 #endif |
109 #include <algorithm> // for max, min | 109 #include <algorithm> // for max, min |
110 #include <limits> // for numeric_limits | 110 #include <limits> // for numeric_limits |
111 #include <new> // for nothrow_t (ptr only), etc | 111 #include <new> // for nothrow_t (ptr only), etc |
112 #include <vector> // for vector | 112 #include <vector> // for vector |
113 | 113 |
114 #include <google/malloc_extension.h> | 114 #include <gperftools/malloc_extension.h> |
115 #include <google/malloc_hook.h> // for MallocHook | 115 #include <gperftools/malloc_hook.h> // for MallocHook |
116 #include "base/basictypes.h" // for int64 | 116 #include "base/basictypes.h" // for int64 |
117 #include "base/commandlineflags.h" // for RegisterFlagValidator, etc | 117 #include "base/commandlineflags.h" // for RegisterFlagValidator, etc |
118 #include "base/dynamic_annotations.h" // for RunningOnValgrind | 118 #include "base/dynamic_annotations.h" // for RunningOnValgrind |
119 #include "base/spinlock.h" // for SpinLockHolder | 119 #include "base/spinlock.h" // for SpinLockHolder |
120 #include "central_freelist.h" // for CentralFreeListPadded | 120 #include "central_freelist.h" // for CentralFreeListPadded |
121 #include "common.h" // for StackTrace, kPageShift, etc | 121 #include "common.h" // for StackTrace, kPageShift, etc |
122 #include "internal_logging.h" // for ASSERT, TCMalloc_Printer, etc | 122 #include "internal_logging.h" // for ASSERT, TCMalloc_Printer, etc |
123 #include "linked_list.h" // for SLL_SetNext | 123 #include "linked_list.h" // for SLL_SetNext |
124 #include "malloc_hook-inl.h" // for MallocHook::InvokeNewHook, etc | 124 #include "malloc_hook-inl.h" // for MallocHook::InvokeNewHook, etc |
125 #include "page_heap.h" // for PageHeap, PageHeap::Stats | 125 #include "page_heap.h" // for PageHeap, PageHeap::Stats |
(...skipping 14 matching lines...) Expand all Loading... |
140 # include <sys/malloc.h> | 140 # include <sys/malloc.h> |
141 # elif defined(HAVE_MALLOC_MALLOC_H) | 141 # elif defined(HAVE_MALLOC_MALLOC_H) |
142 # include <malloc/malloc.h> | 142 # include <malloc/malloc.h> |
143 # endif | 143 # endif |
144 #endif | 144 #endif |
145 | 145 |
146 #if (defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)) && !defi
ned(WIN32_OVERRIDE_ALLOCATORS) | 146 #if (defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)) && !defi
ned(WIN32_OVERRIDE_ALLOCATORS) |
147 # define WIN32_DO_PATCHING 1 | 147 # define WIN32_DO_PATCHING 1 |
148 #endif | 148 #endif |
149 | 149 |
| 150 // Some windows file somewhere (at least on cygwin) #define's small (!) |
| 151 #undef small |
| 152 |
150 using STL_NAMESPACE::max; | 153 using STL_NAMESPACE::max; |
151 using STL_NAMESPACE::numeric_limits; | 154 using STL_NAMESPACE::numeric_limits; |
152 using STL_NAMESPACE::vector; | 155 using STL_NAMESPACE::vector; |
153 | 156 |
154 #include "libc_override.h" | 157 #include "libc_override.h" |
155 | 158 |
156 // __THROW is defined in glibc (via <sys/cdefs.h>). It means, | 159 // __THROW is defined in glibc (via <sys/cdefs.h>). It means, |
157 // counter-intuitively, "This function will never throw an exception." | 160 // counter-intuitively, "This function will never throw an exception." |
158 // It's an optional optimization tool, but we may need to use it to | 161 // It's an optional optimization tool, but we may need to use it to |
159 // match glibc prototypes. | 162 // match glibc prototypes. |
160 #ifndef __THROW // I guess we're not on a glibc system | 163 #ifndef __THROW // I guess we're not on a glibc system |
161 # define __THROW // __THROW is just an optimization, so ok to make it "" | 164 # define __THROW // __THROW is just an optimization, so ok to make it "" |
162 #endif | 165 #endif |
163 | 166 |
164 using tcmalloc::AlignmentForSize; | 167 using tcmalloc::AlignmentForSize; |
| 168 using tcmalloc::kLog; |
| 169 using tcmalloc::kCrash; |
| 170 using tcmalloc::kCrashWithStats; |
| 171 using tcmalloc::Log; |
165 using tcmalloc::PageHeap; | 172 using tcmalloc::PageHeap; |
166 using tcmalloc::PageHeapAllocator; | 173 using tcmalloc::PageHeapAllocator; |
167 using tcmalloc::SizeMap; | 174 using tcmalloc::SizeMap; |
168 using tcmalloc::Span; | 175 using tcmalloc::Span; |
169 using tcmalloc::StackTrace; | 176 using tcmalloc::StackTrace; |
170 using tcmalloc::Static; | 177 using tcmalloc::Static; |
171 using tcmalloc::ThreadCache; | 178 using tcmalloc::ThreadCache; |
172 | 179 |
173 DECLARE_int64(tcmalloc_sample_parameter); | 180 DECLARE_int64(tcmalloc_sample_parameter); |
174 DECLARE_double(tcmalloc_release_rate); | 181 DECLARE_double(tcmalloc_release_rate); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 | 279 |
273 static int tc_new_mode = 0; // See tc_set_new_mode(). | 280 static int tc_new_mode = 0; // See tc_set_new_mode(). |
274 | 281 |
275 // Routines such as free() and realloc() catch some erroneous pointers | 282 // Routines such as free() and realloc() catch some erroneous pointers |
276 // passed to them, and invoke the below when they do. (An erroneous pointer | 283 // passed to them, and invoke the below when they do. (An erroneous pointer |
277 // won't be caught if it's within a valid span or a stale span for which | 284 // won't be caught if it's within a valid span or a stale span for which |
278 // the pagemap cache has a non-zero sizeclass.) This is a cheap (source-editing | 285 // the pagemap cache has a non-zero sizeclass.) This is a cheap (source-editing |
279 // required) kind of exception handling for these routines. | 286 // required) kind of exception handling for these routines. |
280 namespace { | 287 namespace { |
281 void InvalidFree(void* ptr) { | 288 void InvalidFree(void* ptr) { |
282 CRASH("Attempt to free invalid pointer: %p\n", ptr); | 289 Log(kCrash, __FILE__, __LINE__, "Attempt to free invalid pointer", ptr); |
283 } | 290 } |
284 | 291 |
285 size_t InvalidGetSizeForRealloc(void* old_ptr) { | 292 size_t InvalidGetSizeForRealloc(const void* old_ptr) { |
286 CRASH("Attempt to realloc invalid pointer: %p\n", old_ptr); | 293 Log(kCrash, __FILE__, __LINE__, |
| 294 "Attempt to realloc invalid pointer", old_ptr); |
287 return 0; | 295 return 0; |
288 } | 296 } |
289 | 297 |
290 size_t InvalidGetAllocatedSize(void* ptr) { | 298 size_t InvalidGetAllocatedSize(const void* ptr) { |
291 CRASH("Attempt to get the size of an invalid pointer: %p\n", ptr); | 299 Log(kCrash, __FILE__, __LINE__, |
| 300 "Attempt to get the size of an invalid pointer", ptr); |
292 return 0; | 301 return 0; |
293 } | 302 } |
294 } // unnamed namespace | 303 } // unnamed namespace |
295 | 304 |
296 // Extract interesting stats | 305 // Extract interesting stats |
297 struct TCMallocStats { | 306 struct TCMallocStats { |
298 uint64_t thread_bytes; // Bytes in thread caches | 307 uint64_t thread_bytes; // Bytes in thread caches |
299 uint64_t central_bytes; // Bytes in central cache | 308 uint64_t central_bytes; // Bytes in central cache |
300 uint64_t transfer_bytes; // Bytes in central transfer cache | 309 uint64_t transfer_bytes; // Bytes in central transfer cache |
301 uint64_t metadata_bytes; // Bytes alloced for metadata | 310 uint64_t metadata_bytes; // Bytes alloced for metadata |
302 PageHeap::Stats pageheap; // Stats from page heap | 311 PageHeap::Stats pageheap; // Stats from page heap |
303 }; | 312 }; |
304 | 313 |
305 // Get stats into "r". Also get per-size-class counts if class_count != NULL | 314 // Get stats into "r". Also get per-size-class counts if class_count != NULL |
306 static void ExtractStats(TCMallocStats* r, uint64_t* class_count) { | 315 static void ExtractStats(TCMallocStats* r, uint64_t* class_count, |
| 316 PageHeap::SmallSpanStats* small_spans, |
| 317 PageHeap::LargeSpanStats* large_spans) { |
307 r->central_bytes = 0; | 318 r->central_bytes = 0; |
308 r->transfer_bytes = 0; | 319 r->transfer_bytes = 0; |
309 for (int cl = 0; cl < kNumClasses; ++cl) { | 320 for (int cl = 0; cl < kNumClasses; ++cl) { |
310 const int length = Static::central_cache()[cl].length(); | 321 const int length = Static::central_cache()[cl].length(); |
311 const int tc_length = Static::central_cache()[cl].tc_length(); | 322 const int tc_length = Static::central_cache()[cl].tc_length(); |
312 const size_t cache_overhead = Static::central_cache()[cl].OverheadBytes(); | 323 const size_t cache_overhead = Static::central_cache()[cl].OverheadBytes(); |
313 const size_t size = static_cast<uint64_t>( | 324 const size_t size = static_cast<uint64_t>( |
314 Static::sizemap()->ByteSizeForClass(cl)); | 325 Static::sizemap()->ByteSizeForClass(cl)); |
315 r->central_bytes += (size * length) + cache_overhead; | 326 r->central_bytes += (size * length) + cache_overhead; |
316 r->transfer_bytes += (size * tc_length); | 327 r->transfer_bytes += (size * tc_length); |
317 if (class_count) class_count[cl] = length + tc_length; | 328 if (class_count) class_count[cl] = length + tc_length; |
318 } | 329 } |
319 | 330 |
320 // Add stats from per-thread heaps | 331 // Add stats from per-thread heaps |
321 r->thread_bytes = 0; | 332 r->thread_bytes = 0; |
322 { // scope | 333 { // scope |
323 SpinLockHolder h(Static::pageheap_lock()); | 334 SpinLockHolder h(Static::pageheap_lock()); |
324 ThreadCache::GetThreadStats(&r->thread_bytes, class_count); | 335 ThreadCache::GetThreadStats(&r->thread_bytes, class_count); |
325 r->metadata_bytes = tcmalloc::metadata_system_bytes(); | 336 r->metadata_bytes = tcmalloc::metadata_system_bytes(); |
326 r->pageheap = Static::pageheap()->stats(); | 337 r->pageheap = Static::pageheap()->stats(); |
| 338 if (small_spans != NULL) { |
| 339 Static::pageheap()->GetSmallSpanStats(small_spans); |
| 340 } |
| 341 if (large_spans != NULL) { |
| 342 Static::pageheap()->GetLargeSpanStats(large_spans); |
| 343 } |
327 } | 344 } |
328 } | 345 } |
329 | 346 |
| 347 static double PagesToMiB(uint64_t pages) { |
| 348 return (pages << kPageShift) / 1048576.0; |
| 349 } |
| 350 |
330 // WRITE stats to "out" | 351 // WRITE stats to "out" |
331 static void DumpStats(TCMalloc_Printer* out, int level) { | 352 static void DumpStats(TCMalloc_Printer* out, int level) { |
332 TCMallocStats stats; | 353 TCMallocStats stats; |
333 uint64_t class_count[kNumClasses]; | 354 uint64_t class_count[kNumClasses]; |
334 ExtractStats(&stats, (level >= 2 ? class_count : NULL)); | 355 PageHeap::SmallSpanStats small; |
| 356 PageHeap::LargeSpanStats large; |
| 357 if (level >= 2) { |
| 358 ExtractStats(&stats, class_count, &small, &large); |
| 359 } else { |
| 360 ExtractStats(&stats, NULL, NULL, NULL); |
| 361 } |
335 | 362 |
336 static const double MiB = 1048576.0; | 363 static const double MiB = 1048576.0; |
337 | 364 |
338 const uint64_t virtual_memory_used = (stats.pageheap.system_bytes | 365 const uint64_t virtual_memory_used = (stats.pageheap.system_bytes |
339 + stats.metadata_bytes); | 366 + stats.metadata_bytes); |
340 const uint64_t physical_memory_used = (virtual_memory_used | 367 const uint64_t physical_memory_used = (virtual_memory_used |
341 - stats.pageheap.unmapped_bytes); | 368 - stats.pageheap.unmapped_bytes); |
342 const uint64_t bytes_in_use_by_app = (physical_memory_used | 369 const uint64_t bytes_in_use_by_app = (physical_memory_used |
343 - stats.metadata_bytes | 370 - stats.metadata_bytes |
344 - stats.pageheap.free_bytes | 371 - stats.pageheap.free_bytes |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 cumulative += class_bytes; | 424 cumulative += class_bytes; |
398 out->printf("class %3d [ %8" PRIuS " bytes ] : " | 425 out->printf("class %3d [ %8" PRIuS " bytes ] : " |
399 "%8" PRIu64 " objs; %5.1f MiB; %5.1f cum MiB\n", | 426 "%8" PRIu64 " objs; %5.1f MiB; %5.1f cum MiB\n", |
400 cl, Static::sizemap()->ByteSizeForClass(cl), | 427 cl, Static::sizemap()->ByteSizeForClass(cl), |
401 class_count[cl], | 428 class_count[cl], |
402 class_bytes / MiB, | 429 class_bytes / MiB, |
403 cumulative / MiB); | 430 cumulative / MiB); |
404 } | 431 } |
405 } | 432 } |
406 | 433 |
407 SpinLockHolder h(Static::pageheap_lock()); | 434 // append page heap info |
408 Static::pageheap()->Dump(out); | 435 int nonempty_sizes = 0; |
| 436 for (int s = 0; s < kMaxPages; s++) { |
| 437 if (small.normal_length[s] + small.returned_length[s] > 0) { |
| 438 nonempty_sizes++; |
| 439 } |
| 440 } |
| 441 out->printf("------------------------------------------------\n"); |
| 442 out->printf("PageHeap: %d sizes; %6.1f MiB free; %6.1f MiB unmapped\n", |
| 443 nonempty_sizes, stats.pageheap.free_bytes / MiB, |
| 444 stats.pageheap.unmapped_bytes / MiB); |
| 445 out->printf("------------------------------------------------\n"); |
| 446 uint64_t total_normal = 0; |
| 447 uint64_t total_returned = 0; |
| 448 for (int s = 0; s < kMaxPages; s++) { |
| 449 const int n_length = small.normal_length[s]; |
| 450 const int r_length = small.returned_length[s]; |
| 451 if (n_length + r_length > 0) { |
| 452 uint64_t n_pages = s * n_length; |
| 453 uint64_t r_pages = s * r_length; |
| 454 total_normal += n_pages; |
| 455 total_returned += r_pages; |
| 456 out->printf("%6u pages * %6u spans ~ %6.1f MiB; %6.1f MiB cum" |
| 457 "; unmapped: %6.1f MiB; %6.1f MiB cum\n", |
| 458 s, |
| 459 (n_length + r_length), |
| 460 PagesToMiB(n_pages + r_pages), |
| 461 PagesToMiB(total_normal + total_returned), |
| 462 PagesToMiB(r_pages), |
| 463 PagesToMiB(total_returned)); |
| 464 } |
| 465 } |
| 466 |
| 467 total_normal += large.normal_pages; |
| 468 total_returned += large.returned_pages; |
| 469 out->printf(">255 large * %6u spans ~ %6.1f MiB; %6.1f MiB cum" |
| 470 "; unmapped: %6.1f MiB; %6.1f MiB cum\n", |
| 471 static_cast<unsigned int>(large.spans), |
| 472 PagesToMiB(large.normal_pages + large.returned_pages), |
| 473 PagesToMiB(total_normal + total_returned), |
| 474 PagesToMiB(large.returned_pages), |
| 475 PagesToMiB(total_returned)); |
409 } | 476 } |
410 } | 477 } |
411 | 478 |
412 static void PrintStats(int level) { | 479 static void PrintStats(int level) { |
413 const int kBufferSize = 16 << 10; | 480 const int kBufferSize = 16 << 10; |
414 char* buffer = new char[kBufferSize]; | 481 char* buffer = new char[kBufferSize]; |
415 TCMalloc_Printer printer(buffer, kBufferSize); | 482 TCMalloc_Printer printer(buffer, kBufferSize); |
416 DumpStats(&printer, level); | 483 DumpStats(&printer, level); |
417 write(STDERR_FILENO, buffer, strlen(buffer)); | 484 write(STDERR_FILENO, buffer, strlen(buffer)); |
418 delete[] buffer; | 485 delete[] buffer; |
419 } | 486 } |
420 | 487 |
421 static void** DumpHeapGrowthStackTraces() { | 488 static void** DumpHeapGrowthStackTraces() { |
422 // Count how much space we need | 489 // Count how much space we need |
423 int needed_slots = 0; | 490 int needed_slots = 0; |
424 { | 491 { |
425 SpinLockHolder h(Static::pageheap_lock()); | 492 SpinLockHolder h(Static::pageheap_lock()); |
426 for (StackTrace* t = Static::growth_stacks(); | 493 for (StackTrace* t = Static::growth_stacks(); |
427 t != NULL; | 494 t != NULL; |
428 t = reinterpret_cast<StackTrace*>( | 495 t = reinterpret_cast<StackTrace*>( |
429 t->stack[tcmalloc::kMaxStackDepth-1])) { | 496 t->stack[tcmalloc::kMaxStackDepth-1])) { |
430 needed_slots += 3 + t->depth; | 497 needed_slots += 3 + t->depth; |
431 } | 498 } |
432 needed_slots += 100; // Slop in case list grows | 499 needed_slots += 100; // Slop in case list grows |
433 needed_slots += needed_slots/8; // An extra 12.5% slop | 500 needed_slots += needed_slots/8; // An extra 12.5% slop |
434 } | 501 } |
435 | 502 |
436 void** result = new void*[needed_slots]; | 503 void** result = new void*[needed_slots]; |
437 if (result == NULL) { | 504 if (result == NULL) { |
438 MESSAGE("tcmalloc: allocation failed for stack trace slots", | 505 Log(kLog, __FILE__, __LINE__, |
439 needed_slots * sizeof(*result)); | 506 "tcmalloc: allocation failed for stack trace slots", |
| 507 needed_slots * sizeof(*result)); |
440 return NULL; | 508 return NULL; |
441 } | 509 } |
442 | 510 |
443 SpinLockHolder h(Static::pageheap_lock()); | 511 SpinLockHolder h(Static::pageheap_lock()); |
444 int used_slots = 0; | 512 int used_slots = 0; |
445 for (StackTrace* t = Static::growth_stacks(); | 513 for (StackTrace* t = Static::growth_stacks(); |
446 t != NULL; | 514 t != NULL; |
447 t = reinterpret_cast<StackTrace*>( | 515 t = reinterpret_cast<StackTrace*>( |
448 t->stack[tcmalloc::kMaxStackDepth-1])) { | 516 t->stack[tcmalloc::kMaxStackDepth-1])) { |
449 ASSERT(used_slots < needed_slots); // Need to leave room for terminator | 517 ASSERT(used_slots < needed_slots); // Need to leave room for terminator |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
555 | 623 |
556 virtual void Ranges(void* arg, RangeFunction func) { | 624 virtual void Ranges(void* arg, RangeFunction func) { |
557 IterateOverRanges(arg, func); | 625 IterateOverRanges(arg, func); |
558 } | 626 } |
559 | 627 |
560 virtual bool GetNumericProperty(const char* name, size_t* value) { | 628 virtual bool GetNumericProperty(const char* name, size_t* value) { |
561 ASSERT(name != NULL); | 629 ASSERT(name != NULL); |
562 | 630 |
563 if (strcmp(name, "generic.current_allocated_bytes") == 0) { | 631 if (strcmp(name, "generic.current_allocated_bytes") == 0) { |
564 TCMallocStats stats; | 632 TCMallocStats stats; |
565 ExtractStats(&stats, NULL); | 633 ExtractStats(&stats, NULL, NULL, NULL); |
566 *value = stats.pageheap.system_bytes | 634 *value = stats.pageheap.system_bytes |
567 - stats.thread_bytes | 635 - stats.thread_bytes |
568 - stats.central_bytes | 636 - stats.central_bytes |
569 - stats.transfer_bytes | 637 - stats.transfer_bytes |
570 - stats.pageheap.free_bytes | 638 - stats.pageheap.free_bytes |
571 - stats.pageheap.unmapped_bytes; | 639 - stats.pageheap.unmapped_bytes; |
572 return true; | 640 return true; |
573 } | 641 } |
574 | 642 |
575 if (strcmp(name, "generic.heap_size") == 0) { | 643 if (strcmp(name, "generic.heap_size") == 0) { |
576 TCMallocStats stats; | 644 TCMallocStats stats; |
577 ExtractStats(&stats, NULL); | 645 ExtractStats(&stats, NULL, NULL, NULL); |
578 *value = stats.pageheap.system_bytes; | 646 *value = stats.pageheap.system_bytes; |
579 return true; | 647 return true; |
580 } | 648 } |
581 | 649 |
582 if (strcmp(name, "tcmalloc.slack_bytes") == 0) { | 650 if (strcmp(name, "tcmalloc.slack_bytes") == 0) { |
583 // Kept for backwards compatibility. Now defined externally as: | 651 // Kept for backwards compatibility. Now defined externally as: |
584 // pageheap_free_bytes + pageheap_unmapped_bytes. | 652 // pageheap_free_bytes + pageheap_unmapped_bytes. |
585 SpinLockHolder l(Static::pageheap_lock()); | 653 SpinLockHolder l(Static::pageheap_lock()); |
586 PageHeap::Stats stats = Static::pageheap()->stats(); | 654 PageHeap::Stats stats = Static::pageheap()->stats(); |
587 *value = stats.free_bytes + stats.unmapped_bytes; | 655 *value = stats.free_bytes + stats.unmapped_bytes; |
(...skipping 13 matching lines...) Expand all Loading... |
601 } | 669 } |
602 | 670 |
603 if (strcmp(name, "tcmalloc.max_total_thread_cache_bytes") == 0) { | 671 if (strcmp(name, "tcmalloc.max_total_thread_cache_bytes") == 0) { |
604 SpinLockHolder l(Static::pageheap_lock()); | 672 SpinLockHolder l(Static::pageheap_lock()); |
605 *value = ThreadCache::overall_thread_cache_size(); | 673 *value = ThreadCache::overall_thread_cache_size(); |
606 return true; | 674 return true; |
607 } | 675 } |
608 | 676 |
609 if (strcmp(name, "tcmalloc.current_total_thread_cache_bytes") == 0) { | 677 if (strcmp(name, "tcmalloc.current_total_thread_cache_bytes") == 0) { |
610 TCMallocStats stats; | 678 TCMallocStats stats; |
611 ExtractStats(&stats, NULL); | 679 ExtractStats(&stats, NULL, NULL, NULL); |
612 *value = stats.thread_bytes; | 680 *value = stats.thread_bytes; |
613 return true; | 681 return true; |
614 } | 682 } |
615 | 683 |
616 return false; | 684 return false; |
617 } | 685 } |
618 | 686 |
619 virtual bool SetNumericProperty(const char* name, size_t value) { | 687 virtual bool SetNumericProperty(const char* name, size_t value) { |
620 ASSERT(name != NULL); | 688 ASSERT(name != NULL); |
621 | 689 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
682 const size_t alloc_size = Static::sizemap()->ByteSizeForClass(cl); | 750 const size_t alloc_size = Static::sizemap()->ByteSizeForClass(cl); |
683 return alloc_size; | 751 return alloc_size; |
684 } else { | 752 } else { |
685 return tcmalloc::pages(size) << kPageShift; | 753 return tcmalloc::pages(size) << kPageShift; |
686 } | 754 } |
687 } | 755 } |
688 | 756 |
689 // This just calls GetSizeWithCallback, but because that's in an | 757 // This just calls GetSizeWithCallback, but because that's in an |
690 // unnamed namespace, we need to move the definition below it in the | 758 // unnamed namespace, we need to move the definition below it in the |
691 // file. | 759 // file. |
692 virtual size_t GetAllocatedSize(void* ptr); | 760 virtual size_t GetAllocatedSize(const void* ptr); |
693 | 761 |
694 // This duplicates some of the logic in GetSizeWithCallback, but is | 762 // This duplicates some of the logic in GetSizeWithCallback, but is |
695 // faster. This is important on OS X, where this function is called | 763 // faster. This is important on OS X, where this function is called |
696 // on every allocation operation. | 764 // on every allocation operation. |
697 virtual Ownership GetOwnership(const void* ptr) { | 765 virtual Ownership GetOwnership(const void* ptr) { |
698 const PageID p = reinterpret_cast<uintptr_t>(ptr) >> kPageShift; | 766 const PageID p = reinterpret_cast<uintptr_t>(ptr) >> kPageShift; |
699 // The rest of tcmalloc assumes that all allocated pointers use at | 767 // The rest of tcmalloc assumes that all allocated pointers use at |
700 // most kAddressBits bits. If ptr doesn't, then it definitely | 768 // most kAddressBits bits. If ptr doesn't, then it definitely |
701 // wasn't alloacted by tcmalloc. | 769 // wasn't alloacted by tcmalloc. |
702 if ((p >> (kAddressBits - kPageShift)) > 0) { | 770 if ((p >> (kAddressBits - kPageShift)) > 0) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
756 MallocExtension::FreeListInfo i; | 824 MallocExtension::FreeListInfo i; |
757 i.min_object_size = prev_class_size + 1; | 825 i.min_object_size = prev_class_size + 1; |
758 i.max_object_size = Static::sizemap()->ByteSizeForClass(cl); | 826 i.max_object_size = Static::sizemap()->ByteSizeForClass(cl); |
759 i.total_bytes_free = | 827 i.total_bytes_free = |
760 class_count[cl] * Static::sizemap()->ByteSizeForClass(cl); | 828 class_count[cl] * Static::sizemap()->ByteSizeForClass(cl); |
761 i.type = kThreadCacheType; | 829 i.type = kThreadCacheType; |
762 v->push_back(i); | 830 v->push_back(i); |
763 } | 831 } |
764 | 832 |
765 // append page heap info | 833 // append page heap info |
766 int64 page_count_normal[kMaxPages]; | 834 PageHeap::SmallSpanStats small; |
767 int64 page_count_returned[kMaxPages]; | 835 PageHeap::LargeSpanStats large; |
768 int64 span_count_normal; | |
769 int64 span_count_returned; | |
770 { | 836 { |
771 SpinLockHolder h(Static::pageheap_lock()); | 837 SpinLockHolder h(Static::pageheap_lock()); |
772 Static::pageheap()->GetClassSizes(page_count_normal, | 838 Static::pageheap()->GetSmallSpanStats(&small); |
773 page_count_returned, | 839 Static::pageheap()->GetLargeSpanStats(&large); |
774 &span_count_normal, | |
775 &span_count_returned); | |
776 } | 840 } |
777 | 841 |
778 // spans: mapped | 842 // large spans: mapped |
779 MallocExtension::FreeListInfo span_info; | 843 MallocExtension::FreeListInfo span_info; |
780 span_info.type = kLargeSpanType; | 844 span_info.type = kLargeSpanType; |
781 span_info.max_object_size = (numeric_limits<size_t>::max)(); | 845 span_info.max_object_size = (numeric_limits<size_t>::max)(); |
782 span_info.min_object_size = kMaxPages << kPageShift; | 846 span_info.min_object_size = kMaxPages << kPageShift; |
783 span_info.total_bytes_free = span_count_normal << kPageShift; | 847 span_info.total_bytes_free = large.normal_pages << kPageShift; |
784 v->push_back(span_info); | 848 v->push_back(span_info); |
785 | 849 |
786 // spans: unmapped | 850 // large spans: unmapped |
787 span_info.type = kLargeUnmappedSpanType; | 851 span_info.type = kLargeUnmappedSpanType; |
788 span_info.total_bytes_free = span_count_returned << kPageShift; | 852 span_info.total_bytes_free = large.returned_pages << kPageShift; |
789 v->push_back(span_info); | 853 v->push_back(span_info); |
790 | 854 |
| 855 // small spans |
791 for (int s = 1; s < kMaxPages; s++) { | 856 for (int s = 1; s < kMaxPages; s++) { |
792 MallocExtension::FreeListInfo i; | 857 MallocExtension::FreeListInfo i; |
793 i.max_object_size = (s << kPageShift); | 858 i.max_object_size = (s << kPageShift); |
794 i.min_object_size = ((s - 1) << kPageShift); | 859 i.min_object_size = ((s - 1) << kPageShift); |
795 | 860 |
796 i.type = kPageHeapType; | 861 i.type = kPageHeapType; |
797 i.total_bytes_free = (s << kPageShift) * page_count_normal[s]; | 862 i.total_bytes_free = (s << kPageShift) * small.normal_length[s]; |
798 v->push_back(i); | 863 v->push_back(i); |
799 | 864 |
800 i.type = kPageHeapUnmappedType; | 865 i.type = kPageHeapUnmappedType; |
801 i.total_bytes_free = (s << kPageShift) * page_count_returned[s]; | 866 i.total_bytes_free = (s << kPageShift) * small.returned_length[s]; |
802 v->push_back(i); | 867 v->push_back(i); |
803 } | 868 } |
804 } | 869 } |
805 }; | 870 }; |
806 | 871 |
807 // The constructor allocates an object to ensure that initialization | 872 // The constructor allocates an object to ensure that initialization |
808 // runs before main(), and therefore we do not have a chance to become | 873 // runs before main(), and therefore we do not have a chance to become |
809 // multi-threaded before initialization. We also create the TSD key | 874 // multi-threaded before initialization. We also create the TSD key |
810 // here. Presumably by the time this constructor runs, glibc is in | 875 // here. Presumably by the time this constructor runs, glibc is in |
811 // good enough shape to handle pthread_key_create(). | 876 // good enough shape to handle pthread_key_create(). |
812 // | 877 // |
813 // The constructor also takes the opportunity to tell STL to use | 878 // The constructor also takes the opportunity to tell STL to use |
814 // tcmalloc. We want to do this early, before construct time, so | 879 // tcmalloc. We want to do this early, before construct time, so |
815 // all user STL allocations go through tcmalloc (which works really | 880 // all user STL allocations go through tcmalloc (which works really |
816 // well for STL). | 881 // well for STL). |
817 // | 882 // |
818 // The destructor prints stats when the program exits. | 883 // The destructor prints stats when the program exits. |
819 static int tcmallocguard_refcount = 0; // no lock needed: runs before main() | 884 static int tcmallocguard_refcount = 0; // no lock needed: runs before main() |
820 TCMallocGuard::TCMallocGuard() { | 885 TCMallocGuard::TCMallocGuard() { |
821 if (tcmallocguard_refcount++ == 0) { | 886 if (tcmallocguard_refcount++ == 0) { |
822 #ifdef HAVE_TLS // this is true if the cc/ld/libc combo support TLS | 887 #ifdef HAVE_TLS // this is true if the cc/ld/libc combo support TLS |
823 // Check whether the kernel also supports TLS (needs to happen at runtime) | 888 // Check whether the kernel also supports TLS (needs to happen at runtime) |
824 tcmalloc::CheckIfKernelSupportsTLS(); | 889 tcmalloc::CheckIfKernelSupportsTLS(); |
825 #endif | 890 #endif |
826 ReplaceSystemAlloc(); // defined in libc_override_*.h | 891 ReplaceSystemAlloc(); // defined in libc_override_*.h |
827 #if defined(__APPLE__) | |
828 // To break the recursive call of malloc, as malloc -> TCMALLOC_MESSAGE | |
829 // -> snprintf -> localeconv_l -> malloc, on MacOS. | |
830 char buf[32]; | |
831 snprintf(buf, sizeof(buf), "%d", tcmallocguard_refcount); | |
832 #endif | |
833 tc_free(tc_malloc(1)); | 892 tc_free(tc_malloc(1)); |
834 ThreadCache::InitTSD(); | 893 ThreadCache::InitTSD(); |
835 tc_free(tc_malloc(1)); | 894 tc_free(tc_malloc(1)); |
836 // Either we, or debugallocation.cc, or valgrind will control memory | 895 // Either we, or debugallocation.cc, or valgrind will control memory |
837 // management. We register our extension if we're the winner. | 896 // management. We register our extension if we're the winner. |
838 #ifdef TCMALLOC_USING_DEBUGALLOCATION | 897 #ifdef TCMALLOC_USING_DEBUGALLOCATION |
839 // Let debugallocation register its extension. | 898 // Let debugallocation register its extension. |
840 #else | 899 #else |
841 if (RunningOnValgrind()) { | 900 if (RunningOnValgrind()) { |
842 // Let Valgrind uses its own malloc (so don't register our extension). | 901 // Let Valgrind uses its own malloc (so don't register our extension). |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1029 | 1088 |
1030 static inline ThreadCache* GetCacheIfPresent() { | 1089 static inline ThreadCache* GetCacheIfPresent() { |
1031 void* const p = ThreadCache::GetCacheIfPresent(); | 1090 void* const p = ThreadCache::GetCacheIfPresent(); |
1032 return reinterpret_cast<ThreadCache*>(p); | 1091 return reinterpret_cast<ThreadCache*>(p); |
1033 } | 1092 } |
1034 | 1093 |
1035 // This lets you call back to a given function pointer if ptr is invalid. | 1094 // This lets you call back to a given function pointer if ptr is invalid. |
1036 // It is used primarily by windows code which wants a specialized callback. | 1095 // It is used primarily by windows code which wants a specialized callback. |
1037 inline void do_free_with_callback(void* ptr, void (*invalid_free_fn)(void*)) { | 1096 inline void do_free_with_callback(void* ptr, void (*invalid_free_fn)(void*)) { |
1038 if (ptr == NULL) return; | 1097 if (ptr == NULL) return; |
1039 ASSERT(Static::pageheap() != NULL); // Should not call free() before malloc() | 1098 if (Static::pageheap() == NULL) { |
| 1099 // We called free() before malloc(). This can occur if the |
| 1100 // (system) malloc() is called before tcmalloc is loaded, and then |
| 1101 // free() is called after tcmalloc is loaded (and tc_free has |
| 1102 // replaced free), but before the global constructor has run that |
| 1103 // sets up the tcmalloc data structures. |
| 1104 (*invalid_free_fn)(ptr); // Decide how to handle the bad free request |
| 1105 return; |
| 1106 } |
1040 const PageID p = reinterpret_cast<uintptr_t>(ptr) >> kPageShift; | 1107 const PageID p = reinterpret_cast<uintptr_t>(ptr) >> kPageShift; |
1041 Span* span = NULL; | 1108 Span* span = NULL; |
1042 size_t cl = Static::pageheap()->GetSizeClassIfCached(p); | 1109 size_t cl = Static::pageheap()->GetSizeClassIfCached(p); |
1043 | 1110 |
1044 if (cl == 0) { | 1111 if (cl == 0) { |
1045 span = Static::pageheap()->GetDescriptor(p); | 1112 span = Static::pageheap()->GetDescriptor(p); |
1046 if (!span) { | 1113 if (!span) { |
1047 // span can be NULL because the pointer passed in is invalid | 1114 // span can be NULL because the pointer passed in is invalid |
1048 // (not something returned by malloc or friends), or because the | 1115 // (not something returned by malloc or friends), or because the |
1049 // pointer was allocated with some other allocator besides | 1116 // pointer was allocated with some other allocator besides |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1081 } | 1148 } |
1082 } | 1149 } |
1083 | 1150 |
1084 // The default "do_free" that uses the default callback. | 1151 // The default "do_free" that uses the default callback. |
1085 inline void do_free(void* ptr) { | 1152 inline void do_free(void* ptr) { |
1086 return do_free_with_callback(ptr, &InvalidFree); | 1153 return do_free_with_callback(ptr, &InvalidFree); |
1087 } | 1154 } |
1088 | 1155 |
1089 // NOTE: some logic here is duplicated in GetOwnership (above), for | 1156 // NOTE: some logic here is duplicated in GetOwnership (above), for |
1090 // speed. If you change this function, look at that one too. | 1157 // speed. If you change this function, look at that one too. |
1091 inline size_t GetSizeWithCallback(void* ptr, | 1158 inline size_t GetSizeWithCallback(const void* ptr, |
1092 size_t (*invalid_getsize_fn)(void*)) { | 1159 size_t (*invalid_getsize_fn)(const void*)) { |
1093 if (ptr == NULL) | 1160 if (ptr == NULL) |
1094 return 0; | 1161 return 0; |
1095 const PageID p = reinterpret_cast<uintptr_t>(ptr) >> kPageShift; | 1162 const PageID p = reinterpret_cast<uintptr_t>(ptr) >> kPageShift; |
1096 size_t cl = Static::pageheap()->GetSizeClassIfCached(p); | 1163 size_t cl = Static::pageheap()->GetSizeClassIfCached(p); |
1097 if (cl != 0) { | 1164 if (cl != 0) { |
1098 return Static::sizemap()->ByteSizeForClass(cl); | 1165 return Static::sizemap()->ByteSizeForClass(cl); |
1099 } else { | 1166 } else { |
1100 const Span *span = Static::pageheap()->GetDescriptor(p); | 1167 const Span *span = Static::pageheap()->GetDescriptor(p); |
1101 if (span == NULL) { // means we do not own this memory | 1168 if (span == NULL) { // means we do not own this memory |
1102 return (*invalid_getsize_fn)(ptr); | 1169 return (*invalid_getsize_fn)(ptr); |
1103 } else if (span->sizeclass != 0) { | 1170 } else if (span->sizeclass != 0) { |
1104 Static::pageheap()->CacheSizeClass(p, span->sizeclass); | 1171 Static::pageheap()->CacheSizeClass(p, span->sizeclass); |
1105 return Static::sizemap()->ByteSizeForClass(span->sizeclass); | 1172 return Static::sizemap()->ByteSizeForClass(span->sizeclass); |
1106 } else { | 1173 } else { |
1107 return span->length << kPageShift; | 1174 return span->length << kPageShift; |
1108 } | 1175 } |
1109 } | 1176 } |
1110 } | 1177 } |
1111 | 1178 |
1112 // This lets you call back to a given function pointer if ptr is invalid. | 1179 // This lets you call back to a given function pointer if ptr is invalid. |
1113 // It is used primarily by windows code which wants a specialized callback. | 1180 // It is used primarily by windows code which wants a specialized callback. |
1114 inline void* do_realloc_with_callback( | 1181 inline void* do_realloc_with_callback( |
1115 void* old_ptr, size_t new_size, | 1182 void* old_ptr, size_t new_size, |
1116 void (*invalid_free_fn)(void*), | 1183 void (*invalid_free_fn)(void*), |
1117 size_t (*invalid_get_size_fn)(void*)) { | 1184 size_t (*invalid_get_size_fn)(const void*)) { |
1118 // Get the size of the old entry | 1185 // Get the size of the old entry |
1119 const size_t old_size = GetSizeWithCallback(old_ptr, invalid_get_size_fn); | 1186 const size_t old_size = GetSizeWithCallback(old_ptr, invalid_get_size_fn); |
1120 | 1187 |
1121 // Reallocate if the new size is larger than the old size, | 1188 // Reallocate if the new size is larger than the old size, |
1122 // or if the new size is significantly smaller than the old size. | 1189 // or if the new size is significantly smaller than the old size. |
1123 // We do hysteresis to avoid resizing ping-pongs: | 1190 // We do hysteresis to avoid resizing ping-pongs: |
1124 // . If we need to grow, grow to max(new_size, old_size * 1.X) | 1191 // . If we need to grow, grow to max(new_size, old_size * 1.X) |
1125 // . Don't shrink unless new_size < old_size * 0.Y | 1192 // . Don't shrink unless new_size < old_size * 0.Y |
1126 // X and Y trade-off time for wasted space. For now we do 1.25 and 0.5. | 1193 // X and Y trade-off time for wasted space. For now we do 1.25 and 0.5. |
1127 const int lower_bound_to_grow = old_size + old_size / 4; | 1194 const int lower_bound_to_grow = old_size + old_size / 4; |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1248 PrintStats(1); | 1315 PrintStats(1); |
1249 } | 1316 } |
1250 | 1317 |
1251 inline int do_mallopt(int cmd, int value) { | 1318 inline int do_mallopt(int cmd, int value) { |
1252 return 1; // Indicates error | 1319 return 1; // Indicates error |
1253 } | 1320 } |
1254 | 1321 |
1255 #ifdef HAVE_STRUCT_MALLINFO | 1322 #ifdef HAVE_STRUCT_MALLINFO |
1256 inline struct mallinfo do_mallinfo() { | 1323 inline struct mallinfo do_mallinfo() { |
1257 TCMallocStats stats; | 1324 TCMallocStats stats; |
1258 ExtractStats(&stats, NULL); | 1325 ExtractStats(&stats, NULL, NULL, NULL); |
1259 | 1326 |
1260 // Just some of the fields are filled in. | 1327 // Just some of the fields are filled in. |
1261 struct mallinfo info; | 1328 struct mallinfo info; |
1262 memset(&info, 0, sizeof(info)); | 1329 memset(&info, 0, sizeof(info)); |
1263 | 1330 |
1264 // Unfortunately, the struct contains "int" field, so some of the | 1331 // Unfortunately, the struct contains "int" field, so some of the |
1265 // size values will be truncated. | 1332 // size values will be truncated. |
1266 info.arena = static_cast<int>(stats.pageheap.system_bytes); | 1333 info.arena = static_cast<int>(stats.pageheap.system_bytes); |
1267 info.fsmblks = static_cast<int>(stats.thread_bytes | 1334 info.fsmblks = static_cast<int>(stats.thread_bytes |
1268 + stats.central_bytes | 1335 + stats.central_bytes |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1372 } else { // allocation success | 1439 } else { // allocation success |
1373 return p; | 1440 return p; |
1374 } | 1441 } |
1375 #endif // PREANSINEW | 1442 #endif // PREANSINEW |
1376 } | 1443 } |
1377 } | 1444 } |
1378 | 1445 |
1379 } // end unnamed namespace | 1446 } // end unnamed namespace |
1380 | 1447 |
1381 // As promised, the definition of this function, declared above. | 1448 // As promised, the definition of this function, declared above. |
1382 size_t TCMallocImplementation::GetAllocatedSize(void* ptr) { | 1449 size_t TCMallocImplementation::GetAllocatedSize(const void* ptr) { |
1383 ASSERT(TCMallocImplementation::GetOwnership(ptr) | 1450 ASSERT(TCMallocImplementation::GetOwnership(ptr) |
1384 != TCMallocImplementation::kNotOwned); | 1451 != TCMallocImplementation::kNotOwned); |
1385 return GetSizeWithCallback(ptr, &InvalidGetAllocatedSize); | 1452 return GetSizeWithCallback(ptr, &InvalidGetAllocatedSize); |
1386 } | 1453 } |
1387 | 1454 |
1388 void TCMallocImplementation::MarkThreadBusy() { | 1455 void TCMallocImplementation::MarkThreadBusy() { |
1389 // Allocate to force the creation of a thread cache, but avoid | 1456 // Allocate to force the creation of a thread cache, but avoid |
1390 // invoking any hooks. | 1457 // invoking any hooks. |
1391 do_free(do_malloc(0)); | 1458 do_free(do_malloc(0)); |
1392 } | 1459 } |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1576 extern "C" PERFTOOLS_DLL_DECL struct mallinfo tc_mallinfo(void) __THROW { | 1643 extern "C" PERFTOOLS_DLL_DECL struct mallinfo tc_mallinfo(void) __THROW { |
1577 return do_mallinfo(); | 1644 return do_mallinfo(); |
1578 } | 1645 } |
1579 #endif | 1646 #endif |
1580 | 1647 |
1581 extern "C" PERFTOOLS_DLL_DECL size_t tc_malloc_size(void* ptr) __THROW { | 1648 extern "C" PERFTOOLS_DLL_DECL size_t tc_malloc_size(void* ptr) __THROW { |
1582 return MallocExtension::instance()->GetAllocatedSize(ptr); | 1649 return MallocExtension::instance()->GetAllocatedSize(ptr); |
1583 } | 1650 } |
1584 | 1651 |
1585 #endif // TCMALLOC_USING_DEBUGALLOCATION | 1652 #endif // TCMALLOC_USING_DEBUGALLOCATION |
OLD | NEW |