• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 #ifndef SRC_TRACE_PROCESSOR_TRACE_READER_REGISTRY_H_
18 #define SRC_TRACE_PROCESSOR_TRACE_READER_REGISTRY_H_
19 
20 #include <functional>
21 #include <memory>
22 #include <optional>
23 
24 #include "perfetto/ext/base/flat_hash_map.h"
25 
26 #include "perfetto/ext/base/status_or.h"
27 #include "src/trace_processor/importers/common/trace_parser.h"
28 #include "src/trace_processor/sorter/trace_sorter.h"
29 #include "src/trace_processor/util/trace_type.h"
30 
31 namespace perfetto {
32 namespace trace_processor {
33 
34 class ChunkedTraceReader;
35 class TraceProcessorContext;
36 
37 // Maps `TraceType` values to `ChunkedTraceReader` subclasses.
38 // This class is used to create `ChunkedTraceReader` instances for a given
39 // `TraceType`.
40 class TraceReaderRegistry {
41  public:
TraceReaderRegistry(TraceProcessorContext * context)42   explicit TraceReaderRegistry(TraceProcessorContext* context)
43       : context_(context) {}
44 
45   // Registers a mapping from `TraceType` value to `ChunkedTraceReader`
46   // subclass. Only one such mapping can be registered per `TraceType` value.
47   template <typename Reader>
RegisterTraceReader(TraceType trace_type)48   void RegisterTraceReader(TraceType trace_type) {
49     RegisterFactory(trace_type, [](TraceProcessorContext* ctxt) {
50       return std::make_unique<Reader>(ctxt);
51     });
52   }
53 
54   // Creates a new `ChunkedTraceReader` instance for the given `type`. Returns
55   // an error if no mapping has been previously registered.
56   base::StatusOr<std::unique_ptr<ChunkedTraceReader>> CreateTraceReader(
57       TraceType type);
58 
59  private:
60   using Factory = std::function<std::unique_ptr<ChunkedTraceReader>(
61       TraceProcessorContext*)>;
62   void RegisterFactory(TraceType trace_type, Factory factory);
63 
64   TraceProcessorContext* const context_;
65   base::FlatHashMap<TraceType,
66                     std::function<std::unique_ptr<ChunkedTraceReader>(
67                         TraceProcessorContext*)>>
68       factories_;
69 };
70 
71 }  // namespace trace_processor
72 }  // namespace perfetto
73 
74 #endif  // SRC_TRACE_PROCESSOR_TRACE_READER_REGISTRY_H_
75