14f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org// Copyright 2013 the V8 project authors. All rights reserved.
24f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org// Redistribution and use in source and binary forms, with or without
34f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org// modification, are permitted provided that the following conditions are
44f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org// met:
54f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org//
64f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org//     * Redistributions of source code must retain the above copyright
74f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org//       notice, this list of conditions and the following disclaimer.
84f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org//     * Redistributions in binary form must reproduce the above
94f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org//       copyright notice, this list of conditions and the following
104f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org//       disclaimer in the documentation and/or other materials provided
114f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org//       with the distribution.
124f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org//     * Neither the name of Google Inc. nor the names of its
134f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org//       contributors may be used to endorse or promote products derived
144f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org//       from this software without specific prior written permission.
154f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org//
164f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
174f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
184f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
194f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
204f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
214f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
224f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
234f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
244f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
254f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
264f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
274f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org//
284f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org// Tests of profiles generator and utilities.
294f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org
305de0074a922429f5e0ec2cf140c2d2989bf88140yangguo@chromium.org#include "src/base/logging.h"
31196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org#include "test/cctest/profiler-extension.h"
3243c51e51fafad9405752a3d7e953367531469575machenbach@chromium.org
3343c51e51fafad9405752a3d7e953367531469575machenbach@chromium.orgnamespace v8 {
3443c51e51fafad9405752a3d7e953367531469575machenbach@chromium.orgnamespace internal {
354f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org
364f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org
372f599e5925b02d78bd78703b44741d6b27e53a44machenbach@chromium.orgv8::CpuProfile* ProfilerExtension::last_profile = NULL;
384f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.orgconst char* ProfilerExtension::kSource =
394f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org    "native function startProfiling();"
404f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org    "native function stopProfiling();";
414f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org
424f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.orgv8::Handle<v8::FunctionTemplate> ProfilerExtension::GetNativeFunctionTemplate(
434f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org    v8::Isolate* isolate, v8::Handle<v8::String> name) {
444f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org  if (name->Equals(v8::String::NewFromUtf8(isolate, "startProfiling"))) {
450f13e74b7310d8b14f19c6b93b36ff95059f97f6ulan@chromium.org    return v8::FunctionTemplate::New(isolate,
460f13e74b7310d8b14f19c6b93b36ff95059f97f6ulan@chromium.org                                     ProfilerExtension::StartProfiling);
474f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org  } else if (name->Equals(v8::String::NewFromUtf8(isolate, "stopProfiling"))) {
480f13e74b7310d8b14f19c6b93b36ff95059f97f6ulan@chromium.org    return v8::FunctionTemplate::New(isolate,
490f13e74b7310d8b14f19c6b93b36ff95059f97f6ulan@chromium.org                                     ProfilerExtension::StopProfiling);
504f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org  } else {
514f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org    CHECK(false);
524f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org    return v8::Handle<v8::FunctionTemplate>();
534f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org  }
544f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org}
554f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org
564f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org
574f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.orgvoid ProfilerExtension::StartProfiling(
584f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org    const v8::FunctionCallbackInfo<v8::Value>& args) {
594f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org  last_profile = NULL;
604f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org  v8::CpuProfiler* cpu_profiler = args.GetIsolate()->GetCpuProfiler();
612f599e5925b02d78bd78703b44741d6b27e53a44machenbach@chromium.org  cpu_profiler->StartProfiling((args.Length() > 0)
624f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org      ? args[0].As<v8::String>()
634f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org      : v8::String::Empty(args.GetIsolate()));
644f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org}
654f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org
664f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org
674f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.orgvoid ProfilerExtension::StopProfiling(
684f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org    const v8::FunctionCallbackInfo<v8::Value>& args) {
694f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org  v8::CpuProfiler* cpu_profiler = args.GetIsolate()->GetCpuProfiler();
702f599e5925b02d78bd78703b44741d6b27e53a44machenbach@chromium.org  last_profile = cpu_profiler->StopProfiling((args.Length() > 0)
714f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org      ? args[0].As<v8::String>()
724f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org      : v8::String::Empty(args.GetIsolate()));
734f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org}
744f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org
7543c51e51fafad9405752a3d7e953367531469575machenbach@chromium.org} }  // namespace v8::internal
76