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. 4 5 #include "content/renderer/memory_benchmarking_extension.h" 6 7 #include "base/strings/string_util.h" 8 #include "content/common/memory_benchmark_messages.h" 9 #include "content/renderer/render_thread_impl.h" 10 11 #if defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID)) 12 13 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h" 14 15 namespace { 16 17 const char kMemoryBenchmarkingExtensionName[] = "v8/MemoryBenchmarking"; 18 19 } 20 21 namespace { 22 23 class MemoryBenchmarkingWrapper : public v8::Extension { 24 public: MemoryBenchmarkingWrapper()25 MemoryBenchmarkingWrapper() : 26 v8::Extension(kMemoryBenchmarkingExtensionName, 27 "if (typeof(chrome) == 'undefined') {" 28 " chrome = {};" 29 "};" 30 "if (typeof(chrome.memoryBenchmarking) == 'undefined') {" 31 " chrome.memoryBenchmarking = {};" 32 "};" 33 "chrome.memoryBenchmarking.isHeapProfilerRunning = function() {" 34 " native function IsHeapProfilerRunning();" 35 " return IsHeapProfilerRunning();" 36 "};" 37 "chrome.memoryBenchmarking.heapProfilerDump = " 38 " function(process_type, reason) {" 39 " native function HeapProfilerDump();" 40 " HeapProfilerDump(process_type, reason);" 41 "};" 42 ) {} 43 GetNativeFunctionTemplate(v8::Isolate * isolate,v8::Handle<v8::String> name)44 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunctionTemplate( 45 v8::Isolate* isolate, 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); 51 52 return v8::Handle<v8::FunctionTemplate>(); 53 } 54 IsHeapProfilerRunning(const v8::FunctionCallbackInfo<v8::Value> & args)55 static void IsHeapProfilerRunning( 56 const v8::FunctionCallbackInfo<v8::Value>& args) { 57 args.GetReturnValue().Set(::IsHeapProfilerRunning()); 58 } 59 HeapProfilerDump(const v8::FunctionCallbackInfo<v8::Value> & args)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)); 71 } else { 72 ::HeapProfilerDump(reason.c_str()); 73 } 74 } 75 }; 76 77 } // namespace 78 79 namespace content { 80 Get()81v8::Extension* MemoryBenchmarkingExtension::Get() { 82 return new MemoryBenchmarkingWrapper(); 83 } 84 85 } // namespace content 86 87 #endif // defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID)) 88