• 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 #include "src/trace_processor/trace_reader_registry.h"
18 #include "perfetto/base/logging.h"
19 #include "src/trace_processor/importers/common/chunked_trace_reader.h"
20 #include "src/trace_processor/types/trace_processor_context.h"
21 #include "src/trace_processor/util/gzip_utils.h"
22 #include "src/trace_processor/util/trace_type.h"
23 
24 namespace perfetto::trace_processor {
25 namespace {
26 const char kNoZlibErr[] =
27     "Cannot open compressed trace. zlib not enabled in the build config";
28 
RequiresZlibSupport(TraceType type)29 bool RequiresZlibSupport(TraceType type) {
30   switch (type) {
31     case kGzipTraceType:
32     case kCtraceTraceType:
33     case kZipFile:
34       return true;
35 
36     case kNinjaLogTraceType:
37     case kSystraceTraceType:
38     case kPerfDataTraceType:
39     case kUnknownTraceType:
40     case kJsonTraceType:
41     case kFuchsiaTraceType:
42     case kProtoTraceType:
43       return false;
44   }
45   PERFETTO_FATAL("For GCC");
46 }
47 }  // namespace
48 
RegisterFactory(TraceType trace_type,Factory factory)49 void TraceReaderRegistry::RegisterFactory(TraceType trace_type,
50                                           Factory factory) {
51   PERFETTO_CHECK(factories_.Insert(trace_type, std::move(factory)).second);
52 }
53 
54 base::StatusOr<std::unique_ptr<ChunkedTraceReader>>
CreateTraceReader(TraceType type)55 TraceReaderRegistry::CreateTraceReader(TraceType type) {
56   if (auto it = factories_.Find(type); it) {
57     return (*it)(context_);
58   }
59 
60   if (RequiresZlibSupport(type) && !util::IsGzipSupported()) {
61     return base::ErrStatus("%s support is disabled. %s", ToString(type),
62                            kNoZlibErr);
63   }
64 
65   return base::ErrStatus("%s support is disabled", ToString(type));
66 }
67 
68 }  // namespace perfetto::trace_processor
69