v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
vtunedomain-support-extension.cc
Go to the documentation of this file.
1// Copyright 2019 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7#include <string>
8#include <vector>
9
10#include "include/v8-isolate.h"
11#include "include/v8-template.h"
12
13namespace v8 {
14namespace internal {
15
16namespace libvtune {
17
18int startTask(const std::vector<std::string>& vparams);
19int endTask(const std::vector<std::string>& vparams);
20
21const auto& function_map =
22 *new std::map<std::string, int (*)(const std::vector<std::string>&)>{
23 {"start", startTask}, {"end", endTask}};
24
25void split(const std::string& str, char delimiter,
26 std::vector<std::string>* vparams) {
27 std::string::size_type baseindex = 0;
28 std::string::size_type offindex = str.find(delimiter);
29
30 while (offindex != std::string::npos) {
31 (*vparams).push_back(str.substr(baseindex, offindex - baseindex));
32 baseindex = ++offindex;
33 offindex = str.find(delimiter, offindex);
34
35 if (offindex == std::string::npos)
36 (*vparams).push_back(str.substr(baseindex, str.length()));
37 }
38}
39
40int startTask(const std::vector<std::string>& vparams) {
41 int errcode = 0;
42
43 if (const char* domain_name = vparams[1].c_str()) {
44 if (const char* task_name = vparams[2].c_str()) {
45 if (std::shared_ptr<VTuneDomain> domainptr =
46 VTuneDomain::createDomain(domain_name)) {
47 if (!domainptr->beginTask(task_name)) {
48 errcode += TASK_BEGIN_FAILED;
49 }
50 } else {
51 errcode += CREATE_DOMAIN_FAILED;
52 }
53 } else {
54 errcode += NO_TASK_NAME;
55 }
56
57 } else {
58 errcode = NO_DOMAIN_NAME;
59 }
60
61 return errcode;
62}
63
64int endTask(const std::vector<std::string>& vparams) {
65 int errcode = 0;
66
67 if (const char* domain_name = vparams[1].c_str()) {
68 if (std::shared_ptr<VTuneDomain> domainptr =
69 VTuneDomain::createDomain(domain_name)) {
70 domainptr->endTask();
71 } else {
72 errcode += CREATE_DOMAIN_FAILED;
73 }
74 } else {
75 errcode = NO_DOMAIN_NAME;
76 }
77
78 return errcode;
79}
80
81int invoke(const char* params) {
82 int errcode = 0;
83 std::vector<std::string> vparams;
84
85 split(*(new std::string(params)), ' ', &vparams);
86
87 auto it = function_map.find(vparams[0]);
88 if (it != function_map.end()) {
89 (it->second)(vparams);
90 } else {
91 errcode += UNKNOWN_PARAMS;
92 }
93
94 return errcode;
95}
96
97} // namespace libvtune
98
104
105// info should take three parameters
106// %0 : string, which is the domain name. Domain is used to tagging trace data
107// for different modules or libraryies in a program
108// %1 : string, which is the task name. Task is a logical unit of work performed
109// by a particular thread statement. Task can nest.
110// %2 : string, "start" / "end". Action to be taken on a task in a particular
111// domain
114 if (info.Length() != 3 || !info[0]->IsString() || !info[1]->IsString() ||
115 !info[2]->IsString()) {
116 info.GetIsolate()->ThrowError(
117 "Parameter number should be exactly three, first domain name"
118 "second task name, third start/end");
119 return;
120 }
121
122 v8::Isolate* isolate = info.GetIsolate();
123 v8::String::Utf8Value domainName(isolate, info[0]);
124 v8::String::Utf8Value taskName(isolate, info[1]);
125 v8::String::Utf8Value statName(isolate, info[2]);
126
127 char* cdomainName = *domainName;
128 char* ctaskName = *taskName;
129 char* cstatName = *statName;
130
131 std::stringstream params;
132 params << cstatName << " " << cdomainName << " " << ctaskName;
133
134 int r = 0;
135 if ((r = libvtune::invoke(params.str().c_str())) != 0) {
136 info.GetIsolate()->ThrowError(
137 v8::String::NewFromUtf8(info.GetIsolate(), std::to_string(r).c_str())
138 .ToLocalChecked());
139 }
140}
141
142} // namespace internal
143} // namespace v8
static Local< FunctionTemplate > New(Isolate *isolate, FunctionCallback callback=nullptr, Local< Value > data=Local< Value >(), Local< Signature > signature=Local< Signature >(), int length=0, ConstructorBehavior behavior=ConstructorBehavior::kAllow, SideEffectType side_effect_type=SideEffectType::kHasSideEffect, const CFunction *c_function=nullptr, uint16_t instance_type=0, uint16_t allowed_receiver_instance_type_range_start=0, uint16_t allowed_receiver_instance_type_range_end=0)
Definition api.cc:1101
static V8_WARN_UNUSED_RESULT MaybeLocal< String > NewFromUtf8(Isolate *isolate, const char *data, NewStringType type=NewStringType::kNormal, int length=-1)
Definition api.cc:7593
v8::Local< v8::FunctionTemplate > GetNativeFunctionTemplate(v8::Isolate *isolate, v8::Local< v8::String > name) override
static void Mark(const v8::FunctionCallbackInfo< v8::Value > &info)
int r
Definition mul-fft.cc:298
int startTask(const std::vector< std::string > &vparams)
int endTask(const std::vector< std::string > &vparams)
void split(const std::string &str, char delimiter, std::vector< std::string > *vparams)
#define NO_DOMAIN_NAME
#define NO_TASK_NAME
#define TASK_BEGIN_FAILED
#define CREATE_DOMAIN_FAILED
#define UNKNOWN_PARAMS