blob: 53bc8020b7e56cffcaa7eca0f7a21a13b3809bd5 [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"
Tuchila Octavianfaaaa6d2021-06-04 14:13:18 +010020#include "perfetto/ext/base/uuid.h"
Eric Secklera7870e62019-11-01 10:11:58 +000021#include "src/trace_processor/forwarding_trace_parser.h"
Alexander Timin5a99b5c2021-05-11 22:48:07 +000022#include "src/trace_processor/importers/chrome_track_event.descriptor.h"
Lalit Maganti617deae2020-04-14 21:00:49 +010023#include "src/trace_processor/importers/common/args_tracker.h"
Lalit Magantid5c45f42020-04-14 21:01:50 +010024#include "src/trace_processor/importers/common/clock_tracker.h"
25#include "src/trace_processor/importers/common/event_tracker.h"
AndrewB330c879ea42020-07-29 14:12:39 +030026#include "src/trace_processor/importers/common/flow_tracker.h"
Lalit Maganti617deae2020-04-14 21:00:49 +010027#include "src/trace_processor/importers/common/process_tracker.h"
Lalit Magantid5c45f42020-04-14 21:01:50 +010028#include "src/trace_processor/importers/common/slice_tracker.h"
Lalit Maganti617deae2020-04-14 21:00:49 +010029#include "src/trace_processor/importers/common/track_tracker.h"
Lalit Maganti60af4ef2020-04-15 14:27:09 +010030#include "src/trace_processor/importers/default_modules.h"
Lalit Magantif7a1a732020-10-12 15:42:34 +010031#include "src/trace_processor/importers/proto/async_track_set_tracker.h"
Lalit Maganti05819e22020-04-14 21:01:38 +010032#include "src/trace_processor/importers/proto/heap_profile_tracker.h"
33#include "src/trace_processor/importers/proto/metadata_tracker.h"
Ryan Savitskic6c7e842021-03-10 14:26:59 +000034#include "src/trace_processor/importers/proto/perf_sample_tracker.h"
Lalit Maganti0faddc42020-04-10 17:58:24 +010035#include "src/trace_processor/importers/proto/proto_importer_module.h"
Lalit Magantieb63b082020-09-10 14:12:20 +010036#include "src/trace_processor/importers/proto/proto_trace_reader.h"
Lalit Maganti05819e22020-04-14 21:01:38 +010037#include "src/trace_processor/importers/proto/stack_profile_tracker.h"
Alexander Timin5a99b5c2021-05-11 22:48:07 +000038#include "src/trace_processor/importers/track_event.descriptor.h"
Eric Secklera7870e62019-11-01 10:11:58 +000039#include "src/trace_processor/trace_sorter.h"
Alexander Timin5a99b5c2021-05-11 22:48:07 +000040#include "src/trace_processor/util/descriptors.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
Primiano Tucci3264b592021-11-08 18:20:51 +000085util::Status TraceProcessorStorageImpl::Parse(TraceBlobView blob) {
86 if (blob.size() == 0)
Eric Secklera7870e62019-11-01 10:11:58 +000087 return util::OkStatus();
88 if (unrecoverable_parse_error_)
89 return util::ErrStatus(
90 "Failed unrecoverably while parsing in a previous Parse call");
91 if (!context_.chunk_reader)
92 context_.chunk_reader.reset(new ForwardingTraceParser(&context_));
93
94 auto scoped_trace = context_.storage->TraceExecutionTimeIntoStats(
95 stats::parse_trace_duration_ns);
Tuchila Octavianfaaaa6d2021-06-04 14:13:18 +010096
97 if (hash_input_size_remaining_ > 0 && !context_.uuid_found_in_trace) {
Primiano Tucci3264b592021-11-08 18:20:51 +000098 const size_t hash_size = std::min(hash_input_size_remaining_, blob.size());
Tuchila Octavianfaaaa6d2021-06-04 14:13:18 +010099 hash_input_size_remaining_ -= hash_size;
100
Primiano Tucci3264b592021-11-08 18:20:51 +0000101 trace_hash_.Update(reinterpret_cast<const char*>(blob.data()), hash_size);
Tuchila Octavianfaaaa6d2021-06-04 14:13:18 +0100102 base::Uuid uuid(static_cast<int64_t>(trace_hash_.digest()), 0);
103 const StringId id_for_uuid =
104 context_.storage->InternString(base::StringView(uuid.ToPrettyString()));
105 context_.metadata_tracker->SetMetadata(metadata::trace_uuid,
106 Variadic::String(id_for_uuid));
107 }
108
Primiano Tucci3264b592021-11-08 18:20:51 +0000109 util::Status status = context_.chunk_reader->Parse(std::move(blob));
Eric Secklera7870e62019-11-01 10:11:58 +0000110 unrecoverable_parse_error_ |= !status.ok();
111 return status;
112}
113
114void TraceProcessorStorageImpl::NotifyEndOfFile() {
115 if (unrecoverable_parse_error_ || !context_.chunk_reader)
116 return;
117
Primiano Tucci40da82f2020-02-13 18:04:35 +0000118 context_.chunk_reader->NotifyEndOfFile();
Eric Secklera7870e62019-11-01 10:11:58 +0000119 if (context_.sorter)
120 context_.sorter->ExtractEventsForced();
Eric Secklera7870e62019-11-01 10:11:58 +0000121 context_.event_tracker->FlushPendingEvents();
122 context_.slice_tracker->FlushPendingSlices();
Florian Mayer0b6655d2020-02-14 09:09:21 -0500123 context_.heap_profile_tracker->NotifyEndOfFile();
Eric Secklerfbbf0222020-07-23 17:26:29 +0100124 context_.process_tracker->NotifyEndOfFile();
Florian Mayer0b6655d2020-02-14 09:09:21 -0500125 for (std::unique_ptr<ProtoImporterModule>& module : context_.modules) {
126 module->NotifyEndOfFile();
127 }
Sylwester Blaszczyk76aa7002020-11-27 15:48:21 +0100128 context_.args_tracker->Flush();
Eric Secklera7870e62019-11-01 10:11:58 +0000129}
130
131} // namespace trace_processor
132} // namespace perfetto