summaryrefslogtreecommitdiffstats
path: root/tools/profiler/gecko/ChildProfilerController.cpp
blob: 864e45f683d278a2dba07c74b368ee4331cf497a (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* -*- Mode: C++; tab-width: 8; 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/. */

#include "ChildProfilerController.h"

#include "ProfilerChild.h"

#include "mozilla/ProfilerState.h"
#include "mozilla/ipc/Endpoint.h"
#include "nsExceptionHandler.h"
#include "nsIThread.h"
#include "nsThreadUtils.h"

using namespace mozilla::ipc;

namespace mozilla {

/* static */
already_AddRefed<ChildProfilerController> ChildProfilerController::Create(
    mozilla::ipc::Endpoint<PProfilerChild>&& aEndpoint) {
  MOZ_RELEASE_ASSERT(NS_IsMainThread());
  RefPtr<ChildProfilerController> cpc = new ChildProfilerController();
  cpc->Init(std::move(aEndpoint));
  return cpc.forget();
}

ChildProfilerController::ChildProfilerController()
    : mThread(nullptr, "ChildProfilerController::mThread") {
  MOZ_COUNT_CTOR(ChildProfilerController);
}

void ChildProfilerController::Init(Endpoint<PProfilerChild>&& aEndpoint) {
  RefPtr<nsIThread> newProfilerChildThread;
  if (NS_SUCCEEDED(NS_NewNamedThread("ProfilerChild",
                                     getter_AddRefs(newProfilerChildThread)))) {
    {
      auto lock = mThread.Lock();
      RefPtr<nsIThread>& lockedmThread = lock.ref();
      MOZ_ASSERT(!lockedmThread, "There is already a ProfilerChild thread");
      // Copy ref'd ptr into mThread. Don't move/swap, so that
      // newProfilerChildThread can be used below.
      lockedmThread = newProfilerChildThread;
    }
    // Now that mThread has been set, run SetupProfilerChild on the thread.
    newProfilerChildThread->Dispatch(
        NewRunnableMethod<Endpoint<PProfilerChild>&&>(
            "ChildProfilerController::SetupProfilerChild", this,
            &ChildProfilerController::SetupProfilerChild, std::move(aEndpoint)),
        NS_DISPATCH_NORMAL);
  }
}

ProfileAndAdditionalInformation
ChildProfilerController::GrabShutdownProfileAndShutdown() {
  ProfileAndAdditionalInformation profileAndAdditionalInformation;
  ShutdownAndMaybeGrabShutdownProfileFirst(&profileAndAdditionalInformation);
  return profileAndAdditionalInformation;
}

void ChildProfilerController::Shutdown() {
  ShutdownAndMaybeGrabShutdownProfileFirst(nullptr);
}

void ChildProfilerController::ShutdownAndMaybeGrabShutdownProfileFirst(
    ProfileAndAdditionalInformation* aOutShutdownProfileInformation) {
  // First, get the owning reference out of mThread, so it cannot be used in
  // ChildProfilerController after this (including re-entrantly during the
  // profilerChildThread->Shutdown() inner event loop below).
  RefPtr<nsIThread> profilerChildThread;
  {
    auto lock = mThread.Lock();
    RefPtr<nsIThread>& lockedmThread = lock.ref();
    lockedmThread.swap(profilerChildThread);
  }
  if (profilerChildThread) {
    if (profiler_is_active()) {
      CrashReporter::RecordAnnotationCString(
          CrashReporter::Annotation::ProfilerChildShutdownPhase,
          "Profiling - Dispatching ShutdownProfilerChild");
      profilerChildThread->Dispatch(
          NewRunnableMethod<ProfileAndAdditionalInformation*>(
              "ChildProfilerController::ShutdownProfilerChild", this,
              &ChildProfilerController::ShutdownProfilerChild,
              aOutShutdownProfileInformation),
          NS_DISPATCH_NORMAL);
      // Shut down the thread. This call will spin until all runnables
      // (including the ShutdownProfilerChild runnable) have been processed.
      profilerChildThread->Shutdown();
    } else {
      CrashReporter::RecordAnnotationCString(
          CrashReporter::Annotation::ProfilerChildShutdownPhase,
          "Not profiling - Running ShutdownProfilerChild");
      // If we're not profiling, this operation will be very quick, so it can be
      // done synchronously. This avoids having to manually shutdown the thread,
      // which runs a risky inner event loop, see bug 1613798.
      NS_DispatchAndSpinEventLoopUntilComplete(
          "ChildProfilerController::ShutdownProfilerChild SYNC"_ns,
          profilerChildThread,
          NewRunnableMethod<ProfileAndAdditionalInformation*>(
              "ChildProfilerController::ShutdownProfilerChild SYNC", this,
              &ChildProfilerController::ShutdownProfilerChild, nullptr));
    }
    // At this point, `profilerChildThread` should be the last reference to the
    // thread, so it will now get destroyed.
  }
}

ChildProfilerController::~ChildProfilerController() {
  MOZ_COUNT_DTOR(ChildProfilerController);

#ifdef DEBUG
  {
    auto lock = mThread.Lock();
    RefPtr<nsIThread>& lockedmThread = lock.ref();
    MOZ_ASSERT(
        !lockedmThread,
        "Please call Shutdown before destroying ChildProfilerController");
  }
#endif
  MOZ_ASSERT(!mProfilerChild);
}

void ChildProfilerController::SetupProfilerChild(
    Endpoint<PProfilerChild>&& aEndpoint) {
  {
    auto lock = mThread.Lock();
    RefPtr<nsIThread>& lockedmThread = lock.ref();
    // We should be on the ProfilerChild thread. In rare cases, we could already
    // be in shutdown, in which case mThread is null; we still need to continue,
    // so that ShutdownProfilerChild can work on a valid mProfilerChild.
    MOZ_RELEASE_ASSERT(!lockedmThread ||
                       lockedmThread == NS_GetCurrentThread());
  }
  MOZ_ASSERT(aEndpoint.IsValid());

  mProfilerChild = new ProfilerChild();
  Endpoint<PProfilerChild> endpoint = std::move(aEndpoint);

  if (!endpoint.Bind(mProfilerChild)) {
    MOZ_CRASH("Failed to bind ProfilerChild!");
  }
}

void ChildProfilerController::ShutdownProfilerChild(
    ProfileAndAdditionalInformation* aOutShutdownProfileInformation) {
  const bool isProfiling = profiler_is_active();
  if (aOutShutdownProfileInformation) {
    CrashReporter::RecordAnnotationCString(
        CrashReporter::Annotation::ProfilerChildShutdownPhase,
        isProfiling ? "Profiling - GrabShutdownProfile"
                    : "Not profiling - GrabShutdownProfile");
    *aOutShutdownProfileInformation = mProfilerChild->GrabShutdownProfile();
  }
  CrashReporter::RecordAnnotationCString(
      CrashReporter::Annotation::ProfilerChildShutdownPhase,
      isProfiling ? "Profiling - Destroying ProfilerChild"
                  : "Not profiling - Destroying ProfilerChild");
  mProfilerChild->Destroy();
  mProfilerChild = nullptr;
  CrashReporter::RecordAnnotationCString(
      CrashReporter::Annotation::ProfilerChildShutdownPhase,
      isProfiling ? "Profiling - ShutdownProfilerChild complete, waiting for "
                    "thread shutdown"
                  : "Not Profiling - ShutdownProfilerChild complete, waiting "
                    "for thread shutdown");
}

}  // namespace mozilla