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;
33 [&](Tuple
<mozilla::Maybe
<SharedMemoryHandle
>, uint32_t>&& aResult
) {
34 if (Get
<0>(aResult
)) {
35 mActiveCounter
.SetHandle(std::move(*Get
<0>(aResult
)), false);
36 mActiveCounter
.Map(sizeof(int32_t));
37 mChildId
= Get
<1>(aResult
);
38 if (mChildId
&& mIdlePeriodState
&& mIdlePeriodState
->IsActive()) {
44 auto reject
= [&](ResponseRejectReason
) {};
45 SendInitForIdleUse(std::move(resolve
), std::move(reject
));
48 IPCResult
IdleSchedulerChild::RecvIdleTime(uint64_t aId
, TimeDuration aBudget
) {
49 if (mIdlePeriodState
) {
50 mIdlePeriodState
->SetIdleToken(aId
, aBudget
);
55 void IdleSchedulerChild::SetActive() {
56 if (mChildId
&& CanSend() && mActiveCounter
.memory()) {
57 ++(static_cast<Atomic
<int32_t>*>(
58 mActiveCounter
.memory())[NS_IDLE_SCHEDULER_INDEX_OF_ACTIVITY_COUNTER
]);
59 ++(static_cast<Atomic
<int32_t>*>(mActiveCounter
.memory())[mChildId
]);
63 bool IdleSchedulerChild::SetPaused() {
64 if (mChildId
&& CanSend() && mActiveCounter
.memory()) {
65 --(static_cast<Atomic
<int32_t>*>(mActiveCounter
.memory())[mChildId
]);
66 // The following expression reduces the global activity count and checks if
67 // it drops below the cpu counter limit.
68 return (static_cast<Atomic
<int32_t>*>(
70 .memory())[NS_IDLE_SCHEDULER_INDEX_OF_ACTIVITY_COUNTER
])-- ==
71 static_cast<Atomic
<int32_t>*>(
72 mActiveCounter
.memory())[NS_IDLE_SCHEDULER_INDEX_OF_CPU_COUNTER
];
78 RefPtr
<IdleSchedulerChild::MayGCPromise
> IdleSchedulerChild::MayGCNow() {
79 if (mIsRequestingGC
|| mIsDoingGC
) {
80 return MayGCPromise::CreateAndResolve(false, __func__
);
83 mIsRequestingGC
= true;
84 return SendRequestGC()->Then(
85 GetMainThreadSerialEventTarget(), __func__
,
86 [self
= RefPtr(this)](bool aIgnored
) {
87 // Only one of these may be true at a time.
88 MOZ_ASSERT(!(self
->mIsRequestingGC
&& self
->mIsDoingGC
));
90 // The parent process always says yes, sometimes after a delay.
91 if (self
->mIsRequestingGC
) {
92 self
->mIsRequestingGC
= false;
93 self
->mIsDoingGC
= true;
94 return MayGCPromise::CreateAndResolve(true, __func__
);
96 return MayGCPromise::CreateAndResolve(false, __func__
);
98 [self
= RefPtr(this)](ResponseRejectReason reason
) {
99 self
->mIsRequestingGC
= false;
100 return MayGCPromise::CreateAndReject(reason
, __func__
);
104 void IdleSchedulerChild::StartedGC() {
105 // Only one of these may be true at a time.
106 MOZ_ASSERT(!(mIsRequestingGC
&& mIsDoingGC
));
108 // If mRequestingGC was true then when the outstanding GC request returns
109 // it'll see that the GC has already started.
110 mIsRequestingGC
= false;
118 void IdleSchedulerChild::DoneGC() {
125 IdleSchedulerChild
* IdleSchedulerChild::GetMainThreadIdleScheduler() {
126 MOZ_ASSERT(NS_IsMainThread());
128 if (sMainThreadIdleScheduler
) {
129 return sMainThreadIdleScheduler
;
132 if (sIdleSchedulerDestroyed
) {
136 ipc::PBackgroundChild
* background
=
137 ipc::BackgroundChild::GetOrCreateForCurrentThread();
139 sMainThreadIdleScheduler
= new ipc::IdleSchedulerChild();
140 background
->SendPIdleSchedulerConstructor(sMainThreadIdleScheduler
);
142 return sMainThreadIdleScheduler
;
145 } // namespace mozilla::ipc