1b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// Copyright 2013 the V8 project authors. All rights reserved.
21e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block// Redistribution and use in source and binary forms, with or without
31e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block// modification, are permitted provided that the following conditions are
41e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block// met:
51e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block//
61e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block//     * Redistributions of source code must retain the above copyright
71e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block//       notice, this list of conditions and the following disclaimer.
81e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block//     * Redistributions in binary form must reproduce the above
91e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block//       copyright notice, this list of conditions and the following
101e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block//       disclaimer in the documentation and/or other materials provided
111e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block//       with the distribution.
121e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block//     * Neither the name of Google Inc. nor the names of its
131e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block//       contributors may be used to endorse or promote products derived
141e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block//       from this software without specific prior written permission.
151e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block//
161e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
171e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
181e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
191e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
201e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
211e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
221e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
231e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
241e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
251e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
261e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch//
28b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// Tests of profiles generator and utilities.
291e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block
30b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#include "src/base/logging.h"
31b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#include "test/cctest/profiler-extension.h"
32592a9fc1d8ea420377a2e7efd0600e20b058be2bBen Murdoch
3385b71799222b55eb5dd74ea26efe0c64ab655c8cBen Murdochnamespace v8 {
3485b71799222b55eb5dd74ea26efe0c64ab655c8cBen Murdochnamespace internal {
35592a9fc1d8ea420377a2e7efd0600e20b058be2bBen Murdoch
36592a9fc1d8ea420377a2e7efd0600e20b058be2bBen Murdoch
37b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochv8::CpuProfile* ProfilerExtension::last_profile = NULL;
38b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochconst char* ProfilerExtension::kSource =
39b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    "native function startProfiling();"
40b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    "native function stopProfiling();";
4185b71799222b55eb5dd74ea26efe0c64ab655c8cBen Murdoch
42b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochv8::Handle<v8::FunctionTemplate> ProfilerExtension::GetNativeFunctionTemplate(
43b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    v8::Isolate* isolate, v8::Handle<v8::String> name) {
44b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (name->Equals(v8::String::NewFromUtf8(isolate, "startProfiling"))) {
45b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return v8::FunctionTemplate::New(isolate,
46b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                     ProfilerExtension::StartProfiling);
47b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  } else if (name->Equals(v8::String::NewFromUtf8(isolate, "stopProfiling"))) {
48b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return v8::FunctionTemplate::New(isolate,
49b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                     ProfilerExtension::StopProfiling);
503ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  } else {
51b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    CHECK(false);
52b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return v8::Handle<v8::FunctionTemplate>();
533ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  }
543ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch}
5585b71799222b55eb5dd74ea26efe0c64ab655c8cBen Murdoch
5685b71799222b55eb5dd74ea26efe0c64ab655c8cBen Murdoch
57b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochvoid ProfilerExtension::StartProfiling(
58b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    const v8::FunctionCallbackInfo<v8::Value>& args) {
59b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  last_profile = NULL;
60b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  v8::CpuProfiler* cpu_profiler = args.GetIsolate()->GetCpuProfiler();
61b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  cpu_profiler->StartProfiling((args.Length() > 0)
62b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      ? args[0].As<v8::String>()
63b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      : v8::String::Empty(args.GetIsolate()));
643ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch}
6585b71799222b55eb5dd74ea26efe0c64ab655c8cBen Murdoch
6685b71799222b55eb5dd74ea26efe0c64ab655c8cBen Murdoch
67b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochvoid ProfilerExtension::StopProfiling(
68b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    const v8::FunctionCallbackInfo<v8::Value>& args) {
69b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  v8::CpuProfiler* cpu_profiler = args.GetIsolate()->GetCpuProfiler();
70b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  last_profile = cpu_profiler->StopProfiling((args.Length() > 0)
71b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      ? args[0].As<v8::String>()
72b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      : v8::String::Empty(args.GetIsolate()));
73b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
74592a9fc1d8ea420377a2e7efd0600e20b058be2bBen Murdoch
75b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch} }  // namespace v8::internal
76