OLD | NEW |
---|---|
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 "include/libplatform/v8-tracing.h" | 5 #include "include/libplatform/v8-tracing.h" |
6 | 6 |
7 #include "src/base/platform/platform.h" | 7 #include "src/base/platform/platform.h" |
8 #include "src/base/platform/time.h" | 8 #include "src/base/platform/time.h" |
9 #include "src/tracing/trace-event.h" | |
lpy
2016/07/28 21:18:20
I understand you need to use variables in the head
| |
9 | 10 |
10 namespace v8 { | 11 namespace v8 { |
11 namespace platform { | 12 namespace platform { |
12 namespace tracing { | 13 namespace tracing { |
13 | 14 |
15 // We perform checks for NULL strings since it is possible that a string arg | |
16 // value is NULL. | |
17 size_t GetAllocLength(const char* str) { return str ? strlen(str) + 1 : 0; } | |
18 | |
19 // Copies |*member| into |*buffer|, sets |*member| to point to this new | |
20 // location, and then advances |*buffer| by the amount written. | |
21 void CopyTraceObjectParameter(char** buffer, const char** member, | |
22 const char* end) { | |
23 if (*member) { | |
24 strncpy(*buffer, *member, end - *buffer); | |
25 *member = *buffer; | |
26 *buffer += strlen(*member) + 1; | |
27 } | |
28 } | |
29 | |
14 void TraceObject::Initialize(char phase, const uint8_t* category_enabled_flag, | 30 void TraceObject::Initialize(char phase, const uint8_t* category_enabled_flag, |
15 const char* name, const char* scope, uint64_t id, | 31 const char* name, const char* scope, uint64_t id, |
16 uint64_t bind_id, int num_args, | 32 uint64_t bind_id, int num_args, |
17 const char** arg_names, const uint8_t* arg_types, | 33 const char** arg_names, const uint8_t* arg_types, |
18 const uint64_t* arg_values, unsigned int flags) { | 34 const uint64_t* arg_values, unsigned int flags) { |
19 pid_ = base::OS::GetCurrentProcessId(); | 35 pid_ = base::OS::GetCurrentProcessId(); |
20 tid_ = base::OS::GetCurrentThreadId(); | 36 tid_ = base::OS::GetCurrentThreadId(); |
21 phase_ = phase; | 37 phase_ = phase; |
22 category_enabled_flag_ = category_enabled_flag; | 38 category_enabled_flag_ = category_enabled_flag; |
23 name_ = name; | 39 name_ = name; |
24 scope_ = scope; | 40 scope_ = scope; |
25 id_ = id; | 41 id_ = id; |
26 bind_id_ = bind_id; | 42 bind_id_ = bind_id; |
27 num_args_ = num_args; | |
28 flags_ = flags; | 43 flags_ = flags; |
29 ts_ = base::TimeTicks::HighResolutionNow().ToInternalValue(); | 44 ts_ = base::TimeTicks::HighResolutionNow().ToInternalValue(); |
30 tts_ = base::ThreadTicks::Now().ToInternalValue(); | 45 tts_ = base::ThreadTicks::Now().ToInternalValue(); |
31 duration_ = 0; | 46 duration_ = 0; |
32 cpu_duration_ = 0; | 47 cpu_duration_ = 0; |
48 | |
49 // Clamp num_args since it may have been set by a third-party library. | |
50 num_args = (num_args > kTraceMaxNumArgs) ? kTraceMaxNumArgs : num_args; | |
51 int i = 0; | |
52 for (; i < num_args; ++i) { | |
53 arg_names_[i] = arg_names[i]; | |
54 arg_values_[i].as_uint = arg_values[i]; | |
55 arg_types_[i] = arg_types[i]; | |
56 } | |
57 // The NULL pointers tell us that these arguments are not used. | |
58 for (; i < kTraceMaxNumArgs; ++i) { | |
59 arg_names_[i] = NULL; | |
60 } | |
61 | |
62 bool copy = !!(flags & TRACE_EVENT_FLAG_COPY); | |
63 // Allocate a long string to fit all string copies. | |
64 size_t alloc_size = 0; | |
65 if (copy) { | |
66 alloc_size += GetAllocLength(name) + GetAllocLength(scope); | |
67 for (i = 0; i < num_args; ++i) { | |
68 alloc_size += GetAllocLength(arg_names_[i]); | |
69 if (arg_types_[i] == TRACE_VALUE_TYPE_STRING) | |
70 arg_types_[i] = TRACE_VALUE_TYPE_COPY_STRING; | |
71 } | |
72 } | |
73 | |
74 bool arg_is_copy[kTraceMaxNumArgs]; | |
75 for (i = 0; i < num_args; ++i) { | |
76 // We only take a copy of arg_vals if they are of type COPY_STRING. | |
77 arg_is_copy[i] = (arg_types_[i] == TRACE_VALUE_TYPE_COPY_STRING); | |
78 if (arg_is_copy[i]) alloc_size += GetAllocLength(arg_values_[i].as_string); | |
79 } | |
80 | |
81 if (alloc_size) { | |
82 // Since TraceObject can be initialized multiple times, we might need | |
83 // to free old memory. | |
84 delete[] parameter_copy_storage_; | |
85 char* ptr = parameter_copy_storage_ = new char[alloc_size]; | |
86 const char* end = ptr + alloc_size; | |
87 if (copy) { | |
88 CopyTraceObjectParameter(&ptr, &name_, end); | |
89 CopyTraceObjectParameter(&ptr, &scope_, end); | |
90 for (i = 0; i < num_args; ++i) { | |
91 CopyTraceObjectParameter(&ptr, &arg_names_[i], end); | |
92 } | |
93 } | |
94 for (i = 0; i < num_args; ++i) { | |
95 if (arg_is_copy[i]) { | |
96 CopyTraceObjectParameter(&ptr, &arg_values_[i].as_string, end); | |
97 } | |
98 } | |
99 } | |
33 } | 100 } |
34 | 101 |
102 TraceObject::~TraceObject() { delete[] parameter_copy_storage_; } | |
103 | |
35 void TraceObject::UpdateDuration() { | 104 void TraceObject::UpdateDuration() { |
36 duration_ = base::TimeTicks::HighResolutionNow().ToInternalValue() - ts_; | 105 duration_ = base::TimeTicks::HighResolutionNow().ToInternalValue() - ts_; |
37 cpu_duration_ = base::ThreadTicks::Now().ToInternalValue() - tts_; | 106 cpu_duration_ = base::ThreadTicks::Now().ToInternalValue() - tts_; |
38 } | 107 } |
39 | 108 |
40 void TraceObject::InitializeForTesting( | 109 void TraceObject::InitializeForTesting( |
41 char phase, const uint8_t* category_enabled_flag, const char* name, | 110 char phase, const uint8_t* category_enabled_flag, const char* name, |
42 const char* scope, uint64_t id, uint64_t bind_id, int num_args, | 111 const char* scope, uint64_t id, uint64_t bind_id, int num_args, |
43 const char** arg_names, const uint8_t* arg_types, | 112 const char** arg_names, const uint8_t* arg_types, |
44 const uint64_t* arg_values, unsigned int flags, int pid, int tid, | 113 const uint64_t* arg_values, unsigned int flags, int pid, int tid, |
45 int64_t ts, int64_t tts, uint64_t duration, uint64_t cpu_duration) { | 114 int64_t ts, int64_t tts, uint64_t duration, uint64_t cpu_duration) { |
46 pid_ = pid; | 115 pid_ = pid; |
47 tid_ = tid; | 116 tid_ = tid; |
48 phase_ = phase; | 117 phase_ = phase; |
49 category_enabled_flag_ = category_enabled_flag; | 118 category_enabled_flag_ = category_enabled_flag; |
50 name_ = name; | 119 name_ = name; |
51 scope_ = scope; | 120 scope_ = scope; |
52 id_ = id; | 121 id_ = id; |
53 bind_id_ = bind_id; | 122 bind_id_ = bind_id; |
54 num_args_ = num_args; | |
55 flags_ = flags; | 123 flags_ = flags; |
56 ts_ = ts; | 124 ts_ = ts; |
57 tts_ = tts; | 125 tts_ = tts; |
58 duration_ = duration; | 126 duration_ = duration; |
59 cpu_duration_ = cpu_duration; | 127 cpu_duration_ = cpu_duration; |
128 | |
129 for (int i = 0; i < kTraceMaxNumArgs; ++i) { | |
130 arg_names_[i] = NULL; | |
131 } | |
60 } | 132 } |
61 | 133 |
62 } // namespace tracing | 134 } // namespace tracing |
63 } // namespace platform | 135 } // namespace platform |
64 } // namespace v8 | 136 } // namespace v8 |
OLD | NEW |