Chromium Code Reviews| 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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 74 | 74 |
| 75 | 75 |
| 76 #ifndef PATH_MAX | 76 #ifndef PATH_MAX |
| 77 #ifdef MAXPATHLEN | 77 #ifdef MAXPATHLEN |
| 78 #define PATH_MAX MAXPATHLEN | 78 #define PATH_MAX MAXPATHLEN |
| 79 #else | 79 #else |
| 80 #define PATH_MAX 4096 // seems conservative for max filename len! | 80 #define PATH_MAX 4096 // seems conservative for max filename len! |
| 81 #endif | 81 #endif |
| 82 #endif | 82 #endif |
| 83 | 83 |
| 84 #if defined(__ANDROID__) || defined(ANDROID) | |
|
bulach
2013/05/01 17:55:30
nit: perhaps add a short explanation here?
// On
Dai Mikurube (NOT FULLTIME)
2013/05/02 16:26:40
Done.
| |
| 85 #define HEAPPROFILE "heapprof" | |
| 86 #define HEAP_PROFILE_ALLOCATION_INTERVAL "heapprof.allocation_interval" | |
| 87 #define HEAP_PROFILE_DEALLOCATION_INTERVAL "heapprof.deallocation_interval" | |
| 88 #define HEAP_PROFILE_INUSE_INTERVAL "heapprof.inuse_interval" | |
| 89 #define HEAP_PROFILE_TIME_INTERVAL "heapprof.time_interval" | |
| 90 #define HEAP_PROFILE_MMAP_LOG "heapprof.mmap_log" | |
| 91 #define HEAP_PROFILE_MMAP "heapprof.mmap" | |
| 92 #define HEAP_PROFILE_ONLY_MMAP "heapprof.only_mmap" | |
| 93 #define DEEP_HEAP_PROFILE "heapprof.deep_heap_profile" | |
| 94 #define HEAP_PROFILE_TYPE_STATISTICS "heapprof.type_statistics" | |
| 95 #else // defined(__ANDROID__) || defined(ANDROID) | |
| 96 #define HEAPPROFILE "HEAPPROFILE" | |
| 97 #define HEAP_PROFILE_ALLOCATION_INTERVAL "HEAP_PROFILE_ALLOCATION_INTERVAL" | |
| 98 #define HEAP_PROFILE_DEALLOCATION_INTERVAL "HEAP_PROFILE_DEALLOCATION_INTERVAL" | |
| 99 #define HEAP_PROFILE_INUSE_INTERVAL "HEAP_PROFILE_INUSE_INTERVAL" | |
| 100 #define HEAP_PROFILE_TIME_INTERVAL "HEAP_PROFILE_TIME_INTERVAL" | |
| 101 #define HEAP_PROFILE_MMAP_LOG "HEAP_PROFILE_MMAP_LOG" | |
| 102 #define HEAP_PROFILE_MMAP "HEAP_PROFILE_MMAP" | |
| 103 #define HEAP_PROFILE_ONLY_MMAP "HEAP_PROFILE_ONLY_MMAP" | |
| 104 #define DEEP_HEAP_PROFILE "DEEP_HEAP_PROFILE" | |
| 105 #define HEAP_PROFILE_TYPE_STATISTICS "HEAP_PROFILE_TYPE_STATISTICS" | |
| 106 #endif // defined(__ANDROID__) || defined(ANDROID) | |
| 107 | |
| 84 using STL_NAMESPACE::string; | 108 using STL_NAMESPACE::string; |
| 85 using STL_NAMESPACE::sort; | 109 using STL_NAMESPACE::sort; |
| 86 | 110 |
| 87 //---------------------------------------------------------------------- | 111 //---------------------------------------------------------------------- |
| 88 // Flags that control heap-profiling | 112 // Flags that control heap-profiling |
| 89 // | 113 // |
| 90 // The thread-safety of the profiler depends on these being immutable | 114 // The thread-safety of the profiler depends on these being immutable |
| 91 // after main starts, so don't change them. | 115 // after main starts, so don't change them. |
| 92 //---------------------------------------------------------------------- | 116 //---------------------------------------------------------------------- |
| 93 | 117 |
| 94 DEFINE_int64(heap_profile_allocation_interval, | 118 DEFINE_int64(heap_profile_allocation_interval, |
| 95 EnvToInt64("HEAP_PROFILE_ALLOCATION_INTERVAL", 1 << 30 /*1GB*/), | 119 EnvToInt64(HEAP_PROFILE_ALLOCATION_INTERVAL, 1 << 30 /*1GB*/), |
| 96 "If non-zero, dump heap profiling information once every " | 120 "If non-zero, dump heap profiling information once every " |
| 97 "specified number of bytes allocated by the program since " | 121 "specified number of bytes allocated by the program since " |
| 98 "the last dump."); | 122 "the last dump."); |
| 99 DEFINE_int64(heap_profile_deallocation_interval, | 123 DEFINE_int64(heap_profile_deallocation_interval, |
| 100 EnvToInt64("HEAP_PROFILE_DEALLOCATION_INTERVAL", 0), | 124 EnvToInt64(HEAP_PROFILE_DEALLOCATION_INTERVAL, 0), |
| 101 "If non-zero, dump heap profiling information once every " | 125 "If non-zero, dump heap profiling information once every " |
| 102 "specified number of bytes deallocated by the program " | 126 "specified number of bytes deallocated by the program " |
| 103 "since the last dump."); | 127 "since the last dump."); |
| 104 // We could also add flags that report whenever inuse_bytes changes by | 128 // 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. | 129 // X or -X, but there hasn't been a need for that yet, so we haven't. |
| 106 DEFINE_int64(heap_profile_inuse_interval, | 130 DEFINE_int64(heap_profile_inuse_interval, |
| 107 EnvToInt64("HEAP_PROFILE_INUSE_INTERVAL", 100 << 20 /*100MB*/), | 131 EnvToInt64(HEAP_PROFILE_INUSE_INTERVAL, 100 << 20 /*100MB*/), |
| 108 "If non-zero, dump heap profiling information whenever " | 132 "If non-zero, dump heap profiling information whenever " |
| 109 "the high-water memory usage mark increases by the specified " | 133 "the high-water memory usage mark increases by the specified " |
| 110 "number of bytes."); | 134 "number of bytes."); |
| 111 DEFINE_int64(heap_profile_time_interval, | 135 DEFINE_int64(heap_profile_time_interval, |
| 112 EnvToInt64("HEAP_PROFILE_TIME_INTERVAL", 0), | 136 EnvToInt64(HEAP_PROFILE_TIME_INTERVAL, 0), |
| 113 "If non-zero, dump heap profiling information once every " | 137 "If non-zero, dump heap profiling information once every " |
| 114 "specified number of seconds since the last dump."); | 138 "specified number of seconds since the last dump."); |
| 115 DEFINE_bool(mmap_log, | 139 DEFINE_bool(mmap_log, |
| 116 EnvToBool("HEAP_PROFILE_MMAP_LOG", false), | 140 EnvToBool(HEAP_PROFILE_MMAP_LOG, false), |
| 117 "Should mmap/munmap calls be logged?"); | 141 "Should mmap/munmap calls be logged?"); |
| 118 DEFINE_bool(mmap_profile, | 142 DEFINE_bool(mmap_profile, |
| 119 EnvToBool("HEAP_PROFILE_MMAP", false), | 143 EnvToBool(HEAP_PROFILE_MMAP, false), |
| 120 "If heap-profiling is on, also profile mmap, mremap, and sbrk)"); | 144 "If heap-profiling is on, also profile mmap, mremap, and sbrk)"); |
| 121 DEFINE_bool(only_mmap_profile, | 145 DEFINE_bool(only_mmap_profile, |
| 122 EnvToBool("HEAP_PROFILE_ONLY_MMAP", false), | 146 EnvToBool(HEAP_PROFILE_ONLY_MMAP, false), |
| 123 "If heap-profiling is on, only profile mmap, mremap, and sbrk; " | 147 "If heap-profiling is on, only profile mmap, mremap, and sbrk; " |
| 124 "do not profile malloc/new/etc"); | 148 "do not profile malloc/new/etc"); |
| 125 DEFINE_bool(deep_heap_profile, | 149 DEFINE_bool(deep_heap_profile, |
| 126 EnvToBool("DEEP_HEAP_PROFILE", false), | 150 EnvToBool(DEEP_HEAP_PROFILE, false), |
| 127 "If heap-profiling is on, profile deeper (only on Linux)"); | 151 "If heap-profiling is on, profile deeper (only on Linux)"); |
| 128 #if defined(TYPE_PROFILING) | 152 #if defined(TYPE_PROFILING) |
| 129 DEFINE_bool(heap_profile_type_statistics, | 153 DEFINE_bool(heap_profile_type_statistics, |
| 130 EnvToBool("HEAP_PROFILE_TYPE_STATISTICS", false), | 154 EnvToBool(HEAP_PROFILE_TYPE_STATISTICS, false), |
| 131 "If heap-profiling is on, dump type statistics."); | 155 "If heap-profiling is on, dump type statistics."); |
| 132 #endif // defined(TYPE_PROFILING) | 156 #endif // defined(TYPE_PROFILING) |
| 133 | 157 |
| 134 | 158 |
| 135 //---------------------------------------------------------------------- | 159 //---------------------------------------------------------------------- |
| 136 // Locking | 160 // Locking |
| 137 //---------------------------------------------------------------------- | 161 //---------------------------------------------------------------------- |
| 138 | 162 |
| 139 // A pthread_mutex has way too much lock contention to be used here. | 163 // A pthread_mutex has way too much lock contention to be used here. |
| 140 // | 164 // |
| (...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 635 #endif // defined(OS_ANDROID) | 659 #endif // defined(OS_ANDROID) |
| 636 | 660 |
| 637 //---------------------------------------------------------------------- | 661 //---------------------------------------------------------------------- |
| 638 // Initialization/finalization code | 662 // Initialization/finalization code |
| 639 //---------------------------------------------------------------------- | 663 //---------------------------------------------------------------------- |
| 640 | 664 |
| 641 // Initialization code | 665 // Initialization code |
| 642 static void HeapProfilerInit() { | 666 static void HeapProfilerInit() { |
| 643 // Everything after this point is for setting up the profiler based on envvar | 667 // Everything after this point is for setting up the profiler based on envvar |
| 644 char fname[PATH_MAX]; | 668 char fname[PATH_MAX]; |
| 645 if (!GetUniquePathFromEnv("HEAPPROFILE", fname)) { | 669 if (!GetUniquePathFromEnv(HEAPPROFILE, fname)) { |
| 646 return; | 670 return; |
| 647 } | 671 } |
| 648 // We do a uid check so we don't write out files in a setuid executable. | 672 // We do a uid check so we don't write out files in a setuid executable. |
| 649 #ifdef HAVE_GETEUID | 673 #ifdef HAVE_GETEUID |
| 650 if (getuid() != geteuid()) { | 674 if (getuid() != geteuid()) { |
| 651 RAW_LOG(WARNING, ("HeapProfiler: ignoring HEAPPROFILE because " | 675 RAW_LOG(WARNING, ("HeapProfiler: ignoring " HEAPPROFILE " because " |
| 652 "program seems to be setuid\n")); | 676 "program seems to be setuid\n")); |
| 653 return; | 677 return; |
| 654 } | 678 } |
| 655 #endif | 679 #endif |
| 656 | 680 |
| 657 HeapProfileTable::CleanupOldProfiles(fname); | 681 HeapProfileTable::CleanupOldProfiles(fname); |
| 658 | 682 |
| 659 HeapProfilerStart(fname); | 683 HeapProfilerStart(fname); |
| 660 } | 684 } |
| 661 | 685 |
| 662 // class used for finalization -- dumps the heap-profile at program exit | 686 // class used for finalization -- dumps the heap-profile at program exit |
| 663 struct HeapProfileEndWriter { | 687 struct HeapProfileEndWriter { |
| 664 ~HeapProfileEndWriter() { HeapProfilerDump("Exiting"); } | 688 ~HeapProfileEndWriter() { HeapProfilerDump("Exiting"); } |
| 665 }; | 689 }; |
| 666 | 690 |
| 667 // We want to make sure tcmalloc is up and running before starting the profiler | 691 // We want to make sure tcmalloc is up and running before starting the profiler |
| 668 static const TCMallocGuard tcmalloc_initializer; | 692 static const TCMallocGuard tcmalloc_initializer; |
| 669 REGISTER_MODULE_INITIALIZER(heapprofiler, HeapProfilerInit()); | 693 REGISTER_MODULE_INITIALIZER(heapprofiler, HeapProfilerInit()); |
| 670 static HeapProfileEndWriter heap_profile_end_writer; | 694 static HeapProfileEndWriter heap_profile_end_writer; |
| OLD | NEW |