OLD | NEW |
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 28 matching lines...) Expand all Loading... |
39 #include "central_freelist.h" // for CentralFreeListPadded | 39 #include "central_freelist.h" // for CentralFreeListPadded |
40 #include "maybe_threads.h" | 40 #include "maybe_threads.h" |
41 | 41 |
42 using std::min; | 42 using std::min; |
43 using std::max; | 43 using std::max; |
44 | 44 |
45 DEFINE_int64(tcmalloc_max_total_thread_cache_bytes, | 45 DEFINE_int64(tcmalloc_max_total_thread_cache_bytes, |
46 EnvToInt64("TCMALLOC_MAX_TOTAL_THREAD_CACHE_BYTES", | 46 EnvToInt64("TCMALLOC_MAX_TOTAL_THREAD_CACHE_BYTES", |
47 kDefaultOverallThreadCacheSize), | 47 kDefaultOverallThreadCacheSize), |
48 "Bound on the total amount of bytes allocated to " | 48 "Bound on the total amount of bytes allocated to " |
49 "thread caches. This bound is not strict, so it is possible " | 49 "thread caches. This bound is not strict, so it is possible " |
50 "for the cache to go over this bound in certain circumstances. "); | 50 "for the cache to go over this bound in certain circumstances. " |
| 51 "Maximum value of this flag is capped to 1 GB."); |
51 | 52 |
52 namespace tcmalloc { | 53 namespace tcmalloc { |
53 | 54 |
54 static bool phinited = false; | 55 static bool phinited = false; |
55 | 56 |
56 volatile size_t ThreadCache::per_thread_cache_size_ = kMaxThreadCacheSize; | 57 volatile size_t ThreadCache::per_thread_cache_size_ = kMaxThreadCacheSize; |
57 size_t ThreadCache::overall_thread_cache_size_ = kDefaultOverallThreadCacheSize; | 58 size_t ThreadCache::overall_thread_cache_size_ = kDefaultOverallThreadCacheSize; |
58 ssize_t ThreadCache::unclaimed_cache_space_ = kDefaultOverallThreadCacheSize; | 59 ssize_t ThreadCache::unclaimed_cache_space_ = kDefaultOverallThreadCacheSize; |
59 PageHeapAllocator<ThreadCache> threadcache_allocator; | 60 PageHeapAllocator<ThreadCache> threadcache_allocator; |
60 ThreadCache* ThreadCache::thread_heaps_ = NULL; | 61 ThreadCache* ThreadCache::thread_heaps_ = NULL; |
61 int ThreadCache::thread_heap_count_ = 0; | 62 int ThreadCache::thread_heap_count_ = 0; |
62 ThreadCache* ThreadCache::next_memory_steal_ = NULL; | 63 ThreadCache* ThreadCache::next_memory_steal_ = NULL; |
63 #ifdef HAVE_TLS | 64 #ifdef HAVE_TLS |
64 __thread ThreadCache* ThreadCache::threadlocal_heap_ | 65 __thread ThreadCache* ThreadCache::threadlocal_heap_ |
65 # ifdef HAVE___ATTRIBUTE__ | 66 # ifdef HAVE___ATTRIBUTE__ |
66 __attribute__ ((tls_model ("initial-exec"))) | 67 __attribute__ ((tls_model ("initial-exec"))) |
67 # endif | 68 # endif |
68 ; | 69 ; |
69 #endif | 70 #endif |
70 bool ThreadCache::tsd_inited_ = false; | 71 bool ThreadCache::tsd_inited_ = false; |
71 pthread_key_t ThreadCache::heap_key_; | 72 pthread_key_t ThreadCache::heap_key_; |
72 | 73 |
73 #if defined(HAVE_TLS) | 74 #if defined(HAVE_TLS) |
74 bool kernel_supports_tls = false; // be conservative | 75 bool kernel_supports_tls = false; // be conservative |
75 # if !HAVE_DECL_UNAME // if too old for uname, probably too old for TLS | 76 # if defined(_WIN32) // windows has supported TLS since winnt, I think. |
| 77 void CheckIfKernelSupportsTLS() { |
| 78 kernel_supports_tls = true; |
| 79 } |
| 80 # elif !HAVE_DECL_UNAME // if too old for uname, probably too old for TLS |
76 void CheckIfKernelSupportsTLS() { | 81 void CheckIfKernelSupportsTLS() { |
77 kernel_supports_tls = false; | 82 kernel_supports_tls = false; |
78 } | 83 } |
79 # else | 84 # else |
80 # include <sys/utsname.h> // DECL_UNAME checked for <sys/utsname.h> too | 85 # include <sys/utsname.h> // DECL_UNAME checked for <sys/utsname.h> too |
81 void CheckIfKernelSupportsTLS() { | 86 void CheckIfKernelSupportsTLS() { |
82 struct utsname buf; | 87 struct utsname buf; |
83 if (uname(&buf) != 0) { // should be impossible | 88 if (uname(&buf) != 0) { // should be impossible |
84 MESSAGE("uname failed assuming no TLS support (errno=%d)\n", errno); | 89 MESSAGE("uname failed assuming no TLS support (errno=%d)\n", errno); |
85 kernel_supports_tls = false; | 90 kernel_supports_tls = false; |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 Static::InitStaticVars(); | 311 Static::InitStaticVars(); |
307 threadcache_allocator.Init(); | 312 threadcache_allocator.Init(); |
308 phinited = 1; | 313 phinited = 1; |
309 } | 314 } |
310 } | 315 } |
311 | 316 |
312 void ThreadCache::InitTSD() { | 317 void ThreadCache::InitTSD() { |
313 ASSERT(!tsd_inited_); | 318 ASSERT(!tsd_inited_); |
314 perftools_pthread_key_create(&heap_key_, DestroyThreadCache); | 319 perftools_pthread_key_create(&heap_key_, DestroyThreadCache); |
315 tsd_inited_ = true; | 320 tsd_inited_ = true; |
| 321 |
| 322 #ifdef PTHREADS_CRASHES_IF_RUN_TOO_EARLY |
| 323 // We may have used a fake pthread_t for the main thread. Fix it. |
| 324 pthread_t zero; |
| 325 memset(&zero, 0, sizeof(zero)); |
| 326 SpinLockHolder h(Static::pageheap_lock()); |
| 327 for (ThreadCache* h = thread_heaps_; h != NULL; h = h->next_) { |
| 328 if (h->tid_ == zero) { |
| 329 h->tid_ = pthread_self(); |
| 330 } |
| 331 } |
| 332 #endif |
316 } | 333 } |
317 | 334 |
318 ThreadCache* ThreadCache::CreateCacheIfNecessary() { | 335 ThreadCache* ThreadCache::CreateCacheIfNecessary() { |
319 // Initialize per-thread data if necessary | 336 // Initialize per-thread data if necessary |
320 ThreadCache* heap = NULL; | 337 ThreadCache* heap = NULL; |
321 { | 338 { |
322 SpinLockHolder h(Static::pageheap_lock()); | 339 SpinLockHolder h(Static::pageheap_lock()); |
323 // On very old libc's, this call may crash if it happens too | 340 // On some old glibc's, and on freebsd's libc (as of freebsd 8.1), |
324 // early. No libc using NPTL should be affected. If there | 341 // calling pthread routines (even pthread_self) too early could |
325 // is a crash here, we could use code (on linux, at least) | 342 // cause a segfault. Since we can call pthreads quite early, we |
326 // to detect NPTL vs LinuxThreads: | 343 // have to protect against that in such situations by making a |
327 // http://www.redhat.com/archives/phil-list/2003-April/msg00038.html | 344 // 'fake' pthread. This is not ideal since it doesn't work well |
328 // If we detect not-NPTL, we could execute the old code from | 345 // when linking tcmalloc statically with apps that create threads |
329 // http://google-perftools.googlecode.com/svn/tags/google-perftools-1.7/sr
c/thread_cache.cc | 346 // before main, so we only do it if we have to. |
330 // that avoids calling pthread_self too early. The problem with | 347 #ifdef PTHREADS_CRASHES_IF_RUN_TOO_EARLY |
331 // that code is it caused a race condition when tcmalloc is linked | 348 pthread_t me; |
332 // in statically and other libraries spawn threads before main. | 349 if (!tsd_inited_) { |
| 350 memset(&me, 0, sizeof(me)); |
| 351 } else { |
| 352 me = pthread_self(); |
| 353 } |
| 354 #else |
333 const pthread_t me = pthread_self(); | 355 const pthread_t me = pthread_self(); |
| 356 #endif |
334 | 357 |
335 // This may be a recursive malloc call from pthread_setspecific() | 358 // This may be a recursive malloc call from pthread_setspecific() |
336 // In that case, the heap for this thread has already been created | 359 // In that case, the heap for this thread has already been created |
337 // and added to the linked list. So we search for that first. | 360 // and added to the linked list. So we search for that first. |
338 for (ThreadCache* h = thread_heaps_; h != NULL; h = h->next_) { | 361 for (ThreadCache* h = thread_heaps_; h != NULL; h = h->next_) { |
339 if (h->tid_ == me) { | 362 if (h->tid_ == me) { |
340 heap = h; | 363 heap = h; |
341 break; | 364 break; |
342 } | 365 } |
343 } | 366 } |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
467 list_[cl].length_overages()); | 490 list_[cl].length_overages()); |
468 } | 491 } |
469 } | 492 } |
470 | 493 |
471 void ThreadCache::PrintThreads(TCMalloc_Printer* out) { | 494 void ThreadCache::PrintThreads(TCMalloc_Printer* out) { |
472 size_t actual_limit = 0; | 495 size_t actual_limit = 0; |
473 for (ThreadCache* h = thread_heaps_; h != NULL; h = h->next_) { | 496 for (ThreadCache* h = thread_heaps_; h != NULL; h = h->next_) { |
474 h->Print(out); | 497 h->Print(out); |
475 actual_limit += h->max_size_; | 498 actual_limit += h->max_size_; |
476 } | 499 } |
477 out->printf("ThreadCache overall: %"PRIuS ", unclaimed: %"PRIuS | 500 out->printf("ThreadCache overall: %"PRIuS ", unclaimed: %"PRIdS |
478 ", actual: %"PRIuS"\n", | 501 ", actual: %"PRIuS"\n", |
479 overall_thread_cache_size_, unclaimed_cache_space_, actual_limit); | 502 overall_thread_cache_size_, unclaimed_cache_space_, actual_limit); |
480 } | 503 } |
481 | 504 |
482 void ThreadCache::GetThreadStats(uint64_t* total_bytes, uint64_t* class_count) { | 505 void ThreadCache::GetThreadStats(uint64_t* total_bytes, uint64_t* class_count) { |
483 for (ThreadCache* h = thread_heaps_; h != NULL; h = h->next_) { | 506 for (ThreadCache* h = thread_heaps_; h != NULL; h = h->next_) { |
484 *total_bytes += h->Size(); | 507 *total_bytes += h->Size(); |
485 if (class_count) { | 508 if (class_count) { |
486 for (int cl = 0; cl < kNumClasses; ++cl) { | 509 for (int cl = 0; cl < kNumClasses; ++cl) { |
487 class_count[cl] += h->freelist_length(cl); | 510 class_count[cl] += h->freelist_length(cl); |
488 } | 511 } |
489 } | 512 } |
490 } | 513 } |
491 } | 514 } |
492 | 515 |
493 void ThreadCache::set_overall_thread_cache_size(size_t new_size) { | 516 void ThreadCache::set_overall_thread_cache_size(size_t new_size) { |
494 // Clip the value to a reasonable range | 517 // Clip the value to a reasonable range |
495 if (new_size < kMinThreadCacheSize) new_size = kMinThreadCacheSize; | 518 if (new_size < kMinThreadCacheSize) new_size = kMinThreadCacheSize; |
496 if (new_size > (1<<30)) new_size = (1<<30); // Limit to 1GB | 519 if (new_size > (1<<30)) new_size = (1<<30); // Limit to 1GB |
497 overall_thread_cache_size_ = new_size; | 520 overall_thread_cache_size_ = new_size; |
498 | 521 |
499 RecomputePerThreadCacheSize(); | 522 RecomputePerThreadCacheSize(); |
500 } | 523 } |
501 | 524 |
502 } // namespace tcmalloc | 525 } // namespace tcmalloc |
OLD | NEW |