forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerfSection.cpp
More file actions
187 lines (170 loc) · 5.08 KB
/
PerfSection.cpp
File metadata and controls
187 lines (170 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "hermes/Support/PerfSection.h"
#if defined(HERMES_USE_FBSYSTRACE) || defined(HERMESVM_PLATFORM_LOGGING)
#ifdef HERMES_USE_FBSYSTRACE
#include "fbsystrace.h"
#include "fbsystrace_tags.h"
#endif
#include "hermes/Platform/Logging.h"
#include "hermes/Support/SNPrintfBuf.h"
#include <algorithm>
#include <iterator>
#include <memory>
#include <utility>
namespace hermes {
using KVPair = std::pair<std::string, std::string>;
PerfSection::PerfSection(const char *name, const char *category)
:
#ifdef HERMESVM_PLATFORM_LOGGING
name_(name),
category_(category),
#endif
enabled_(false),
argValues_(0) {
#ifdef HERMESVM_PLATFORM_LOGGING
enabled_ = true;
if (category_) {
hermesLog("Hermes", "%s[%s] BEGIN.", name_, category_);
} else {
hermesLog("Hermes", "%s BEGIN.", name_);
}
#endif
#ifdef HERMES_USE_FBSYSTRACE
if (fbsystrace_is_tracing(TRACE_TAG_JS_VM)) {
enabled_ = true;
fbsystrace_begin_section(TRACE_TAG_JS_VM, name);
}
#endif
}
void PerfSection::addArg(const char *argName, size_t value) {
if (!enabled_)
return;
// Note that if the argument has already been set, we overwrite it.
freeDataIfExists(argName);
auto &val = argValues_[argName];
val.type = ArgType::SIZE_T;
val.value.sz_t = value;
}
void PerfSection::addArgD(const char *argName, double d) {
if (!enabled_)
return;
// Note that if the argument has already been set, we overwrite it.
freeDataIfExists(argName);
auto &val = argValues_[argName];
val.type = ArgType::DOUBLE;
val.value.d = d;
}
void PerfSection::addArg(
const char *argName,
const llvh::StringRef value,
bool doCopy) {
if (!enabled_)
return;
freeDataIfExists(argName);
auto &val = argValues_[argName];
val.type = ArgType::STRINGREF;
const char *data;
if (doCopy) {
char *copy = new char[value.size()];
std::memcpy(copy, value.data(), value.size());
data = copy;
} else {
data = value.data();
}
val.value.stringref = {data, value.size(), doCopy};
}
PerfSection::~PerfSection() {
if (!enabled_)
return;
if (argValues_.empty()) {
#ifdef HERMES_USE_FBSYSTRACE
fbsystrace_end_section(TRACE_TAG_JS_VM);
#endif
#ifdef HERMESVM_PLATFORM_LOGGING
if (category_) {
hermesLog("Hermes", "%s[%s] END.", name_, category_);
} else {
hermesLog("Hermes", "%s END.", name_);
}
#endif
return;
}
// First, transform the ArgValue parts of argValues_ into strings.
std::vector<KVPair> kvPairs(argValues_.size());
std::transform(
argValues_.begin(),
argValues_.end(),
kvPairs.begin(),
[](const std::pair<std::string, ArgValue> &pair) -> KVPair {
// If more elements are added to ArgType, more cases must be added to
// this switch.
switch (pair.second.type) {
case ArgType::SIZE_T: {
SNPrintfBuf buf(20);
buf.printf("%zu", pair.second.value.sz_t);
// Note that KVPair has std::string elements, so the
// c_str() is copied.
return KVPair{pair.first, buf.c_str()};
}
case ArgType::DOUBLE: {
SNPrintfBuf buf(20);
buf.printf("%f", pair.second.value.d);
// Note that KVPair has std::string elements, so the
// c_str() is copied.
return KVPair{pair.first, buf.c_str()};
}
case ArgType::STRINGREF:
return KVPair{pair.first,
std::string(
pair.second.value.stringref.data,
pair.second.value.stringref.size)};
default:
llvm_unreachable("Unhandled argtype");
}
});
#ifdef HERMES_USE_FBSYSTRACE
// To output via fbsystrace, we must further translate into
// FBSystraceSectionArg format.
auto argArr = std::unique_ptr<FbSystraceSectionArg[]>{
new FbSystraceSectionArg[argValues_.size()]};
std::transform(
kvPairs.begin(), kvPairs.end(), argArr.get(), [](const KVPair &kvPair) {
return FbSystraceSectionArg{
kvPair.first.c_str(),
static_cast<int>(kvPair.first.size()),
kvPair.second.c_str(),
static_cast<int>(kvPair.second.size()),
};
});
fbsystrace_end_section_with_args(
TRACE_TAG_JS_VM, argValues_.size(), argArr.get());
#endif
#ifdef HERMESVM_PLATFORM_LOGGING
SNPrintfBuf buf(1000);
bool first = true;
for (const KVPair &kvPair : kvPairs) {
if (first) {
first = false;
} else {
buf.printf(", ");
}
buf.printf("%s: %s", kvPair.first.c_str(), kvPair.second.c_str());
}
if (category_) {
hermesLog("Hermes", "%s[%s] END: %s.", name_, category_, buf.c_str());
} else {
hermesLog("Hermes", "%s END: %s.", name_, buf.c_str());
}
#endif
for (auto &arg : argValues_) {
arg.second.freeDependencies();
}
}
} // namespace hermes
#endif // defined(HERMES_USE_FBSYSTRACE) ||
// defined(HERMESVM_PLATFORM_LOGGING)