Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Member Functions | Friends | List of all members
dart::PageSpaceController Class Reference

#include <pages.h>

Public Member Functions

 PageSpaceController (Heap *heap, int heap_growth_ratio, int heap_growth_max, int garbage_collection_time_ratio)
 
 ~PageSpaceController ()
 
bool ReachedHardThreshold (SpaceUsage after) const
 
bool ReachedSoftThreshold (SpaceUsage after) const
 
bool ReachedIdleThreshold (SpaceUsage current) const
 
void EvaluateGarbageCollection (SpaceUsage before, SpaceUsage after, int64_t start, int64_t end)
 
void EvaluateAfterLoading (SpaceUsage after)
 
void set_last_usage (SpaceUsage current)
 

Friends

class PageSpace
 

Detailed Description

Definition at line 56 of file pages.h.

Constructor & Destructor Documentation

◆ PageSpaceController()

dart::PageSpaceController::PageSpaceController ( Heap heap,
int  heap_growth_ratio,
int  heap_growth_max,
int  garbage_collection_time_ratio 
)

Definition at line 1369 of file pages.cc.

1373 : heap_(heap),
1374 heap_growth_ratio_(heap_growth_ratio),
1375 desired_utilization_((100.0 - heap_growth_ratio) / 100.0),
1376 heap_growth_max_(heap_growth_max),
1377 garbage_collection_time_ratio_(garbage_collection_time_ratio),
1378 idle_gc_threshold_in_words_(0) {
1379 const intptr_t growth_in_pages = heap_growth_max / 2;
1380 RecordUpdate(last_usage_, last_usage_, growth_in_pages, "initial");
1381}

◆ ~PageSpaceController()

dart::PageSpaceController::~PageSpaceController ( )

Definition at line 1383 of file pages.cc.

1383{}

Member Function Documentation

◆ EvaluateAfterLoading()

void dart::PageSpaceController::EvaluateAfterLoading ( SpaceUsage  after)

Definition at line 1517 of file pages.cc.

1517 {
1518 // Number of pages we can allocate and still be within the desired growth
1519 // ratio.
1520 intptr_t growth_in_pages;
1521 if (desired_utilization_ == 0.0) {
1522 growth_in_pages = heap_growth_max_;
1523 } else {
1524 growth_in_pages = (static_cast<intptr_t>(after.CombinedUsedInWords() /
1525 desired_utilization_) -
1526 (after.CombinedUsedInWords())) /
1528 }
1529
1530 // Apply growth cap.
1531 growth_in_pages =
1532 Utils::Minimum(static_cast<intptr_t>(heap_growth_max_), growth_in_pages);
1533
1534 RecordUpdate(after, after, growth_in_pages, "loaded");
1535}
static T Minimum(T x, T y)
Definition utils.h:21
static constexpr intptr_t kPageSizeInWords
Definition page.h:28

◆ EvaluateGarbageCollection()

void dart::PageSpaceController::EvaluateGarbageCollection ( SpaceUsage  before,
SpaceUsage  after,
int64_t  start,
int64_t  end 
)

Definition at line 1412 of file pages.cc.

