Chromium Code Reviews| Index: chrome/renderer/memory_benchmarking_extension.cc |
| diff --git a/chrome/renderer/memory_benchmarking_extension.cc b/chrome/renderer/memory_benchmarking_extension.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..99483dce674a13fc58d286fb52ea9dc41abc2d4e |
| --- /dev/null |
| +++ b/chrome/renderer/memory_benchmarking_extension.cc |
| @@ -0,0 +1,77 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/renderer/memory_benchmarking_extension.h" |
| + |
| +#include "base/string_util.h" |
| +#include "chrome/common/benchmarking_messages.h" |
| +#include "content/public/renderer/render_thread.h" |
| +#if !defined(NO_TCMALLOC) && (defined(OS_LINUX) || defined(OS_CHROMEOS)) |
|
jochen (gone - plz use gerrit)
2013/02/06 11:57:57
#if clauses should come after all includes with an
Dai Mikurube (NOT FULLTIME)
2013/02/06 12:24:45
Done.
|
| +#include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h" |
| +#endif // !defined(NO_TCMALLOC) && (defined(OS_LINUX) || defined(OS_CHROMEOS)) |
| +#include "v8/include/v8.h" |
| + |
| +const char kMemoryBenchmarkingExtensionName[] = "v8/MemoryBenchmarking"; |
|
jochen (gone - plz use gerrit)
2013/02/06 11:57:57
should also go into an anonymous namespace
Dai Mikurube (NOT FULLTIME)
2013/02/06 12:24:45
Done.
|
| + |
| +namespace extensions_v8 { |
| + |
| +class MemoryBenchmarkingWrapper : public v8::Extension { |
|
jochen (gone - plz use gerrit)
2013/02/06 11:57:57
this class and the static functions below should g
Dai Mikurube (NOT FULLTIME)
2013/02/06 12:24:45
In an anonymous namespace, how can I call MemoryBe
jochen (gone - plz use gerrit)
2013/02/06 12:31:41
I meant that the Wrapper should go into the anon n
Dai Mikurube (NOT FULLTIME)
2013/02/06 12:38:01
Makes sense. Done.
|
| + public: |
| + MemoryBenchmarkingWrapper() : |
| + v8::Extension(kMemoryBenchmarkingExtensionName, |
|
jochen (gone - plz use gerrit)
2013/02/06 11:57:57
usually, we put the code in a .js file in the reso
Dai Mikurube (NOT FULLTIME)
2013/02/06 12:24:45
Almost all the benchmarking_extension does this.
jochen (gone - plz use gerrit)
2013/02/06 12:31:41
I'd argue they do it wrong :)
|
| + "if (typeof(chrome) == 'undefined') {" |
| + " chrome = {};" |
|
jochen (gone - plz use gerrit)
2013/02/06 12:31:41
this should be just
chrome = chrome || {};
chrom
Dai Mikurube (NOT FULLTIME)
2013/02/06 12:38:01
IIRC, they have different meaning in JS. I'm not
|
| + "};" |
| + "if (typeof(chrome.benchmarking) == 'undefined') {" |
| + " chrome.benchmarking = {};" |
| + "};" |
| + "chrome.benchmarking.isHeapProfilerRunning = function() {" |
| + " native function IsHeapProfilerRunning();" |
| + " return IsHeapProfilerRunning();" |
| + "};" |
| + "chrome.benchmarking.heapProfilerDump = function(reason) {" |
| + " native function HeapProfilerDump();" |
| + " HeapProfilerDump(reason);" |
| + "};" |
| + ) {} |
| + |
| + virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
| + v8::Handle<v8::String> name) { |
| + if (name->Equals(v8::String::New("IsHeapProfilerRunning"))) { |
|
jochen (gone - plz use gerrit)
2013/02/06 12:31:41
no { } for single-line bodies
Dai Mikurube (NOT FULLTIME)
2013/02/06 12:38:01
Done.
|
| + return v8::FunctionTemplate::New(IsHeapProfilerRunning); |
| + } else if (name->Equals(v8::String::New("HeapProfilerDump"))) { |
| + return v8::FunctionTemplate::New(HeapProfilerDump); |
| + } |
| + |
| + return v8::Handle<v8::FunctionTemplate>(); |
| + } |
| + |
| + static v8::Handle<v8::Value> |
| + IsHeapProfilerRunning(const v8::Arguments& args) { |
|
jochen (gone - plz use gerrit)
2013/02/06 11:57:57
i think you should wrap after IsHeapProfilerRunnin
Dai Mikurube (NOT FULLTIME)
2013/02/06 12:24:45
Done.
|
| +#if !defined(NO_TCMALLOC) && (defined(OS_LINUX) || defined(OS_CHROMEOS)) |
| + return v8::Boolean::New(::IsHeapProfilerRunning()); |
| +#else |
| + return v8::Boolean::New(false); |
| +#endif // !defined(NO_TCMALLOC) && (defined(OS_LINUX) || defined(OS_CHROMEOS)) |
| + } |
| + |
| + static v8::Handle<v8::Value> HeapProfilerDump(const v8::Arguments& args) { |
| +#if !defined(NO_TCMALLOC) && (defined(OS_LINUX) || defined(OS_CHROMEOS)) |
| + char reason[256]; |
|
jochen (gone - plz use gerrit)
2013/02/06 11:57:57
why not
std::string reason("benchmarking_extensi
Dai Mikurube (NOT FULLTIME)
2013/02/06 12:24:45
Done.
I didn't know AsciiValue(). I did the same
|
| + if (args.Length() && args[0]->IsString()) |
| + args[0]->ToString()->WriteAscii(reason, 0, sizeof(reason) - 1); |
| + else |
| + base::strlcpy(reason, "benchmarking_extension", sizeof(reason)); |
| + |
| + ::HeapProfilerDump(reason); |
| +#endif // !defined(NO_TCMALLOC) && (defined(OS_LINUX) || defined(OS_CHROMEOS)) |
| + return v8::Undefined(); |
| + } |
| +}; |
| + |
| +v8::Extension* MemoryBenchmarkingExtension::Get() { |
| + return new MemoryBenchmarkingWrapper(); |
| +} |
| + |
| +} // namespace extensions_v8 |