• 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 "src/trace_processor/forwarding_trace_parser.h"
21 #include "src/trace_processor/importers/common/args_tracker.h"
22 #include "src/trace_processor/importers/common/clock_tracker.h"
23 #include "src/trace_processor/importers/common/event_tracker.h"
24 #include "src/trace_processor/importers/common/process_tracker.h"
25 #include "src/trace_processor/importers/common/slice_tracker.h"
26 #include "src/trace_processor/importers/common/track_tracker.h"
27 #include "src/trace_processor/importers/default_modules.h"
28 #include "src/trace_processor/importers/proto/heap_profile_tracker.h"
29 #include "src/trace_processor/importers/proto/metadata_tracker.h"
30 #include "src/trace_processor/importers/proto/perf_sample_tracker.h"
31 #include "src/trace_processor/importers/proto/proto_importer_module.h"
32 #include "src/trace_processor/importers/proto/proto_trace_tokenizer.h"
33 #include "src/trace_processor/importers/proto/stack_profile_tracker.h"
34 #include "src/trace_processor/trace_blob_view.h"
35 #include "src/trace_processor/trace_sorter.h"
36 
37 namespace perfetto {
38 namespace trace_processor {
39 
TraceProcessorStorageImpl(const Config & cfg)40 TraceProcessorStorageImpl::TraceProcessorStorageImpl(const Config& cfg) {
41   context_.config = cfg;
42   context_.storage.reset(new TraceStorage(context_.config));
43   context_.track_tracker.reset(new TrackTracker(&context_));
44   context_.args_tracker.reset(new ArgsTracker(&context_));
45   context_.slice_tracker.reset(new SliceTracker(&context_));
46   context_.event_tracker.reset(new EventTracker(&context_));
47   context_.process_tracker.reset(new ProcessTracker(&context_));
48   context_.clock_tracker.reset(new ClockTracker(&context_));
49   context_.heap_profile_tracker.reset(new HeapProfileTracker(&context_));
50   context_.metadata_tracker.reset(new MetadataTracker(&context_));
51   context_.global_args_tracker.reset(new GlobalArgsTracker(&context_));
52   context_.perf_sample_tracker.reset(new PerfSampleTracker(&context_));
53 
54   RegisterDefaultModules(&context_);
55 }
56 
~TraceProcessorStorageImpl()57 TraceProcessorStorageImpl::~TraceProcessorStorageImpl() {}
58 
Parse(std::unique_ptr<uint8_t[]> data,size_t size)59 util::Status TraceProcessorStorageImpl::Parse(std::unique_ptr<uint8_t[]> data,
60                                               size_t size) {
61   if (size == 0)
62     return util::OkStatus();
63   if (unrecoverable_parse_error_)
64     return util::ErrStatus(
65         "Failed unrecoverably while parsing in a previous Parse call");
66   if (!context_.chunk_reader)
67     context_.chunk_reader.reset(new ForwardingTraceParser(&context_));
68 
69   auto scoped_trace = context_.storage->TraceExecutionTimeIntoStats(
70       stats::parse_trace_duration_ns);
71   util::Status status = context_.chunk_reader->Parse(std::move(data), size);
72   unrecoverable_parse_error_ |= !status.ok();
73   return status;
74 }
75 
NotifyEndOfFile()76 void TraceProcessorStorageImpl::NotifyEndOfFile() {
77   if (unrecoverable_parse_error_ || !context_.chunk_reader)
78     return;
79 
80   context_.chunk_reader->NotifyEndOfFile();
81   if (context_.sorter)
82     context_.sorter->ExtractEventsForced();
83   context_.event_tracker->FlushPendingEvents();
84   context_.slice_tracker->FlushPendingSlices();
85   context_.heap_profile_tracker->NotifyEndOfFile();
86   for (std::unique_ptr<ProtoImporterModule>& module : context_.modules) {
87     module->NotifyEndOfFile();
88   }
89 }
90 
91 }  // namespace trace_processor
92 }  // namespace perfetto
93