1 /* 2 * Copyright (C) 2023 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_CLOUD_TRACE_PROCESSOR_TRACE_PROCESSOR_WRAPPER_H_ 18 #define SRC_CLOUD_TRACE_PROCESSOR_TRACE_PROCESSOR_WRAPPER_H_ 19 20 #include "perfetto/ext/base/threading/future.h" 21 #include "perfetto/ext/base/threading/stream.h" 22 #include "perfetto/ext/base/threading/thread_pool.h" 23 #include "perfetto/trace_processor/trace_processor.h" 24 #include "src/trace_processor/rpc/query_result_serializer.h" 25 26 namespace perfetto { 27 namespace protos { 28 29 class TracePoolShardQueryResponse; 30 31 } // namespace protos 32 } // namespace perfetto 33 34 namespace perfetto { 35 namespace cloud_trace_processor { 36 37 // Wrapper class around an instance of TraceProcessor to adapt it for the needs 38 // of a CloudTraceProcessor Worker. 39 class TraceProcessorWrapper { 40 public: 41 enum Statefulness { 42 // Indicates that the state of the trace processor instance should be purged 43 // after every query. 44 kStateless, 45 46 // Indicates that the state of the trace processor instance should be 47 // preserved across queries. 48 kStateful, 49 }; 50 51 TraceProcessorWrapper(std::string trace_path, 52 base::ThreadPool*, 53 Statefulness); 54 55 // Loads the trace given a stream of chunks to parse. 56 base::StatusFuture LoadTrace( 57 base::StatusOrStream<std::vector<uint8_t>> file_stream); 58 59 // Executes the given query on the trace processor and returns the results 60 // as a stream. 61 base::StatusOrStream<protos::TracePoolShardQueryResponse> Query( 62 const std::string& sql); 63 64 private: 65 using TraceProcessor = trace_processor::TraceProcessor; 66 67 TraceProcessorWrapper(const TraceProcessorWrapper&) = delete; 68 TraceProcessorWrapper& operator=(const TraceProcessorWrapper&) = delete; 69 70 TraceProcessorWrapper(TraceProcessorWrapper&&) = delete; 71 TraceProcessorWrapper& operator=(TraceProcessorWrapper&&) = delete; 72 73 const std::string trace_path_; 74 base::ThreadPool* thread_pool_ = nullptr; 75 const Statefulness statefulness_ = Statefulness::kStateless; 76 std::shared_ptr<TraceProcessor> trace_processor_; 77 }; 78 79 } // namespace cloud_trace_processor 80 } // namespace perfetto 81 82 #endif // SRC_CLOUD_TRACE_PROCESSOR_TRACE_PROCESSOR_WRAPPER_H_ 83