1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "perfetto/tracing/track.h"
18
19 #include "perfetto/ext/base/file_utils.h"
20 #include "perfetto/ext/base/hash.h"
21 #include "perfetto/ext/base/scoped_file.h"
22 #include "perfetto/ext/base/string_splitter.h"
23 #include "perfetto/ext/base/string_utils.h"
24 #include "perfetto/ext/base/thread_utils.h"
25 #include "perfetto/ext/base/uuid.h"
26 #include "perfetto/tracing/internal/track_event_data_source.h"
27 #include "perfetto/tracing/internal/track_event_internal.h"
28 #include "protos/perfetto/trace/track_event/counter_descriptor.gen.h"
29 #include "protos/perfetto/trace/track_event/process_descriptor.gen.h"
30 #include "protos/perfetto/trace/track_event/process_descriptor.pbzero.h"
31 #include "protos/perfetto/trace/track_event/thread_descriptor.gen.h"
32 #include "protos/perfetto/trace/track_event/thread_descriptor.pbzero.h"
33
34 namespace perfetto {
35
36 // static
37 uint64_t Track::process_uuid;
38
Serialize() const39 protos::gen::TrackDescriptor Track::Serialize() const {
40 protos::gen::TrackDescriptor desc;
41 desc.set_uuid(uuid);
42 if (parent_uuid)
43 desc.set_parent_uuid(parent_uuid);
44 return desc;
45 }
46
Serialize(protos::pbzero::TrackDescriptor * desc) const47 void Track::Serialize(protos::pbzero::TrackDescriptor* desc) const {
48 auto bytes = Serialize().SerializeAsString();
49 desc->AppendRawProtoBytes(bytes.data(), bytes.size());
50 }
51
52 // static
ThreadScoped(const void * ptr,Track parent)53 Track Track::ThreadScoped(const void* ptr, Track parent) {
54 if (parent.uuid == 0)
55 return Track::FromPointer(ptr, ThreadTrack::Current());
56 return Track::FromPointer(ptr, parent);
57 }
58
Serialize() const59 protos::gen::TrackDescriptor ProcessTrack::Serialize() const {
60 auto desc = Track::Serialize();
61 auto pd = desc.mutable_process();
62 pd->set_pid(static_cast<int32_t>(pid));
63 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
64 PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
65 std::string cmdline;
66 if (base::ReadFile("/proc/self/cmdline", &cmdline)) {
67 // Since cmdline is a zero-terminated list of arguments, this ends up
68 // writing just the first element, i.e., the process name, into the process
69 // name field.
70 pd->set_process_name(cmdline.c_str());
71 base::StringSplitter splitter(std::move(cmdline), '\0');
72 while (splitter.Next()) {
73 pd->add_cmdline(
74 std::string(splitter.cur_token(), splitter.cur_token_size()));
75 }
76 }
77 // TODO(skyostil): Record command line on Windows and Mac.
78 #endif
79 return desc;
80 }
81
Serialize(protos::pbzero::TrackDescriptor * desc) const82 void ProcessTrack::Serialize(protos::pbzero::TrackDescriptor* desc) const {
83 auto bytes = Serialize().SerializeAsString();
84 desc->AppendRawProtoBytes(bytes.data(), bytes.size());
85 }
86
Serialize() const87 protos::gen::TrackDescriptor ThreadTrack::Serialize() const {
88 auto desc = Track::Serialize();
89 auto td = desc.mutable_thread();
90 td->set_pid(static_cast<int32_t>(pid));
91 td->set_tid(static_cast<int32_t>(tid));
92 if (disallow_merging_with_system_tracks) {
93 desc.set_disallow_merging_with_system_tracks(true);
94 }
95 std::string thread_name;
96 if (base::GetThreadName(thread_name))
97 td->set_thread_name(thread_name);
98 return desc;
99 }
100
101 // static
Current()102 ThreadTrack ThreadTrack::Current() {
103 return ThreadTrack(
104 internal::TracingMuxer::Get()->GetCurrentThreadId(),
105 internal::TrackEventInternal::GetDisallowMergingWithSystemTracks());
106 }
107
108 // static
ForThread(base::PlatformThreadId tid_)109 ThreadTrack ThreadTrack::ForThread(base::PlatformThreadId tid_) {
110 return ThreadTrack(
111 tid_, internal::TrackEventInternal::GetDisallowMergingWithSystemTracks());
112 }
113
Serialize(protos::pbzero::TrackDescriptor * desc) const114 void ThreadTrack::Serialize(protos::pbzero::TrackDescriptor* desc) const {
115 auto bytes = Serialize().SerializeAsString();
116 desc->AppendRawProtoBytes(bytes.data(), bytes.size());
117 }
118
Serialize() const119 protos::gen::TrackDescriptor NamedTrack::Serialize() const {
120 auto desc = Track::Serialize();
121 if (static_name_) {
122 desc.set_static_name(static_name_.value);
123 } else {
124 desc.set_name(dynamic_name_.value);
125 }
126 return desc;
127 }
128
Serialize(protos::pbzero::TrackDescriptor * desc) const129 void NamedTrack::Serialize(protos::pbzero::TrackDescriptor* desc) const {
130 auto bytes = Serialize().SerializeAsString();
131 desc->AppendRawProtoBytes(bytes.data(), bytes.size());
132 }
133
Serialize() const134 protos::gen::TrackDescriptor CounterTrack::Serialize() const {
135 auto desc = Track::Serialize();
136 auto* counter = desc.mutable_counter();
137 if (static_name_) {
138 desc.set_static_name(static_name_.value);
139 } else {
140 desc.set_name(dynamic_name_.value);
141 }
142
143 if (category_)
144 counter->add_categories(category_);
145 if (unit_ != perfetto::protos::pbzero::CounterDescriptor::UNIT_UNSPECIFIED)
146 counter->set_unit(static_cast<protos::gen::CounterDescriptor_Unit>(unit_));
147 {
148 // if |type| is set, we don't want to emit |unit_name|. Trace processor
149 // infers the track name from the type in that case.
150 if (type_ !=
151 perfetto::protos::gen::CounterDescriptor::COUNTER_UNSPECIFIED) {
152 counter->set_type(type_);
153 } else if (unit_name_) {
154 counter->set_unit_name(unit_name_);
155 }
156 }
157 if (unit_multiplier_ != 1)
158 counter->set_unit_multiplier(unit_multiplier_);
159 if (is_incremental_)
160 counter->set_is_incremental(is_incremental_);
161 return desc;
162 }
163
Serialize(protos::pbzero::TrackDescriptor * desc) const164 void CounterTrack::Serialize(protos::pbzero::TrackDescriptor* desc) const {
165 auto bytes = Serialize().SerializeAsString();
166 desc->AppendRawProtoBytes(bytes.data(), bytes.size());
167 }
168
169 namespace internal {
170 namespace {
171
GetProcessStartTime()172 uint64_t GetProcessStartTime() {
173 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
174 std::string stat;
175 if (!base::ReadFile("/proc/self/stat", &stat))
176 return 0u;
177 // The stat file is a single line split into space-separated fields as "pid
178 // (comm) state ppid ...". However because the command name can contain any
179 // characters (including parentheses and spaces), we need to skip past it
180 // before parsing the rest of the fields. To do that, we look for the last
181 // instance of ") " (parentheses followed by space) and parse forward from
182 // that point.
183 size_t comm_end = stat.rfind(") ");
184 if (comm_end == std::string::npos)
185 return 0u;
186 stat = stat.substr(comm_end + strlen(") "));
187 base::StringSplitter splitter(stat, ' ');
188 for (size_t skip = 0; skip < 20; skip++) {
189 if (!splitter.Next())
190 return 0u;
191 }
192 return base::CStringToUInt64(splitter.cur_token()).value_or(0u);
193 #else
194 return 0;
195 #endif // !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
196 }
197
198 } // namespace
199
200 // static
201 TrackRegistry* TrackRegistry::instance_;
202
203 TrackRegistry::TrackRegistry() = default;
204 TrackRegistry::~TrackRegistry() = default;
205
206 // static
InitializeInstance()207 void TrackRegistry::InitializeInstance() {
208 if (instance_)
209 return;
210 instance_ = new TrackRegistry();
211 Track::process_uuid = ComputeProcessUuid();
212 }
213
214 // static
ComputeProcessUuid()215 uint64_t TrackRegistry::ComputeProcessUuid() {
216 // Use the process start time + pid as the unique identifier for this process.
217 // This ensures that if there are two independent copies of the Perfetto SDK
218 // in the same process (e.g., one in the app and another in a system
219 // framework), events emitted by each will be consistently interleaved on
220 // common thread and process tracks.
221 if (uint64_t start_time = GetProcessStartTime()) {
222 base::Hasher hash;
223 hash.Update(start_time);
224 hash.Update(Platform::GetCurrentProcessId());
225 return hash.digest();
226 }
227 // Fall back to a randomly generated identifier.
228 static uint64_t random_once = static_cast<uint64_t>(base::Uuidv4().lsb());
229 return random_once;
230 }
231
ResetForTesting()232 void TrackRegistry::ResetForTesting() {
233 instance_->tracks_.clear();
234 }
235
UpdateTrack(Track track,const std::string & serialized_desc)236 void TrackRegistry::UpdateTrack(Track track,
237 const std::string& serialized_desc) {
238 std::lock_guard<std::mutex> lock(mutex_);
239 tracks_[track.uuid] = {serialized_desc, track.parent_uuid};
240 }
241
EraseTrack(Track track)242 void TrackRegistry::EraseTrack(Track track) {
243 std::lock_guard<std::mutex> lock(mutex_);
244 tracks_.erase(track.uuid);
245 }
246
247 // static
WriteTrackDescriptor(const SerializedTrackDescriptor & desc,protozero::MessageHandle<protos::pbzero::TracePacket> packet)248 void TrackRegistry::WriteTrackDescriptor(
249 const SerializedTrackDescriptor& desc,
250 protozero::MessageHandle<protos::pbzero::TracePacket> packet) {
251 packet->AppendString(
252 perfetto::protos::pbzero::TracePacket::kTrackDescriptorFieldNumber, desc);
253 }
254
255 } // namespace internal
256 } // namespace perfetto
257