int () { //We need a time series forecasting here: // 1) Value we're trying to predict is statistics.totalPagesAllocated() increase on next turn. // 2) We don't need a precise forecast: it is fine to be systematically biased 'above' -- we // usually over-provision pages for reclaim anyway, so being biased 'above' doesn't change // much, as long as we're not order of magnitude off. // 3) We should try hard to NOT systematically forecast 'below', since this leads to worker threads // stalling -- we could use statistics.totalPageAllocationsWaited() as a feedback to auto-tune // forecast parameters to reach that goal. //Right now we use very primitive forecasting: forecast is just current value is multiplied by // some 'safety margin factor'. Safety margin factor is a value in range [10/10...36/10], and we // double it each time some there are 'waits' during turn before, and decrement it each time // there were no waits -- so it is a simple feedback loop 'multiplicative-increase/additive-decrease'. long totalPagesAllocatedNow = statistics.totalPagesAllocated(); int totalPagesWaitedNow = statistics.totalPageAllocationsWaited(); int pagesAllocatedInTurn = Math.toIntExact(totalPagesAllocatedNow - totalPagesAllocatedTurnBefore); int pagesWaitedInTurn = Math.toIntExact(totalPagesWaitedNow - totalPagesWaitedTurnBefore); totalPagesAllocatedTurnBefore = totalPagesAllocatedNow; totalPagesWaitedTurnBefore = totalPagesWaitedNow; if (pagesWaitedInTurn > 0) { safetyMarginFactor.update(safetyMarginFactor.value() * 2); } else { safetyMarginFactor.dec(); } return Math.max( pagesAllocatedInTurn * safetyMarginFactor.value() / 10 + 1, 1 ); }