Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / third_party / tcmalloc / chromium / src / heap-profiler.cc
blob512dc7cd33139b22b47c0ad3218aa6b1ec94cdb3
1 // Copyright (c) 2005, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
30 // ---
31 // Author: Sanjay Ghemawat
33 // TODO: Log large allocations
35 #include <config.h>
36 #include <stddef.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42 #ifdef HAVE_INTTYPES_H
43 #include <inttypes.h>
44 #endif
45 #ifdef HAVE_FCNTL_H
46 #include <fcntl.h> // for open()
47 #endif
48 #ifdef HAVE_MMAP
49 #include <sys/mman.h>
50 #endif
51 #include <errno.h>
52 #include <assert.h>
53 #include <sys/types.h>
55 #include <algorithm>
56 #include <string>
58 #include <gperftools/heap-profiler.h>
60 #include "base/logging.h"
61 #include "base/basictypes.h" // for PRId64, among other things
62 #include "base/googleinit.h"
63 #include "base/commandlineflags.h"
64 #include "malloc_hook-inl.h"
65 #include "tcmalloc_guard.h"
66 #include <gperftools/malloc_hook.h>
67 #include <gperftools/malloc_extension.h>
68 #include "base/spinlock.h"
69 #include "base/low_level_alloc.h"
70 #include "base/sysinfo.h" // for GetUniquePathFromEnv()
71 #include "deep-heap-profile.h"
72 #include "heap-profile-table.h"
73 #include "memory_region_map.h"
76 #ifndef PATH_MAX
77 #ifdef MAXPATHLEN
78 #define PATH_MAX MAXPATHLEN
79 #else
80 #define PATH_MAX 4096 // seems conservative for max filename len!
81 #endif
82 #endif
84 using STL_NAMESPACE::string;
85 using STL_NAMESPACE::sort;
87 //----------------------------------------------------------------------
88 // Flags that control heap-profiling
90 // The thread-safety of the profiler depends on these being immutable
91 // after main starts, so don't change them.
92 //----------------------------------------------------------------------
94 DEFINE_int64(heap_profile_allocation_interval,
95 EnvToInt64("HEAP_PROFILE_ALLOCATION_INTERVAL", 1 << 30 /*1GB*/),
96 "If non-zero, dump heap profiling information once every "
97 "specified number of bytes allocated by the program since "
98 "the last dump.");
99 DEFINE_int64(heap_profile_deallocation_interval,
100 EnvToInt64("HEAP_PROFILE_DEALLOCATION_INTERVAL", 0),
101 "If non-zero, dump heap profiling information once every "
102 "specified number of bytes deallocated by the program "
103 "since the last dump.");
104 // We could also add flags that report whenever inuse_bytes changes by
105 // X or -X, but there hasn't been a need for that yet, so we haven't.
106 DEFINE_int64(heap_profile_inuse_interval,
107 EnvToInt64("HEAP_PROFILE_INUSE_INTERVAL", 100 << 20 /*100MB*/),
108 "If non-zero, dump heap profiling information whenever "
109 "the high-water memory usage mark increases by the specified "
110 "number of bytes.");
111 DEFINE_int64(heap_profile_time_interval,
112 EnvToInt64("HEAP_PROFILE_TIME_INTERVAL", 0),
113 "If non-zero, dump heap profiling information once every "
114 "specified number of seconds since the last dump.");
115 DEFINE_bool(mmap_log,
116 EnvToBool("HEAP_PROFILE_MMAP_LOG", false),
117 "Should mmap/munmap calls be logged?");
118 DEFINE_bool(mmap_profile,
119 EnvToBool("HEAP_PROFILE_MMAP", false),
120 "If heap-profiling is on, also profile mmap, mremap, and sbrk)");
121 DEFINE_bool(only_mmap_profile,
122 EnvToBool("HEAP_PROFILE_ONLY_MMAP", false),
123 "If heap-profiling is on, only profile mmap, mremap, and sbrk; "
124 "do not profile malloc/new/etc");
125 DEFINE_bool(deep_heap_profile,
126 EnvToBool("DEEP_HEAP_PROFILE", false),
127 "If heap-profiling is on, profile deeper (only on Linux)");
128 #if defined(TYPE_PROFILING)
129 DEFINE_bool(heap_profile_type_statistics,
130 EnvToBool("HEAP_PROFILE_TYPE_STATISTICS", false),
131 "If heap-profiling is on, dump type statistics.");
132 #endif // defined(TYPE_PROFILING)
135 //----------------------------------------------------------------------
136 // Locking
137 //----------------------------------------------------------------------
139 // A pthread_mutex has way too much lock contention to be used here.
141 // I would like to use Mutex, but it can call malloc(),
142 // which can cause us to fall into an infinite recursion.
144 // So we use a simple spinlock.
145 static SpinLock heap_lock(SpinLock::LINKER_INITIALIZED);
147 //----------------------------------------------------------------------
148 // Simple allocator for heap profiler's internal memory
149 //----------------------------------------------------------------------
151 static LowLevelAlloc::Arena* heap_profiler_memory;
153 static void* ProfilerMalloc(size_t bytes) {
154 return LowLevelAlloc::AllocWithArena(bytes, heap_profiler_memory);
156 static void ProfilerFree(void* p) {
157 LowLevelAlloc::Free(p);
160 // We use buffers of this size in DoGetHeapProfile.
161 // The size is 1 << 20 in the original google-perftools. Changed it to
162 // 5 << 20 since a larger buffer is requried for deeper profiling in Chromium.
163 // The buffer is allocated only when the environment variable HEAPPROFILE is
164 // specified to dump heap information.
165 static const int kProfileBufferSize = 5 << 20;
167 // This is a last-ditch buffer we use in DumpProfileLocked in case we
168 // can't allocate more memory from ProfilerMalloc. We expect this
169 // will be used by HeapProfileEndWriter when the application has to
170 // exit due to out-of-memory. This buffer is allocated in
171 // HeapProfilerStart. Access to this must be protected by heap_lock.
172 static char* global_profiler_buffer = NULL;
175 //----------------------------------------------------------------------
176 // Profiling control/state data
177 //----------------------------------------------------------------------
179 // Access to all of these is protected by heap_lock.
180 static bool is_on = false; // If are on as a subsytem.
181 static bool dumping = false; // Dumping status to prevent recursion
182 static char* filename_prefix = NULL; // Prefix used for profile file names
183 // (NULL if no need for dumping yet)
184 static int dump_count = 0; // How many dumps so far
185 static int64 last_dump_alloc = 0; // alloc_size when did we last dump
186 static int64 last_dump_free = 0; // free_size when did we last dump
187 static int64 high_water_mark = 0; // In-use-bytes at last high-water dump
188 static int64 last_dump_time = 0; // The time of the last dump
190 static HeapProfileTable* heap_profile = NULL; // the heap profile table
191 static DeepHeapProfile* deep_profile = NULL; // deep memory profiler
193 //----------------------------------------------------------------------
194 // Profile generation
195 //----------------------------------------------------------------------
197 // Input must be a buffer of size at least 1MB.
198 static char* DoGetHeapProfileLocked(char* buf, int buflen) {
199 // We used to be smarter about estimating the required memory and
200 // then capping it to 1MB and generating the profile into that.
201 if (buf == NULL || buflen < 1)
202 return NULL;
204 RAW_DCHECK(heap_lock.IsHeld(), "");
205 int bytes_written = 0;
206 if (is_on) {
207 HeapProfileTable::Stats const stats = heap_profile->total();
208 (void)stats; // avoid an unused-variable warning in non-debug mode.
209 if (deep_profile) {
210 bytes_written = deep_profile->FillOrderedProfile(buf, buflen - 1);
211 } else {
212 bytes_written = heap_profile->FillOrderedProfile(buf, buflen - 1);
214 // FillOrderedProfile should not reduce the set of active mmap-ed regions,
215 // hence MemoryRegionMap will let us remove everything we've added above:
216 RAW_DCHECK(stats.Equivalent(heap_profile->total()), "");
217 // if this fails, we somehow removed by FillOrderedProfile
218 // more than we have added.
220 buf[bytes_written] = '\0';
221 RAW_DCHECK(bytes_written == strlen(buf), "");
223 return buf;
226 extern "C" char* GetHeapProfile() {
227 // Use normal malloc: we return the profile to the user to free it:
228 char* buffer = reinterpret_cast<char*>(malloc(kProfileBufferSize));
229 SpinLockHolder l(&heap_lock);
230 return DoGetHeapProfileLocked(buffer, kProfileBufferSize);
233 // defined below
234 static void NewHook(const void* ptr, size_t size);
235 static void DeleteHook(const void* ptr);
237 // Helper for HeapProfilerDump.
238 static void DumpProfileLocked(const char* reason,
239 char* filename_buffer,
240 size_t filename_buffer_length) {
241 RAW_DCHECK(heap_lock.IsHeld(), "");
242 RAW_DCHECK(is_on, "");
243 RAW_DCHECK(!dumping, "");
245 if (filename_prefix == NULL) return; // we do not yet need dumping
247 dumping = true;
249 // Make file name
250 dump_count++;
251 snprintf(filename_buffer, filename_buffer_length, "%s.%05d.%04d%s",
252 filename_prefix, getpid(), dump_count, HeapProfileTable::kFileExt);
254 // Dump the profile
255 RAW_VLOG(0, "Dumping heap profile to %s (%s)", filename_buffer, reason);
256 // We must use file routines that don't access memory, since we hold
257 // a memory lock now.
258 RawFD fd = RawOpenForWriting(filename_buffer);
259 if (fd == kIllegalRawFD) {
260 RAW_LOG(ERROR, "Failed dumping heap profile to %s", filename_buffer);
261 dumping = false;
262 return;
265 // This case may be impossible, but it's best to be safe.
266 // It's safe to use the global buffer: we're protected by heap_lock.
267 if (global_profiler_buffer == NULL) {
268 global_profiler_buffer =
269 reinterpret_cast<char*>(ProfilerMalloc(kProfileBufferSize));
272 char* profile = DoGetHeapProfileLocked(global_profiler_buffer,
273 kProfileBufferSize);
274 RawWrite(fd, profile, strlen(profile));
275 RawClose(fd);
277 #if defined(TYPE_PROFILING)
278 if (FLAGS_heap_profile_type_statistics) {
279 snprintf(filename_buffer, filename_buffer_length, "%s.%05d.%04d.type",
280 filename_prefix, getpid(), dump_count);
281 RAW_VLOG(0, "Dumping type statistics to %s", filename_buffer);
282 heap_profile->DumpTypeStatistics(filename_buffer);
284 #endif // defined(TYPE_PROFILING)
286 dumping = false;
289 //----------------------------------------------------------------------
290 // Profile collection
291 //----------------------------------------------------------------------
293 // Dump a profile after either an allocation or deallocation, if
294 // the memory use has changed enough since the last dump.
295 static void MaybeDumpProfileLocked() {
296 if (!dumping) {
297 const HeapProfileTable::Stats& total = heap_profile->total();
298 const int64 inuse_bytes = total.alloc_size - total.free_size;
299 bool need_to_dump = false;
300 char buf[128];
301 int64 current_time = time(NULL);
302 if (FLAGS_heap_profile_allocation_interval > 0 &&
303 total.alloc_size >=
304 last_dump_alloc + FLAGS_heap_profile_allocation_interval) {
305 snprintf(buf, sizeof(buf), ("%"PRId64" MB allocated cumulatively, "
306 "%"PRId64" MB currently in use"),
307 total.alloc_size >> 20, inuse_bytes >> 20);
308 need_to_dump = true;
309 } else if (FLAGS_heap_profile_deallocation_interval > 0 &&
310 total.free_size >=
311 last_dump_free + FLAGS_heap_profile_deallocation_interval) {
312 snprintf(buf, sizeof(buf), ("%"PRId64" MB freed cumulatively, "
313 "%"PRId64" MB currently in use"),
314 total.free_size >> 20, inuse_bytes >> 20);
315 need_to_dump = true;
316 } else if (FLAGS_heap_profile_inuse_interval > 0 &&
317 inuse_bytes >
318 high_water_mark + FLAGS_heap_profile_inuse_interval) {
319 snprintf(buf, sizeof(buf), "%"PRId64" MB currently in use",
320 inuse_bytes >> 20);
321 need_to_dump = true;
322 } else if (FLAGS_heap_profile_time_interval > 0 &&
323 current_time - last_dump_time >=
324 FLAGS_heap_profile_time_interval) {
325 snprintf(buf, sizeof(buf), "%d sec since the last dump",
326 current_time - last_dump_time);
327 need_to_dump = true;
328 last_dump_time = current_time;
330 if (need_to_dump) {
331 char filename_buffer[1000];
332 DumpProfileLocked(buf, filename_buffer, sizeof(filename_buffer));
334 last_dump_alloc = total.alloc_size;
335 last_dump_free = total.free_size;
336 if (inuse_bytes > high_water_mark)
337 high_water_mark = inuse_bytes;
342 // Record an allocation in the profile.
343 static void RecordAlloc(const void* ptr, size_t bytes, int skip_count) {
344 // Take the stack trace outside the critical section.
345 void* stack[HeapProfileTable::kMaxStackDepth];
346 int depth = HeapProfileTable::GetCallerStackTrace(skip_count + 1, stack);
347 SpinLockHolder l(&heap_lock);
348 if (is_on) {
349 heap_profile->RecordAlloc(ptr, bytes, depth, stack);
350 MaybeDumpProfileLocked();
354 // Record a deallocation in the profile.
355 static void RecordFree(const void* ptr) {
356 SpinLockHolder l(&heap_lock);
357 if (is_on) {
358 heap_profile->RecordFree(ptr);
359 MaybeDumpProfileLocked();
363 //----------------------------------------------------------------------
364 // Allocation/deallocation hooks for MallocHook
365 //----------------------------------------------------------------------
367 // static
368 void NewHook(const void* ptr, size_t size) {
369 if (ptr != NULL) RecordAlloc(ptr, size, 0);
372 // static
373 void DeleteHook(const void* ptr) {
374 if (ptr != NULL) RecordFree(ptr);
377 // TODO(jandrews): Re-enable stack tracing
378 #ifdef TODO_REENABLE_STACK_TRACING
379 static void RawInfoStackDumper(const char* message, void*) {
380 RAW_LOG(INFO, "%.*s", static_cast<int>(strlen(message) - 1), message);
381 // -1 is to chop the \n which will be added by RAW_LOG
383 #endif
385 static void MmapHook(const void* result, const void* start, size_t size,
386 int prot, int flags, int fd, off_t offset) {
387 if (FLAGS_mmap_log) { // log it
388 // We use PRIxS not just '%p' to avoid deadlocks
389 // in pretty-printing of NULL as "nil".
390 // TODO(maxim): instead should use a safe snprintf reimplementation
391 RAW_LOG(INFO,
392 "mmap(start=0x%"PRIxPTR", len=%"PRIuS", prot=0x%x, flags=0x%x, "
393 "fd=%d, offset=0x%x) = 0x%"PRIxPTR"",
394 (uintptr_t) start, size, prot, flags, fd, (unsigned int) offset,
395 (uintptr_t) result);
396 #ifdef TODO_REENABLE_STACK_TRACING
397 DumpStackTrace(1, RawInfoStackDumper, NULL);
398 #endif
402 static void MremapHook(const void* result, const void* old_addr,
403 size_t old_size, size_t new_size,
404 int flags, const void* new_addr) {
405 if (FLAGS_mmap_log) { // log it
406 // We use PRIxS not just '%p' to avoid deadlocks
407 // in pretty-printing of NULL as "nil".
408 // TODO(maxim): instead should use a safe snprintf reimplementation
409 RAW_LOG(INFO,
410 "mremap(old_addr=0x%"PRIxPTR", old_size=%"PRIuS", "
411 "new_size=%"PRIuS", flags=0x%x, new_addr=0x%"PRIxPTR") = "
412 "0x%"PRIxPTR"",
413 (uintptr_t) old_addr, old_size, new_size, flags,
414 (uintptr_t) new_addr, (uintptr_t) result);
415 #ifdef TODO_REENABLE_STACK_TRACING
416 DumpStackTrace(1, RawInfoStackDumper, NULL);
417 #endif
421 static void MunmapHook(const void* ptr, size_t size) {
422 if (FLAGS_mmap_log) { // log it
423 // We use PRIxS not just '%p' to avoid deadlocks
424 // in pretty-printing of NULL as "nil".
425 // TODO(maxim): instead should use a safe snprintf reimplementation
426 RAW_LOG(INFO, "munmap(start=0x%"PRIxPTR", len=%"PRIuS")",
427 (uintptr_t) ptr, size);
428 #ifdef TODO_REENABLE_STACK_TRACING
429 DumpStackTrace(1, RawInfoStackDumper, NULL);
430 #endif
434 static void SbrkHook(const void* result, ptrdiff_t increment) {
435 if (FLAGS_mmap_log) { // log it
436 RAW_LOG(INFO, "sbrk(inc=%"PRIdS") = 0x%"PRIxPTR"",
437 increment, (uintptr_t) result);
438 #ifdef TODO_REENABLE_STACK_TRACING
439 DumpStackTrace(1, RawInfoStackDumper, NULL);
440 #endif
444 //----------------------------------------------------------------------
445 // Starting/stopping/dumping
446 //----------------------------------------------------------------------
448 extern "C" void HeapProfilerStart(const char* prefix) {
449 SpinLockHolder l(&heap_lock);
451 if (is_on) return;
453 is_on = true;
455 RAW_VLOG(0, "Starting tracking the heap");
457 // This should be done before the hooks are set up, since it should
458 // call new, and we want that to be accounted for correctly.
459 MallocExtension::Initialize();
461 if (FLAGS_only_mmap_profile) {
462 FLAGS_mmap_profile = true;
465 if (FLAGS_mmap_profile) {
466 // Ask MemoryRegionMap to record all mmap, mremap, and sbrk
467 // call stack traces of at least size kMaxStackDepth:
468 MemoryRegionMap::Init(HeapProfileTable::kMaxStackDepth,
469 /* use_buckets */ true);
472 if (FLAGS_mmap_log) {
473 // Install our hooks to do the logging:
474 RAW_CHECK(MallocHook::AddMmapHook(&MmapHook), "");
475 RAW_CHECK(MallocHook::AddMremapHook(&MremapHook), "");
476 RAW_CHECK(MallocHook::AddMunmapHook(&MunmapHook), "");
477 RAW_CHECK(MallocHook::AddSbrkHook(&SbrkHook), "");
480 heap_profiler_memory =
481 LowLevelAlloc::NewArena(0, LowLevelAlloc::DefaultArena());
483 // Reserve space now for the heap profiler, so we can still write a
484 // heap profile even if the application runs out of memory.
485 global_profiler_buffer =
486 reinterpret_cast<char*>(ProfilerMalloc(kProfileBufferSize));
488 heap_profile = new(ProfilerMalloc(sizeof(HeapProfileTable)))
489 HeapProfileTable(ProfilerMalloc, ProfilerFree, FLAGS_mmap_profile);
491 last_dump_alloc = 0;
492 last_dump_free = 0;
493 high_water_mark = 0;
494 last_dump_time = 0;
496 if (FLAGS_deep_heap_profile) {
497 // Initialize deep memory profiler
498 RAW_VLOG(0, "[%d] Starting a deep memory profiler", getpid());
499 deep_profile = new(ProfilerMalloc(sizeof(DeepHeapProfile)))
500 DeepHeapProfile(heap_profile, prefix);
503 // We do not reset dump_count so if the user does a sequence of
504 // HeapProfilerStart/HeapProfileStop, we will get a continuous
505 // sequence of profiles.
507 if (FLAGS_only_mmap_profile == false) {
508 // Now set the hooks that capture new/delete and malloc/free.
509 RAW_CHECK(MallocHook::AddNewHook(&NewHook), "");
510 RAW_CHECK(MallocHook::AddDeleteHook(&DeleteHook), "");
513 // Copy filename prefix
514 RAW_DCHECK(filename_prefix == NULL, "");
515 const int prefix_length = strlen(prefix);
516 filename_prefix = reinterpret_cast<char*>(ProfilerMalloc(prefix_length + 1));
517 memcpy(filename_prefix, prefix, prefix_length);
518 filename_prefix[prefix_length] = '\0';
521 extern "C" void IterateAllocatedObjects(AddressVisitor visitor, void* data) {
522 SpinLockHolder l(&heap_lock);
524 if (!is_on) return;
526 heap_profile->IterateAllocationAddresses(visitor, data);
529 extern "C" int IsHeapProfilerRunning() {
530 SpinLockHolder l(&heap_lock);
531 return is_on ? 1 : 0; // return an int, because C code doesn't have bool
534 extern "C" void HeapProfilerStop() {
535 SpinLockHolder l(&heap_lock);
537 if (!is_on) return;
539 if (FLAGS_only_mmap_profile == false) {
540 // Unset our new/delete hooks, checking they were set:
541 RAW_CHECK(MallocHook::RemoveNewHook(&NewHook), "");
542 RAW_CHECK(MallocHook::RemoveDeleteHook(&DeleteHook), "");
544 if (FLAGS_mmap_log) {
545 // Restore mmap/sbrk hooks, checking that our hooks were set:
546 RAW_CHECK(MallocHook::RemoveMmapHook(&MmapHook), "");
547 RAW_CHECK(MallocHook::RemoveMremapHook(&MremapHook), "");
548 RAW_CHECK(MallocHook::RemoveSbrkHook(&SbrkHook), "");
549 RAW_CHECK(MallocHook::RemoveMunmapHook(&MunmapHook), "");
552 if (deep_profile) {
553 // free deep memory profiler
554 deep_profile->~DeepHeapProfile();
555 ProfilerFree(deep_profile);
556 deep_profile = NULL;
559 // free profile
560 heap_profile->~HeapProfileTable();
561 ProfilerFree(heap_profile);
562 heap_profile = NULL;
564 // free output-buffer memory
565 ProfilerFree(global_profiler_buffer);
567 // free prefix
568 ProfilerFree(filename_prefix);
569 filename_prefix = NULL;
571 if (!LowLevelAlloc::DeleteArena(heap_profiler_memory)) {
572 RAW_LOG(FATAL, "Memory leak in HeapProfiler:");
575 if (FLAGS_mmap_profile) {
576 MemoryRegionMap::Shutdown();
579 is_on = false;
582 extern "C" void HeapProfilerDump(const char* reason) {
583 SpinLockHolder l(&heap_lock);
584 if (is_on && !dumping) {
585 char filename_buffer[1000];
586 DumpProfileLocked(reason, filename_buffer, sizeof(filename_buffer));
590 extern "C" void HeapProfilerDumpWithFileName(const char* reason,
591 char* dumped_filename_buffer,
592 int filename_buffer_length) {
593 SpinLockHolder l(&heap_lock);
594 if (is_on && !dumping) {
595 DumpProfileLocked(reason, dumped_filename_buffer, filename_buffer_length);
599 extern "C" void HeapProfilerMarkBaseline() {
600 SpinLockHolder l(&heap_lock);
602 if (!is_on) return;
604 heap_profile->MarkCurrentAllocations(HeapProfileTable::MARK_ONE);
607 extern "C" void HeapProfilerMarkInteresting() {
608 SpinLockHolder l(&heap_lock);
610 if (!is_on) return;
612 heap_profile->MarkUnmarkedAllocations(HeapProfileTable::MARK_TWO);
615 extern "C" void HeapProfilerDumpAliveObjects(const char* filename) {
616 SpinLockHolder l(&heap_lock);
618 if (!is_on) return;
620 heap_profile->DumpMarkedObjects(HeapProfileTable::MARK_TWO, filename);
623 //----------------------------------------------------------------------
624 // Initialization/finalization code
625 //----------------------------------------------------------------------
627 // Initialization code
628 static void HeapProfilerInit() {
629 // Everything after this point is for setting up the profiler based on envvar
630 char fname[PATH_MAX];
631 if (!GetUniquePathFromEnv("HEAPPROFILE", fname)) {
632 return;
634 // We do a uid check so we don't write out files in a setuid executable.
635 #ifdef HAVE_GETEUID
636 if (getuid() != geteuid()) {
637 RAW_LOG(WARNING, ("HeapProfiler: ignoring HEAPPROFILE because "
638 "program seems to be setuid\n"));
639 return;
641 #endif
643 HeapProfileTable::CleanupOldProfiles(fname);
645 HeapProfilerStart(fname);
648 // class used for finalization -- dumps the heap-profile at program exit
649 struct HeapProfileEndWriter {
650 ~HeapProfileEndWriter() { HeapProfilerDump("Exiting"); }
653 // We want to make sure tcmalloc is up and running before starting the profiler
654 static const TCMallocGuard tcmalloc_initializer;
655 REGISTER_MODULE_INITIALIZER(heapprofiler, HeapProfilerInit());
656 static HeapProfileEndWriter heap_profile_end_writer;