v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
trace-object.cc
Go to the documentation of this file.
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
3// found in the LICENSE file.
4
10
11namespace v8 {
12namespace platform {
13namespace tracing {
14
15// We perform checks for nullptr strings since it is possible that a string arg
16// value is nullptr.
17V8_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.
23V8_INLINE static void CopyTraceObjectParameter(char** buffer,
24 const char** member) {
25 if (*member == nullptr) return;
26 size_t length = strlen(*member) + 1;
27 memcpy(*buffer, *member, length);
28 *member = *buffer;
29 *buffer += length;
30}
31
33 char phase, const uint8_t* category_enabled_flag, const char* name,
34 const char* scope, uint64_t id, uint64_t bind_id, int num_args,
35 const char** arg_names, const uint8_t* arg_types,
36 const uint64_t* arg_values,
37 std::unique_ptr<v8::ConvertableToTraceFormat>* arg_convertables,
38 unsigned int flags, int64_t timestamp, int64_t cpu_timestamp) {
41 phase_ = phase;
43 name_ = name;
44 scope_ = scope;
45 id_ = id;
47 flags_ = flags;
48 ts_ = timestamp;
49 tts_ = cpu_timestamp;
50 duration_ = 0;
51 cpu_duration_ = 0;
52
53 // Clamp num_args since it may have been set by a third-party library.
55 for (int i = 0; i < num_args_; ++i) {
61 }
62
63 bool copy = !!(flags & TRACE_EVENT_FLAG_COPY);
64 // Allocate a long string to fit all string copies.
65 size_t alloc_size = 0;
66 if (copy) {
67 alloc_size += GetAllocLength(name) + GetAllocLength(scope);
68 for (int i = 0; i < num_args_; ++i) {
69 alloc_size += GetAllocLength(arg_names_[i]);
72 }
73 }
74
75 bool arg_is_copy[kTraceMaxNumArgs];
76 for (int i = 0; i < num_args_; ++i) {
77 // We only take a copy of arg_vals if they are of type COPY_STRING.
78 arg_is_copy[i] = (arg_types_[i] == TRACE_VALUE_TYPE_COPY_STRING);
79 if (arg_is_copy[i]) alloc_size += GetAllocLength(arg_values_[i].as_string);
80 }
81
82 if (alloc_size) {
83 // Since TraceObject can be initialized multiple times, we might need
84 // to free old memory.
86 char* ptr = parameter_copy_storage_ = new char[alloc_size];
87 if (copy) {
90 for (int i = 0; i < num_args_; ++i) {
92 }
93 }
94 for (int i = 0; i < num_args_; ++i) {
95 if (arg_is_copy[i]) {
96 CopyTraceObjectParameter(&ptr, &arg_values_[i].as_string);
97 }
98 }
99 }
100}
101
103
104void TraceObject::UpdateDuration(int64_t timestamp, int64_t cpu_timestamp) {
105 duration_ = timestamp - ts_;
106 cpu_duration_ = cpu_timestamp - tts_;
107}
108
110 char phase, const uint8_t* category_enabled_flag, const char* name,
111 const char* scope, uint64_t id, uint64_t bind_id, int num_args,
112 const char** arg_names, const uint8_t* arg_types,
113 const uint64_t* arg_values,
114 std::unique_ptr<v8::ConvertableToTraceFormat>* arg_convertables,
115 unsigned int flags, int pid, int tid, int64_t ts, int64_t tts,
116 uint64_t duration, uint64_t cpu_duration) {
117 pid_ = pid;
118 tid_ = tid;
119 phase_ = phase;
121 name_ = name;
122 scope_ = scope;
123 id_ = id;
126 flags_ = flags;
127 ts_ = ts;
128 tts_ = tts;
131}
132
133} // namespace tracing
134} // namespace platform
135} // namespace v8
static int GetCurrentProcessId()
static int GetCurrentThreadId()
Definition platform.cc:29
const char * arg_names_[kTraceMaxNumArgs]
Definition v8-tracing.h:98
ArgValue arg_values_[kTraceMaxNumArgs]
Definition v8-tracing.h:100
std::unique_ptr< v8::ConvertableToTraceFormat > * arg_convertables()
Definition v8-tracing.h:79
const uint8_t * category_enabled_flag() const
Definition v8-tracing.h:68
const uint8_t * category_enabled_flag_
Definition v8-tracing.h:94
uint8_t arg_types_[kTraceMaxNumArgs]
Definition v8-tracing.h:99
void Initialize(char phase, const uint8_t *category_enabled_flag, const char *name, const char *scope, uint64_t id, uint64_t bind_id, int num_args, const char **arg_names, const uint8_t *arg_types, const uint64_t *arg_values, std::unique_ptr< v8::ConvertableToTraceFormat > *arg_convertables, unsigned int flags, int64_t timestamp, int64_t cpu_timestamp)
void InitializeForTesting(char phase, const uint8_t *category_enabled_flag, const char *name, const char *scope, uint64_t id, uint64_t bind_id, int num_args, const char **arg_names, const uint8_t *arg_types, const uint64_t *arg_values, std::unique_ptr< v8::ConvertableToTraceFormat > *arg_convertables, unsigned int flags, int pid, int tid, int64_t ts, int64_t tts, uint64_t duration, uint64_t cpu_duration)
void UpdateDuration(int64_t timestamp, int64_t cpu_timestamp)
std::unique_ptr< v8::ConvertableToTraceFormat > arg_convertables_[kTraceMaxNumArgs]
Definition v8-tracing.h:102
static V8_INLINE size_t GetAllocLength(const char *str)
const int kTraceMaxNumArgs
Definition v8-tracing.h:34
static V8_INLINE void CopyTraceObjectParameter(char **buffer, const char **member)
#define TRACE_VALUE_TYPE_STRING
#define TRACE_VALUE_TYPE_COPY_STRING
#define TRACE_VALUE_TYPE_CONVERTABLE
#define TRACE_EVENT_FLAG_COPY
#define V8_INLINE
Definition v8config.h:500