1 /*
2  * Copyright (C) 2021 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_TRACING_INTERNAL_TRACING_MUXER_FAKE_H_
18 #define SRC_TRACING_INTERNAL_TRACING_MUXER_FAKE_H_
19 
20 #include "perfetto/base/compiler.h"
21 #include "perfetto/tracing/internal/tracing_muxer.h"
22 
23 namespace perfetto {
24 namespace internal {
25 
26 // An always-fail implementation of TracingMuxer. Before tracing has been
27 // initialiazed, all muxer operations will route here and fail with a helpful
28 // error message. This is to avoid introducing null checks in
29 // performance-critical parts of the codebase.
30 class TracingMuxerFake : public TracingMuxer {
31   class FakePlatform : public Platform {
32    public:
33     ~FakePlatform() override;
34     ThreadLocalObject* GetOrCreateThreadLocalObject() override;
35     std::unique_ptr<base::TaskRunner> CreateTaskRunner(
36         const CreateTaskRunnerArgs&) override;
37     std::string GetCurrentProcessName() override;
38 
39     static FakePlatform instance;
40   };
41 
42  public:
TracingMuxerFake()43   TracingMuxerFake() : TracingMuxer(&FakePlatform::instance) {}
44 
Get()45   static constexpr TracingMuxerFake* Get() {
46 #if PERFETTO_HAS_NO_DESTROY()
47     return &instance;
48 #else
49     return nullptr;
50 #endif
51   }
52 
53   // TracingMuxer implementation.
54   bool RegisterDataSource(const DataSourceDescriptor&,
55                           DataSourceFactory,
56                           DataSourceStaticState*) override;
57   std::unique_ptr<TraceWriterBase> CreateTraceWriter(
58       DataSourceStaticState*,
59       uint32_t data_source_instance_index,
60       DataSourceState*,
61       BufferExhaustedPolicy buffer_exhausted_policy) override;
62   void DestroyStoppedTraceWritersForCurrentThread() override;
63   void RegisterInterceptor(const InterceptorDescriptor&,
64                            InterceptorFactory,
65                            InterceptorBase::TLSFactory,
66                            InterceptorBase::TracePacketCallback) override;
67 
68  private:
69   static TracingMuxerFake instance;
70 };
71 
72 }  // namespace internal
73 }  // namespace perfetto
74 
75 #endif  // SRC_TRACING_INTERNAL_TRACING_MUXER_FAKE_H_
76