1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/dfx/tracing/tracing.h"
17 #include "ecmascript/jspandafile/js_pandafile_manager.h"
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/debugger/js_debugger_manager.h"
20
21 namespace panda::ecmascript {
GetEventBufferSize()22 uint64_t TraceEvent::GetEventBufferSize()
23 {
24 uint64_t size = sizeof(*this) + cat_.size() + name_.size() + ph_.size() + args_.size() + s_.size() + id_.size();
25 if (cpuProfileArgs_.has_value()) {
26 size += cpuProfileArgs_.value().nodes.size() * sizeof(int);
27 size += cpuProfileArgs_.value().samples.size() * sizeof(int);
28 size += cpuProfileArgs_.value().timeDeltas.size() * sizeof(int);
29 }
30 return size;
31 }
32
Tracing(const EcmaVM * vm)33 Tracing::Tracing(const EcmaVM *vm) : vm_(vm)
34 {
35 traceEvents_ = std::make_unique<std::vector<TraceEvent>>();
36 traceEventsCpuProfiler_ = std::make_unique<std::vector<TraceEvent>>();
37 }
38
~Tracing()39 Tracing::~Tracing()
40 {
41 }
42
StartTracing(std::string & categories)43 void Tracing::StartTracing(std::string &categories)
44 {
45 if (isTracing_) {
46 return;
47 }
48
49 categories_ = categories;
50 if (categories_.find("cpu_profiler") != std::string::npos) {
51 panda::JSNApi::SetProfilerState(vm_, true);
52 panda::DFXJSNApi::StartCpuProfilerForInfo(vm_);
53 }
54
55 tid_ = static_cast<pthread_t>(syscall(SYS_gettid));
56 isTracing_ = true;
57 vm_->GetJsDebuggerManager()->GetNotificationManager()->AddListener(this);
58 vm_->GetJSThread()->SetIsTracing(true);
59
60 TraceEventRecordTracingStart();
61 return;
62 }
63
StopTracing()64 std::unique_ptr<std::vector<TraceEvent>> Tracing::StopTracing()
65 {
66 isTracing_ = false;
67 vm_->GetJsDebuggerManager()->GetNotificationManager()->RemoveListener(this);
68 if (categories_.find("cpu_profiler") != std::string::npos) {
69 std::unique_ptr<ProfileInfo> profileInfo = panda::DFXJSNApi::StopCpuProfilerForInfo(vm_);
70 panda::JSNApi::SetProfilerState(vm_, false);
71 if (profileInfo) {
72 TraceEventUpdateCpuProfiler(profileInfo.get());
73 }
74 }
75 vm_->GetJSThread()->SetIsTracing(false);
76 return std::move(traceEvents_);
77 }
78
GetTimeStamp()79 uint64_t Tracing::GetTimeStamp()
80 {
81 const int USEC_PER_SEC = 1000 * 1000;
82 const int NSEC_PER_USEC = 1000;
83 struct timespec time;
84 clock_gettime(CLOCK_MONOTONIC, &time);
85 return time.tv_sec * USEC_PER_SEC + time.tv_nsec / NSEC_PER_USEC;
86 }
87
RecordTraceEvent(TraceEvent & event)88 void Tracing::RecordTraceEvent(TraceEvent &event)
89 {
90 bufferSize_ += event.GetEventBufferSize();
91
92 std::lock_guard<std::mutex> lock(lock_);
93 traceEvents_->emplace_back(event);
94 }
95
TraceEventRecordTracingStart()96 void Tracing::TraceEventRecordTracingStart()
97 {
98 int64_t ts = GetTimeStamp();
99 std::string args = "{\"data\":{\"frameTreeNodeId\":1,\"frames\":[{";
100 args += "\"frame\":\"0\",\"name\":\"\",";
101 args += "\"processId\":" + std::to_string(getpid()) + ",";
102 args += "\"url\":\"https://url not exist/\"}],";
103 args += "\"persistentIds\":true}}";
104
105 TraceEvent event("disabled-by-default-devtools.timeline", "TracingStartedInBrowser", "I", getpid(), tid_);
106 event.SetTs(ts);
107 event.SetTts(ts);
108 event.SetS("t");
109 event.SetArgs(args);
110
111 RecordTraceEvent(event);
112 }
113
TraceEventRecordMemory()114 void Tracing::TraceEventRecordMemory()
115 {
116 if (!isTracing_) {
117 return;
118 }
119
120 int64_t ts = GetTimeStamp();
121 TraceEvent event("disabled-by-default-devtools.timeline", "UpdateCounters", "I", getpid(), tid_);
122 event.SetTs(ts);
123 event.SetTts(ts);
124 event.SetS("t");
125 std::string args = "{\"data\":{\"jsHeapSizeUsed\":" + std::to_string(DFXJSNApi::GetHeapUsedSize(vm_)) + "}}";
126 event.SetArgs(args);
127
128 RecordTraceEvent(event);
129 }
130
TraceEventRecordCpuProfilerStart(struct ProfileInfo * profileInfo)131 void Tracing::TraceEventRecordCpuProfilerStart(struct ProfileInfo* profileInfo)
132 {
133 int64_t ts = GetTimeStamp();
134 std::string args = "{\"data\":{\"startTime\":" + std::to_string(profileInfo->startTime) + "}}";
135 TraceEvent event("disabled-by-default-v8.cpu_profiler", "Profile", "P", getpid(), tid_);
136 event.SetTs(ts);
137 event.SetTts(ts);
138 event.SetId("0x1");
139 event.SetArgs(args);
140
141 bufferSize_ += event.GetEventBufferSize();
142 traceEventsCpuProfiler_->emplace_back(event);
143 }
144
TraceEventRecordCpuProfiler(struct ProfileInfo * profileInfo,int & nodePos,uint32_t & samplePos)145 void Tracing::TraceEventRecordCpuProfiler(struct ProfileInfo* profileInfo, int &nodePos, uint32_t &samplePos)
146 {
147 if (!isTracing_) {
148 return;
149 }
150
151 int64_t ts = GetTimeStamp();
152 TraceEvent event("disabled-by-default-v8.cpu_profiler", "ProfileChunk", "P", getpid(), tid_);
153 event.SetTs(ts);
154 event.SetTts(ts);
155 event.SetId("0x1");
156
157 struct TraceEventCpuProfileArg args;
158 if (profileInfo->nodeCount > nodePos) {
159 for (int i = nodePos; i < profileInfo->nodeCount; ++i) {
160 args.nodes.emplace_back(profileInfo->nodes[i].id);
161 }
162 nodePos = profileInfo->nodeCount;
163 }
164
165 std::copy(profileInfo->samples.begin() + samplePos, profileInfo->samples.end(),
166 std::back_inserter(args.samples));
167 std::copy(profileInfo->timeDeltas.begin() + samplePos, profileInfo->timeDeltas.end(),
168 std::back_inserter(args.timeDeltas));
169 samplePos = profileInfo->samples.size();
170
171 event.SetCpuProfileArgs(args);
172
173 bufferSize_ += event.GetEventBufferSize();
174 traceEventsCpuProfiler_->emplace_back(event);
175 }
176
TraceEventUpdateCpuProfiler(struct ProfileInfo * profileInfo)177 void Tracing::TraceEventUpdateCpuProfiler(struct ProfileInfo *profileInfo)
178 {
179 for (auto &event : *traceEventsCpuProfiler_) {
180 if (!event.cpuProfileArgs_.has_value()) {
181 traceEvents_->emplace_back(event);
182 continue;
183 }
184
185 struct TraceEventCpuProfileArg &cpuProfileArg = event.cpuProfileArgs_.value();
186 std::string args = "{\"data\":{\"cpuProfile\":{";
187 // nodes
188 if (cpuProfileArg.nodes.size() > 0) {
189 args += "\"nodes\": [";
190 for (auto &nodeId : cpuProfileArg.nodes) {
191 struct CpuProfileNode &nodeInfo = profileInfo->nodes[nodeId - 1];
192 args += "{\"callFrame\":{\"codeType\":\"JS\",";
193 if (nodeInfo.codeEntry.columnNumber != -1) {
194 args += "\"columnNumber\":" + std::to_string(nodeInfo.codeEntry.columnNumber) + ",";
195 }
196 args += "\"functionName\":\"" + nodeInfo.codeEntry.functionName + "\",";
197 if (nodeInfo.codeEntry.lineNumber != -1) {
198 args += "\"lineNumber\":" + std::to_string(nodeInfo.codeEntry.lineNumber) + ",";
199 }
200 args += "\"scriptId\":" + std::to_string(nodeInfo.codeEntry.scriptId) + ",";
201 if (nodeInfo.codeEntry.scriptId != 0) {
202 args += "\"url\":\"" + nodeInfo.codeEntry.url + "\"";
203 } else {
204 args.pop_back();
205 }
206 args += "},";
207 args += "\"id\":" + std::to_string(nodeInfo.id) + ",";
208 if (nodeInfo.parentId != 0) {
209 args += "\"parent\":" + std::to_string(nodeInfo.parentId) + ",";
210 }
211 args += "\"hitCount\":" + std::to_string(nodeInfo.hitCount) + ",";
212 args += "\"children\":[";
213 for (auto &it : nodeInfo.children) {
214 args += std::to_string(it) + ",";
215 }
216 if (nodeInfo.children.size() != 0) {
217 args.pop_back();
218 }
219 args += "]},";
220 }
221 args.pop_back();
222 args += "],";
223 }
224
225 // samples
226 args += "\"samples\": [";
227 for (auto sample : cpuProfileArg.samples) {
228 args += std::to_string(sample) + ",";
229 }
230 args.pop_back();
231 args += "]},";
232
233 // lines
234 args += "\"lines\": [";
235 for (auto sample : cpuProfileArg.samples) {
236 args += std::to_string(profileInfo->nodes[profileInfo->samples[sample - 1]].codeEntry.lineNumber + 1) + ",";
237 }
238 args.pop_back();
239 args += "],";
240
241 // timeDeltas
242 args += "\"timeDeltas\": [";
243 for (auto timeDelta : cpuProfileArg.timeDeltas) {
244 args += std::to_string(timeDelta) + ",";
245 }
246 args.pop_back();
247 args += "]}}";
248
249 event.SetArgs(args);
250 traceEvents_->emplace_back(event);
251 }
252 }
253
TraceEventRecordCpuProfilerEnd(struct ProfileInfo * profileInfo)254 void Tracing::TraceEventRecordCpuProfilerEnd(struct ProfileInfo* profileInfo)
255 {
256 int64_t ts = GetTimeStamp();
257 std::string args = "{\"data\":{\"endTime\":" + std::to_string(profileInfo->stopTime) + "}}";
258 TraceEvent event("disabled-by-default-v8.cpu_profiler", "ProfileChunk", "P", getpid(), tid_);
259 event.SetTs(ts);
260 event.SetTts(ts);
261 event.SetId("0x1");
262 event.SetArgs(args);
263
264 bufferSize_ += event.GetEventBufferSize();
265 traceEventsCpuProfiler_->emplace_back(event);
266 }
267
GetBufferUseage(double & percentFull,uint32_t & eventCount,double & value)268 void Tracing::GetBufferUseage(double &percentFull, uint32_t &eventCount, double &value)
269 {
270 percentFull = (bufferSize_ >= maxBufferSize_) ? 1.0 : static_cast<double>(bufferSize_) / maxBufferSize_;
271 eventCount = 0;
272 value = percentFull;
273 }
274
LoadModule(std::string_view name,std::string_view)275 void Tracing::LoadModule([[maybe_unused]] std::string_view name, [[maybe_unused]] std::string_view)
276 {
277 return;
278 }
BytecodePcChanged(JSThread * thread,JSHandle<Method> method,uint32_t bcOffset)279 void Tracing::BytecodePcChanged([[maybe_unused]] JSThread *thread, [[maybe_unused]] JSHandle<Method> method,
280 [[maybe_unused]] uint32_t bcOffset)
281 {
282 return;
283 }
HandleDebuggerStmt(JSHandle<Method> method,uint32_t bcOffset)284 bool Tracing::HandleDebuggerStmt([[maybe_unused]] JSHandle<Method> method, [[maybe_unused]] uint32_t bcOffset)
285 {
286 return true;
287 }
VmStart()288 void Tracing::VmStart()
289 {
290 return;
291 }
VmDeath()292 void Tracing::VmDeath()
293 {
294 return;
295 }
NativeCalling(const void * nativeAddress)296 void Tracing::NativeCalling([[maybe_unused]] const void *nativeAddress)
297 {
298 return;
299 }
NativeReturn(const void * nativeAddress)300 void Tracing::NativeReturn([[maybe_unused]] const void *nativeAddress)
301 {
302 return;
303 }
MethodEntry(JSHandle<Method> method,JSHandle<JSTaggedValue> envHandle)304 void Tracing::MethodEntry([[maybe_unused]] JSHandle<Method> method, [[maybe_unused]] JSHandle<JSTaggedValue> envHandle)
305 {
306 return;
307 }
MethodExit(JSHandle<Method> method)308 void Tracing::MethodExit([[maybe_unused]] JSHandle<Method> method)
309 {
310 TraceEventRecordMemory();
311 return;
312 }
313 } // namespace panda::ecmascript
314