blob: e9c0f37e74d6efb5d18bfeaa554221b84b5a2a0f [file] [log] [blame]
Eric Secklera7870e62019-11-01 10:11:58 +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 "src/trace_processor/trace_processor_storage_impl.h"
18
19#include "perfetto/base/logging.h"
Eric Secklera7870e62019-11-01 10:11:58 +000020#include "src/trace_processor/forwarding_trace_parser.h"
Alexander Timin5a99b5c2021-05-11 22:48:07 +000021#include "src/trace_processor/importers/chrome_track_event.descriptor.h"
Lalit Maganti617deae2020-04-14 21:00:49 +010022#include "src/trace_processor/importers/common/args_tracker.h"
Lalit Magantid5c45f42020-04-14 21:01:50 +010023#include "src/trace_processor/importers/common/clock_tracker.h"
24#include "src/trace_processor/importers/common/event_tracker.h"
AndrewB330c879ea42020-07-29 14:12:39 +030025#include "src/trace_processor/importers/common/flow_tracker.h"
Lalit Maganti617deae2020-04-14 21:00:49 +010026#include "src/trace_processor/importers/common/process_tracker.h"
Lalit Magantid5c45f42020-04-14 21:01:50 +010027#include "src/trace_processor/importers/common/slice_tracker.h"
Lalit Maganti617deae2020-04-14 21:00:49 +010028#include "src/trace_processor/importers/common/track_tracker.h"
Lalit Maganti60af4ef2020-04-15 14:27:09 +010029#include "src/trace_processor/importers/default_modules.h"
Lalit Magantif7a1a732020-10-12 15:42:34 +010030#include "src/trace_processor/importers/proto/async_track_set_tracker.h"
Lalit Maganti05819e22020-04-14 21:01:38 +010031#include "src/trace_processor/importers/proto/heap_profile_tracker.h"
32#include "src/trace_processor/importers/proto/metadata_tracker.h"
Ryan Savitskic6c7e842021-03-10 14:26:59 +000033#include "src/trace_processor/importers/proto/perf_sample_tracker.h"
Lalit Maganti0faddc42020-04-10 17:58:24 +010034#include "src/trace_processor/importers/proto/proto_importer_module.h"
Lalit Magantieb63b082020-09-10 14:12:20 +010035#include "src/trace_processor/importers/proto/proto_trace_reader.h"
Lalit Maganti05819e22020-04-14 21:01:38 +010036#include "src/trace_processor/importers/proto/stack_profile_tracker.h"
Alexander Timin5a99b5c2021-05-11 22:48:07 +000037#include "src/trace_processor/importers/track_event.descriptor.h"
Eric Secklera7870e62019-11-01 10:11:58 +000038#include "src/trace_processor/trace_sorter.h"
Alexander Timin5a99b5c2021-05-11 22:48:07 +000039#include "src/trace_processor/util/descriptors.h"
Alexander Timin97d87852021-05-17 18:01:33 +000040#include "src/trace_processor/util/trace_blob_view.h"
Eric Secklera7870e62019-11-01 10:11:58 +000041
42namespace perfetto {
43namespace trace_processor {
44
45TraceProcessorStorageImpl::TraceProcessorStorageImpl(const Config& cfg) {
46 context_.config = cfg;
Lalit Maganti88eb6982021-03-01 14:28:51 +000047
Eric Seckler0eccc522019-12-04 11:21:21 +000048 context_.storage.reset(new TraceStorage(context_.config));
Eric Secklera7870e62019-11-01 10:11:58 +000049 context_.track_tracker.reset(new TrackTracker(&context_));
Lalit Magantif7a1a732020-10-12 15:42:34 +010050 context_.async_track_set_tracker.reset(new AsyncTrackSetTracker(&context_));
Eric Secklera7870e62019-11-01 10:11:58 +000051 context_.args_tracker.reset(new ArgsTracker(&context_));
52 context_.slice_tracker.reset(new SliceTracker(&context_));
AndrewB330c879ea42020-07-29 14:12:39 +030053 context_.flow_tracker.reset(new FlowTracker(&context_));
Eric Secklera7870e62019-11-01 10:11:58 +000054 context_.event_tracker.reset(new EventTracker(&context_));
55 context_.process_tracker.reset(new ProcessTracker(&context_));
Eric Secklera7870e62019-11-01 10:11:58 +000056 context_.clock_tracker.reset(new ClockTracker(&context_));
57 context_.heap_profile_tracker.reset(new HeapProfileTracker(&context_));
Ryan Savitskic6c7e842021-03-10 14:26:59 +000058 context_.perf_sample_tracker.reset(new PerfSampleTracker(&context_));
Florian Mayer22668742020-08-13 10:39:41 +010059 context_.global_stack_profile_tracker.reset(new GlobalStackProfileTracker());
Lalit Magantiededb0e2020-01-08 12:50:34 +000060 context_.metadata_tracker.reset(new MetadataTracker(&context_));
Lalit Maganti1908e262020-01-09 14:33:19 +000061 context_.global_args_tracker.reset(new GlobalArgsTracker(&context_));
Alexander Timin5a99b5c2021-05-11 22:48:07 +000062 {
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 }
Mikhail Khokhlovdd1db002019-12-09 16:34:07 +000074
AndrewB330c879ea42020-07-29 14:12:39 +030075 context_.slice_tracker->SetOnSliceBeginCallback(
76 [this](TrackId track_id, SliceId slice_id) {
77 context_.flow_tracker->ClosePendingEventsOnTrack(track_id, slice_id);
78 });
79
Eric Secklerf2de4db2020-02-14 14:17:20 +000080 RegisterDefaultModules(&context_);
Eric Secklera7870e62019-11-01 10:11:58 +000081}
82
83TraceProcessorStorageImpl::~TraceProcessorStorageImpl() {}
84
85util::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
102void TraceProcessorStorageImpl::NotifyEndOfFile() {
103 if (unrecoverable_parse_error_ || !context_.chunk_reader)
104 return;
105
Primiano Tucci40da82f2020-02-13 18:04:35 +0000106 context_.chunk_reader->NotifyEndOfFile();
Eric Secklera7870e62019-11-01 10:11:58 +0000107 if (context_.sorter)
108 context_.sorter->ExtractEventsForced();
Eric Secklera7870e62019-11-01 10:11:58 +0000109 context_.event_tracker->FlushPendingEvents();
110 context_.slice_tracker->FlushPendingSlices();
Florian Mayer0b6655d2020-02-14 09:09:21 -0500111 context_.heap_profile_tracker->NotifyEndOfFile();
Eric Secklerfbbf0222020-07-23 17:26:29 +0100112 context_.process_tracker->NotifyEndOfFile();
Florian Mayer0b6655d2020-02-14 09:09:21 -0500113 for (std::unique_ptr<ProtoImporterModule>& module : context_.modules) {
114 module->NotifyEndOfFile();
115 }
Sylwester Blaszczyk76aa7002020-11-27 15:48:21 +0100116 context_.args_tracker->Flush();
Eric Secklera7870e62019-11-01 10:11:58 +0000117}
118
119} // namespace trace_processor
120} // namespace perfetto