• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 INCLUDE_PERFETTO_TRACING_INTERNAL_INTERCEPTOR_TRACE_WRITER_H_
18 #define INCLUDE_PERFETTO_TRACING_INTERNAL_INTERCEPTOR_TRACE_WRITER_H_
19 
20 #include "perfetto/protozero/scattered_heap_buffer.h"
21 #include "perfetto/tracing/interceptor.h"
22 #include "perfetto/tracing/internal/basic_types.h"
23 #include "perfetto/tracing/trace_writer_base.h"
24 #include "protos/perfetto/trace/trace_packet.pbzero.h"
25 
26 namespace perfetto {
27 namespace internal {
28 
29 // A heap-backed trace writer used to reroute trace packets to an interceptor.
30 class InterceptorTraceWriter : public TraceWriterBase {
31  public:
32   InterceptorTraceWriter(std::unique_ptr<InterceptorBase::ThreadLocalState> tls,
33                          InterceptorBase::TracePacketCallback packet_callback,
34                          DataSourceStaticState* static_state,
35                          uint32_t instance_index);
36   ~InterceptorTraceWriter() override;
37 
38   // TraceWriterBase implementation.
39   protozero::MessageHandle<protos::pbzero::TracePacket> NewTracePacket()
40       override;
41   void Flush(std::function<void()> callback = {}) override;
42   uint64_t written() const override;
43 
44  private:
45   std::unique_ptr<InterceptorBase::ThreadLocalState> tls_;
46   InterceptorBase::TracePacketCallback packet_callback_;
47 
48   protozero::HeapBuffered<protos::pbzero::TracePacket> cur_packet_;
49   uint64_t bytes_written_ = 0;
50 
51   // Static state of the data source we are intercepting.
52   DataSourceStaticState* const static_state_;
53 
54   // Index of the data source tracing session which we are intercepting
55   // (0...kMaxDataSourceInstances - 1). Used to look up this interceptor's
56   // session state (i.e., the Interceptor class instance) in the
57   // DataSourceStaticState::instances array.
58   const uint32_t instance_index_;
59 
60   const uint32_t sequence_id_;
61 
62   static std::atomic<uint32_t> next_sequence_id_;
63 };
64 
65 }  // namespace internal
66 }  // namespace perfetto
67 
68 #endif  // INCLUDE_PERFETTO_TRACING_INTERNAL_INTERCEPTOR_TRACE_WRITER_H_
69