Skip to content

Adjust GC threshold if num_roots higher than threshold after collection #13758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Zend/tests/gh13670_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
GH-13670 001
--FILE--
<?php

register_shutdown_function(function () {
global $shutdown;
$shutdown = true;
});

class Cycle {
public $self;
public function __construct() {
$this->self = $this;
}
public function __destruct() {
global $shutdown;
if (!$shutdown) {
new Cycle();
}
}
}

$defaultThreshold = gc_status()['threshold'];
for ($i = 0; $i < $defaultThreshold+1; $i++) {
new Cycle();
}

$objs = [];
for ($i = 0; $i < 100; $i++) {
$obj = new stdClass;
$objs[] = $obj;
}

$st = gc_status();

if ($st['runs'] > 10) {
var_dump($st);
}
?>
==DONE==
--EXPECT--
==DONE==
66 changes: 66 additions & 0 deletions Zend/tests/gh13670_002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
--TEST--
GH-13670 002
--FILE--
<?php

register_shutdown_function(function () {
global $shutdown;
$shutdown = true;
});

class Cycle {
public $self;
public function __construct() {
$this->self = $this;
}
}

class Canary {
public $self;
public function __construct() {
$this->self = $this;
}
public function __destruct() {
global $shutdown;
if (!$shutdown) {
work();
}
}
}

function work() {
global $objs, $defaultThreshold;
new Canary();
// Create some collectable garbage so the next run will not adjust
// threshold
for ($i = 0; $i < 100; $i++) {
new Cycle();
}
// Add potential garbage to buffer
foreach (array_slice($objs, 0, $defaultThreshold) as $obj) {
$o = $obj;
}
}

$defaultThreshold = gc_status()['threshold'];
$objs = [];
for ($i = 0; $i < $defaultThreshold*2; $i++) {
$obj = new stdClass;
$objs[] = $obj;
}

work();

foreach ($objs as $obj) {
$o = $obj;
}

$st = gc_status();

if ($st['runs'] > 10) {
var_dump($st);
}
?>
==DONE==
--EXPECT--
==DONE==
68 changes: 68 additions & 0 deletions Zend/tests/gh13670_003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
--TEST--
GH-13670 003
--FILE--
<?php

register_shutdown_function(function () {
global $shutdown;
$shutdown = true;
});

class Cycle {
public $self;
public function __construct() {
$this->self = $this;
}
}

class Canary {
public $self;
public function __construct() {
$this->self = $this;
}
public function __destruct() {
global $shutdown;
if (!$shutdown) {
work();
}
}
}

function work() {
global $objs, $defaultThreshold;
new Canary();
// Create some collectable garbage so the next run will not adjust
// threshold
for ($i = 0; $i < 100; $i++) {
new Cycle();
}
// Add potential garbage to buffer
foreach (array_slice($objs, 0, $defaultThreshold) as $obj) {
$o = $obj;
}
}

$defaultThreshold = gc_status()['threshold'];
$objs = [];
for ($i = 0; $i < $defaultThreshold*2; $i++) {
$obj = new stdClass;
$objs[] = $obj;
}

work();

// Result of array_slice() is a tmpvar that will be checked by
// zend_gc_check_root_tmpvars()
foreach (array_slice($objs, -10) as $obj) {
$o = $obj;
}

$st = gc_status();

if ($st['runs'] > 10) {
var_dump($st);
}
?>
==DONE==
--EXPECT--
==DONE==
8 changes: 7 additions & 1 deletion Zend/zend_gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ static void gc_adjust_threshold(int count)
/* TODO Very simple heuristic for dynamic GC buffer resizing:
* If there are "too few" collections, increase the collection threshold
* by a fixed step */
if (count < GC_THRESHOLD_TRIGGER) {
if (count < GC_THRESHOLD_TRIGGER || GC_G(num_roots) >= GC_G(gc_threshold)) {
/* increase */
if (GC_G(gc_threshold) < GC_THRESHOLD_MAX) {
new_threshold = GC_G(gc_threshold) + GC_THRESHOLD_STEP;
Expand Down Expand Up @@ -1674,7 +1674,13 @@ ZEND_API int zend_gc_collect_cycles(void)

finish:
zend_get_gc_buffer_release();

/* Prevent GC from running during zend_gc_check_root_tmpvars, before
* gc_threshold is adjusted, as this may result in unbounded recursion */
GC_G(gc_active) = 1;
zend_gc_check_root_tmpvars();
Comment on lines +1677 to 1681
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this lead to store after gc_grow_root_buffer() instead of immediate collection?
I didn't understand, how this is related to the main part of the patch and why this might be a problem.
Please explain.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this lead to store after gc_grow_root_buffer() instead of immediate collection?

Yes exactly

I didn't understand, how this is related to the main part of the patch and why this might be a problem

The main part of the patch increases gc_threshold when num_roots is still >= gc_threshold after collection, to prevent a collection from being triggered immediately. However, this adjustment happens after zend_gc_check_root_tmpvars(), so this function may still trigger an immediate collection. Moreover this happens recursively and it leads to a stack overflow in one of the test cases.

By setting gc_active=1 before zend_gc_check_root_tmpvars(), we prevent collections temporarily.

An alternative would be to adjust the threshold before zend_gc_check_root_tmpvars(). This requires a few more changes because the threshold should not be adjusted when collection is triggered manually, for example.

GC_G(gc_active) = 0;

return total_count;
}

Expand Down