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/uuid.h"
20 #include "perfetto/tracing/internal/track_event_data_source.h"
21 #include "protos/perfetto/trace/track_event/process_descriptor.gen.h"
22 #include "protos/perfetto/trace/track_event/process_descriptor.pbzero.h"
23 #include "protos/perfetto/trace/track_event/thread_descriptor.gen.h"
24 #include "protos/perfetto/trace/track_event/thread_descriptor.pbzero.h"
25
26 namespace perfetto {
27
28 // static
29 uint64_t Track::process_uuid;
30
Serialize() const31 protos::gen::TrackDescriptor Track::Serialize() const {
32 protos::gen::TrackDescriptor desc;
33 desc.set_uuid(uuid);
34 if (parent_uuid)
35 desc.set_parent_uuid(parent_uuid);
36 return desc;
37 }
38
Serialize(protos::pbzero::TrackDescriptor * desc) const39 void Track::Serialize(protos::pbzero::TrackDescriptor* desc) const {
40 auto bytes = Serialize().SerializeAsString();
41 desc->AppendRawProtoBytes(bytes.data(), bytes.size());
42 }
43
Serialize() const44 protos::gen::TrackDescriptor ProcessTrack::Serialize() const {
45 auto desc = Track::Serialize();
46 auto pd = desc.mutable_process();
47 pd->set_pid(static_cast<int32_t>(pid));
48 // TODO(skyostil): Record command line.
49 return desc;
50 }
51
Serialize(protos::pbzero::TrackDescriptor * desc) const52 void ProcessTrack::Serialize(protos::pbzero::TrackDescriptor* desc) const {
53 auto bytes = Serialize().SerializeAsString();
54 desc->AppendRawProtoBytes(bytes.data(), bytes.size());
55 }
56
Serialize() const57 protos::gen::TrackDescriptor ThreadTrack::Serialize() const {
58 auto desc = Track::Serialize();
59 auto td = desc.mutable_thread();
60 td->set_pid(static_cast<int32_t>(pid));
61 td->set_tid(static_cast<int32_t>(tid));
62 // TODO(skyostil): Record thread name.
63 return desc;
64 }
65
Serialize(protos::pbzero::TrackDescriptor * desc) const66 void ThreadTrack::Serialize(protos::pbzero::TrackDescriptor* desc) const {
67 auto bytes = Serialize().SerializeAsString();
68 desc->AppendRawProtoBytes(bytes.data(), bytes.size());
69 }
70
71 namespace internal {
72
73 // static
74 TrackRegistry* TrackRegistry::instance_;
75
76 TrackRegistry::TrackRegistry() = default;
77 TrackRegistry::~TrackRegistry() = default;
78
79 // static
InitializeInstance()80 void TrackRegistry::InitializeInstance() {
81 // TODO(eseckler): Chrome may call this more than once. Once Chrome doesn't
82 // call this directly anymore, bring back DCHECK(!instance_) instead.
83 if (instance_)
84 return;
85 instance_ = new TrackRegistry();
86 Track::process_uuid = static_cast<uint64_t>(base::Uuidv4().lsb());
87 }
88
UpdateTrack(Track track,const std::string & serialized_desc)89 void TrackRegistry::UpdateTrack(Track track,
90 const std::string& serialized_desc) {
91 std::lock_guard<std::mutex> lock(mutex_);
92 tracks_[track.uuid] = std::move(serialized_desc);
93 }
94
UpdateTrackImpl(Track track,std::function<void (protos::pbzero::TrackDescriptor *)> fill_function)95 void TrackRegistry::UpdateTrackImpl(
96 Track track,
97 std::function<void(protos::pbzero::TrackDescriptor*)> fill_function) {
98 constexpr size_t kInitialSliceSize = 32;
99 constexpr size_t kMaximumSliceSize = 4096;
100 protozero::HeapBuffered<protos::pbzero::TrackDescriptor> new_descriptor(
101 kInitialSliceSize, kMaximumSliceSize);
102 fill_function(new_descriptor.get());
103 auto serialized_desc = new_descriptor.SerializeAsString();
104 UpdateTrack(track, serialized_desc);
105 }
106
EraseTrack(Track track)107 void TrackRegistry::EraseTrack(Track track) {
108 std::lock_guard<std::mutex> lock(mutex_);
109 tracks_.erase(track.uuid);
110 }
111
112 // static
WriteTrackDescriptor(const SerializedTrackDescriptor & desc,protozero::MessageHandle<protos::pbzero::TracePacket> packet)113 void TrackRegistry::WriteTrackDescriptor(
114 const SerializedTrackDescriptor& desc,
115 protozero::MessageHandle<protos::pbzero::TracePacket> packet) {
116 packet->AppendString(
117 perfetto::protos::pbzero::TracePacket::kTrackDescriptorFieldNumber, desc);
118 }
119
120 } // namespace internal
121 } // namespace perfetto
122