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
Serialize() const52 protos::gen::TrackDescriptor ProcessTrack::Serialize() const {
53 auto desc = Track::Serialize();
54 auto pd = desc.mutable_process();
55 pd->set_pid(static_cast<int32_t>(pid));
56 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
57 PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
58 std::string cmdline;
59 if (base::ReadFile("/proc/self/cmdline", &cmdline)) {
60 // Since cmdline is a zero-terminated list of arguments, this ends up
61 // writing just the first element, i.e., the process name, into the process
62 // name field.
63 pd->set_process_name(cmdline.c_str());
64 base::StringSplitter splitter(std::move(cmdline), '\0');
65 while (splitter.Next()) {
66 pd->add_cmdline(
67 std::string(splitter.cur_token(), splitter.cur_token_size()));
68 }
69 }
70 // TODO(skyostil): Record command line on Windows and Mac.
71 #endif
72 return desc;
73 }
74
Serialize(protos::pbzero::TrackDescriptor * desc) const75 void ProcessTrack::Serialize(protos::pbzero::TrackDescriptor* desc) const {
76 auto bytes = Serialize().SerializeAsString();
77 desc->AppendRawProtoBytes(bytes.data(), bytes.size());
78 }
79
Serialize() const80 protos::gen::TrackDescriptor ThreadTrack::Serialize() const {
81 auto desc = Track::Serialize();
82 auto td = desc.mutable_thread();
83 td->set_pid(static_cast<int32_t>(pid));
84 td->set_tid(static_cast<int32_t>(tid));
85 if (disallow_merging_with_system_tracks) {
86 desc.set_disallow_merging_with_system_tracks(true);
87 }
88 std::string thread_name;
89 if (base::GetThreadName(thread_name))
90 td->set_thread_name(thread_name);
91 return desc;
92 }
93
94 // static
Current()95 ThreadTrack ThreadTrack::Current() {
96 return ThreadTrack(
97 internal::TracingMuxer::Get()->GetCurrentThreadId(),
98 internal::TrackEventInternal::GetDisallowMergingWithSystemTracks());
99 }
100
101 // static
ForThread(base::PlatformThreadId tid_)102 ThreadTrack ThreadTrack::ForThread(base::PlatformThreadId tid_) {
103 return ThreadTrack(
104 tid_, internal::TrackEventInternal::GetDisallowMergingWithSystemTracks());
105 }
106
Serialize(protos::pbzero::TrackDescriptor * desc) const107 void ThreadTrack::Serialize(protos::pbzero::TrackDescriptor* desc) const {
108 auto bytes = Serialize().SerializeAsString();
109 desc->AppendRawProtoBytes(bytes.data(), bytes.size());
110 }
111
Serialize() const112 protos::gen::TrackDescriptor CounterTrack::Serialize() const {
113 auto desc = Track::Serialize();
114 desc.set_name(name_);
115 auto* counter = desc.mutable_counter();
116 if (category_)
117 counter->add_categories(category_);
118 if (unit_ != perfetto::protos::pbzero::CounterDescriptor::UNIT_UNSPECIFIED)
119 counter->set_unit(static_cast<protos::gen::CounterDescriptor_Unit>(unit_));
120 {
121 // if |type| is set, we don't want to emit |unit_name|. Trace processor
122 // infers the track name from the type in that case.
123 if (type_ !=
124 perfetto::protos::gen::CounterDescriptor::COUNTER_UNSPECIFIED) {
125 counter->set_type(type_);
126 } else if (unit_name_) {
127 counter->set_unit_name(unit_name_);
128 }
129 }
130 if (unit_multiplier_ != 1)
131 counter->set_unit_multiplier(unit_multiplier_);
132 if (is_incremental_)
133 counter->set_is_incremental(is_incremental_);
134 return desc;
135 }
136
Serialize(protos::pbzero::TrackDescriptor * desc) const137 void CounterTrack::Serialize(protos::pbzero::TrackDescriptor* desc) const {
138 auto bytes = Serialize().SerializeAsString();
139 desc->AppendRawProtoBytes(bytes.data(), bytes.size());
140 }
141
142 namespace internal {
143 namespace {
144
GetProcessStartTime()145 uint64_t GetProcessStartTime() {
146 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
147 std::string stat;
148 if (!base::ReadFile("/proc/self/stat", &stat))
149 return 0u;
150 // The stat file is a single line split into space-separated fields as "pid
151 // (comm) state ppid ...". However because the command name can contain any
152 // characters (including parentheses and spaces), we need to skip past it
153 // before parsing the rest of the fields. To do that, we look for the last
154 // instance of ") " (parentheses followed by space) and parse forward from
155 // that point.
156 size_t comm_end = stat.rfind(") ");
157 if (comm_end == std::string::npos)
158 return 0u;
159 stat = stat.substr(comm_end + strlen(") "));
160 base::StringSplitter splitter(stat, ' ');
161 for (size_t skip = 0; skip < 20; skip++) {
162 if (!splitter.Next())
163 return 0u;
164 }
165 return base::CStringToUInt64(splitter.cur_token()).value_or(0u);
166 #else
167 return 0;
168 #endif // !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
169 }
170
171 } // namespace
172
173 // static
174 TrackRegistry* TrackRegistry::instance_;
175
176 TrackRegistry::TrackRegistry() = default;
177 TrackRegistry::~TrackRegistry() = default;
178
179 // static
InitializeInstance()180 void TrackRegistry::InitializeInstance() {
181 if (instance_)
182 return;
183 instance_ = new TrackRegistry();
184
185 // Use the process start time + pid as the unique identifier for this process.
186 // This ensures that if there are two independent copies of the Perfetto SDK
187 // in the same process (e.g., one in the app and another in a system
188 // framework), events emitted by each will be consistently interleaved on
189 // common thread and process tracks.
190 if (uint64_t start_time = GetProcessStartTime()) {
191 base::Hasher hash;
192 hash.Update(start_time);
193 hash.Update(Platform::GetCurrentProcessId());
194 Track::process_uuid = hash.digest();
195 } else {
196 // Fall back to a randomly generated identifier.
197 Track::process_uuid = static_cast<uint64_t>(base::Uuidv4().lsb());
198 }
199 }
200
ResetForTesting()201 void TrackRegistry::ResetForTesting() {
202 instance_->tracks_.clear();
203 }
204
UpdateTrack(Track track,const std::string & serialized_desc)205 void TrackRegistry::UpdateTrack(Track track,
206 const std::string& serialized_desc) {
207 std::lock_guard<std::mutex> lock(mutex_);
208 tracks_[track.uuid] = std::move(serialized_desc);
209 }
210
UpdateTrackImpl(Track track,std::function<void (protos::pbzero::TrackDescriptor *)> fill_function)211 void TrackRegistry::UpdateTrackImpl(
212 Track track,
213 std::function<void(protos::pbzero::TrackDescriptor*)> fill_function) {
214 constexpr size_t kInitialSliceSize = 32;
215 constexpr size_t kMaximumSliceSize = 4096;
216 protozero::HeapBuffered<protos::pbzero::TrackDescriptor> new_descriptor(
217 kInitialSliceSize, kMaximumSliceSize);
218 fill_function(new_descriptor.get());
219 auto serialized_desc = new_descriptor.SerializeAsString();
220 UpdateTrack(track, serialized_desc);
221 }
222
EraseTrack(Track track)223 void TrackRegistry::EraseTrack(Track track) {
224 std::lock_guard<std::mutex> lock(mutex_);
225 tracks_.erase(track.uuid);
226 }
227
228 // static
WriteTrackDescriptor(const SerializedTrackDescriptor & desc,protozero::MessageHandle<protos::pbzero::TracePacket> packet)229 void TrackRegistry::WriteTrackDescriptor(
230 const SerializedTrackDescriptor& desc,
231 protozero::MessageHandle<protos::pbzero::TracePacket> packet) {
232 packet->AppendString(
233 perfetto::protos::pbzero::TracePacket::kTrackDescriptorFieldNumber, desc);
234 }
235
236 } // namespace internal
237 } // namespace perfetto
238