Sami Kyostila | 26a0437 | 2020-01-13 12:46:48 +0000 | [diff] [blame] | 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" |
Sami Kyostila | d4eec8e | 2020-05-05 15:56:42 +0100 | [diff] [blame] | 21 | #include "protos/perfetto/trace/track_event/process_descriptor.gen.h" |
Sami Kyostila | 26a0437 | 2020-01-13 12:46:48 +0000 | [diff] [blame] | 22 | #include "protos/perfetto/trace/track_event/process_descriptor.pbzero.h" |
Sami Kyostila | d4eec8e | 2020-05-05 15:56:42 +0100 | [diff] [blame] | 23 | #include "protos/perfetto/trace/track_event/thread_descriptor.gen.h" |
Sami Kyostila | 26a0437 | 2020-01-13 12:46:48 +0000 | [diff] [blame] | 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 | |
Sami Kyostila | d4eec8e | 2020-05-05 15:56:42 +0100 | [diff] [blame] | 31 | protos::gen::TrackDescriptor Track::Serialize() const { |
| 32 | protos::gen::TrackDescriptor desc; |
| 33 | desc.set_uuid(uuid); |
Sami Kyostila | 26a0437 | 2020-01-13 12:46:48 +0000 | [diff] [blame] | 34 | if (parent_uuid) |
Sami Kyostila | d4eec8e | 2020-05-05 15:56:42 +0100 | [diff] [blame] | 35 | desc.set_parent_uuid(parent_uuid); |
| 36 | return desc; |
| 37 | } |
| 38 | |
| 39 | void Track::Serialize(protos::pbzero::TrackDescriptor* desc) const { |
| 40 | auto bytes = Serialize().SerializeAsString(); |
| 41 | desc->AppendRawProtoBytes(bytes.data(), bytes.size()); |
| 42 | } |
| 43 | |
| 44 | 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; |
Sami Kyostila | 26a0437 | 2020-01-13 12:46:48 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void ProcessTrack::Serialize(protos::pbzero::TrackDescriptor* desc) const { |
Sami Kyostila | d4eec8e | 2020-05-05 15:56:42 +0100 | [diff] [blame] | 53 | auto bytes = Serialize().SerializeAsString(); |
| 54 | desc->AppendRawProtoBytes(bytes.data(), bytes.size()); |
Sami Kyostila | 26a0437 | 2020-01-13 12:46:48 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Sami Kyostila | d4eec8e | 2020-05-05 15:56:42 +0100 | [diff] [blame] | 57 | protos::gen::TrackDescriptor ThreadTrack::Serialize() const { |
| 58 | auto desc = Track::Serialize(); |
| 59 | auto td = desc.mutable_thread(); |
Primiano Tucci | 81759a9 | 2020-01-14 11:52:05 +0000 | [diff] [blame] | 60 | td->set_pid(static_cast<int32_t>(pid)); |
| 61 | td->set_tid(static_cast<int32_t>(tid)); |
Sami Kyostila | 26a0437 | 2020-01-13 12:46:48 +0000 | [diff] [blame] | 62 | // TODO(skyostil): Record thread name. |
Sami Kyostila | d4eec8e | 2020-05-05 15:56:42 +0100 | [diff] [blame] | 63 | return desc; |
| 64 | } |
| 65 | |
| 66 | void ThreadTrack::Serialize(protos::pbzero::TrackDescriptor* desc) const { |
| 67 | auto bytes = Serialize().SerializeAsString(); |
| 68 | desc->AppendRawProtoBytes(bytes.data(), bytes.size()); |
Sami Kyostila | 26a0437 | 2020-01-13 12:46:48 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | namespace internal { |
| 72 | |
| 73 | // static |
| 74 | TrackRegistry* TrackRegistry::instance_; |
| 75 | |
| 76 | TrackRegistry::TrackRegistry() = default; |
| 77 | TrackRegistry::~TrackRegistry() = default; |
| 78 | |
| 79 | // static |
| 80 | void TrackRegistry::InitializeInstance() { |
Eric Seckler | f1c74b7 | 2020-01-20 16:55:15 +0000 | [diff] [blame] | 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; |
Sami Kyostila | 26a0437 | 2020-01-13 12:46:48 +0000 | [diff] [blame] | 85 | instance_ = new TrackRegistry(); |
| 86 | Track::process_uuid = static_cast<uint64_t>(base::Uuidv4().lsb()); |
| 87 | } |
| 88 | |
Sami Kyostila | d4eec8e | 2020-05-05 15:56:42 +0100 | [diff] [blame] | 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 | |
Sami Kyostila | 26a0437 | 2020-01-13 12:46:48 +0000 | [diff] [blame] | 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(); |
Sami Kyostila | d4eec8e | 2020-05-05 15:56:42 +0100 | [diff] [blame] | 104 | UpdateTrack(track, serialized_desc); |
Sami Kyostila | 26a0437 | 2020-01-13 12:46:48 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void TrackRegistry::EraseTrack(Track track) { |
| 108 | std::lock_guard<std::mutex> lock(mutex_); |
| 109 | tracks_.erase(track.uuid); |
| 110 | } |
| 111 | |
| 112 | // static |
| 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 |