1 // Copyright (C) 2018 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef IORAP_SRC_PERFETTO_PERFETTO_CONSUMER_H_
16 #define IORAP_SRC_PERFETTO_PERFETTO_CONSUMER_H_
17 
18 #include <fruit/fruit.h>
19 #include <perfetto/public/consumer_api.h>  // libperfetto
20 
21 namespace iorap::perfetto {
22 
23 // Abstract out the Perfetto C API behind a virtual interface:
24 // This enables us to use dependency injection to provide mock implementations
25 // during tests.
26 struct PerfettoConsumer {
27   // 1:1 aliasing of type definitions and constants in perfetto/public/consumer_api.h
28   // Refer to the documentation there.
29   using State = ::perfetto::consumer::State;
30   using Handle = ::perfetto::consumer::Handle;
31   static constexpr Handle kInvalidHandle = ::perfetto::consumer::kInvalidHandle;
32   using OnStateChangedCb = ::perfetto::consumer::OnStateChangedCb;
33   using TraceBuffer = ::perfetto::consumer::TraceBuffer;
34 
35   // 1:1 forwarding of C-style functions in perfetto/public/consumer_api.h
36   // Refer to the documentation there.
37 
38   virtual Handle Create(const void* config_proto,
39                         size_t config_len,
40                         OnStateChangedCb callback,
41                         void* callback_arg) = 0;
42   virtual void StartTracing(Handle) = 0;
43   virtual TraceBuffer ReadTrace(Handle) = 0;
44   virtual void Destroy(Handle) = 0;
45   virtual State PollState(Handle) = 0;
46 
~PerfettoConsumerPerfettoConsumer47   virtual ~PerfettoConsumer() {}
48 };
49 
50 // "Live" implementation that calls down to libperfetto.
51 struct PerfettoConsumerImpl : public PerfettoConsumer {
52   // Marks this constructor as the one to use for injection.
53   INJECT(PerfettoConsumerImpl()) = default;
54 
CreatePerfettoConsumerImpl55   virtual Handle Create(const void* config_proto,
56                         size_t config_len,
57                         OnStateChangedCb callback,
58                         void* callback_arg) override {
59     return ::perfetto::consumer::Create(config_proto,
60                                       config_len,
61                                       callback,
62                                       callback_arg);
63   }
64 
StartTracingPerfettoConsumerImpl65   virtual void StartTracing(Handle handle) override {
66     ::perfetto::consumer::StartTracing(handle);
67   }
68 
ReadTracePerfettoConsumerImpl69   virtual TraceBuffer ReadTrace(Handle handle) override {
70     return ::perfetto::consumer::ReadTrace(handle);
71   }
72 
DestroyPerfettoConsumerImpl73   virtual void Destroy(Handle handle) override {
74     ::perfetto::consumer::Destroy(handle);
75   }
PollStatePerfettoConsumerImpl76   virtual State PollState(Handle handle) override {
77     return ::perfetto::consumer::PollState(handle);
78   }
79 
~PerfettoConsumerImplPerfettoConsumerImpl80   virtual ~PerfettoConsumerImpl() {}
81 };
82 
83 }  // namespace iorap::perfetto
84 
85 #endif  // IORAP_SRC_PERFETTO_PERFETTO_CONSUMER_H_
86 
87