IdleDecommitPageAllocator.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "CommonMemoryPch.h"
  6. IdleDecommitPageAllocator::IdleDecommitPageAllocator(AllocationPolicyManager * policyManager, PageAllocatorType type,
  7. Js::ConfigFlagsTable& flagTable,
  8. uint maxFreePageCount, uint maxIdleFreePageCount,
  9. bool zeroPages,
  10. #if ENABLE_BACKGROUND_PAGE_FREEING
  11. BackgroundPageQueue * backgroundPageQueue,
  12. #endif
  13. uint maxAllocPageCount, bool enableWriteBarrier) :
  14. #ifdef IDLE_DECOMMIT_ENABLED
  15. idleDecommitTryEnterWaitFactor(0),
  16. hasDecommitTimer(false),
  17. hadDecommitTimer(false),
  18. #endif
  19. PageAllocator(policyManager, flagTable, type, maxFreePageCount, zeroPages,
  20. #if ENABLE_BACKGROUND_PAGE_FREEING
  21. backgroundPageQueue,
  22. #endif
  23. maxAllocPageCount, 0, false, false, GetCurrentProcess(), enableWriteBarrier),
  24. maxIdleDecommitFreePageCount(maxIdleFreePageCount),
  25. maxNonIdleDecommitFreePageCount(maxFreePageCount)
  26. {
  27. // if maxIdle is the same as max free, disable idleDecommit but setting the entry count to 1
  28. this->idleDecommitEnterCount = (maxIdleFreePageCount == maxFreePageCount);
  29. #ifdef IDLE_DECOMMIT_ENABLED
  30. #if DBG_DUMP
  31. idleDecommitCount = 0;
  32. #endif
  33. #endif
  34. }
  35. void
  36. IdleDecommitPageAllocator::EnterIdleDecommit()
  37. {
  38. this->idleDecommitEnterCount++;
  39. if (this->idleDecommitEnterCount != 1)
  40. {
  41. return;
  42. }
  43. #ifdef IDLE_DECOMMIT_ENABLED
  44. if (!cs.TryEnter())
  45. {
  46. AutoResetWaitingToEnterIdleDecommitFlag autoResetWaitingToEnterIdleDecommitFlag(this);
  47. #ifdef ENABLE_BASIC_TELEMETRY
  48. AllocatorDecommitStats * decommitStats = this->GetDecommitStats();
  49. decommitStats->lastEnterLeaveIdleDecommitTick = Js::Tick::Now();
  50. #endif
  51. cs.Enter();
  52. #ifdef ENABLE_BASIC_TELEMETRY
  53. decommitStats->lastEnterLeaveIdleDecommitCSWaitTime = Js::Tick::Now() - decommitStats->lastEnterLeaveIdleDecommitTick;
  54. decommitStats->totalEnterLeaveIdleDecommitCSWaitTime += decommitStats->lastEnterLeaveIdleDecommitCSWaitTime;
  55. if (decommitStats->lastEnterLeaveIdleDecommitCSWaitTime > decommitStats->maxEnterLeaveIdleDecommitCSWaitTime)
  56. {
  57. decommitStats->maxEnterLeaveIdleDecommitCSWaitTime = decommitStats->lastEnterLeaveIdleDecommitCSWaitTime;
  58. }
  59. #endif
  60. }
  61. this->isUsed = false;
  62. this->hadDecommitTimer = hasDecommitTimer;
  63. PAGE_ALLOC_VERBOSE_TRACE(_u("EnterIdleDecommit"));
  64. if (hasDecommitTimer)
  65. {
  66. // Cancel the decommit timer
  67. Assert(this->maxFreePageCount == maxIdleDecommitFreePageCount);
  68. hasDecommitTimer = false;
  69. PAGE_ALLOC_TRACE(_u("Cancel Decommit Timer"));
  70. }
  71. else
  72. {
  73. // Switch to maxIdleDecommitFreePageCount
  74. Assert(this->maxFreePageCount == maxNonIdleDecommitFreePageCount);
  75. Assert(minFreePageCount == 0);
  76. this->maxFreePageCount = maxIdleDecommitFreePageCount;
  77. }
  78. cs.Leave();
  79. Assert(!hasDecommitTimer);
  80. #else
  81. Assert(this->maxFreePageCount == maxNonIdleDecommitFreePageCount);
  82. this->maxFreePageCount = maxIdleDecommitFreePageCount;
  83. #endif
  84. }
  85. IdleDecommitSignal
  86. IdleDecommitPageAllocator::LeaveIdleDecommit(bool allowTimer)
  87. {
  88. Assert(this->idleDecommitEnterCount > 0);
  89. Assert(this->maxFreePageCount == maxIdleDecommitFreePageCount);
  90. #ifdef ENABLE_BASIC_TELEMETRY
  91. AllocatorDecommitStats * decommitStats = this->GetDecommitStats();
  92. decommitStats->lastLeaveDecommitRegion = Js::Tick::Now();
  93. #endif
  94. #ifdef IDLE_DECOMMIT_ENABLED
  95. Assert(!hasDecommitTimer);
  96. #endif
  97. this->idleDecommitEnterCount--;
  98. if (this->idleDecommitEnterCount != 0)
  99. {
  100. return IdleDecommitSignal_None;
  101. }
  102. #ifdef IDLE_DECOMMIT_ENABLED
  103. if (allowTimer)
  104. {
  105. if (!cs.TryEnter())
  106. {
  107. AutoResetWaitingToEnterIdleDecommitFlag autoResetWaitingToEnterIdleDecommitFlag(this);
  108. #ifdef ENABLE_BASIC_TELEMETRY
  109. decommitStats->lastEnterLeaveIdleDecommitTick = Js::Tick::Now();
  110. #endif
  111. cs.Enter();
  112. #ifdef ENABLE_BASIC_TELEMETRY
  113. decommitStats->lastEnterLeaveIdleDecommitCSWaitTime = Js::Tick::Now() - decommitStats->lastEnterLeaveIdleDecommitTick;
  114. decommitStats->totalEnterLeaveIdleDecommitCSWaitTime += decommitStats->lastEnterLeaveIdleDecommitCSWaitTime;
  115. if (decommitStats->lastEnterLeaveIdleDecommitCSWaitTime > decommitStats->maxEnterLeaveIdleDecommitCSWaitTime)
  116. {
  117. decommitStats->maxEnterLeaveIdleDecommitCSWaitTime = decommitStats->lastEnterLeaveIdleDecommitCSWaitTime;
  118. }
  119. #endif
  120. }
  121. PAGE_ALLOC_VERBOSE_TRACE(_u("LeaveIdleDecommit"));
  122. Assert(maxIdleDecommitFreePageCount != maxNonIdleDecommitFreePageCount);
  123. IdleDecommitSignal idleDecommitSignal = IdleDecommitSignal_None;
  124. if (freePageCount == 0 && !isUsed && !hadDecommitTimer)
  125. {
  126. Assert(minFreePageCount == 0);
  127. Assert(minFreePageCount == debugMinFreePageCount);
  128. // Nothing to decommit, it isn't used, and there was no timer before.
  129. // Just switch it back to non idle decommit mode
  130. this->maxFreePageCount = maxNonIdleDecommitFreePageCount;
  131. }
  132. else
  133. {
  134. UpdateMinFreePageCount();
  135. hasDecommitTimer = true;
  136. idleDecommitSignal = IdleDecommitSignal_NeedTimer;
  137. if (isUsed)
  138. {
  139. // Reschedule the timer
  140. decommitTime = ::GetTickCount() + IdleDecommitTimeout;
  141. PAGE_ALLOC_TRACE( _u("Schedule idle decommit at %d (%d)"), decommitTime, IdleDecommitTimeout);
  142. }
  143. else
  144. {
  145. int timeDiff = (int)decommitTime - ::GetTickCount();
  146. if (timeDiff < 20)
  147. {
  148. idleDecommitSignal = IdleDecommitSignal_NeedSignal;
  149. }
  150. PAGE_ALLOC_TRACE(_u("Reschedule idle decommit at %d (%d)"), decommitTime, decommitTime - ::GetTickCount());
  151. }
  152. }
  153. cs.Leave();
  154. return idleDecommitSignal;
  155. }
  156. #endif
  157. this->maxFreePageCount = maxNonIdleDecommitFreePageCount;
  158. __super::DecommitNow();
  159. ClearMinFreePageCount();
  160. return IdleDecommitSignal_None;
  161. }
  162. #ifdef IDLE_DECOMMIT_ENABLED
  163. void
  164. IdleDecommitPageAllocator::DecommitNow(bool all)
  165. {
  166. SuspendIdleDecommit();
  167. // If we are in non-idle-decommit mode, then always decommit all.
  168. // Otherwise, we will end up with some un-decommitted pages and get confused later.
  169. if (maxFreePageCount == maxNonIdleDecommitFreePageCount)
  170. all = true;
  171. __super::DecommitNow(all);
  172. if (all)
  173. {
  174. if (this->hasDecommitTimer)
  175. {
  176. Assert(idleDecommitEnterCount == 0);
  177. Assert(this->maxFreePageCount == maxIdleDecommitFreePageCount);
  178. this->hasDecommitTimer = false;
  179. this->maxFreePageCount = maxNonIdleDecommitFreePageCount;
  180. }
  181. else
  182. {
  183. Assert((idleDecommitEnterCount > 0? maxIdleDecommitFreePageCount : maxNonIdleDecommitFreePageCount)
  184. == this->maxFreePageCount);
  185. }
  186. ClearMinFreePageCount();
  187. }
  188. else
  189. {
  190. ResetMinFreePageCount();
  191. }
  192. ResumeIdleDecommit();
  193. }
  194. DWORD
  195. IdleDecommitPageAllocator::IdleDecommit()
  196. {
  197. // We can check hasDecommitTimer outside of the lock because when it change to true
  198. // the Recycler::concurrentIdleDecommitEvent will signal and we try to IdleDecommit again
  199. // If it change to false, we check again when we acquired the lock
  200. if (!hasDecommitTimer)
  201. {
  202. return INFINITE;
  203. }
  204. if (!cs.TryEnter())
  205. {
  206. // Failed to acquire the lock, wait for a variable time.
  207. PAGE_ALLOC_TRACE(_u("IdleDecommit Retry"));
  208. // Varies the wait time between 11 - 99
  209. idleDecommitTryEnterWaitFactor++;
  210. if (idleDecommitTryEnterWaitFactor >= 10)
  211. {
  212. idleDecommitTryEnterWaitFactor = 1;
  213. }
  214. DWORD waitTime = 11 * idleDecommitTryEnterWaitFactor;
  215. return waitTime; // Retry time
  216. }
  217. idleDecommitTryEnterWaitFactor = 0;
  218. DWORD waitTime = INFINITE;
  219. if (hasDecommitTimer)
  220. {
  221. Assert(this->maxFreePageCount == maxIdleDecommitFreePageCount);
  222. int timediff = (int)(decommitTime - ::GetTickCount());
  223. if (timediff >= 20) // Ignore time diff is it is < 20 since the system timer doesn't have that high of precision anyways
  224. {
  225. waitTime = (DWORD)timediff;
  226. }
  227. else
  228. {
  229. // Do the decommit in normal priority so that we don't block the main thread for too long
  230. PAGE_ALLOC_TRACE(_u("IdleDecommit"));
  231. #if DBG_DUMP
  232. idleDecommitCount++;
  233. #endif
  234. __super::DecommitNow();
  235. hasDecommitTimer = false;
  236. ClearMinFreePageCount();
  237. this->maxFreePageCount = maxNonIdleDecommitFreePageCount;
  238. }
  239. }
  240. cs.Leave();
  241. return waitTime;
  242. }
  243. #endif
  244. void
  245. IdleDecommitPageAllocator::Prime(uint primePageCount)
  246. {
  247. while (this->freePageCount < primePageCount)
  248. {
  249. PageSegment * segment = AddPageSegment(emptySegments);
  250. if (segment == nullptr)
  251. {
  252. return;
  253. }
  254. segment->Prime();
  255. }
  256. }
  257. #if DBG
  258. bool
  259. IdleDecommitPageAllocator::HasMultiThreadAccess() const
  260. {
  261. #ifdef IDLE_DECOMMIT_ENABLED
  262. return this->hasDecommitTimer && !cs.IsLocked();
  263. #else
  264. return false;
  265. #endif
  266. }
  267. void
  268. IdleDecommitPageAllocator::ShutdownIdleDecommit()
  269. {
  270. // The recycler thread should have died already
  271. // Just set the state
  272. idleDecommitEnterCount = 1;
  273. #ifdef IDLE_DECOMMIT_ENABLED
  274. hasDecommitTimer = false;
  275. #endif
  276. }
  277. #endif
  278. #ifdef IDLE_DECOMMIT_ENABLED
  279. #if DBG_DUMP
  280. void
  281. IdleDecommitPageAllocator::DumpStats() const
  282. {
  283. __super::DumpStats();
  284. Output::Print(_u(" Idle Decommit Count : %4d\n"),
  285. this->idleDecommitCount);
  286. }
  287. #endif
  288. #endif