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