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 "protos/perfetto/trace/track_event/counter_descriptor.gen.h"
28 #include "protos/perfetto/trace/track_event/process_descriptor.gen.h"
29 #include "protos/perfetto/trace/track_event/process_descriptor.pbzero.h"
30 #include "protos/perfetto/trace/track_event/thread_descriptor.gen.h"
31 #include "protos/perfetto/trace/track_event/thread_descriptor.pbzero.h"
32
33 namespace perfetto {
34
35 // static
36 uint64_t Track::process_uuid;
37
Serialize() const38 protos::gen::TrackDescriptor Track::Serialize() const {
39 protos::gen::TrackDescriptor desc;
40 desc.set_uuid(uuid);
41 if (parent_uuid)
42 desc.set_parent_uuid(parent_uuid);
43 return desc;
44 }
45
Serialize(protos::pbzero::TrackDescriptor * desc) const46 void Track::Serialize(protos::pbzero::TrackDescriptor* desc) const {
47 auto bytes = Serialize().SerializeAsString();
48 desc->AppendRawProtoBytes(bytes.data(), bytes.size());
49 }
50
Serialize() const51 protos::gen::TrackDescriptor ProcessTrack::Serialize() const {
52 auto desc = Track::Serialize();
53 auto pd = desc.mutable_process();
54 pd->set_pid(static_cast<int32_t>(pid));
55 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
56 PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
57 std::string cmdline;
58 if (base::ReadFile("/proc/self/cmdline", &cmdline)) {
59 // Since cmdline is a zero-terminated list of arguments, this ends up
60 // writing just the first element, i.e., the process name, into the process
61 // name field.
62 pd->set_process_name(cmdline.c_str());
63 base::StringSplitter splitter(std::move(cmdline), '\0');
64 while (splitter.Next()) {
65 pd->add_cmdline(
66 std::string(splitter.cur_token(), splitter.cur_token_size()));
67 }
68 }
69 // TODO(skyostil): Record command line on Windows and Mac.
70 #endif
71 return desc;
72 }
73
Serialize(protos::pbzero::TrackDescriptor * desc) const74 void ProcessTrack::Serialize(protos::pbzero::TrackDescriptor* desc) const {
75 auto bytes = Serialize().SerializeAsString();
76 desc->AppendRawProtoBytes(bytes.data(), bytes.size());
77 }
78
Serialize() const79 protos::gen::TrackDescriptor ThreadTrack::Serialize() const {
80 auto desc = Track::Serialize();
81 auto td = desc.mutable_thread();
82 td->set_pid(static_cast<int32_t>(pid));
83 td->set_tid(static_cast<int32_t>(tid));
84 std::string thread_name;
85 if (base::GetThreadName(thread_name))
86 td->set_thread_name(thread_name);
87 return desc;
88 }
89
Serialize(protos::pbzero::TrackDescriptor * desc) const90 void ThreadTrack::Serialize(protos::pbzero::TrackDescriptor* desc) const {
91 auto bytes = Serialize().SerializeAsString();
92 desc->AppendRawProtoBytes(bytes.data(), bytes.size());
93 }
94
Serialize() const95 protos::gen::TrackDescriptor CounterTrack::Serialize() const {
96 auto desc = Track::Serialize();
97 desc.set_name(name_);
98 auto* counter = desc.mutable_counter();
99 if (category_)
100 counter->add_categories(category_);
101 if (unit_ != perfetto::protos::pbzero::CounterDescriptor::UNIT_UNSPECIFIED)
102 counter->set_unit(static_cast<protos::gen::CounterDescriptor_Unit>(unit_));
103 if (unit_name_)
104 counter->set_unit_name(unit_name_);
105 if (unit_multiplier_ != 1)
106 counter->set_unit_multiplier(unit_multiplier_);
107 if (is_incremental_)
108 counter->set_is_incremental(is_incremental_);
109 return desc;
110 }
111
Serialize(protos::pbzero::TrackDescriptor * desc) const112 void CounterTrack::Serialize(protos::pbzero::TrackDescriptor* desc) const {
113 auto bytes = Serialize().SerializeAsString();
114 desc->AppendRawProtoBytes(bytes.data(), bytes.size());
115 }
116
117 namespace internal {
118 namespace {
119
GetProcessStartTime()120 uint64_t GetProcessStartTime() {
121 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
122 std::string stat;
123 if (!base::ReadFile("/proc/self/stat", &stat))
124 return 0u;
125 // The stat file is a single line split into space-separated fields as "pid
126 // (comm) state ppid ...". However because the command name can contain any
127 // characters (including parentheses and spaces), we need to skip past it
128 // before parsing the rest of the fields. To do that, we look for the last
129 // instance of ") " (parentheses followed by space) and parse forward from
130 // that point.
131 size_t comm_end = stat.rfind(") ");
132 if (comm_end == std::string::npos)
133 return 0u;
134 stat = stat.substr(comm_end + strlen(") "));
135 base::StringSplitter splitter(stat, ' ');
136 for (size_t skip = 0; skip < 20; skip++) {
137 if (!splitter.Next())
138 return 0u;
139 }
140 return base::CStringToUInt64(splitter.cur_token()).value_or(0u);
141 #else
142 return 0;
143 #endif // !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
144 }
145
146 } // namespace
147
148 // static
149 TrackRegistry* TrackRegistry::instance_;
150
151 TrackRegistry::TrackRegistry() = default;
152 TrackRegistry::~TrackRegistry() = default;
153
154 // static
InitializeInstance()155 void TrackRegistry::InitializeInstance() {
156 // TODO(eseckler): Chrome may call this more than once. Once Chrome doesn't
157 // call this directly anymore, bring back DCHECK(!instance_) instead.
158 if (instance_)
159 return;
160 instance_ = new TrackRegistry();
161
162 // Use the process start time + pid as the unique identifier for this process.
163 // This ensures that if there are two independent copies of the Perfetto SDK
164 // in the same process (e.g., one in the app and another in a system
165 // framework), events emitted by each will be consistently interleaved on
166 // common thread and process tracks.
167 if (uint64_t start_time = GetProcessStartTime()) {
168 base::Hash hash;
169 hash.Update(start_time);
170 hash.Update(base::GetProcessId());
171 Track::process_uuid = hash.digest();
172 } else {
173 // Fall back to a randomly generated identifier.
174 Track::process_uuid = static_cast<uint64_t>(base::Uuidv4().lsb());
175 }
176 }
177
UpdateTrack(Track track,const std::string & serialized_desc)178 void TrackRegistry::UpdateTrack(Track track,
179 const std::string& serialized_desc) {
180 std::lock_guard<std::mutex> lock(mutex_);
181 tracks_[track.uuid] = std::move(serialized_desc);
182 }
183
UpdateTrackImpl(Track track,std::function<void (protos::pbzero::TrackDescriptor *)> fill_function)184 void TrackRegistry::UpdateTrackImpl(
185 Track track,
186 std::function<void(protos::pbzero::TrackDescriptor*)> fill_function) {
187 constexpr size_t kInitialSliceSize = 32;
188 constexpr size_t kMaximumSliceSize = 4096;
189 protozero::HeapBuffered<protos::pbzero::TrackDescriptor> new_descriptor(
190 kInitialSliceSize, kMaximumSliceSize);
191 fill_function(new_descriptor.get());
192 auto serialized_desc = new_descriptor.SerializeAsString();
193 UpdateTrack(track, serialized_desc);
194 }
195
EraseTrack(Track track)196 void TrackRegistry::EraseTrack(Track track) {
197 std::lock_guard<std::mutex> lock(mutex_);
198 tracks_.erase(track.uuid);
199 }
200
201 // static
WriteTrackDescriptor(const SerializedTrackDescriptor & desc,protozero::MessageHandle<protos::pbzero::TracePacket> packet)202 void TrackRegistry::WriteTrackDescriptor(
203 const SerializedTrackDescriptor& desc,
204 protozero::MessageHandle<protos::pbzero::TracePacket> packet) {
205 packet->AppendString(
206 perfetto::protos::pbzero::TracePacket::kTrackDescriptorFieldNumber, desc);
207 }
208
209 } // namespace internal
210 } // namespace perfetto
211