blob: 56525f76b7a42725812fe0a5d9ae63f124d8660a [file] [log] [blame]
Sami Kyostila26a04372020-01-13 12:46:48 +00001/*
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"
Sami Kyostilad4eec8e2020-05-05 15:56:42 +010021#include "protos/perfetto/trace/track_event/process_descriptor.gen.h"
Sami Kyostila26a04372020-01-13 12:46:48 +000022#include "protos/perfetto/trace/track_event/process_descriptor.pbzero.h"
Sami Kyostilad4eec8e2020-05-05 15:56:42 +010023#include "protos/perfetto/trace/track_event/thread_descriptor.gen.h"
Sami Kyostila26a04372020-01-13 12:46:48 +000024#include "protos/perfetto/trace/track_event/thread_descriptor.pbzero.h"
25
26namespace perfetto {
27
28// static
29uint64_t Track::process_uuid;
30
Sami Kyostilad4eec8e2020-05-05 15:56:42 +010031protos::gen::TrackDescriptor Track::Serialize() const {
32 protos::gen::TrackDescriptor desc;
33 desc.set_uuid(uuid);
Sami Kyostila26a04372020-01-13 12:46:48 +000034 if (parent_uuid)
Sami Kyostilad4eec8e2020-05-05 15:56:42 +010035 desc.set_parent_uuid(parent_uuid);
36 return desc;
37}
38
39void Track::Serialize(protos::pbzero::TrackDescriptor* desc) const {
40 auto bytes = Serialize().SerializeAsString();
41 desc->AppendRawProtoBytes(bytes.data(), bytes.size());
42}
43
44protos::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;
Sami Kyostila26a04372020-01-13 12:46:48 +000050}
51
52void ProcessTrack::Serialize(protos::pbzero::TrackDescriptor* desc) const {
Sami Kyostilad4eec8e2020-05-05 15:56:42 +010053 auto bytes = Serialize().SerializeAsString();
54 desc->AppendRawProtoBytes(bytes.data(), bytes.size());
Sami Kyostila26a04372020-01-13 12:46:48 +000055}
56
Sami Kyostilad4eec8e2020-05-05 15:56:42 +010057protos::gen::TrackDescriptor ThreadTrack::Serialize() const {
58 auto desc = Track::Serialize();
59 auto td = desc.mutable_thread();
Primiano Tucci81759a92020-01-14 11:52:05 +000060 td->set_pid(static_cast<int32_t>(pid));
61 td->set_tid(static_cast<int32_t>(tid));
Sami Kyostila26a04372020-01-13 12:46:48 +000062 // TODO(skyostil): Record thread name.
Sami Kyostilad4eec8e2020-05-05 15:56:42 +010063 return desc;
64}
65
66void ThreadTrack::Serialize(protos::pbzero::TrackDescriptor* desc) const {
67 auto bytes = Serialize().SerializeAsString();
68 desc->AppendRawProtoBytes(bytes.data(), bytes.size());
Sami Kyostila26a04372020-01-13 12:46:48 +000069}
70
71namespace internal {
72
73// static
74TrackRegistry* TrackRegistry::instance_;
75
76TrackRegistry::TrackRegistry() = default;
77TrackRegistry::~TrackRegistry() = default;
78
79// static
80void TrackRegistry::InitializeInstance() {
Eric Secklerf1c74b72020-01-20 16:55:15 +000081 // 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;
Sami Kyostila26a04372020-01-13 12:46:48 +000085 instance_ = new TrackRegistry();
86 Track::process_uuid = static_cast<uint64_t>(base::Uuidv4().lsb());
87}
88
Sami Kyostilad4eec8e2020-05-05 15:56:42 +010089void 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
Sami Kyostila26a04372020-01-13 12:46:48 +000095void 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();
Sami Kyostilad4eec8e2020-05-05 15:56:42 +0100104 UpdateTrack(track, serialized_desc);
Sami Kyostila26a04372020-01-13 12:46:48 +0000105}
106
107void TrackRegistry::EraseTrack(Track track) {
108 std::lock_guard<std::mutex> lock(mutex_);
109 tracks_.erase(track.uuid);
110}
111
112// static
113void 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