Igor Murashkin | b250cd8 | 2019-01-09 11:33:52 -0800 | [diff] [blame] | 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 | |
Igor Murashkin | 8c55619 | 2020-02-20 17:17:19 -0800 | [diff] [blame] | 21 | namespace android { |
| 22 | class Printer; |
| 23 | } // namespace android |
| 24 | |
Igor Murashkin | b250cd8 | 2019-01-09 11:33:52 -0800 | [diff] [blame] | 25 | namespace iorap::perfetto { |
| 26 | |
| 27 | // Abstract out the Perfetto C API behind a virtual interface: |
| 28 | // This enables us to use dependency injection to provide mock implementations |
| 29 | // during tests. |
| 30 | struct PerfettoConsumer { |
| 31 | // 1:1 aliasing of type definitions and constants in perfetto/public/consumer_api.h |
| 32 | // Refer to the documentation there. |
| 33 | using State = ::perfetto::consumer::State; |
| 34 | using Handle = ::perfetto::consumer::Handle; |
| 35 | static constexpr Handle kInvalidHandle = ::perfetto::consumer::kInvalidHandle; |
| 36 | using OnStateChangedCb = ::perfetto::consumer::OnStateChangedCb; |
| 37 | using TraceBuffer = ::perfetto::consumer::TraceBuffer; |
| 38 | |
| 39 | // 1:1 forwarding of C-style functions in perfetto/public/consumer_api.h |
| 40 | // Refer to the documentation there. |
| 41 | |
| 42 | virtual Handle Create(const void* config_proto, |
| 43 | size_t config_len, |
| 44 | OnStateChangedCb callback, |
| 45 | void* callback_arg) = 0; |
| 46 | virtual void StartTracing(Handle) = 0; |
| 47 | virtual TraceBuffer ReadTrace(Handle) = 0; |
| 48 | virtual void Destroy(Handle) = 0; |
| 49 | virtual State PollState(Handle) = 0; |
| 50 | |
| 51 | virtual ~PerfettoConsumer() {} |
| 52 | }; |
| 53 | |
| 54 | // "Live" implementation that calls down to libperfetto. |
Igor Murashkin | 4cf8984 | 2020-02-20 13:27:43 -0800 | [diff] [blame] | 55 | struct PerfettoConsumerRawImpl : public PerfettoConsumer { |
Igor Murashkin | b250cd8 | 2019-01-09 11:33:52 -0800 | [diff] [blame] | 56 | // Marks this constructor as the one to use for injection. |
Igor Murashkin | 4cf8984 | 2020-02-20 13:27:43 -0800 | [diff] [blame] | 57 | INJECT(PerfettoConsumerRawImpl()) = default; |
Igor Murashkin | b250cd8 | 2019-01-09 11:33:52 -0800 | [diff] [blame] | 58 | |
| 59 | virtual Handle Create(const void* config_proto, |
| 60 | size_t config_len, |
| 61 | OnStateChangedCb callback, |
| 62 | void* callback_arg) override { |
| 63 | return ::perfetto::consumer::Create(config_proto, |
| 64 | config_len, |
| 65 | callback, |
| 66 | callback_arg); |
| 67 | } |
| 68 | |
| 69 | virtual void StartTracing(Handle handle) override { |
| 70 | ::perfetto::consumer::StartTracing(handle); |
| 71 | } |
| 72 | |
| 73 | virtual TraceBuffer ReadTrace(Handle handle) override { |
| 74 | return ::perfetto::consumer::ReadTrace(handle); |
| 75 | } |
| 76 | |
| 77 | virtual void Destroy(Handle handle) override { |
| 78 | ::perfetto::consumer::Destroy(handle); |
| 79 | } |
| 80 | virtual State PollState(Handle handle) override { |
| 81 | return ::perfetto::consumer::PollState(handle); |
| 82 | } |
| 83 | |
Igor Murashkin | 4cf8984 | 2020-02-20 13:27:43 -0800 | [diff] [blame] | 84 | virtual ~PerfettoConsumerRawImpl() {} |
| 85 | }; |
| 86 | |
| 87 | // "Safe" implementation that has extra checking around it. |
| 88 | class PerfettoConsumerImpl : public PerfettoConsumer { |
| 89 | public: |
| 90 | // Marks this constructor as the one to use for injection. |
| 91 | INJECT(PerfettoConsumerImpl()) { Initialize(); } |
| 92 | |
| 93 | virtual Handle Create(const void* config_proto, |
| 94 | size_t config_len, |
| 95 | OnStateChangedCb callback, |
| 96 | void* callback_arg) override; |
| 97 | virtual void StartTracing(Handle handle) override; |
| 98 | virtual TraceBuffer ReadTrace(Handle handle) override; |
| 99 | virtual void Destroy(Handle handle) override; |
| 100 | virtual State PollState(Handle handle) override; |
| 101 | |
| 102 | virtual ~PerfettoConsumerImpl(); |
Igor Murashkin | 8c55619 | 2020-02-20 17:17:19 -0800 | [diff] [blame] | 103 | |
| 104 | static void Dump(/*borrow*/::android::Printer& printer); |
| 105 | |
Igor Murashkin | 4cf8984 | 2020-02-20 13:27:43 -0800 | [diff] [blame] | 106 | private: |
| 107 | void Initialize(); |
| 108 | struct Impl; |
| 109 | PerfettoConsumerImpl::Impl* impl_; |
Igor Murashkin | b250cd8 | 2019-01-09 11:33:52 -0800 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | } // namespace iorap::perfetto |
| 113 | |
| 114 | #endif // IORAP_SRC_PERFETTO_PERFETTO_CONSUMER_H_ |
| 115 | |