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