12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/renderer/memory_benchmarking_extension.h"
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "content/common/memory_benchmark_messages.h"
890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "content/renderer/render_thread_impl.h"
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "gin/arguments.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "gin/handle.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "gin/object_template_builder.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "third_party/WebKit/public/web/WebFrame.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "third_party/WebKit/public/web/WebKit.h"
1490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID))
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h"
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace content {
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)gin::WrapperInfo MemoryBenchmarkingExtension::kWrapperInfo = {
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    gin::kEmbedderNativeGin};
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// static
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void MemoryBenchmarkingExtension::Install(blink::WebFrame* frame) {
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  v8::Isolate* isolate = blink::mainThreadIsolate();
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  v8::HandleScope handle_scope(isolate);
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  v8::Handle<v8::Context> context = frame->mainWorldScriptContext();
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (context.IsEmpty())
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  v8::Context::Scope context_scope(context);
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gin::Handle<MemoryBenchmarkingExtension> controller =
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      gin::CreateHandle(isolate, new MemoryBenchmarkingExtension());
355c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  if (controller.IsEmpty())
365c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    return;
375c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  v8::Handle<v8::Object> global = context->Global();
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  v8::Handle<v8::Object> chrome =
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      global->Get(gin::StringToV8(isolate, "chrome"))->ToObject();
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (chrome.IsEmpty()) {
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    chrome = v8::Object::New(isolate);
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    global->Set(gin::StringToV8(isolate, "chrome"), chrome);
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  chrome->Set(gin::StringToV8(isolate, "memoryBenchmarking"),
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              controller.ToV8());
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)MemoryBenchmarkingExtension::MemoryBenchmarkingExtension() {}
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)MemoryBenchmarkingExtension::~MemoryBenchmarkingExtension() {}
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)gin::ObjectTemplateBuilder
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)MemoryBenchmarkingExtension::GetObjectTemplateBuilder(v8::Isolate* isolate) {
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return gin::Wrappable<MemoryBenchmarkingExtension>::GetObjectTemplateBuilder(
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)             isolate)
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      .SetMethod("isHeapProfilerRunning",
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                 &MemoryBenchmarkingExtension::IsHeapProfilerRunning)
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      .SetMethod("heapProfilerDump",
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                 &MemoryBenchmarkingExtension::HeapProfilerDump);
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool MemoryBenchmarkingExtension::IsHeapProfilerRunning() {
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID))
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return ::IsHeapProfilerRunning();
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#else
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return false;
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void MemoryBenchmarkingExtension::HeapProfilerDump(gin::Arguments* args) {
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string process_type;
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string reason("benchmarking_extension");
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (args->PeekNext()->IsString()) {
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    args->GetNext(&process_type);
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (args->PeekNext()->IsString())
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      args->GetNext(&reason);
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID))
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (process_type == "browser") {
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    content::RenderThreadImpl::current()->Send(
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        new MemoryBenchmarkHostMsg_HeapProfilerDump(reason));
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  } else {
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ::HeapProfilerDump(reason.c_str());
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace content
92