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