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/common/memory_benchmark_messages.h" |
8 #include "content/public/renderer/render_thread.h" | 9 #include "content/public/renderer/render_thread.h" |
| 10 #include "content/renderer/render_view_impl.h" |
| 11 #if !defined(NO_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID)) |
| 12 #define HAS_MEMORY_BENCHMARK 1 |
| 13 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h" |
| 14 #endif |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
9 | 17 |
10 #if !defined(NO_TCMALLOC) && defined(OS_LINUX) | 18 using content::RenderViewImpl; |
11 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h" | 19 using WebKit::WebFrame; |
12 #endif // !defined(NO_TCMALLOC) && defined(OS_LINUX) | 20 using WebKit::WebView; |
13 | 21 |
14 namespace { | 22 namespace { |
15 | 23 |
16 const char kMemoryBenchmarkingExtensionName[] = "v8/MemoryBenchmarking"; | 24 const char kMemoryBenchmarkingExtensionName[] = "v8/MemoryBenchmarking"; |
17 | 25 |
18 } | 26 } |
19 | 27 |
20 namespace { | 28 namespace { |
21 | 29 |
22 class MemoryBenchmarkingWrapper : public v8::Extension { | 30 class MemoryBenchmarkingWrapper : public v8::Extension { |
(...skipping 21 matching lines...) Expand all Loading... |
44 if (name->Equals(v8::String::New("IsHeapProfilerRunning"))) | 52 if (name->Equals(v8::String::New("IsHeapProfilerRunning"))) |
45 return v8::FunctionTemplate::New(IsHeapProfilerRunning); | 53 return v8::FunctionTemplate::New(IsHeapProfilerRunning); |
46 else if (name->Equals(v8::String::New("HeapProfilerDump"))) | 54 else if (name->Equals(v8::String::New("HeapProfilerDump"))) |
47 return v8::FunctionTemplate::New(HeapProfilerDump); | 55 return v8::FunctionTemplate::New(HeapProfilerDump); |
48 | 56 |
49 return v8::Handle<v8::FunctionTemplate>(); | 57 return v8::Handle<v8::FunctionTemplate>(); |
50 } | 58 } |
51 | 59 |
52 static v8::Handle<v8::Value> IsHeapProfilerRunning( | 60 static v8::Handle<v8::Value> IsHeapProfilerRunning( |
53 const v8::Arguments& args) { | 61 const v8::Arguments& args) { |
54 #if !defined(NO_TCMALLOC) && defined(OS_LINUX) | 62 #if defined(HAS_MEMORY_BENCHMARK) |
55 return v8::Boolean::New(::IsHeapProfilerRunning()); | 63 return v8::Boolean::New(::IsHeapProfilerRunning()); |
56 #else | 64 #else |
57 return v8::Boolean::New(false); | 65 return v8::Boolean::New(false); |
58 #endif // !defined(NO_TCMALLOC) && defined(OS_LINUX) | 66 #endif // defined(HAS_MEMORY_BENCHMARK) |
59 } | 67 } |
60 | 68 |
| 69 #if defined(HAS_MEMORY_BENCHMARK) |
| 70 static v8::Handle<v8::Value> HeapProfilerDumpBrowser() { |
| 71 WebFrame* web_frame = WebFrame::frameForCurrentContext(); |
| 72 if (!web_frame) |
| 73 return v8::Undefined(); |
| 74 |
| 75 WebView* web_view = web_frame->view(); |
| 76 if (!web_view) |
| 77 return v8::Undefined(); |
| 78 |
| 79 RenderViewImpl* render_view_impl = RenderViewImpl::FromWebView(web_view); |
| 80 if (!render_view_impl) |
| 81 return v8::Undefined(); |
| 82 std::string dump_file_name; |
| 83 |
| 84 render_view_impl->Send( |
| 85 new MemoryBenchmarkHostMsg_HeapProfilerDump( |
| 86 render_view_impl->routing_id(), &dump_file_name)); |
| 87 return v8::String::New(dump_file_name.c_str()); |
| 88 } |
| 89 #endif // defined(HAS_MEMORY_BENCHMARK) |
| 90 |
61 static v8::Handle<v8::Value> HeapProfilerDump(const v8::Arguments& args) { | 91 static v8::Handle<v8::Value> HeapProfilerDump(const v8::Arguments& args) { |
62 #if !defined(NO_TCMALLOC) && defined(OS_LINUX) | 92 #if defined(HAS_MEMORY_BENCHMARK) |
63 char dumped_filename_buffer[1000]; | 93 char dumped_filename_buffer[1000]; |
64 std::string reason("benchmarking_extension"); | 94 std::string reason("benchmarking_extension"); |
65 if (args.Length() && args[0]->IsString()) | 95 if (args.Length() && args[0]->IsString()) |
66 reason = *v8::String::AsciiValue(args[0]); | 96 reason = *v8::String::AsciiValue(args[0]); |
| 97 if (reason == "browser") |
| 98 return HeapProfilerDumpBrowser(); |
67 ::HeapProfilerDumpWithFileName(reason.c_str(), | 99 ::HeapProfilerDumpWithFileName(reason.c_str(), |
68 dumped_filename_buffer, | 100 dumped_filename_buffer, |
69 sizeof(dumped_filename_buffer)); | 101 sizeof(dumped_filename_buffer)); |
70 return v8::String::New(dumped_filename_buffer); | 102 return v8::String::New(dumped_filename_buffer); |
71 #endif // !defined(NO_TCMALLOC) && defined(OS_LINUX) | 103 #endif // defined(HAS_MEMORY_BENCHMARK) |
72 return v8::Undefined(); | 104 return v8::Undefined(); |
73 } | 105 } |
74 }; | 106 }; |
75 | 107 |
76 } // namespace | 108 } // namespace |
77 | 109 |
78 namespace content { | 110 namespace content { |
79 | 111 |
80 v8::Extension* MemoryBenchmarkingExtension::Get() { | 112 v8::Extension* MemoryBenchmarkingExtension::Get() { |
81 return new MemoryBenchmarkingWrapper(); | 113 return new MemoryBenchmarkingWrapper(); |
82 } | 114 } |
83 | 115 |
84 } // namespace content | 116 } // namespace content |
OLD | NEW |