1415 {
1416 ASSERT(end >= start);
1418 const int gc_time_fraction = history_.GarbageCollectionTimeFraction();
1419
1420 // Assume garbage increases linearly with allocation:
1421 // G = kA, and estimate k from the previous cycle.
1422 const intptr_t allocated_since_previous_gc =
1423 before.CombinedUsedInWords() - last_usage_.CombinedUsedInWords();
1424 intptr_t grow_heap;
1425 if (allocated_since_previous_gc > 0) {
1426 intptr_t garbage =
1427 before.CombinedUsedInWords() - after.CombinedUsedInWords();
1428 // Garbage may be negative if when the OOM reservation is refilled.
1429 garbage = Utils::Maximum(static_cast<intptr_t>(0), garbage);
1430 // It makes no sense to expect that each kb allocated will cause more than
1431 // one kb of garbage, so we clamp k at 1.0.
1432 const double k = Utils::Minimum(
1433 1.0, garbage / static_cast<double>(allocated_since_previous_gc));
1434
1435 const int garbage_ratio = static_cast<int>(k * 100);
1436
1437 // Define GC to be 'worthwhile' iff at least fraction t of heap is garbage.
1438 double t = 1.0 - desired_utilization_;
1439 // If we spend too much time in GC, strive for even more free space.
1440 if (gc_time_fraction > garbage_collection_time_ratio_) {
1441 t += (gc_time_fraction - garbage_collection_time_ratio_) / 100.0;
1442 }
1443
1444 // Number of pages we can allocate and still be within the desired growth
1445 // ratio.
1446 const intptr_t grow_pages =
1447 (static_cast<intptr_t>(after.CombinedUsedInWords() /
1448 desired_utilization_) -
1449 (after.CombinedUsedInWords())) /
1451 if (garbage_ratio == 0) {
1452 // No garbage in the previous cycle so it would be hard to compute a
1453 // grow_heap size based on estimated garbage so we use growth ratio
1454 // heuristics instead.
1455 grow_heap =
1456 Utils::Maximum(static_cast<intptr_t>(heap_growth_max_), grow_pages);
1457 } else if (garbage_collection_time_ratio_ == 0) {
1458 // Exclude time from the growth policy decision for --deterministic.
1459 grow_heap =
1460 Utils::Maximum(static_cast<intptr_t>(heap_growth_max_), grow_pages);
1461 } else {
1462 // Find minimum 'grow_heap' such that after increasing capacity by
1463 // 'grow_heap' pages and filling them, we expect a GC to be worthwhile.
1464 intptr_t max = heap_growth_max_;
1465 intptr_t min = 0;
1466 intptr_t local_grow_heap = 0;
1467 while (min < max) {
1468 local_grow_heap = (max + min) / 2;
1469 const intptr_t limit =
1470 after.CombinedUsedInWords() + (local_grow_heap * kPageSizeInWords);
1471 const intptr_t allocated_before_next_gc =
1472 limit - (after.CombinedUsedInWords());
1473 const double estimated_garbage = k * allocated_before_next_gc;
1474 if (t <= estimated_garbage / limit) {
1475 max = local_grow_heap - 1;
1476 } else {
1477 min = local_grow_heap + 1;
1478 }
1479 }
1480 local_grow_heap = (max + min) / 2;
1481 grow_heap = local_grow_heap;
1482 ASSERT(grow_heap >= 0);
1483 // If we are going to grow by heap_grow_max_ then ensure that we
1484 // will be growing the heap at least by the growth ratio heuristics.
1485 if (grow_heap >= heap_growth_max_) {
1486 grow_heap = Utils::Maximum(grow_pages, grow_heap);
1487 }
1488 }
1489 } else {
1490 grow_heap = 0;
1491 }
1492 last_usage_ = after;
1493
1494 intptr_t max_capacity_in_words = heap_->old_space()->max_capacity_in_words_;
1495 if (max_capacity_in_words != 0) {
1496 ASSERT(grow_heap >= 0);
1497 // Fraction of asymptote used.
1498 double f = static_cast<double>(after.CombinedUsedInWords() +
1499 (kPageSizeInWords * grow_heap)) /
1500 static_cast<double>(max_capacity_in_words);
1501 ASSERT(f >= 0.0);
1502 // Increase weight at the high end.
1503 f = f * f;
1504 // Fraction of asymptote available.
1505 f = 1.0 - f;
1506 ASSERT(f <= 1.0);
1507 // Discount growth more the closer we get to the desired asymptote.
1508 grow_heap = static_cast<intptr_t>(grow_heap * f);
1509 // Minimum growth step after reaching the asymptote.
1510 intptr_t min_step = (2 * MB) / kPageSize;
1511 grow_heap = Utils::Maximum(min_step, grow_heap);
1512 }
1513
1514 RecordUpdate(before, after, grow_heap, "gc");
1515}
PageSpace * old_space()
Definition heap.h:63
void AddGarbageCollectionTime(int64_t start, int64_t end)
Definition pages.cc:1587
intptr_t CombinedUsedInWords() const
Definition spaces.h:27
static constexpr T Maximum(T x, T y)
Definition utils.h:26
#define ASSERT(E)
glong glong end
static float max(float r, float g, float b)
Definition hsl.cpp:49
static float min(float r, float g, float b)
Definition hsl.cpp:48
constexpr intptr_t MB
Definition globals.h:530
static constexpr intptr_t kPageSize
Definition page.h:27

◆ ReachedHardThreshold()

bool dart::PageSpaceController::ReachedHardThreshold ( SpaceUsage  after) const

Definition at line 1385 of file pages.cc.

1385 {
1386 if (heap_growth_ratio_ == 100) {
1387 return false;
1388 }
1389 if ((heap_ != nullptr) && (heap_->mode() == Dart_PerformanceMode_Latency)) {
1390 return false;
1391 }
1392 return after.CombinedUsedInWords() > hard_gc_threshold_in_words_;
1393}
Dart_PerformanceMode mode() const
Definition heap.h:103
@ Dart_PerformanceMode_Latency
Definition dart_api.h:1379

◆ ReachedIdleThreshold()

bool dart::PageSpaceController::ReachedIdleThreshold ( SpaceUsage  current) const

Definition at line 1405 of file pages.cc.

1405 {
1406 if (heap_growth_ratio_ == 100) {
1407 return false;
1408 }
1409 return current.CombinedUsedInWords() > idle_gc_threshold_in_words_;
1410}

◆ ReachedSoftThreshold()

bool dart::PageSpaceController::ReachedSoftThreshold ( SpaceUsage  after) const

Definition at line 1395 of file pages.cc.

1395 {
1396 if (heap_growth_ratio_ == 100) {
1397 return false;
1398 }
1399 if ((heap_ != nullptr) && (heap_->mode() == Dart_PerformanceMode_Latency)) {
1400 return false;
1401 }
1402 return after.CombinedUsedInWords() > soft_gc_threshold_in_words_;
1403}

◆ set_last_usage()

void dart::PageSpaceController::set_last_usage ( SpaceUsage  current)
inline

Definition at line 82 of file pages.h.

82{ last_usage_ = current; }

Friends And Related Symbol Documentation

◆ PageSpace

friend class PageSpace
friend

Definition at line 85 of file pages.h.


The documentation for this class was generated from the following files: