Flutter Engine
The Flutter Engine
Functions
ChromeTracingTracer.cpp File Reference
#include "include/core/SkStream.h"
#include "include/private/base/SkThreadID.h"
#include "src/core/SkOSFile.h"
#include "src/core/SkTraceEvent.h"
#include "src/utils/SkJSONWriter.h"
#include "src/utils/SkOSPath.h"
#include "tools/trace/ChromeTracingTracer.h"
#include <chrono>

Go to the source code of this file.

Functions

static void trace_value_to_json (SkJSONWriter *writer, uint64_t argValue, uint8_t argType, const char *stringTableBase)
 
static void trace_event_to_json (SkJSONWriter *writer, TraceEvent *traceEvent, TraceEventSerializationState *serializationState)
 

Function Documentation

◆ trace_event_to_json()

static void trace_event_to_json ( SkJSONWriter writer,
TraceEvent *  traceEvent,
TraceEventSerializationState *  serializationState 
)
static

Definition at line 196 of file ChromeTracingTracer.cpp.

198 {
199 // We track the original (creation time) "name" of each currently live object, so we can
200 // automatically insert "base_name" fields in object snapshot events.
201 auto baseTypeResolver = &(serializationState->fBaseTypeResolver);
202 if (TRACE_EVENT_PHASE_CREATE_OBJECT == traceEvent->fPhase) {
203 SkASSERT(nullptr == baseTypeResolver->find(traceEvent->fID));
204 baseTypeResolver->set(traceEvent->fID, traceEvent->fName);
205 } else if (TRACE_EVENT_PHASE_DELETE_OBJECT == traceEvent->fPhase) {
206 SkASSERT(nullptr != baseTypeResolver->find(traceEvent->fID));
207 baseTypeResolver->remove(traceEvent->fID);
208 }
209
210 writer->beginObject();
211
212 writer->appendString("ph", &traceEvent->fPhase, 1);
213 writer->appendCString("name", traceEvent->fName);
214 if (0 != traceEvent->fID) {
215 // IDs are (almost) always pointers
216 writer->appendPointer("id", reinterpret_cast<void*>(traceEvent->fID));
217 }
218
219 // Offset timestamps to reduce JSON length, then convert nanoseconds to microseconds
220 // (standard time unit for tracing JSON files).
221 uint64_t relativeTimestamp =
222 static_cast<int64_t>(traceEvent->fClockBegin - serializationState->fClockOffset);
223 writer->appendDouble("ts", static_cast<double>(relativeTimestamp) * 1E-3);
224 if (0 != traceEvent->fClockEnd) {
225 double dur = static_cast<double>(traceEvent->fClockEnd - traceEvent->fClockBegin) * 1E-3;
226 writer->appendDouble("dur", dur);
227 }
228
229 writer->appendS64("tid", serializationState->getShortThreadID(traceEvent->fThreadID));
230 // Trace events *must* include a process ID, but for internal tools this isn't particularly
231 // important (and certainly not worth adding a cross-platform API to get it).
232 writer->appendS32("pid", 0);
233
234 if (traceEvent->fNumArgs) {
235 writer->beginObject("args");
236 const char* stringTable = traceEvent->stringTable();
237 bool addedSnapshot = false;
238 if (TRACE_EVENT_PHASE_SNAPSHOT_OBJECT == traceEvent->fPhase &&
239 baseTypeResolver->find(traceEvent->fID) &&
240 0 != strcmp(*baseTypeResolver->find(traceEvent->fID), traceEvent->fName)) {
241 // Special handling for snapshots where the name differs from creation.
242 writer->beginObject("snapshot");
243 writer->appendCString("base_type", *baseTypeResolver->find(traceEvent->fID));
244 addedSnapshot = true;
245 }
246
247 for (int i = 0; i < traceEvent->fNumArgs; ++i) {
248 const TraceEventArg* arg = traceEvent->args() + i;
249 // TODO: Skip '#'
250 writer->appendName(arg->fArgName);
251
252 if (arg->fArgName && '#' == arg->fArgName[0]) {
253 writer->beginObject();
254 writer->appendName("id_ref");
255 trace_value_to_json(writer, arg->fArgValue, arg->fArgType, stringTable);
256 writer->endObject();
257 } else {
258 trace_value_to_json(writer, arg->fArgValue, arg->fArgType, stringTable);
259 }
260 }
261
262 if (addedSnapshot) {
263 writer->endObject();
264 }
265
266 writer->endObject();
267 }
268
269 writer->endObject();
270}
static void trace_value_to_json(SkJSONWriter *writer, uint64_t argValue, uint8_t argType, const char *stringTableBase)
#define SkASSERT(cond)
Definition: SkAssert.h:116
#define TRACE_EVENT_PHASE_SNAPSHOT_OBJECT
#define TRACE_EVENT_PHASE_CREATE_OBJECT
#define TRACE_EVENT_PHASE_DELETE_OBJECT
void appendS32(int32_t value)
Definition: SkJSONWriter.h:237
void appendDouble(double value)
Definition: SkJSONWriter.h:242
void beginObject(const char *name=nullptr, bool multiline=true)
Definition: SkJSONWriter.h:114
void appendPointer(const void *value)
Definition: SkJSONWriter.h:228
void appendS64(int64_t value)
void endObject()
Definition: SkJSONWriter.h:126
void appendName(const char *name)
Definition: SkJSONWriter.h:90
void appendCString(const char *value)
Definition: SkJSONWriter.h:224
void appendString(const char *value, size_t size)
Definition: SkJSONWriter.h:176

◆ trace_value_to_json()

static void trace_value_to_json ( SkJSONWriter writer,
uint64_t  argValue,
uint8_t  argType,
const char *  stringTableBase 
)
static

Definition at line 150 of file ChromeTracingTracer.cpp.

153 {
154 switch (argType) {
155 case TRACE_VALUE_TYPE_BOOL: writer->appendBool(argValue); break;
156 case TRACE_VALUE_TYPE_UINT: writer->appendU64(argValue); break;
157 case TRACE_VALUE_TYPE_INT: writer->appendS64(static_cast<int64_t>(argValue)); break;
158 case TRACE_VALUE_TYPE_DOUBLE: writer->appendDouble(sk_bit_cast<double>(argValue)); break;
161 break;
164 break;
166 // See addTraceEvent(), the value in _COPY_STRING events is replaced with an offset
167 writer->appendCString(stringTableBase + argValue);
168 break;
169 default: writer->appendNString("<unknown type>"); break;
170 }
171}
#define TRACE_VALUE_TYPE_STRING
#define TRACE_VALUE_TYPE_BOOL
#define TRACE_VALUE_TYPE_POINTER
#define TRACE_VALUE_TYPE_COPY_STRING
#define TRACE_VALUE_TYPE_INT
#define TRACE_VALUE_TYPE_UINT
#define TRACE_VALUE_TYPE_DOUBLE
void appendNString(char const (&value)[N])
Definition: SkJSONWriter.h:220
void appendU64(uint64_t value)
void appendBool(bool value)
Definition: SkJSONWriter.h:229
static const void * TraceValueAsPointer(uint64_t value)
Definition: SkTraceEvent.h:300
static const char * TraceValueAsString(uint64_t value)
Definition: SkTraceEvent.h:296