| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/memory_benchmarking_extension.h" | 5 #include "content/renderer/memory_benchmarking_extension.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "content/public/renderer/render_thread.h" | 8 #include "content/common/memory_benchmark_messages.h" |
| 9 #include "content/renderer/render_thread_impl.h" |
| 9 | 10 |
| 10 #if !defined(NO_TCMALLOC) && defined(OS_LINUX) | 11 #if defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID)) |
| 12 |
| 11 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h" | 13 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h" |
| 12 #endif // !defined(NO_TCMALLOC) && defined(OS_LINUX) | |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 const char kMemoryBenchmarkingExtensionName[] = "v8/MemoryBenchmarking"; | 17 const char kMemoryBenchmarkingExtensionName[] = "v8/MemoryBenchmarking"; |
| 17 | 18 |
| 18 } | 19 } |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 class MemoryBenchmarkingWrapper : public v8::Extension { | 23 class MemoryBenchmarkingWrapper : public v8::Extension { |
| 23 public: | 24 public: |
| 24 MemoryBenchmarkingWrapper() : | 25 MemoryBenchmarkingWrapper() : |
| 25 v8::Extension(kMemoryBenchmarkingExtensionName, | 26 v8::Extension(kMemoryBenchmarkingExtensionName, |
| 26 "if (typeof(chrome) == 'undefined') {" | 27 "if (typeof(chrome) == 'undefined') {" |
| 27 " chrome = {};" | 28 " chrome = {};" |
| 28 "};" | 29 "};" |
| 29 "if (typeof(chrome.memoryBenchmarking) == 'undefined') {" | 30 "if (typeof(chrome.memoryBenchmarking) == 'undefined') {" |
| 30 " chrome.memoryBenchmarking = {};" | 31 " chrome.memoryBenchmarking = {};" |
| 31 "};" | 32 "};" |
| 32 "chrome.memoryBenchmarking.isHeapProfilerRunning = function() {" | 33 "chrome.memoryBenchmarking.isHeapProfilerRunning = function() {" |
| 33 " native function IsHeapProfilerRunning();" | 34 " native function IsHeapProfilerRunning();" |
| 34 " return IsHeapProfilerRunning();" | 35 " return IsHeapProfilerRunning();" |
| 35 "};" | 36 "};" |
| 36 "chrome.memoryBenchmarking.heapProfilerDump = function(reason) {" | 37 "chrome.memoryBenchmarking.heapProfilerDump = " |
| 38 " function(process_type, reason) {" |
| 37 " native function HeapProfilerDump();" | 39 " native function HeapProfilerDump();" |
| 38 " HeapProfilerDump(reason);" | 40 " HeapProfilerDump(process_type, reason);" |
| 39 "};" | 41 "};" |
| 40 ) {} | 42 ) {} |
| 41 | 43 |
| 42 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( | 44 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
| 43 v8::Handle<v8::String> name) OVERRIDE { | 45 v8::Handle<v8::String> name) OVERRIDE { |
| 44 if (name->Equals(v8::String::New("IsHeapProfilerRunning"))) | 46 if (name->Equals(v8::String::New("IsHeapProfilerRunning"))) |
| 45 return v8::FunctionTemplate::New(IsHeapProfilerRunning); | 47 return v8::FunctionTemplate::New(IsHeapProfilerRunning); |
| 46 else if (name->Equals(v8::String::New("HeapProfilerDump"))) | 48 else if (name->Equals(v8::String::New("HeapProfilerDump"))) |
| 47 return v8::FunctionTemplate::New(HeapProfilerDump); | 49 return v8::FunctionTemplate::New(HeapProfilerDump); |
| 48 | 50 |
| 49 return v8::Handle<v8::FunctionTemplate>(); | 51 return v8::Handle<v8::FunctionTemplate>(); |
| 50 } | 52 } |
| 51 | 53 |
| 52 static v8::Handle<v8::Value> IsHeapProfilerRunning( | 54 static v8::Handle<v8::Value> IsHeapProfilerRunning( |
| 53 const v8::Arguments& args) { | 55 const v8::Arguments& args) { |
| 54 #if !defined(NO_TCMALLOC) && defined(OS_LINUX) | |
| 55 return v8::Boolean::New(::IsHeapProfilerRunning()); | 56 return v8::Boolean::New(::IsHeapProfilerRunning()); |
| 56 #else | |
| 57 return v8::Boolean::New(false); | |
| 58 #endif // !defined(NO_TCMALLOC) && defined(OS_LINUX) | |
| 59 } | 57 } |
| 60 | 58 |
| 61 static v8::Handle<v8::Value> HeapProfilerDump(const v8::Arguments& args) { | 59 static v8::Handle<v8::Value> HeapProfilerDump(const v8::Arguments& args) { |
| 62 #if !defined(NO_TCMALLOC) && defined(OS_LINUX) | 60 std::string process_type; |
| 61 if (args.Length() && args[0]->IsString()) |
| 62 process_type = *v8::String::AsciiValue(args[0]); |
| 63 std::string reason("benchmarking_extension"); | 63 std::string reason("benchmarking_extension"); |
| 64 if (args.Length() && args[0]->IsString()) | 64 if (args.Length() > 1 && args[1]->IsString()) |
| 65 reason = *v8::String::AsciiValue(args[0]); | 65 reason = *v8::String::AsciiValue(args[1]); |
| 66 ::HeapProfilerDump(reason.c_str()); | 66 if (process_type == "browser") { |
| 67 #endif // !defined(NO_TCMALLOC) && defined(OS_LINUX) | 67 content::RenderThreadImpl::current()->Send( |
| 68 new MemoryBenchmarkHostMsg_HeapProfilerDump(reason)); |
| 69 } else { |
| 70 ::HeapProfilerDump(reason.c_str()); |
| 71 } |
| 68 return v8::Undefined(); | 72 return v8::Undefined(); |
| 69 } | 73 } |
| 70 }; | 74 }; |
| 71 | 75 |
| 72 } // namespace | 76 } // namespace |
| 73 | 77 |
| 74 namespace content { | 78 namespace content { |
| 75 | 79 |
| 76 v8::Extension* MemoryBenchmarkingExtension::Get() { | 80 v8::Extension* MemoryBenchmarkingExtension::Get() { |
| 77 return new MemoryBenchmarkingWrapper(); | 81 return new MemoryBenchmarkingWrapper(); |
| 78 } | 82 } |
| 79 | 83 |
| 80 } // namespace content | 84 } // namespace content |
| 85 |
| 86 #endif // defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID)) |
| OLD | NEW |