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 } |
57 return v8::Boolean::New(false); | 58 |
58 #endif // !defined(NO_TCMALLOC) && defined(OS_LINUX) | 59 static void HeapProfilerDumpBrowser(const std::string& reason) { |
palmer
2013/05/15 20:27:50
What is the range of possible |reason|s? Normally
Dai Mikurube (NOT FULLTIME)
2013/05/15 22:36:07
It can be any string, and it has no effects on any
| |
60 content::RenderThreadImpl::current()->Send( | |
61 new MemoryBenchmarkHostMsg_HeapProfilerDump(reason)); | |
59 } | 62 } |
piman
2013/05/15 19:18:54
nit: you inlined this function into HeapProfilerDu
bulach
2013/05/16 08:29:09
ops, sorry, I forgot to remove. done.
| |
60 | 63 |
61 static v8::Handle<v8::Value> HeapProfilerDump(const v8::Arguments& args) { | 64 static v8::Handle<v8::Value> HeapProfilerDump(const v8::Arguments& args) { |
62 #if !defined(NO_TCMALLOC) && defined(OS_LINUX) | 65 std::string process_type; |
66 if (args.Length() && args[0]->IsString()) | |
67 process_type = *v8::String::AsciiValue(args[0]); | |
63 std::string reason("benchmarking_extension"); | 68 std::string reason("benchmarking_extension"); |
64 if (args.Length() && args[0]->IsString()) | 69 if (args.Length() > 1 && args[1]->IsString()) |
65 reason = *v8::String::AsciiValue(args[0]); | 70 reason = *v8::String::AsciiValue(args[1]); |
66 ::HeapProfilerDump(reason.c_str()); | 71 if (process_type == "browser") { |
67 #endif // !defined(NO_TCMALLOC) && defined(OS_LINUX) | 72 content::RenderThreadImpl::current()->Send( |
73 new MemoryBenchmarkHostMsg_HeapProfilerDump(reason)); | |
74 } else { | |
75 ::HeapProfilerDump(reason.c_str()); | |
76 } | |
68 return v8::Undefined(); | 77 return v8::Undefined(); |
69 } | 78 } |
70 }; | 79 }; |
71 | 80 |
72 } // namespace | 81 } // namespace |
73 | 82 |
74 namespace content { | 83 namespace content { |
75 | 84 |
76 v8::Extension* MemoryBenchmarkingExtension::Get() { | 85 v8::Extension* MemoryBenchmarkingExtension::Get() { |
77 return new MemoryBenchmarkingWrapper(); | 86 return new MemoryBenchmarkingWrapper(); |
78 } | 87 } |
79 | 88 |
80 } // namespace content | 89 } // namespace content |
90 | |
91 #endif // defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID)) | |
OLD | NEW |