IdleSchedulerParent.h (4224B)
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/. */ 6 7 #ifndef mozilla_ipc_IdleSchedulerParent_h__ 8 #define mozilla_ipc_IdleSchedulerParent_h__ 9 10 #include "mozilla/LinkedList.h" 11 #include "mozilla/ipc/PIdleSchedulerParent.h" 12 #include "mozilla/ipc/SharedMemoryMapping.h" 13 #include <bitset> 14 15 #define NS_IDLE_SCHEDULER_COUNTER_ARRAY_LENGHT 1024 16 #define NS_IDLE_SCHEDULER_INDEX_OF_ACTIVITY_COUNTER 0 17 #define NS_IDLE_SCHEDULER_INDEX_OF_CPU_COUNTER 1 18 19 class nsITimer; 20 21 namespace mozilla { 22 23 namespace ipc { 24 25 class BackgroundParentImpl; 26 27 class IdleSchedulerParent final 28 : public PIdleSchedulerParent, 29 public LinkedListElement<IdleSchedulerParent> { 30 public: 31 NS_INLINE_DECL_REFCOUNTING(IdleSchedulerParent) 32 33 IPCResult RecvInitForIdleUse(InitForIdleUseResolver&& aResolve); 34 IPCResult RecvRequestIdleTime(uint64_t aId, TimeDuration aBudget); 35 IPCResult RecvIdleTimeUsed(uint64_t aId); 36 IPCResult RecvSchedule(); 37 IPCResult RecvRunningPrioritizedOperation(); 38 IPCResult RecvPrioritizedOperationDone(); 39 IPCResult RecvRequestGC(RequestGCResolver&& aResolve); 40 IPCResult RecvStartedGC(); 41 IPCResult RecvDoneGC(); 42 43 private: 44 friend class BackgroundParentImpl; 45 IdleSchedulerParent(); 46 ~IdleSchedulerParent(); 47 48 static void CalculateNumIdleTasks(); 49 50 static int32_t ActiveCount(); 51 static void Schedule(IdleSchedulerParent* aRequester); 52 static bool HasSpareCycles(int32_t aActiveCount); 53 static bool HasSpareGCCycles(); 54 using PIdleSchedulerParent::SendIdleTime; 55 void SendIdleTime(); 56 void SendMayGC(); 57 58 static void EnsureStarvationTimer(); 59 static void StarvationCallback(nsITimer* aTimer, void* aData); 60 61 uint64_t mCurrentRequestId = 0; 62 // For now we don't really use idle budget for scheduling. Zero if the 63 // process isn't requestiong or running an idle task. 64 TimeDuration mRequestedIdleBudget; 65 66 // Counting all the prioritized operations the process is doing. 67 uint32_t mRunningPrioritizedOperation = 0; 68 69 // Only one of these may be true at a time, giving three states: 70 // No active GC request, A pending GC request, or a granted GC request. 71 Maybe<RequestGCResolver> mRequestingGC; 72 bool mDoingGC = false; 73 74 uint32_t mChildId = 0; 75 76 // Current state, only one of these may be true at a time. 77 bool IsWaitingForIdle() const { return isInList() && mRequestedIdleBudget; } 78 bool IsDoingIdleTask() const { return !isInList() && mRequestedIdleBudget; } 79 bool IsNotDoingIdleTask() const { return !mRequestedIdleBudget; } 80 81 // A bit is set if there is a child with child Id as the offset. 82 // The bit is used to check per child specific activity counters in 83 // sActiveChildCounter. 84 static std::bitset<NS_IDLE_SCHEDULER_COUNTER_ARRAY_LENGHT> 85 sInUseChildCounters; 86 87 // Processes on this list have requested (but the request hasn't yet been 88 // granted) idle time or to start a GC or both. 89 // 90 // Either or both their mRequestedIdleBudget or mRequestingGC fields are 91 // non-zero. Child processes not on this list have either been granted all 92 // their requests not made a request ever or since they last finished an idle 93 // or GC task. 94 // 95 // Use the methods above to determine a process' idle time state, or check the 96 // mRequestingGC and mDoingGC fields for the GC state. 97 static LinkedList<IdleSchedulerParent> sIdleAndGCRequests; 98 99 static int32_t sMaxConcurrentIdleTasksInChildProcesses; 100 static uint32_t sMaxConcurrentGCs; 101 static uint32_t sActiveGCs; 102 103 // Counting all the child processes which have at least one prioritized 104 // operation. 105 static uint32_t sChildProcessesRunningPrioritizedOperation; 106 107 // When this hits zero, it's time to free the shared memory and pack up. 108 static uint32_t sChildProcessesAlive; 109 110 static nsITimer* sStarvationPreventer; 111 112 static uint32_t sNumCPUs; 113 static uint32_t sPrefConcurrentGCsMax; 114 static uint32_t sPrefConcurrentGCsCPUDivisor; 115 }; 116 117 } // namespace ipc 118 } // namespace mozilla 119 120 #endif // mozilla_ipc_IdleSchedulerParent_h__