1 // Copyright (c) 2005, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Author: Craig Silverstein
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.
40 #include "config_for_unittests.h"
43 #include <fcntl.h> // for mkdir()
44 #include <sys/stat.h> // for mkdir() on freebsd and os x
46 #include <unistd.h> // for fork()
48 #include <sys/wait.h> // for wait()
50 #include "base/basictypes.h"
51 #include "base/logging.h"
52 #include <gperftools/heap-profiler.h>
56 static const int kMaxCount
= 100000;
57 int* g_array
[kMaxCount
]; // an array of int-vectors
59 static ATTRIBUTE_NOINLINE
void Allocate(int start
, int end
, int size
) {
60 for (int i
= start
; i
< end
; ++i
) {
62 g_array
[i
] = new int[size
];
66 static ATTRIBUTE_NOINLINE
void Allocate2(int start
, int end
, int size
) {
67 for (int i
= start
; i
< end
; ++i
) {
69 g_array
[i
] = new int[size
];
73 static void Deallocate(int start
, int end
) {
74 for (int i
= start
; i
< end
; ++i
) {
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");
87 mkdir(tmpdir
, 0755); // if necessary
88 HeapProfilerStart((string(tmpdir
) + "/start_stop").c_str());
89 CHECK(IsHeapProfilerRunning());
95 CHECK(!IsHeapProfilerRunning());
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");
106 mkdir(tmpdir
, 0755); // if necessary
107 HeapProfilerStart((string(tmpdir
) + "/dump").c_str());
108 CHECK(IsHeapProfilerRunning());
110 Allocate(0, 40, 100);
113 char* output
= GetHeapProfile();
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]);
127 num_forks
= atoi(argv
[1]);
130 TestHeapProfilerStartStopIsRunning();
131 TestDumpHeapProfiler();
133 Allocate(0, 40, 100);
136 Allocate(0, 40, 100);
137 Allocate(0, 40, 100);
138 Allocate2(40, 400, 1000);
139 Allocate2(400, 1000, 10000);
142 Allocate(0, 100, 100000);
148 while (num_forks
-- > 0) {
151 printf("FORK failed!\n");
154 return execl(argv
[0], argv
[0], NULL
); // run child with no args
156 wait(NULL
); // we'll let the kids run one at a time