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 #ifndef mozilla_ipc_IdleSchedulerParent_h__
8 #define mozilla_ipc_IdleSchedulerParent_h__
10 #include "mozilla/Assertions.h"
11 #include "mozilla/Atomics.h"
12 #include "mozilla/Attributes.h"
13 #include "mozilla/LinkedList.h"
14 #include "mozilla/ipc/PIdleSchedulerParent.h"
15 #include "base/shared_memory.h"
18 #define NS_IDLE_SCHEDULER_COUNTER_ARRAY_LENGHT 1024
19 #define NS_IDLE_SCHEDULER_INDEX_OF_ACTIVITY_COUNTER 0
20 #define NS_IDLE_SCHEDULER_INDEX_OF_CPU_COUNTER 1
28 class BackgroundParentImpl
;
30 class IdleSchedulerParent final
31 : public PIdleSchedulerParent
,
32 public LinkedListElement
<IdleSchedulerParent
> {
34 NS_INLINE_DECL_REFCOUNTING(IdleSchedulerParent
)
36 IPCResult
RecvInitForIdleUse(InitForIdleUseResolver
&& aResolve
);
37 IPCResult
RecvRequestIdleTime(uint64_t aId
, TimeDuration aBudget
);
38 IPCResult
RecvIdleTimeUsed(uint64_t aId
);
39 IPCResult
RecvSchedule();
40 IPCResult
RecvRunningPrioritizedOperation();
41 IPCResult
RecvPrioritizedOperationDone();
42 IPCResult
RecvRequestGC(RequestGCResolver
&& aResolve
);
43 IPCResult
RecvStartedGC();
44 IPCResult
RecvDoneGC();
47 friend class BackgroundParentImpl
;
48 IdleSchedulerParent();
49 ~IdleSchedulerParent();
51 static void CalculateNumIdleTasks();
53 static int32_t ActiveCount();
54 static void Schedule(IdleSchedulerParent
* aRequester
);
55 static bool HasSpareCycles(int32_t aActiveCount
);
56 static bool HasSpareGCCycles();
57 using PIdleSchedulerParent::SendIdleTime
;
61 static void EnsureStarvationTimer();
62 static void StarvationCallback(nsITimer
* aTimer
, void* aData
);
64 uint64_t mCurrentRequestId
= 0;
65 // For now we don't really use idle budget for scheduling. Zero if the
66 // process isn't requestiong or running an idle task.
67 TimeDuration mRequestedIdleBudget
;
69 // Counting all the prioritized operations the process is doing.
70 uint32_t mRunningPrioritizedOperation
= 0;
72 // Only one of these may be true at a time, giving three states:
73 // No active GC request, A pending GC request, or a granted GC request.
74 Maybe
<RequestGCResolver
> mRequestingGC
;
75 bool mDoingGC
= false;
77 uint32_t mChildId
= 0;
79 // Current state, only one of these may be true at a time.
80 bool IsWaitingForIdle() const { return isInList() && mRequestedIdleBudget
; }
81 bool IsDoingIdleTask() const { return !isInList() && mRequestedIdleBudget
; }
82 bool IsNotDoingIdleTask() const { return !mRequestedIdleBudget
; }
84 // Shared memory for counting how many child processes are running
85 // tasks. This memory is shared across all the child processes.
86 // The [0] is used for counting all the processes and
87 // [childId] is for counting per process activity.
88 // This way the global activity can be checked in a fast way by just looking
90 // [1] is used for cpu count for child processes.
91 static base::SharedMemory
* sActiveChildCounter
;
92 // A bit is set if there is a child with child Id as the offset.
93 // The bit is used to check per child specific activity counters in
94 // sActiveChildCounter.
95 static std::bitset
<NS_IDLE_SCHEDULER_COUNTER_ARRAY_LENGHT
>
98 // Processes on this list have requested (but the request hasn't yet been
99 // granted) idle time or to start a GC or both.
101 // Either or both their mRequestedIdleBudget or mRequestingGC fields are
102 // non-zero. Child processes not on this list have either been granted all
103 // their requests not made a request ever or since they last finished an idle
106 // Use the methods above to determine a process' idle time state, or check the
107 // mRequestingGC and mDoingGC fields for the GC state.
108 static LinkedList
<IdleSchedulerParent
> sIdleAndGCRequests
;
110 static int32_t sMaxConcurrentIdleTasksInChildProcesses
;
111 static uint32_t sMaxConcurrentGCs
;
112 static uint32_t sActiveGCs
;
114 // Counting all the child processes which have at least one prioritized
116 static uint32_t sChildProcessesRunningPrioritizedOperation
;
118 // When this hits zero, it's time to free the shared memory and pack up.
119 static uint32_t sChildProcessesAlive
;
121 static nsITimer
* sStarvationPreventer
;
123 static uint32_t sNumCPUs
;
124 static uint32_t sPrefConcurrentGCsMax
;
125 static uint32_t sPrefConcurrentGCsCPUDivisor
;
129 } // namespace mozilla
131 #endif // mozilla_ipc_IdleSchedulerParent_h__