• 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 "src/trace_processor/trace_processor_storage_impl.h"
18 
19 #include "perfetto/base/logging.h"
20 #include "perfetto/ext/base/uuid.h"
21 #include "src/trace_processor/forwarding_trace_parser.h"
22 #include "src/trace_processor/importers/chrome_track_event.descriptor.h"
23 #include "src/trace_processor/importers/common/args_tracker.h"
24 #include "src/trace_processor/importers/common/args_translation_table.h"
25 #include "src/trace_processor/importers/common/clock_tracker.h"
26 #include "src/trace_processor/importers/common/event_tracker.h"
27 #include "src/trace_processor/importers/common/flow_tracker.h"
28 #include "src/trace_processor/importers/common/process_tracker.h"
29 #include "src/trace_processor/importers/common/slice_tracker.h"
30 #include "src/trace_processor/importers/common/slice_translation_table.h"
31 #include "src/trace_processor/importers/common/track_tracker.h"
32 #include "src/trace_processor/importers/default_modules.h"
33 #include "src/trace_processor/importers/proto/async_track_set_tracker.h"
34 #include "src/trace_processor/importers/proto/heap_profile_tracker.h"
35 #include "src/trace_processor/importers/proto/metadata_tracker.h"
36 #include "src/trace_processor/importers/proto/perf_sample_tracker.h"
37 #include "src/trace_processor/importers/proto/proto_importer_module.h"
38 #include "src/trace_processor/importers/proto/proto_trace_reader.h"
39 #include "src/trace_processor/importers/proto/stack_profile_tracker.h"
40 #include "src/trace_processor/importers/track_event.descriptor.h"
41 #include "src/trace_processor/trace_sorter.h"
42 #include "src/trace_processor/util/descriptors.h"
43 
44 namespace perfetto {
45 namespace trace_processor {
46 
TraceProcessorStorageImpl(const Config & cfg)47 TraceProcessorStorageImpl::TraceProcessorStorageImpl(const Config& cfg) {
48   context_.config = cfg;
49 
50   context_.storage.reset(new TraceStorage(context_.config));
51   context_.track_tracker.reset(new TrackTracker(&context_));
52   context_.async_track_set_tracker.reset(new AsyncTrackSetTracker(&context_));
53   context_.args_tracker.reset(new ArgsTracker(&context_));
54   context_.args_translation_table.reset(
55       new ArgsTranslationTable(context_.storage.get()));
56   context_.slice_tracker.reset(new SliceTracker(&context_));
57   context_.slice_translation_table.reset(
58       new SliceTranslationTable(context_.storage.get()));
59   context_.flow_tracker.reset(new FlowTracker(&context_));
60   context_.event_tracker.reset(new EventTracker(&context_));
61   context_.process_tracker.reset(new ProcessTracker(&context_));
62   context_.clock_tracker.reset(new ClockTracker(&context_));
63   context_.heap_profile_tracker.reset(new HeapProfileTracker(&context_));
64   context_.perf_sample_tracker.reset(new PerfSampleTracker(&context_));
65   context_.global_stack_profile_tracker.reset(new GlobalStackProfileTracker());
66   context_.metadata_tracker.reset(new MetadataTracker(&context_));
67   context_.global_args_tracker.reset(new GlobalArgsTracker(&context_));
68   {
69     context_.descriptor_pool_.reset(new DescriptorPool());
70     auto status = context_.descriptor_pool_->AddFromFileDescriptorSet(
71         kTrackEventDescriptor.data(), kTrackEventDescriptor.size());
72 
73     PERFETTO_DCHECK(status.ok());
74 
75     status = context_.descriptor_pool_->AddFromFileDescriptorSet(
76         kChromeTrackEventDescriptor.data(), kChromeTrackEventDescriptor.size());
77 
78     PERFETTO_DCHECK(status.ok());
79   }
80 
81   context_.slice_tracker->SetOnSliceBeginCallback(
82       [this](TrackId track_id, SliceId slice_id) {
83         context_.flow_tracker->ClosePendingEventsOnTrack(track_id, slice_id);
84       });
85 
86   RegisterDefaultModules(&context_);
87 }
88 
~TraceProcessorStorageImpl()89 TraceProcessorStorageImpl::~TraceProcessorStorageImpl() {}
90 
Parse(TraceBlobView blob)91 util::Status TraceProcessorStorageImpl::Parse(TraceBlobView blob) {
92   if (blob.size() == 0)
93     return util::OkStatus();
94   if (unrecoverable_parse_error_)
95     return util::ErrStatus(
96         "Failed unrecoverably while parsing in a previous Parse call");
97   if (!context_.chunk_reader)
98     context_.chunk_reader.reset(new ForwardingTraceParser(&context_));
99 
100   auto scoped_trace = context_.storage->TraceExecutionTimeIntoStats(
101       stats::parse_trace_duration_ns);
102 
103   if (hash_input_size_remaining_ > 0 && !context_.uuid_found_in_trace) {
104     const size_t hash_size = std::min(hash_input_size_remaining_, blob.size());
105     hash_input_size_remaining_ -= hash_size;
106 
107     trace_hash_.Update(reinterpret_cast<const char*>(blob.data()), hash_size);
108     base::Uuid uuid(static_cast<int64_t>(trace_hash_.digest()), 0);
109     const StringId id_for_uuid =
110         context_.storage->InternString(base::StringView(uuid.ToPrettyString()));
111     context_.metadata_tracker->SetMetadata(metadata::trace_uuid,
112                                            Variadic::String(id_for_uuid));
113   }
114 
115   util::Status status = context_.chunk_reader->Parse(std::move(blob));
116   unrecoverable_parse_error_ |= !status.ok();
117   return status;
118 }
119 
NotifyEndOfFile()120 void TraceProcessorStorageImpl::NotifyEndOfFile() {
121   if (unrecoverable_parse_error_ || !context_.chunk_reader)
122     return;
123 
124   context_.chunk_reader->NotifyEndOfFile();
125   if (context_.sorter)
126     context_.sorter->ExtractEventsForced();
127   context_.event_tracker->FlushPendingEvents();
128   context_.slice_tracker->FlushPendingSlices();
129   context_.heap_profile_tracker->NotifyEndOfFile();
130   context_.process_tracker->NotifyEndOfFile();
131   for (std::unique_ptr<ProtoImporterModule>& module : context_.modules) {
132     module->NotifyEndOfFile();
133   }
134   context_.args_tracker->Flush();
135 }
136 
137 }  // namespace trace_processor
138 }  // namespace perfetto
139