Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / ipc / glue / IdleSchedulerChild.cpp
blob9a094c395f3a069c894e8939147c03a80451f9a3
1 /* -*- Mode: C++; tab-width: 8; 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 #include "mozilla/ipc/IdleSchedulerChild.h"
8 #include "mozilla/ipc/IdleSchedulerParent.h"
9 #include "mozilla/ipc/PBackgroundChild.h"
10 #include "mozilla/Atomics.h"
11 #include "mozilla/IdlePeriodState.h"
12 #include "mozilla/Telemetry.h"
13 #include "BackgroundChild.h"
15 namespace mozilla::ipc {
17 static IdleSchedulerChild* sMainThreadIdleScheduler = nullptr;
18 static bool sIdleSchedulerDestroyed = false;
20 IdleSchedulerChild::~IdleSchedulerChild() {
21 if (sMainThreadIdleScheduler == this) {
22 sMainThreadIdleScheduler = nullptr;
23 sIdleSchedulerDestroyed = true;
25 MOZ_ASSERT(!mIdlePeriodState);
28 void IdleSchedulerChild::Init(IdlePeriodState* aIdlePeriodState) {
29 mIdlePeriodState = aIdlePeriodState;
31 RefPtr<IdleSchedulerChild> scheduler = this;
32 auto resolve =
33 [&](std::tuple<mozilla::Maybe<SharedMemoryHandle>, uint32_t>&& aResult) {
34 if (std::get<0>(aResult)) {
35 mActiveCounter->SetHandle(std::move(*std::get<0>(aResult)),
36 SharedMemory::RightsReadWrite);
37 mActiveCounter->Map(sizeof(int32_t));
38 mChildId = std::get<1>(aResult);
39 if (mChildId && mIdlePeriodState && mIdlePeriodState->IsActive()) {
40 SetActive();
45 auto reject = [&](ResponseRejectReason) {};
46 SendInitForIdleUse(std::move(resolve), std::move(reject));
49 IPCResult IdleSchedulerChild::RecvIdleTime(uint64_t aId, TimeDuration aBudget) {
50 if (mIdlePeriodState) {
51 mIdlePeriodState->SetIdleToken(aId, aBudget);
53 return IPC_OK();
56 void IdleSchedulerChild::SetActive() {
57 if (mChildId && CanSend() && mActiveCounter->Memory()) {
58 ++(static_cast<Atomic<int32_t>*>(
59 mActiveCounter->Memory())[NS_IDLE_SCHEDULER_INDEX_OF_ACTIVITY_COUNTER]);
60 ++(static_cast<Atomic<int32_t>*>(mActiveCounter->Memory())[mChildId]);
64 bool IdleSchedulerChild::SetPaused() {
65 if (mChildId && CanSend() && mActiveCounter->Memory()) {
66 --(static_cast<Atomic<int32_t>*>(mActiveCounter->Memory())[mChildId]);
67 // The following expression reduces the global activity count and checks if
68 // it drops below the cpu counter limit.
69 return (static_cast<Atomic<int32_t>*>(mActiveCounter->Memory())
70 [NS_IDLE_SCHEDULER_INDEX_OF_ACTIVITY_COUNTER])-- ==
71 static_cast<Atomic<int32_t>*>(
72 mActiveCounter
73 ->Memory())[NS_IDLE_SCHEDULER_INDEX_OF_CPU_COUNTER];
76 return false;
79 RefPtr<IdleSchedulerChild::MayGCPromise> IdleSchedulerChild::MayGCNow() {
80 if (mIsRequestingGC || mIsDoingGC) {
81 return MayGCPromise::CreateAndResolve(false, __func__);
84 mIsRequestingGC = true;
85 return SendRequestGC()->Then(
86 GetMainThreadSerialEventTarget(), __func__,
87 [self = RefPtr(this)](bool aIgnored) {
88 // Only one of these may be true at a time.
89 MOZ_ASSERT(!(self->mIsRequestingGC && self->mIsDoingGC));
91 // The parent process always says yes, sometimes after a delay.
92 if (self->mIsRequestingGC) {
93 self->mIsRequestingGC = false;
94 self->mIsDoingGC = true;
95 return MayGCPromise::CreateAndResolve(true, __func__);
97 return MayGCPromise::CreateAndResolve(false, __func__);
99 [self = RefPtr(this)](ResponseRejectReason reason) {
100 self->mIsRequestingGC = false;
101 return MayGCPromise::CreateAndReject(reason, __func__);
105 void IdleSchedulerChild::StartedGC() {
106 // Only one of these may be true at a time.
107 MOZ_ASSERT(!(mIsRequestingGC && mIsDoingGC));
109 // If mRequestingGC was true then when the outstanding GC request returns
110 // it'll see that the GC has already started.
111 mIsRequestingGC = false;
113 if (!mIsDoingGC) {
114 if (CanSend()) {
115 SendStartedGC();
117 mIsDoingGC = true;
121 void IdleSchedulerChild::DoneGC() {
122 if (mIsDoingGC) {
123 if (CanSend()) {
124 SendDoneGC();
126 mIsDoingGC = false;
130 IdleSchedulerChild* IdleSchedulerChild::GetMainThreadIdleScheduler() {
131 MOZ_ASSERT(NS_IsMainThread());
133 if (sMainThreadIdleScheduler) {
134 return sMainThreadIdleScheduler;
137 if (sIdleSchedulerDestroyed) {
138 return nullptr;
141 ipc::PBackgroundChild* background =
142 ipc::BackgroundChild::GetOrCreateForCurrentThread();
143 if (background) {
144 // this is nulled out on our destruction, so we don't need to worry
145 sMainThreadIdleScheduler = new ipc::IdleSchedulerChild();
146 MOZ_ALWAYS_TRUE(
147 background->SendPIdleSchedulerConstructor(sMainThreadIdleScheduler));
149 return sMainThreadIdleScheduler;
152 } // namespace mozilla::ipc