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 #include "perfetto/base/logging.h" 29 #include "perfetto/base/proc_utils.h" 30 #include "perfetto/tracing/tracing.h" 31 32 namespace perfetto { 33 34 namespace base { 35 class TaskRunner; 36 } // namespace base 37 38 // This abstract class is used to abstract dependencies on platform-specific 39 // primitives that cannot be implemented by the perfetto codebase and must be 40 // provided or overridden by the embedder. 41 // This is, for instance, for cases where we want to use some particular 42 // base:: class in Chrome and provide instead POSIX fallbacks for other 43 // embedders. 44 45 // Base class for thread-local objects. This is to get a basic object vtable and 46 // delegate destruction to the embedder. See Platform::CreateThreadLocalObject. 47 class PERFETTO_EXPORT PlatformThreadLocalObject { 48 public: 49 // Implemented by perfetto internal code. The embedder must call this when 50 // implementing GetOrCreateThreadLocalObject() to create an instance for the 51 // first time on each thread. 52 static std::unique_ptr<PlatformThreadLocalObject> CreateInstance(); 53 virtual ~PlatformThreadLocalObject(); 54 }; 55 56 class PERFETTO_EXPORT Platform { 57 public: 58 // Embedders can use this unless they have custom needs (e.g. Chrome wanting 59 // to use its own base class for TLS). 60 static Platform* GetDefaultPlatform(); 61 62 // Embedders can call this to set process ID in those cases where getpid() 63 // returns incorrect values (e.g. for sandboxed processes in Chromium). 64 // Should only be called once, before tracing has been initialized. SetCurrentProcessId(base::PlatformProcessId process_id)65 static void SetCurrentProcessId(base::PlatformProcessId process_id) { 66 PERFETTO_CHECK(!process_id_); 67 PERFETTO_DCHECK(!Tracing::IsInitialized()); 68 process_id_ = process_id; 69 } 70 71 // Returns process ID previously set by SetCurrentProcessId, or the process 72 // ID provided by the OS if no custom ID was provided. GetCurrentProcessId()73 static base::PlatformProcessId GetCurrentProcessId() { 74 if (process_id_) 75 return process_id_; 76 return base::GetProcessId(); 77 } 78 79 virtual ~Platform(); 80 81 // Creates a thread-local object. The embedder must: 82 // - Create an instance per-thread calling ThreadLocalObject::CreateInstance. 83 // - Own the lifetime of the returned object as long as the thread is alive. 84 // - Destroy it when the thread exits. 85 // Perfetto requires only one thread-local object overall (obviously, one 86 // instance per-thread) from the embedder. 87 using ThreadLocalObject = ::perfetto::PlatformThreadLocalObject; 88 virtual ThreadLocalObject* GetOrCreateThreadLocalObject() = 0; 89 90 // Creates a sequenced task runner. The easiest implementation is to create 91 // a new thread (e.g. use base::ThreadTaskRunner) but this can also be 92 // implemented in some more clever way (e.g. using chromiums's scheduler). 93 struct CreateTaskRunnerArgs { 94 // Optional. Sets the name to the newly created task runner. In the default 95 // PosixPlatform implementation this causes a pthread_setname_np(). This is 96 // only for ease of debugging, it does not affect the tracing behavior. 97 std::string name_for_debugging; 98 }; 99 virtual std::unique_ptr<base::TaskRunner> CreateTaskRunner( 100 const CreateTaskRunnerArgs&) = 0; 101 102 // Used to derive the producer name. Mostly relevant when using the 103 // kSystemBackend mode. It can be an arbitrary string when using the 104 // in-process mode. 105 virtual std::string GetCurrentProcessName() = 0; 106 107 private: 108 static base::PlatformProcessId process_id_; 109 }; 110 111 } // namespace perfetto 112 113 #endif // INCLUDE_PERFETTO_TRACING_PLATFORM_H_ 114