|
6 | 6 | // Copyright © 2023. Progress. All rights reserved. |
7 | 7 | // |
8 | 8 |
|
9 | | -//#include <iostream> |
10 | | -//#include <vector> |
11 | | -//#include <string> |
| 9 | +// #include <iostream> |
| 10 | +// #include <vector> |
| 11 | +// #include <string> |
12 | 12 | #include <sstream> |
13 | 13 |
|
14 | | -#include "ns-v8-tracing-agent-impl.h" |
15 | | -#include "Runtime.h" |
16 | 14 | #include "Helpers.h" |
| 15 | +#include "Runtime.h" |
| 16 | +#include "ns-v8-tracing-agent-impl.h" |
17 | 17 |
|
18 | 18 | namespace tns { |
19 | 19 | namespace inspector { |
20 | 20 |
|
21 | 21 | using v8::platform::tracing::TraceBuffer; |
22 | 22 | using v8::platform::tracing::TraceBufferChunk; |
23 | | -using v8::platform::tracing::TraceWriter; |
24 | | -using v8::platform::tracing::TraceObject; |
25 | | -using v8::platform::tracing::TracingController; |
26 | 23 | using v8::platform::tracing::TraceConfig; |
| 24 | +using v8::platform::tracing::TraceObject; |
27 | 25 | using v8::platform::tracing::TraceRecordMode; |
| 26 | +using v8::platform::tracing::TraceWriter; |
| 27 | +using v8::platform::tracing::TracingController; |
28 | 28 |
|
29 | | -void NSInMemoryTraceWriter::AppendTraceEvent(TraceObject *trace_event) { |
30 | | - if(total_traces_ == 0) { |
31 | | - // create a v8 JSON trace writer |
32 | | - json_trace_writer_.reset(TraceWriter::CreateJSONTraceWriter(stream_)); |
33 | | - } |
34 | | - |
35 | | - ++total_traces_; |
36 | | - json_trace_writer_->AppendTraceEvent(trace_event); |
| 29 | +int traces_per_chunk_ = 20; |
| 30 | + |
| 31 | +void NSInMemoryTraceWriter::AppendTraceEvent(TraceObject* trace_event) { |
| 32 | + MaybeCreateChunk(); |
| 33 | + |
| 34 | + json_trace_writer_->AppendTraceEvent(trace_event); |
| 35 | + total_traces_++; |
| 36 | + if (total_traces_ > 0 && (total_traces_ % traces_per_chunk_ == 0)) { |
| 37 | + MaybeFinalizeChunk(); |
| 38 | + } |
37 | 39 | } |
38 | 40 |
|
39 | | -void NSInMemoryTraceWriter::Flush() { |
40 | | - if(json_trace_writer_.get() != nullptr) { |
41 | | - json_trace_writer_->Flush(); |
42 | | - } |
| 41 | +void NSInMemoryTraceWriter::MaybeCreateChunk() { |
| 42 | + if (json_trace_writer_.get() != nullptr) { |
| 43 | + return; |
| 44 | + } |
| 45 | + stream_.str(prefix_); |
| 46 | + stream_.seekp(0, std::ios::end); |
| 47 | + // create a v8 JSON trace writer |
| 48 | + json_trace_writer_.reset(TraceWriter::CreateJSONTraceWriter(stream_, "value")); |
43 | 49 | } |
44 | 50 |
|
45 | | -std::string NSInMemoryTraceWriter::getTrace() { |
46 | | - if(total_traces_ == 0) { |
47 | | - return ""; |
48 | | - } |
49 | | - |
50 | | - // ensures the trace json is closed |
51 | | - json_trace_writer_.reset(); |
52 | | - std::string str = stream_.str(); |
53 | | - |
54 | | - // clear stream? |
55 | | - stream_.str(""); |
56 | | - stream_.clear(); |
57 | | - |
58 | | - return str; |
| 51 | +void NSInMemoryTraceWriter::MaybeFinalizeChunk() { |
| 52 | + if (json_trace_writer_.get() == nullptr) { |
| 53 | + return; |
| 54 | + } |
| 55 | + json_trace_writer_.reset(); |
| 56 | + stream_ << suffix_; |
| 57 | + traces_.push_back(stream_.str()); |
| 58 | + stream_.str(""); |
| 59 | +} |
| 60 | + |
| 61 | +void NSInMemoryTraceWriter::Flush() { |
| 62 | + if (json_trace_writer_.get() != nullptr) { |
| 63 | + json_trace_writer_->Flush(); |
| 64 | + } |
59 | 65 | } |
60 | 66 |
|
| 67 | +std::vector<std::string> NSInMemoryTraceWriter::getTrace() { |
| 68 | + MaybeFinalizeChunk(); |
| 69 | + return std::move(traces_); |
| 70 | +} |
61 | 71 |
|
62 | 72 | TracingAgentImpl::TracingAgentImpl() { |
63 | | - tracing_controller_ = reinterpret_cast<TracingController*>(tns::Runtime::GetPlatform()->GetTracingController()); |
| 73 | + tracing_controller_ = |
| 74 | + reinterpret_cast<TracingController*>(tns::Runtime::GetPlatform()->GetTracingController()); |
64 | 75 | } |
65 | 76 |
|
66 | 77 | bool TracingAgentImpl::start() { |
67 | | - if(!tracing_) { |
68 | | - tracing_ = true; |
69 | | - |
70 | | - // start tracing... |
71 | | - current_trace_writer_ = new NSInMemoryTraceWriter(); |
72 | | - tracing_controller_->Initialize( |
73 | | - TraceBuffer::CreateTraceBufferRingBuffer( |
74 | | - TraceBuffer::kRingBufferChunks, |
75 | | - current_trace_writer_ |
76 | | - ) |
77 | | - ); |
78 | | - // todo: create TraceConfig based on params. |
79 | | - TraceConfig* config = TraceConfig::CreateDefaultTraceConfig(); |
80 | | - config->AddIncludedCategory("disabled-by-default-v8.cpu_profiler"); |
81 | | - config->SetTraceRecordMode(TraceRecordMode::RECORD_CONTINUOUSLY); |
82 | | - tracing_controller_->StartTracing(config); |
83 | | - } |
84 | | - |
85 | | - return true; |
| 78 | + if (!tracing_) { |
| 79 | + tracing_ = true; |
| 80 | + |
| 81 | + // start tracing... |
| 82 | + current_trace_writer_ = |
| 83 | + new NSInMemoryTraceWriter(R"({"method": "Tracing.dataCollected", "params":)", "}"); |
| 84 | + tracing_controller_->Initialize(TraceBuffer::CreateTraceBufferRingBuffer( |
| 85 | + TraceBuffer::kRingBufferChunks, current_trace_writer_)); |
| 86 | + // todo: create TraceConfig based on params. |
| 87 | + TraceConfig* config = TraceConfig::CreateDefaultTraceConfig(); |
| 88 | + config->AddIncludedCategory("disabled-by-default-v8.cpu_profiler"); |
| 89 | + config->SetTraceRecordMode(TraceRecordMode::RECORD_CONTINUOUSLY); |
| 90 | + tracing_controller_->StartTracing(config); |
| 91 | + } |
| 92 | + |
| 93 | + return true; |
86 | 94 | } |
87 | 95 |
|
88 | 96 | bool TracingAgentImpl::end() { |
89 | | - if(tracing_) { |
90 | | - tracing_controller_->StopTracing(); |
91 | | - |
92 | | - if(current_trace_writer_ != nullptr) { |
93 | | - // store last trace on the agent. |
94 | | - lastTrace_ = current_trace_writer_->getTrace(); |
95 | | - current_trace_writer_ = nullptr; |
96 | | - } |
97 | | - tracing_controller_->Initialize(nullptr); |
98 | | - |
99 | | - tracing_ = false; |
| 97 | + if (tracing_) { |
| 98 | + tracing_controller_->StopTracing(); |
| 99 | + |
| 100 | + if (current_trace_writer_ != nullptr) { |
| 101 | + // store last trace on the agent. |
| 102 | + lastTrace_ = current_trace_writer_->getTrace(); |
| 103 | + if (lastTrace_.size() > 0) { |
| 104 | + lastTrace_.push_back( |
| 105 | + R"({"method": "Tracing.tracingComplete", "params": {"dataLossOccurred": false}})"); |
| 106 | + } |
| 107 | + |
| 108 | + current_trace_writer_ = nullptr; |
100 | 109 | } |
101 | | - |
102 | | - return true; |
103 | | -} |
| 110 | + tracing_controller_->Initialize(nullptr); |
104 | 111 |
|
105 | | -void TracingAgentImpl::SendToDevtools(v8::Local<v8::Context> context, std::string jsonData) { |
106 | | - std::string source = R"( |
107 | | - (() => { |
108 | | - function processTraceData(traceJSON) { |
109 | | - |
110 | | - try { |
111 | | - // handle empty traces (e.g. when the user cancels the trace immediately) |
112 | | - if(!traceJSON || traceJSON.trim().length === 0) { |
113 | | - traceJSON = "{}"; |
114 | | - } |
115 | | - const data = JSON.parse(traceJSON); |
116 | | -
|
117 | | - for (let i = 0; i < data.traceEvents?.length; i = i + 20) { |
118 | | - const chunk = data.traceEvents.slice(i, i + 20); |
119 | | -
|
120 | | - __inspectorSendEvent( |
121 | | - JSON.stringify({ |
122 | | - method: "Tracing.dataCollected", |
123 | | - params: { |
124 | | - value: chunk, |
125 | | - }, |
126 | | - }) |
127 | | - ); |
128 | | - } |
129 | | -
|
130 | | - __inspectorSendEvent( |
131 | | - JSON.stringify({ |
132 | | - method: "Tracing.tracingComplete", |
133 | | - params: { |
134 | | - dataLossOccurred: false, |
135 | | - }, |
136 | | - }) |
137 | | - ); |
138 | | - } catch (err) { |
139 | | - console.log("[Inspector] Failed to parse trace data", err, traceJSON); |
140 | | - } |
141 | | - } |
142 | | - return processTraceData; |
143 | | - })(); |
144 | | - )"; |
145 | | - |
146 | | - v8::Isolate* isolate = context->GetIsolate(); |
147 | | - v8::Locker locker(isolate); |
148 | | - v8::Isolate::Scope isolate_scope(isolate); |
149 | | - v8::HandleScope handle_scope(isolate); |
150 | | - |
151 | | - v8::Local<v8::Script> script; |
152 | | - bool success = v8::Script::Compile(context, tns::ToV8String(isolate, source)).ToLocal(&script); |
153 | | - tns::Assert(success && !script.IsEmpty(), isolate); |
154 | | - |
155 | | - v8::Local<v8::Value> result; |
156 | | - success = script->Run(context).ToLocal(&result); |
157 | | - tns::Assert(success, isolate); |
158 | | - |
159 | | - v8::Local<v8::Function> processTraceData = result.As<v8::Function>(); |
160 | | - |
161 | | - v8::Local<v8::Value> args[1] = { tns::ToV8String(isolate, jsonData) }; |
162 | | - success = processTraceData->Call(context, processTraceData, 1, args).ToLocal(&result); |
163 | | - tns::Assert(success, isolate); |
164 | | -} |
| 112 | + tracing_ = false; |
| 113 | + } |
165 | 114 |
|
| 115 | + return true; |
| 116 | +} |
166 | 117 |
|
167 | | -} // namespace inspector |
168 | | -} // namespace tns |
| 118 | +} // namespace inspector |
| 119 | +} // namespace tns |
0 commit comments