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. 29 30 // --- 31 // Author: Craig Silverstein 32 // 33 // A small program that just exercises our heap profiler by allocating 34 // memory and letting the heap-profiler emit a profile. We don't test 35 // threads (TODO). By itself, this unittest tests that the heap-profiler 36 // doesn't crash on simple programs, but its output can be analyzed by 37 // another testing script to actually verify correctness. See, eg, 38 // heap-profiler_unittest.sh. 39 40 #include "config_for_unittests.h" 41 #include <stdlib.h> 42 #include <stdio.h> 43 #include <fcntl.h> // for mkdir() 44 #include <sys/stat.h> // for mkdir() on freebsd and os x 45 #ifdef HAVE_UNISTD_H 46 #include <unistd.h> // for fork() 47 #endif 48 #include <sys/wait.h> // for wait() 49 #include <string> 50 #include "base/basictypes.h" 51 #include "base/logging.h" 52 #include <gperftools/heap-profiler.h> 53 54 using std::string; 55 56 static const int kMaxCount = 100000; 57 int* g_array[kMaxCount]; // an array of int-vectors 58 59 static ATTRIBUTE_NOINLINE void Allocate(int start, int end, int size) { 60 for (int i = start; i < end; ++i) { 61 if (i < kMaxCount) 62 g_array[i] = new int[size]; 63 } 64 } 65 66 static ATTRIBUTE_NOINLINE void Allocate2(int start, int end, int size) { 67 for (int i = start; i < end; ++i) { 68 if (i < kMaxCount) 69 g_array[i] = new int[size]; 70 } 71 } 72 73 static void Deallocate(int start, int end) { 74 for (int i = start; i < end; ++i) { 75 delete[] g_array[i]; 76 g_array[i] = 0; 77 } 78 } 79 80 static void TestHeapProfilerStartStopIsRunning() { 81 // If you run this with whole-program heap-profiling on, than 82 // IsHeapProfilerRunning should return true. 83 if (!IsHeapProfilerRunning()) { 84 const char* tmpdir = getenv("TMPDIR"); 85 if (tmpdir == NULL) 86 tmpdir = "/tmp"; 87 mkdir(tmpdir, 0755); // if necessary 88 HeapProfilerStart((string(tmpdir) + "/start_stop").c_str()); 89 CHECK(IsHeapProfilerRunning()); 90 91 Allocate(0, 40, 100); 92 Deallocate(0, 40); 93 94 HeapProfilerStop(); 95 CHECK(!IsHeapProfilerRunning()); 96 } 97 } 98 99 static void TestDumpHeapProfiler() { 100 // If you run this with whole-program heap-profiling on, than 101 // IsHeapProfilerRunning should return true. 102 if (!IsHeapProfilerRunning()) { 103 const char* tmpdir = getenv("TMPDIR"); 104 if (tmpdir == NULL) 105 tmpdir = "/tmp"; 106 mkdir(tmpdir, 0755); // if necessary 107 HeapProfilerStart((string(tmpdir) + "/dump").c_str()); 108 CHECK(IsHeapProfilerRunning()); 109 110 Allocate(0, 40, 100); 111 Deallocate(0, 40); 112 113 char* output = GetHeapProfile(); 114 free(output); 115 HeapProfilerStop(); 116 } 117 } 118 119 120 int main(int argc, char** argv) { 121 if (argc > 2 || (argc == 2 && argv[1][0] == '-')) { 122 printf("USAGE: %s [number of children to fork]\n", argv[0]); 123 exit(0); 124 } 125 int num_forks = 0; 126 if (argc == 2) { 127 num_forks = atoi(argv[1]); 128 } 129 130 TestHeapProfilerStartStopIsRunning(); 131 TestDumpHeapProfiler(); 132 133 Allocate(0, 40, 100); 134 Deallocate(0, 40); 135 136 Allocate(0, 40, 100); 137 Allocate(0, 40, 100); 138 Allocate2(40, 400, 1000); 139 Allocate2(400, 1000, 10000); 140 Deallocate(0, 1000); 141 142 Allocate(0, 100, 100000); 143 Deallocate(0, 10); 144 Deallocate(10, 20); 145 Deallocate(90, 100); 146 Deallocate(20, 90); 147 148 while (num_forks-- > 0) { 149 switch (fork()) { 150 case -1: 151 printf("FORK failed!\n"); 152 return 1; 153 case 0: // child 154 return execl(argv[0], argv[0], NULL); // run child with no args 155 default: 156 wait(NULL); // we'll let the kids run one at a time 157 } 158 } 159 160 printf("DONE.\n"); 161 162 return 0; 163 } 164