- add sources.
[platform/framework/web/crosswalk.git] / src / third_party / tcmalloc / chromium / src / tests / sampling_test.cc
1 // Copyright (c) 2008, 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 // This tests ReadStackTraces and ReadGrowthStackTraces.  It does this
34 // by doing a bunch of allocations and then calling those functions.
35 // A driver shell-script can call this, and then call pprof, and
36 // verify the expected output.  The output is written to
37 // argv[1].heap and argv[1].growth
38
39 #include "config_for_unittests.h"
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string>
43 #include "base/logging.h"
44 #include <gperftools/malloc_extension.h>
45
46 using std::string;
47
48 extern "C" void* AllocateAllocate() ATTRIBUTE_NOINLINE;
49
50 extern "C" void* AllocateAllocate() {
51   // The VLOG's are mostly to discourage inlining
52   VLOG(1, "Allocating some more");
53   void* p = malloc(10000);
54   VLOG(1, "Done allocating");
55   return p;
56 }
57
58 static void WriteStringToFile(const string& s, const string& filename) {
59   FILE* fp = fopen(filename.c_str(), "w");
60   fwrite(s.data(), 1, s.length(), fp);
61   fclose(fp);
62 }
63
64 int main(int argc, char** argv) {
65   if (argc < 2) {
66     fprintf(stderr, "USAGE: %s <base of output files>\n", argv[0]);
67     exit(1);
68   }
69   for (int i = 0; i < 8000; i++) {
70     AllocateAllocate();
71   }
72
73   string s;
74   MallocExtension::instance()->GetHeapSample(&s);
75   WriteStringToFile(s, string(argv[1]) + ".heap");
76
77   s.clear();
78   MallocExtension::instance()->GetHeapGrowthStacks(&s);
79   WriteStringToFile(s, string(argv[1]) + ".growth");
80
81   return 0;
82 }