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