• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 CounterTrack::Serialize() const {
120   auto desc = Track::Serialize();
121   auto* counter = desc.mutable_counter();
122   if (static_name_) {
123     desc.set_static_name(static_name_.value);
124   } else {
125     desc.set_name(dynamic_name_.value);
126   }
127 
128   if (category_)
129     counter->add_categories(category_);
130   if (unit_ != perfetto::protos::pbzero::CounterDescriptor::UNIT_UNSPECIFIED)
131     counter->set_unit(static_cast<protos::gen::CounterDescriptor_Unit>(unit_));
132   {
133     // if |type| is set, we don't want to emit |unit_name|. Trace processor
134     // infers the track name from the type in that case.
135     if (type_ !=
136         perfetto::protos::gen::CounterDescriptor::COUNTER_UNSPECIFIED) {
137       counter->set_type(type_);
138     } else if (unit_name_) {
139       counter->set_unit_name(unit_name_);
140     }
141   }
142   if (unit_multiplier_ != 1)
143     counter->set_unit_multiplier(unit_multiplier_);
144   if (is_incremental_)
145     counter->set_is_incremental(is_incremental_);
146   return desc;
147 }
148 
Serialize(protos::pbzero::TrackDescriptor * desc) const149 void CounterTrack::Serialize(protos::pbzero::TrackDescriptor* desc) const {
150   auto bytes = Serialize().SerializeAsString();
151   desc->AppendRawProtoBytes(bytes.data(), bytes.size());
152 }
153 
154 namespace internal {
155 namespace {
156 
GetProcessStartTime()157 uint64_t GetProcessStartTime() {
158 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
159   std::string stat;
160   if (!base::ReadFile("/proc/self/stat", &stat))
161     return 0u;
162   // The stat file is a single line split into space-separated fields as "pid
163   // (comm) state ppid ...". However because the command name can contain any
164   // characters (including parentheses and spaces), we need to skip past it
165   // before parsing the rest of the fields. To do that, we look for the last
166   // instance of ") " (parentheses followed by space) and parse forward from
167   // that point.
168   size_t comm_end = stat.rfind(") ");
169   if (comm_end == std::string::npos)
170     return 0u;
171   stat = stat.substr(comm_end + strlen(") "));
172   base::StringSplitter splitter(stat, ' ');
173   for (size_t skip = 0; skip < 20; skip++) {
174     if (!splitter.Next())
175       return 0u;
176   }
177   return base::CStringToUInt64(splitter.cur_token()).value_or(0u);
178 #else
179   return 0;
180 #endif  // !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
181 }
182 
183 }  // namespace
184 
185 // static
186 TrackRegistry* TrackRegistry::instance_;
187 
188 TrackRegistry::TrackRegistry() = default;
189 TrackRegistry::~TrackRegistry() = default;
190 
191 // static
InitializeInstance()192 void TrackRegistry::InitializeInstance() {
193   if (instance_)
194     return;
195   instance_ = new TrackRegistry();
196   Track::process_uuid = ComputeProcessUuid();
197 }
198 
199 // static
ComputeProcessUuid()200 uint64_t TrackRegistry::ComputeProcessUuid() {
201   // Use the process start time + pid as the unique identifier for this process.
202   // This ensures that if there are two independent copies of the Perfetto SDK
203   // in the same process (e.g., one in the app and another in a system
204   // framework), events emitted by each will be consistently interleaved on
205   // common thread and process tracks.
206   if (uint64_t start_time = GetProcessStartTime()) {
207     base::Hasher hash;
208     hash.Update(start_time);
209     hash.Update(Platform::GetCurrentProcessId());
210     return hash.digest();
211   }
212   // Fall back to a randomly generated identifier.
213   static uint64_t random_once = static_cast<uint64_t>(base::Uuidv4().lsb());
214   return random_once;
215 }
216 
ResetForTesting()217 void TrackRegistry::ResetForTesting() {
218   instance_->tracks_.clear();
219 }
220 
UpdateTrack(Track track,const std::string & serialized_desc)221 void TrackRegistry::UpdateTrack(Track track,
222                                 const std::string& serialized_desc) {
223   std::lock_guard<std::mutex> lock(mutex_);
224   tracks_[track.uuid] = std::move(serialized_desc);
225 }
226 
EraseTrack(Track track)227 void TrackRegistry::EraseTrack(Track track) {
228   std::lock_guard<std::mutex> lock(mutex_);
229   tracks_.erase(track.uuid);
230 }
231 
232 // static
WriteTrackDescriptor(const SerializedTrackDescriptor & desc,protozero::MessageHandle<protos::pbzero::TracePacket> packet)233 void TrackRegistry::WriteTrackDescriptor(
234     const SerializedTrackDescriptor& desc,
235     protozero::MessageHandle<protos::pbzero::TracePacket> packet) {
236   packet->AppendString(
237       perfetto::protos::pbzero::TracePacket::kTrackDescriptorFieldNumber, desc);
238 }
239 
240 }  // namespace internal
241 }  // namespace perfetto
242