summaryrefslogtreecommitdiffstats
path: root/tools/profiler/public/ProfilerThreadState.h
blob: 6ac48e41dd6ee1d4f06b0c872638676590973b8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// This header contains functions that give information about the Profiler state
// with regards to the current thread.

#ifndef ProfilerThreadState_h
#define ProfilerThreadState_h

#include "mozilla/ProfilerState.h"
#include "mozilla/ProfilerThreadRegistration.h"
#include "mozilla/ProfilerThreadRegistry.h"
#include "mozilla/ProfilerThreadSleep.h"

// During profiling, if the current thread is registered, return true
// (regardless of whether it is actively being profiled).
// (Same caveats and recommended usage as profiler_is_active().)
[[nodiscard]] inline bool profiler_is_active_and_thread_is_registered() {
  return profiler_is_active() &&
         mozilla::profiler::ThreadRegistration::IsRegistered();
}

// Is the profiler active and unpaused, and is the current thread being
// profiled for any of the given features? (Same caveats and recommended usage
// as profiler_is_active().)
[[nodiscard]] inline bool profiler_thread_is_being_profiled(
    ThreadProfilingFeatures aThreadProfilingFeatures) {
  return profiler_is_active_and_unpaused() &&
         mozilla::profiler::ThreadRegistration::WithOnThreadRefOr(
             [aThreadProfilingFeatures](
                 mozilla::profiler::ThreadRegistration::OnThreadRef aTR) {
               return DoFeaturesIntersect(
                   aTR.UnlockedConstReaderAndAtomicRWCRef().ProfilingFeatures(),
                   aThreadProfilingFeatures);
             },
             false);
}

// Is the profiler active and unpaused, and is the given thread being profiled?
// (Same caveats and recommended usage as profiler_is_active().)
// Safe to use with the current thread id, or unspecified ProfilerThreadId (same
// as current thread id).
[[nodiscard]] inline bool profiler_thread_is_being_profiled(
    const ProfilerThreadId& aThreadId,
    ThreadProfilingFeatures aThreadProfilingFeatures) {
  if (!profiler_is_active_and_unpaused()) {
    return false;
  }

  if (!aThreadId.IsSpecified() || aThreadId == profiler_current_thread_id()) {
    // For the current thread id, use the ThreadRegistration directly, it is
    // more efficient.
    return mozilla::profiler::ThreadRegistration::WithOnThreadRefOr(
        [aThreadProfilingFeatures](
            mozilla::profiler::ThreadRegistration::OnThreadRef aTR) {
          return DoFeaturesIntersect(
              aTR.UnlockedConstReaderAndAtomicRWCRef().ProfilingFeatures(),
              aThreadProfilingFeatures);
        },
        false);
  }

  // For other threads, go through the ThreadRegistry.
  return mozilla::profiler::ThreadRegistry::WithOffThreadRefOr(
      aThreadId,
      [aThreadProfilingFeatures](
          mozilla::profiler::ThreadRegistry::OffThreadRef aTR) {
        return DoFeaturesIntersect(
            aTR.UnlockedConstReaderAndAtomicRWCRef().ProfilingFeatures(),
            aThreadProfilingFeatures);
      },
      false);
}

// Is the current thread registered and sleeping?
[[nodiscard]] inline bool profiler_thread_is_sleeping() {
  return profiler_is_active() &&
         mozilla::profiler::ThreadRegistration::WithOnThreadRefOr(
             [](mozilla::profiler::ThreadRegistration::OnThreadRef aTR) {
               return aTR.UnlockedConstReaderAndAtomicRWCRef().IsSleeping();
             },
             false);
}

#ifndef MOZ_GECKO_PROFILER

#  define AUTO_PROFILER_THREAD_WAKE

#else  // !MOZ_GECKO_PROFILER

// Mark a thread as awake within a scope.
// (See also AUTO_PROFILER_THREAD_SLEEP in mozilla/ProfilerThreadSleep.h)
#  define AUTO_PROFILER_THREAD_WAKE \
    mozilla::AutoProfilerThreadWake PROFILER_RAII

namespace mozilla {

// Temporarily wake up the profiling of a thread while servicing events such as
// Asynchronous Procedure Calls (APCs).
// (See also AutoProfilerThreadSleep in ProfilerThreadSleep.h)
class MOZ_RAII AutoProfilerThreadWake {
 public:
  explicit AutoProfilerThreadWake()
      : mIssuedWake(profiler_thread_is_sleeping()) {
    if (mIssuedWake) {
      profiler_thread_wake();
    }
  }

  ~AutoProfilerThreadWake() {
    if (mIssuedWake) {
      MOZ_ASSERT(!profiler_thread_is_sleeping());
      profiler_thread_sleep();
    }
  }

 private:
  bool mIssuedWake;
};

}  // namespace mozilla

#endif  // !MOZ_GECKO_PROFILER

#endif  // ProfilerThreadState_h