Table of Contents

Turn gc_collect_cycles into function pointer

Introduction

This RFC proposes to change gc_collect_cycles into a hookable function pointer. The goal is to allow extensions to hook into garbage collection for profiling.

Proposal

PHP Profilers usually hook into zend_execute and zend_execute_internal to measure execution time before and after the call. Because Garbage Collection is triggered during ALLOC_ZVAL and FREE_ZVAL (5.*) and during variable/object release this can happen “undeterministically” in a random function call from the perspective of the developer. This skews reporting actual execution times.

Turning garbage collection into a function pointer would allow to measure the time spent in garbage collection and substract that from the actual function calls. This would allow Profilers to correct time spent in execution for accidentally triggered GC. It would also allow to give hints about inefficient garbage collection runs that should prompt a developer to consider calling gc_disable() in userland.

A default implementation zend_gc_collect_cycles will implement the current behavior.

Adam tested the performance of this change and there was no measurable overhead:

https://github.com/LawnGnome/php-gc-benchmark

Relation to possible 7.0 extension API refactoring

On the mailinglist a discussion began weather this change should be part of a grand refactoring of hooks and extension API.

Making this change part of a grand refactoring will never lead to the inclusion since as of now, we know of nobody planning on working on the extension API for 7.0.

The RFC proposes to include the change AS-IS and then be part of a refactoring in the future, should one happen.

Backward Incompatible Changes

No changes to userland.

This changes the signature of gc_collect_cycles in Zend/zend_gc.h file and should therefore target only 7.0

Proposed PHP Version(s)

- PHP 7.0

RFC Impact

To SAPIs

None

To Existing Extensions

None

To Opcache

None

Unaffected PHP Functionality

This does not actually add profiling to garbage collection, it only allows extensions to build this on top.

Patches and Tests

Adam Harvey has a patch for this feature here: https://github.com/LawnGnome/php-src/compare/gc-fn-pointer

References

Initial discussion on hooking into Garbage Collection: https://github.com/php/php-src/pull/930

Vote

Turn gc_collect_cycles into function pointer?
Real name Yes No
ab (ab)  
aharvey (aharvey)  
ajf (ajf)  
bwoebi (bwoebi)  
derick (derick)  
dragoonis (dragoonis)  
guilhermeblanco (guilhermeblanco)  
irker (irker)  
jedibc (jedibc)  
kalle (kalle)  
krakjoe (krakjoe)  
leigh (leigh)  
lstrojny (lstrojny)  
rdlowrey (rdlowrey)  
rdohms (rdohms)  
reeze (reeze)  
salathe (salathe)  
treffynnon (treffynnon)  
Final result: 18 0
This poll has been closed.

Voting started: 2015-01-07 19:31 UTC Voting ended: 2015-01-21 19:31 UTC (14 days)