1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/renderer/memory_benchmarking_extension.h"
7 #include "base/strings/string_util.h"
8 #include "content/common/memory_benchmark_messages.h"
9 #include "content/renderer/render_thread_impl.h"
11 #if defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID))
13 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h"
17 const char kMemoryBenchmarkingExtensionName
[] = "v8/MemoryBenchmarking";
23 class MemoryBenchmarkingWrapper
: public v8::Extension
{
25 MemoryBenchmarkingWrapper() :
26 v8::Extension(kMemoryBenchmarkingExtensionName
,
27 "if (typeof(chrome) == 'undefined') {"
30 "if (typeof(chrome.memoryBenchmarking) == 'undefined') {"
31 " chrome.memoryBenchmarking = {};"
33 "chrome.memoryBenchmarking.isHeapProfilerRunning = function() {"
34 " native function IsHeapProfilerRunning();"
35 " return IsHeapProfilerRunning();"
37 "chrome.memoryBenchmarking.heapProfilerDump = "
38 " function(process_type, reason) {"
39 " native function HeapProfilerDump();"
40 " HeapProfilerDump(process_type, reason);"
44 virtual v8::Handle
<v8::FunctionTemplate
> GetNativeFunctionTemplate(
46 v8::Handle
<v8::String
> name
) OVERRIDE
{
47 if (name
->Equals(v8::String::NewFromUtf8(isolate
, "IsHeapProfilerRunning")))
48 return v8::FunctionTemplate::New(isolate
, IsHeapProfilerRunning
);
49 else if (name
->Equals(v8::String::NewFromUtf8(isolate
, "HeapProfilerDump")))
50 return v8::FunctionTemplate::New(isolate
, HeapProfilerDump
);
52 return v8::Handle
<v8::FunctionTemplate
>();
55 static void IsHeapProfilerRunning(
56 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
57 args
.GetReturnValue().Set(::IsHeapProfilerRunning());
60 static void HeapProfilerDump(
61 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
62 std::string process_type
;
63 if (args
.Length() && args
[0]->IsString())
64 process_type
= *v8::String::Utf8Value(args
[0]);
65 std::string
reason("benchmarking_extension");
66 if (args
.Length() > 1 && args
[1]->IsString())
67 reason
= *v8::String::Utf8Value(args
[1]);
68 if (process_type
== "browser") {
69 content::RenderThreadImpl::current()->Send(
70 new MemoryBenchmarkHostMsg_HeapProfilerDump(reason
));
72 ::HeapProfilerDump(reason
.c_str());
81 v8::Extension
* MemoryBenchmarkingExtension::Get() {
82 return new MemoryBenchmarkingWrapper();
85 } // namespace content
87 #endif // defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID))