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" | |
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 size_t GetAllocLength(const char* str) { return str ? strlen(str) + 1 : 0; } | |
lpy
2016/08/02 00:07:09
V8_INLINE, maybe also static? I am not sure if sta
rskang
2016/08/02 00:46:55
Done.
| |
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 for (int i = 0; i < num_args_; ++i) { | |
52 arg_names_[i] = arg_names[i]; | |
53 arg_values_[i].as_uint = arg_values[i]; | |
54 arg_types_[i] = arg_types[i]; | |
55 } | |
56 | |
57 bool copy = !!(flags & TRACE_EVENT_FLAG_COPY); | |
58 // Allocate a long string to fit all string copies. | |
59 size_t alloc_size = 0; | |
60 if (copy) { | |
61 alloc_size += GetAllocLength(name) + GetAllocLength(scope); | |
62 for (int i = 0; i < num_args_; ++i) { | |
63 alloc_size += GetAllocLength(arg_names_[i]); | |
64 if (arg_types_[i] == TRACE_VALUE_TYPE_STRING) | |
65 arg_types_[i] = TRACE_VALUE_TYPE_COPY_STRING; | |
66 } | |
67 } | |
68 | |
69 bool arg_is_copy[kTraceMaxNumArgs]; | |
70 for (int i = 0; i < num_args_; ++i) { | |
71 // We only take a copy of arg_vals if they are of type COPY_STRING. | |
72 arg_is_copy[i] = (arg_types_[i] == TRACE_VALUE_TYPE_COPY_STRING); | |
73 if (arg_is_copy[i]) alloc_size += GetAllocLength(arg_values_[i].as_string); | |
74 } | |
75 | |
76 if (alloc_size) { | |
77 // Since TraceObject can be initialized multiple times, we might need | |
78 // to free old memory. | |
79 delete[] parameter_copy_storage_; | |
80 char* ptr = parameter_copy_storage_ = new char[alloc_size]; | |
81 const char* end = ptr + alloc_size; | |
82 if (copy) { | |
83 CopyTraceObjectParameter(&ptr, &name_, end); | |
84 CopyTraceObjectParameter(&ptr, &scope_, end); | |
85 for (int i = 0; i < num_args_; ++i) { | |
86 CopyTraceObjectParameter(&ptr, &arg_names_[i], end); | |
87 } | |
88 } | |
89 for (int i = 0; i < num_args_; ++i) { | |
90 if (arg_is_copy[i]) { | |
91 CopyTraceObjectParameter(&ptr, &arg_values_[i].as_string, end); | |
92 } | |
93 } | |
94 } | |
33 } | 95 } |
34 | 96 |
97 TraceObject::~TraceObject() { delete[] parameter_copy_storage_; } | |
98 | |
35 void TraceObject::UpdateDuration() { | 99 void TraceObject::UpdateDuration() { |
36 duration_ = base::TimeTicks::HighResolutionNow().ToInternalValue() - ts_; | 100 duration_ = base::TimeTicks::HighResolutionNow().ToInternalValue() - ts_; |
37 cpu_duration_ = base::ThreadTicks::Now().ToInternalValue() - tts_; | 101 cpu_duration_ = base::ThreadTicks::Now().ToInternalValue() - tts_; |
38 } | 102 } |
39 | 103 |
40 void TraceObject::InitializeForTesting( | 104 void TraceObject::InitializeForTesting( |
41 char phase, const uint8_t* category_enabled_flag, const char* name, | 105 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, | 106 const char* scope, uint64_t id, uint64_t bind_id, int num_args, |
43 const char** arg_names, const uint8_t* arg_types, | 107 const char** arg_names, const uint8_t* arg_types, |
44 const uint64_t* arg_values, unsigned int flags, int pid, int tid, | 108 const uint64_t* arg_values, unsigned int flags, int pid, int tid, |
(...skipping 10 matching lines...) Expand all Loading... | |
55 flags_ = flags; | 119 flags_ = flags; |
56 ts_ = ts; | 120 ts_ = ts; |
57 tts_ = tts; | 121 tts_ = tts; |
58 duration_ = duration; | 122 duration_ = duration; |
59 cpu_duration_ = cpu_duration; | 123 cpu_duration_ = cpu_duration; |
60 } | 124 } |
61 | 125 |
62 } // namespace tracing | 126 } // namespace tracing |
63 } // namespace platform | 127 } // namespace platform |
64 } // namespace v8 | 128 } // namespace v8 |
OLD | NEW |