Commit 3e068605 authored by Tianyou Li's avatar Tianyou Li Committed by Commit Bot

cputracemark extension

Add CPU trace mark extension for adding a magic instruction like 'cpuid'
to the code stream when perform trace collection.

This feature can be enabled by --expose-cputracemark-as=THE_NAME_YOU_SPECIFIED option.

Change-Id: I33e94793cddf4956dbb3ddddf2f599420aa4a945
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1699749
Commit-Queue: Tianyou Li <tianyou.li@intel.com>
Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Reviewed-by: 's avatarYang Guo <yangguo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#62749}
parent ed915416
......@@ -2206,6 +2206,8 @@ v8_source_set("v8_base_without_compiler") {
"src/execution/v8threads.h",
"src/execution/vm-state-inl.h",
"src/execution/vm-state.h",
"src/extensions/cputracemark-extension.cc",
"src/extensions/cputracemark-extension.h",
"src/extensions/externalize-string-extension.cc",
"src/extensions/externalize-string-extension.h",
"src/extensions/free-buffer-extension.cc",
......
// Copyright 2019 the V8 project 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 "src/extensions/cputracemark-extension.h"
namespace v8 {
namespace internal {
v8::Local<v8::FunctionTemplate>
CpuTraceMarkExtension::GetNativeFunctionTemplate(v8::Isolate* isolate,
v8::Local<v8::String> str) {
return v8::FunctionTemplate::New(isolate, CpuTraceMarkExtension::Mark);
}
void CpuTraceMarkExtension::Mark(
const v8::FunctionCallbackInfo<v8::Value>& args) {
if (args.Length() < 1 || !args[0]->IsUint32()) {
args.GetIsolate()->ThrowException(
v8::String::NewFromUtf8(
args.GetIsolate(),
"First parameter to cputracemark() must be a unsigned int32.",
NewStringType::kNormal)
.ToLocalChecked());
}
#if V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64
#if !V8_LIBC_MSVCRT
// for non msvc build
uint32_t param =
args[0]->Uint32Value(args.GetIsolate()->GetCurrentContext()).ToChecked();
int magic_dummy;
#if defined(__i386__) && defined(__pic__)
__asm__ __volatile__("push %%ebx; cpuid; pop %%ebx"
: "=a"(magic_dummy)
: "a"(0x4711 | ((unsigned)(param) << 16))
: "ecx", "edx");
#else
__asm__ __volatile__("cpuid"
: "=a"(magic_dummy)
: "a"(0x4711 | ((unsigned)(param) << 16))
: "ecx", "edx", "ebx");
#endif // defined(__i386__) && defined(__pic__)
#else
// no msvc build support yet.
#endif //! V8_LIBC_MSVCRT
#endif // V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64
}
} // namespace internal
} // namespace v8
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_EXTENSIONS_CPUTRACEMARK_EXTENSION_H_
#define V8_EXTENSIONS_CPUTRACEMARK_EXTENSION_H_
#include "include/v8.h"
#include "src/utils/utils.h"
namespace v8 {
namespace internal {
class CpuTraceMarkExtension : public v8::Extension {
public:
explicit CpuTraceMarkExtension(const char* fun_name)
: v8::Extension("v8/cpumark",
BuildSource(buffer_, sizeof(buffer_), fun_name)) {}
v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate(
v8::Isolate* isolate, v8::Local<v8::String> name) override;
private:
static void Mark(const v8::FunctionCallbackInfo<v8::Value>& args);
static const char* BuildSource(char* buf, size_t size, const char* fun_name) {
SNPrintF(Vector<char>(buf, static_cast<int>(size)), "native function %s();",
fun_name);
return buf;
}
char buffer_[50];
};
} // namespace internal
} // namespace v8
#endif // V8_EXTENSIONS_CPUTRACEMARK_EXTENSION_H_
......@@ -999,6 +999,8 @@ DEFINE_BOOL(experimental_stack_trace_frames, false,
DEFINE_BOOL(disallow_code_generation_from_strings, false,
"disallow eval and friends")
DEFINE_BOOL(expose_async_hooks, false, "expose async_hooks object")
DEFINE_STRING(expose_cputracemark_as, nullptr,
"expose cputracemark extension under the specified name")
// builtins.cc
DEFINE_BOOL(allow_unsafe_function_constructor, false,
......
......@@ -12,6 +12,7 @@
#include "src/debug/debug.h"
#include "src/execution/isolate-inl.h"
#include "src/execution/microtask-queue.h"
#include "src/extensions/cputracemark-extension.h"
#include "src/extensions/externalize-string-extension.h"
#include "src/extensions/free-buffer-extension.h"
#include "src/extensions/gc-extension.h"
......@@ -123,6 +124,11 @@ static const char* GCFunctionName() {
return flag_given ? FLAG_expose_gc_as : "gc";
}
static bool isValidCpuTraceMarkFunctionName() {
return FLAG_expose_cputracemark_as != nullptr &&
strlen(FLAG_expose_cputracemark_as) != 0;
}
void Bootstrapper::InitializeOncePerProcess() {
v8::RegisterExtension(v8::base::make_unique<FreeBufferExtension>());
v8::RegisterExtension(v8::base::make_unique<GCExtension>(GCFunctionName()));
......@@ -130,6 +136,10 @@ void Bootstrapper::InitializeOncePerProcess() {
v8::RegisterExtension(v8::base::make_unique<StatisticsExtension>());
v8::RegisterExtension(v8::base::make_unique<TriggerFailureExtension>());
v8::RegisterExtension(v8::base::make_unique<IgnitionStatisticsExtension>());
if (isValidCpuTraceMarkFunctionName()) {
v8::RegisterExtension(v8::base::make_unique<CpuTraceMarkExtension>(
FLAG_expose_cputracemark_as));
}
}
void Bootstrapper::TearDown() {
......@@ -5087,6 +5097,8 @@ bool Genesis::InstallExtensions(Isolate* isolate,
(!FLAG_trace_ignition_dispatches ||
InstallExtension(isolate, "v8/ignition-statistics",
&extension_states)) &&
(!isValidCpuTraceMarkFunctionName() ||
InstallExtension(isolate, "v8/cpumark", &extension_states)) &&
InstallRequestedExtensions(isolate, extensions, &extension_states);
}
......
// Copyright 2019 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Flags: --expose-cputracemark-as=cputracemark
// Test --expose-cputracemark-as option.
cputracemark(100);
cputracemark(100, 'a');
assertThrows(() => cputracemark(-1));
assertThrows(() => cputracemark(null));
assertThrows(() => cputracemark(""));
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment