summaryrefslogtreecommitdiffstats
path: root/content/renderer/memory_benchmarking_extension.cc
blob: 20e3e166c448e39938f0974e40d1417cca484bbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/renderer/memory_benchmarking_extension.h"

#include "base/string_util.h"
#include "content/public/renderer/render_thread.h"

#if !defined(NO_TCMALLOC) && defined(OS_LINUX)
#include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h"
#endif  // !defined(NO_TCMALLOC) && defined(OS_LINUX)

namespace {

const char kMemoryBenchmarkingExtensionName[] = "v8/MemoryBenchmarking";

}

namespace {

class MemoryBenchmarkingWrapper : public v8::Extension {
 public:
  MemoryBenchmarkingWrapper() :
      v8::Extension(kMemoryBenchmarkingExtensionName,
        "if (typeof(chrome) == 'undefined') {"
        "  chrome = {};"
        "};"
        "if (typeof(chrome.memoryBenchmarking) == 'undefined') {"
        "  chrome.memoryBenchmarking = {};"
        "};"
        "chrome.memoryBenchmarking.isHeapProfilerRunning = function() {"
        "  native function IsHeapProfilerRunning();"
        "  return IsHeapProfilerRunning();"
        "};"
        "chrome.memoryBenchmarking.heapProfilerDump = function(reason) {"
        "  native function HeapProfilerDump();"
        "  return HeapProfilerDump(reason);"
        "};"
        ) {}

  virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction(
      v8::Handle<v8::String> name) {
    if (name->Equals(v8::String::New("IsHeapProfilerRunning")))
      return v8::FunctionTemplate::New(IsHeapProfilerRunning);
    else if (name->Equals(v8::String::New("HeapProfilerDump")))
      return v8::FunctionTemplate::New(HeapProfilerDump);

    return v8::Handle<v8::FunctionTemplate>();
  }

  static v8::Handle<v8::Value> IsHeapProfilerRunning(
      const v8::Arguments& args) {
#if !defined(NO_TCMALLOC) && defined(OS_LINUX)
    return v8::Boolean::New(::IsHeapProfilerRunning());
#else
    return v8::Boolean::New(false);
#endif  // !defined(NO_TCMALLOC) && defined(OS_LINUX)
  }

  static v8::Handle<v8::Value> HeapProfilerDump(const v8::Arguments& args) {
#if !defined(NO_TCMALLOC) && defined(OS_LINUX)
    char dumped_filename_buffer[1000];
    std::string reason("benchmarking_extension");
    if (args.Length() && args[0]->IsString())
      reason = *v8::String::AsciiValue(args[0]);
    ::HeapProfilerDumpWithFileName(reason.c_str(),
                                   dumped_filename_buffer,
                                   sizeof(dumped_filename_buffer));
    return v8::String::New(dumped_filename_buffer);
#endif  // !defined(NO_TCMALLOC) && defined(OS_LINUX)
    return v8::Undefined();
  }
};

}  // namespace

namespace content {

v8::Extension* MemoryBenchmarkingExtension::Get() {
  return new MemoryBenchmarkingWrapper();
}

}  // namespace content