1 /* 2 * Copyright (C) 2019 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_PLATFORM_H_ 18 #define INCLUDE_PERFETTO_TRACING_PLATFORM_H_ 19 20 #include <stddef.h> 21 #include <stdint.h> 22 23 #include <functional> 24 #include <memory> 25 #include <string> 26 27 #include "perfetto/base/export.h" 28 29 namespace perfetto { 30 31 namespace base { 32 class TaskRunner; 33 } // namespace base 34 35 // This abstract class is used to abstract dependencies on platform-specific 36 // primitives that cannot be implemented by the perfetto codebase and must be 37 // provided or overridden by the embedder. 38 // This is, for instance, for cases where we want to use some particular 39 // base:: class in Chrome and provide instead POSIX fallbacks for other 40 // embedders. 41 42 // Base class for thread-local objects. This is to get a basic object vtable and 43 // delegate destruction to the embedder. See Platform::CreateThreadLocalObject. 44 class PERFETTO_EXPORT PlatformThreadLocalObject { 45 public: 46 // Implemented by perfetto internal code. The embedder must call this when 47 // implementing GetOrCreateThreadLocalObject() to create an instance for the 48 // first time on each thread. 49 static std::unique_ptr<PlatformThreadLocalObject> CreateInstance(); 50 virtual ~PlatformThreadLocalObject(); 51 }; 52 53 class PERFETTO_EXPORT Platform { 54 public: 55 // Embedders can use this unless they have custom needs (e.g. Chrome wanting 56 // to use its own base class for TLS). 57 static Platform* GetDefaultPlatform(); 58 59 virtual ~Platform(); 60 61 // Creates a thread-local object. The embedder must: 62 // - Create an instance per-thread calling ThreadLocalObject::CreateInstance. 63 // - Own the lifetime of the returned object as long as the thread is alive. 64 // - Destroy it when the thread exits. 65 // Perfetto requires only one thread-local object overall (obviously, one 66 // instance per-thread) from the embedder. 67 using ThreadLocalObject = ::perfetto::PlatformThreadLocalObject; 68 virtual ThreadLocalObject* GetOrCreateThreadLocalObject() = 0; 69 70 // Creates a sequenced task runner. The easiest implementation is to create 71 // a new thread (e.g. use base::ThreadTaskRunner) but this can also be 72 // implemented in some more clever way (e.g. using chromiums's scheduler). 73 struct CreateTaskRunnerArgs {}; 74 virtual std::unique_ptr<base::TaskRunner> CreateTaskRunner( 75 const CreateTaskRunnerArgs&) = 0; 76 77 // Used to derive the producer name. Mostly relevant when using the 78 // kSystemBackend mode. It can be an arbitrary string when using the 79 // in-process mode. 80 virtual std::string GetCurrentProcessName() = 0; 81 }; 82 83 } // namespace perfetto 84 85 #endif // INCLUDE_PERFETTO_TRACING_PLATFORM_H_ 86