Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: src/libplatform/tracing/trace-writer.cc

Issue 2190973003: [Tracing] V8 Tracing Controller - Add args and copy support (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Use V8_INLINE static for non-class functions Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 the V8 project authors. All rights reserved. 1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/libplatform/tracing/trace-writer.h" 5 #include "src/libplatform/tracing/trace-writer.h"
6 6
7 #include <cmath>
8
9 #include "base/trace_event/common/trace_event_common.h"
7 #include "src/base/platform/platform.h" 10 #include "src/base/platform/platform.h"
8 11
9 namespace v8 { 12 namespace v8 {
10 namespace platform { 13 namespace platform {
11 namespace tracing { 14 namespace tracing {
12 15
16 // Currently we do not support JSON-escaping strings in trace arguments.
17 // Thus we perform an IsJSONString() check before writing any string argument.
18 // In particular, this means strings cannot have control characters or " or \.
19 V8_INLINE static bool IsJSONString(const char* str) {
20 size_t len = strlen(str);
21 for (size_t i = 0; i < len; ++i) {
22 if (iscntrl(str[i]) || str[i] == '\"' || str[i] == '\\') {
23 return false;
24 }
25 }
26 return true;
27 }
28
29 void JSONTraceWriter::AppendArgValue(uint8_t type,
30 TraceObject::ArgValue value) {
31 switch (type) {
32 case TRACE_VALUE_TYPE_BOOL:
33 stream_ << (value.as_bool ? "true" : "false");
34 break;
35 case TRACE_VALUE_TYPE_UINT:
36 stream_ << value.as_uint;
37 break;
38 case TRACE_VALUE_TYPE_INT:
39 stream_ << value.as_int;
40 break;
41 case TRACE_VALUE_TYPE_DOUBLE: {
42 std::string real;
43 double val = value.as_double;
44 if (std::isfinite(val)) {
45 std::ostringstream convert_stream;
46 convert_stream << val;
47 real = convert_stream.str();
48 // Ensure that the number has a .0 if there's no decimal or 'e'. This
49 // makes sure that when we read the JSON back, it's interpreted as a
50 // real rather than an int.
51 if (real.find('.') == std::string::npos &&
52 real.find('e') == std::string::npos &&
53 real.find('E') == std::string::npos) {
54 real += ".0";
55 }
56 } else if (std::isnan(val)) {
57 // The JSON spec doesn't allow NaN and Infinity (since these are
58 // objects in EcmaScript). Use strings instead.
59 real = "\"NaN\"";
60 } else if (val < 0) {
61 real = "\"-Infinity\"";
62 } else {
63 real = "\"Infinity\"";
64 }
65 stream_ << real;
66 break;
67 }
68 case TRACE_VALUE_TYPE_POINTER:
69 // JSON only supports double and int numbers.
70 // So as not to lose bits from a 64-bit pointer, output as a hex string.
71 stream_ << "\"" << value.as_pointer << "\"";
72 break;
73 case TRACE_VALUE_TYPE_STRING:
74 case TRACE_VALUE_TYPE_COPY_STRING:
75 // Strings are currently not JSON-escaped, so we need to perform a check
76 // to see if they are valid JSON strings.
77 CHECK(value.as_string == nullptr || IsJSONString(value.as_string));
78 stream_ << "\"" << (value.as_string ? value.as_string : "NULL") << "\"";
79 break;
80 default:
81 UNREACHABLE();
82 break;
83 }
84 }
85
13 JSONTraceWriter::JSONTraceWriter(std::ostream& stream) : stream_(stream) { 86 JSONTraceWriter::JSONTraceWriter(std::ostream& stream) : stream_(stream) {
14 stream_ << "{\"traceEvents\":["; 87 stream_ << "{\"traceEvents\":[";
15 } 88 }
16 89
17 JSONTraceWriter::~JSONTraceWriter() { stream_ << "]}"; } 90 JSONTraceWriter::~JSONTraceWriter() { stream_ << "]}"; }
18 91
19 void JSONTraceWriter::AppendTraceEvent(TraceObject* trace_event) { 92 void JSONTraceWriter::AppendTraceEvent(TraceObject* trace_event) {
20 if (append_comma_) stream_ << ","; 93 if (append_comma_) stream_ << ",";
21 append_comma_ = true; 94 append_comma_ = true;
22 if (trace_event->scope() == NULL) { 95 stream_ << "{\"pid\":" << trace_event->pid()
23 stream_ << "{\"pid\":" << trace_event->pid() 96 << ",\"tid\":" << trace_event->tid()
24 << ",\"tid\":" << trace_event->tid() 97 << ",\"ts\":" << trace_event->ts()
25 << ",\"ts\":" << trace_event->ts() 98 << ",\"tts\":" << trace_event->tts() << ",\"ph\":\""
26 << ",\"tts\":" << trace_event->tts() << ",\"ph\":\"" 99 << trace_event->phase() << "\",\"cat\":\""
27 << trace_event->phase() << "\",\"cat\":\"" 100 << TracingController::GetCategoryGroupName(
28 << TracingController::GetCategoryGroupName( 101 trace_event->category_enabled_flag())
29 trace_event->category_enabled_flag()) 102 << "\",\"name\":\"" << trace_event->name()
30 << "\",\"name\":\"" << trace_event->name() 103 << "\",\"dur\":" << trace_event->duration()
31 << "\",\"args\":{},\"dur\":" << trace_event->duration() 104 << ",\"tdur\":" << trace_event->cpu_duration();
32 << ",\"tdur\":" << trace_event->cpu_duration() << "}"; 105 if (trace_event->flags() & TRACE_EVENT_FLAG_HAS_ID) {
33 } else { 106 if (trace_event->scope() != nullptr) {
34 stream_ << "{\"pid\":" << trace_event->pid() 107 stream_ << ",\"scope\":\"" << trace_event->scope() << "\"";
35 << ",\"tid\":" << trace_event->tid() 108 }
36 << ",\"ts\":" << trace_event->ts() 109 // So as not to lose bits from a 64-bit integer, output as a hex string.
37 << ",\"tts\":" << trace_event->tts() << ",\"ph\":\"" 110 stream_ << ",\"id\":\"0x" << std::hex << trace_event->id() << "\"";
38 << trace_event->phase() << "\",\"cat\":\""
39 << TracingController::GetCategoryGroupName(
40 trace_event->category_enabled_flag())
41 << "\",\"name\":\"" << trace_event->name() << "\",\"scope\":\""
42 << trace_event->scope()
43 << "\",\"args\":{},\"dur\":" << trace_event->duration()
44 << ",\"tdur\":" << trace_event->cpu_duration() << "}";
45 } 111 }
112 stream_ << ",\"args\":{";
113 const char** arg_names = trace_event->arg_names();
114 const uint8_t* arg_types = trace_event->arg_types();
115 TraceObject::ArgValue* arg_values = trace_event->arg_values();
116 for (int i = 0; i < trace_event->num_args(); ++i) {
117 if (i > 0) stream_ << ",";
118 stream_ << "\"" << arg_names[i] << "\":";
119 AppendArgValue(arg_types[i], arg_values[i]);
120 }
121 stream_ << "}}";
46 // TODO(fmeawad): Add support for Flow Events. 122 // TODO(fmeawad): Add support for Flow Events.
47 } 123 }
48 124
49 void JSONTraceWriter::Flush() {} 125 void JSONTraceWriter::Flush() {}
50 126
51 TraceWriter* TraceWriter::CreateJSONTraceWriter(std::ostream& stream) { 127 TraceWriter* TraceWriter::CreateJSONTraceWriter(std::ostream& stream) {
52 return new JSONTraceWriter(stream); 128 return new JSONTraceWriter(stream);
53 } 129 }
54 130
55 } // namespace tracing 131 } // namespace tracing
56 } // namespace platform 132 } // namespace platform
57 } // namespace v8 133 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698