This source file includes following definitions.
- Allocate
- Allocate2
- Deallocate
- TestHeapProfilerStartStopIsRunning
- TestDumpHeapProfiler
- main
#include "config_for_unittests.h"
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/wait.h>
#include <string>
#include "base/basictypes.h"
#include "base/logging.h"
#include <gperftools/heap-profiler.h>
using std::string;
static const int kMaxCount = 100000;
int* g_array[kMaxCount];
static ATTRIBUTE_NOINLINE void Allocate(int start, int end, int size) {
for (int i = start; i < end; ++i) {
if (i < kMaxCount)
g_array[i] = new int[size];
}
}
static ATTRIBUTE_NOINLINE void Allocate2(int start, int end, int size) {
for (int i = start; i < end; ++i) {
if (i < kMaxCount)
g_array[i] = new int[size];
}
}
static void Deallocate(int start, int end) {
for (int i = start; i < end; ++i) {
delete[] g_array[i];
g_array[i] = 0;
}
}
static void TestHeapProfilerStartStopIsRunning() {
if (!IsHeapProfilerRunning()) {
const char* tmpdir = getenv("TMPDIR");
if (tmpdir == NULL)
tmpdir = "/tmp";
mkdir(tmpdir, 0755);
HeapProfilerStart((string(tmpdir) + "/start_stop").c_str());
CHECK(IsHeapProfilerRunning());
Allocate(0, 40, 100);
Deallocate(0, 40);
HeapProfilerStop();
CHECK(!IsHeapProfilerRunning());
}
}
static void TestDumpHeapProfiler() {
if (!IsHeapProfilerRunning()) {
const char* tmpdir = getenv("TMPDIR");
if (tmpdir == NULL)
tmpdir = "/tmp";
mkdir(tmpdir, 0755);
HeapProfilerStart((string(tmpdir) + "/dump").c_str());
CHECK(IsHeapProfilerRunning());
Allocate(0, 40, 100);
Deallocate(0, 40);
char* output = GetHeapProfile();
free(output);
HeapProfilerStop();
}
}
int main(int argc, char** argv) {
if (argc > 2 || (argc == 2 && argv[1][0] == '-')) {
printf("USAGE: %s [number of children to fork]\n", argv[0]);
exit(0);
}
int num_forks = 0;
if (argc == 2) {
num_forks = atoi(argv[1]);
}
TestHeapProfilerStartStopIsRunning();
TestDumpHeapProfiler();
Allocate(0, 40, 100);
Deallocate(0, 40);
Allocate(0, 40, 100);
Allocate(0, 40, 100);
Allocate2(40, 400, 1000);
Allocate2(400, 1000, 10000);
Deallocate(0, 1000);
Allocate(0, 100, 100000);
Deallocate(0, 10);
Deallocate(10, 20);
Deallocate(90, 100);
Deallocate(20, 90);
while (num_forks-- > 0) {
switch (fork()) {
case -1:
printf("FORK failed!\n");
return 1;
case 0:
return execl(argv[0], argv[0], NULL);
default:
wait(NULL);
}
}
printf("DONE.\n");
return 0;
}