Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / tools / profiler / public / ProfilerThreadPlatformData.h
blobc243a8ee02a5618a057171eb566a512682547310
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef ProfilerThreadPlatformData_h
8 #define ProfilerThreadPlatformData_h
10 #include "mozilla/ProfilerUtils.h"
12 #if defined(__APPLE__)
13 # include <mach/mach_types.h>
14 #elif defined(__linux__) || defined(__ANDROID__) || defined(__FreeBSD__)
15 # include "mozilla/Maybe.h"
16 # include <time.h>
17 #endif
19 namespace mozilla::profiler {
21 class PlatformData {
22 #if (defined(_MSC_VER) || defined(__MINGW32__)) && defined(MOZ_GECKO_PROFILER)
23 public:
24 explicit PlatformData(ProfilerThreadId aThreadId);
25 ~PlatformData();
27 // Faking win32's HANDLE, because #including "windows.h" here causes trouble
28 // (e.g., it #defines `Yield` as nothing!)
29 // This type is static_check'ed against HANDLE in platform-win32.cpp.
30 using WindowsHandle = void*;
31 WindowsHandle ProfiledThread() const { return mProfiledThread; }
33 private:
34 WindowsHandle mProfiledThread;
35 #elif defined(__APPLE__) && defined(MOZ_GECKO_PROFILER)
36 public:
37 explicit PlatformData(ProfilerThreadId aThreadId);
38 ~PlatformData();
39 thread_act_t ProfiledThread() const { return mProfiledThread; }
41 private:
42 // Note: for mProfiledThread Mach primitives are used instead of pthread's
43 // because the latter doesn't provide thread manipulation primitives
44 // required. For details, consult "Mac OS X Internals" book, Section 7.3.
45 thread_act_t mProfiledThread;
46 #elif (defined(__linux__) || defined(__ANDROID__) || defined(__FreeBSD__)) && \
47 defined(MOZ_GECKO_PROFILER)
48 public:
49 explicit PlatformData(ProfilerThreadId aThreadId);
50 ~PlatformData();
51 // Clock Id for this profiled thread. `Nothing` if `pthread_getcpuclockid`
52 // failed (e.g., if the system doesn't support per-thread clocks).
53 Maybe<clockid_t> GetClockId() const { return mClockId; }
55 private:
56 Maybe<clockid_t> mClockId;
57 #else
58 public:
59 explicit PlatformData(ProfilerThreadId aThreadId) {}
60 #endif
63 /**
64 * Return the number of nanoseconds of CPU time used since thread start.
66 * @return true on success.
68 #if defined(MOZ_GECKO_PROFILER)
69 bool GetCpuTimeSinceThreadStartInNs(uint64_t* aResult,
70 const PlatformData& aPlatformData);
71 #else
72 static inline bool GetCpuTimeSinceThreadStartInNs(
73 uint64_t* aResult, const PlatformData& aPlatformData) {
74 return false;
76 #endif
78 } // namespace mozilla::profiler
80 #endif // ProfilerThreadPlatformData_h