LCOV - code coverage report
Current view: top level - src - excimer.c (source / functions) Hit Total Coverage
Test: mediawiki/php/excimer test coverage report Lines: 399 495 80.6 %
Date: 2026-08-03 01:54:34 Functions: 61 71 85.9 %

          Line data    Source code
       1             : /* Copyright 2018 Wikimedia Foundation
       2             :  *
       3             :  * Licensed under the Apache License, Version 2.0 (the "License");
       4             :  * you may not use this file except in compliance with the License.
       5             :  * You may obtain a copy of the License at
       6             :  *
       7             :  *     http://www.apache.org/licenses/LICENSE-2.0
       8             :  *
       9             :  * Unless required by applicable law or agreed to in writing, software
      10             :  * distributed under the License is distributed on an "AS IS" BASIS,
      11             :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             :  * See the License for the specific language governing permissions and
      13             :  * limitations under the License.
      14             :  */
      15             : 
      16             : #ifdef HAVE_CONFIG_H
      17             : #include "config.h"
      18             : #endif
      19             : 
      20             : #include <time.h>
      21             : 
      22             : #include "php.h"
      23             : #include "zend_exceptions.h"
      24             : #include "zend_interfaces.h"
      25             : #include "ext/spl/spl_exceptions.h"
      26             : #if PHP_VERSION_ID < 80400
      27             : #include "ext/standard/php_mt_rand.h"
      28             : #else
      29             : #include "ext/random/php_random.h"
      30             : #endif
      31             : #include "ext/standard/info.h"
      32             : 
      33             : #if PHP_VERSION_ID < 70200
      34             : /* For spl_ce_Countable */
      35             : #include "ext/spl/spl_iterators.h"
      36             : #endif
      37             : 
      38             : #include "php_excimer.h"
      39             : #include "excimer_timer.h"
      40             : #include "excimer_log.h"
      41             : 
      42             : #define EXCIMER_OBJ(type, object) \
      43             :     ((type ## _obj*)excimer_check_object(object, offsetof(type ## _obj, std), &type ## _handlers))
      44             : 
      45             : #define EXCIMER_OBJ_Z(type, zval) (Z_TYPE(zval) == IS_OBJECT ? EXCIMER_OBJ(type, Z_OBJ(zval)) : NULL)
      46             : 
      47             : #define EXCIMER_OBJ_ZP(type, zval_ptr) EXCIMER_OBJ(type, Z_OBJ_P(zval_ptr))
      48             : 
      49             : #define EXCIMER_NEW_OBJECT(type, ce) \
      50             :     excimer_object_alloc_init(sizeof(type ## _obj), &type ## _handlers, ce)
      51             : 
      52             : #define EXCIMER_DEFAULT_PERIOD 0.1
      53             : #define EXCIMER_BILLION 1000000000LL
      54             : /* {{{ types */
      55             : 
      56             : /**
      57             :  * ExcimerProfiler_obj: underlying storage for ExcimerProfiler
      58             :  */
      59             : typedef struct {
      60             :     /** The period which will be used when the timer is next started */
      61             :     struct timespec period;
      62             : 
      63             :     /** The initial interval */
      64             :     struct timespec initial;
      65             : 
      66             :     /** The event type, either EXCIMER_CPU or EXCIMER_REAL */
      67             :     zend_long event_type;
      68             : 
      69             :     /** The currently-attached log */
      70             :     zval z_log;
      71             : 
      72             :     /** The flush callback. If this is set, max_samples will also be set. */
      73             :     zval z_callback;
      74             : 
      75             :     /** The maximum number of samples in z_log before z_callback is called. */
      76             :     zend_long max_samples;
      77             : 
      78             :     /** Whether a parameter has changed that requires reinitialisation of the timer. */
      79             :     int need_reinit;
      80             : 
      81             :     /** The timer backend object */
      82             :     excimer_timer timer;
      83             :     zend_object std;
      84             : } ExcimerProfiler_obj;
      85             : 
      86             : /**
      87             :  * ExcimerLog_iterator: Iterator object returned by get_iterator handler, used
      88             :  * by foreach.
      89             :  */
      90             : typedef struct {
      91             :     /**
      92             :      * FIXME Iterators use PHP 5 style inheritance. This is actually a
      93             :      * zend_object header. Maybe it is harmless but it seems dodgy to me,
      94             :      * should be fixed upstream.
      95             :      */
      96             :     zend_user_iterator intern;
      97             : 
      98             :     /** Cached (lazy-initialised) value to use for current() */
      99             :     zval z_current;
     100             : 
     101             :     /** Current log index */
     102             :     zend_long index;
     103             : } ExcimerLog_iterator;
     104             : 
     105             : /**
     106             :  * ExcimerLog_obj: underlying storage for ExcimerLog
     107             :  */
     108             : typedef struct {
     109             :     /** The log backend object */
     110             :     excimer_log log;
     111             : 
     112             :     /** The cached value to use for current() */
     113             :     zval z_current;
     114             : 
     115             :     /** The current index, for key() etc. */
     116             :     zend_long iter_entry_index;
     117             :     zend_object std;
     118             : } ExcimerLog_obj;
     119             : 
     120             : /**
     121             :  * ExcimerLogEntry_obj: underlying storage for ExcimerLogEntry
     122             :  */
     123             : typedef struct {
     124             :     /**
     125             :      * The ExcimerLog. Note that this can be a circular reference if
     126             :      * ExcimerLog_obj.z_current points here.
     127             :      */
     128             :     zval z_log;
     129             : 
     130             :     /** The index of this entry in the ExcimerLog */
     131             :     zend_long index;
     132             :     zend_object std;
     133             : } ExcimerLogEntry_obj;
     134             : 
     135             : /**
     136             :  * ExcimerTimer_obj: underlying storage for ExcimerTimer
     137             :  */
     138             : typedef struct {
     139             :     /** The timer backend object */
     140             :     excimer_timer timer;
     141             : 
     142             :     /** The timer period */
     143             :     struct timespec period;
     144             : 
     145             :     /** The initial expiry, or zero to use the period */
     146             :     struct timespec initial;
     147             : 
     148             :     /** The event type, EXCIMER_REAL or EXCIMER_CPU */
     149             :     zend_long event_type;
     150             : 
     151             :     /** Whether a parameter has changed that requires reinitialisation of the timer. */
     152             :     int need_reinit;
     153             : 
     154             :     /** The event function, or null for no callback */
     155             :     zval z_callback;
     156             :     zend_object std;
     157             : } ExcimerTimer_obj;
     158             : /* }}} */
     159             : 
     160             : /* {{{ static function declarations */
     161             : static void ExcimerProfiler_start(ExcimerProfiler_obj *profiler);
     162             : static void ExcimerProfiler_stop(ExcimerProfiler_obj *profiler);
     163             : static void ExcimerProfiler_event(zend_long event_count, void *user_data);
     164             : static void ExcimerProfiler_flush(ExcimerProfiler_obj *profiler, zval *zp_old_log);
     165             : 
     166             : static zend_object *ExcimerProfiler_new(zend_class_entry *ce);
     167             : static void ExcimerProfiler_free_object(zend_object *object);
     168             : static void ExcimerProfiler_dtor(zend_object *object);
     169             : static PHP_METHOD(ExcimerProfiler, setPeriod);
     170             : static PHP_METHOD(ExcimerProfiler, setEventType);
     171             : static PHP_METHOD(ExcimerProfiler, setMaxDepth);
     172             : static PHP_METHOD(ExcimerProfiler, setFlushCallback);
     173             : static PHP_METHOD(ExcimerProfiler, clearFlushCallback);
     174             : static PHP_METHOD(ExcimerProfiler, start);
     175             : static PHP_METHOD(ExcimerProfiler, stop);
     176             : static PHP_METHOD(ExcimerProfiler, getLog);
     177             : static PHP_METHOD(ExcimerProfiler, flush);
     178             : 
     179             : static zend_object *ExcimerLog_new(zend_class_entry *ce);
     180             : static void ExcimerLog_free_object(zend_object *object);
     181             : static zend_object_iterator *ExcimerLog_get_iterator(zend_class_entry *ce, zval *object, int by_ref);
     182             : 
     183             : #if PHP_VERSION_ID < 80000
     184             : static int ExcimerLog_count_elements(zval *zp_log, zend_long *lp_count);
     185             : #else
     186             : static int ExcimerLog_count_elements(zend_object *object, zend_long *lp_count);
     187             : #endif
     188             : 
     189             : static void ExcimerLog_init_entry(zval *zp_dest, zval *zp_log, zend_long index);
     190             : 
     191             : static void ExcimerLog_iterator_dtor(zend_object_iterator *iter);
     192             : static int ExcimerLog_iterator_valid(zend_object_iterator *iter);
     193             : static zval *ExcimerLog_iterator_get_current_data(zend_object_iterator *iter);
     194             : static void ExcimerLog_iterator_get_current_key(zend_object_iterator *iter, zval *key);
     195             : static void ExcimerLog_iterator_move_forward(zend_object_iterator *iter);
     196             : static void ExcimerLog_iterator_rewind(zend_object_iterator *iter);
     197             : static void ExcimerLog_iterator_invalidate_current(zend_object_iterator *iter);
     198             : 
     199             : static PHP_METHOD(ExcimerLog, __construct);
     200             : static PHP_METHOD(ExcimerLog, formatCollapsed);
     201             : static PHP_METHOD(ExcimerLog, getSpeedscopeData);
     202             : static PHP_METHOD(ExcimerLog, aggregateByFunction);
     203             : static PHP_METHOD(ExcimerLog, getEventCount);
     204             : static PHP_METHOD(ExcimerLog, current);
     205             : static PHP_METHOD(ExcimerLog, key);
     206             : static PHP_METHOD(ExcimerLog, next);
     207             : static PHP_METHOD(ExcimerLog, rewind);
     208             : static PHP_METHOD(ExcimerLog, valid);
     209             : static PHP_METHOD(ExcimerLog, count);
     210             : static PHP_METHOD(ExcimerLog, offsetExists);
     211             : static PHP_METHOD(ExcimerLog, offsetGet);
     212             : static PHP_METHOD(ExcimerLog, offsetSet);
     213             : static PHP_METHOD(ExcimerLog, offsetUnset);
     214             : 
     215             : static zend_object *ExcimerLogEntry_new(zend_class_entry *ce);
     216             : static void ExcimerLogEntry_free_object(zend_object *object);
     217             : 
     218             : static PHP_METHOD(ExcimerLogEntry, __construct);
     219             : static PHP_METHOD(ExcimerLogEntry, getTimestamp);
     220             : static PHP_METHOD(ExcimerLogEntry, getEventCount);
     221             : static PHP_METHOD(ExcimerLogEntry, getTrace);
     222             : 
     223             : static zend_object *ExcimerTimer_new(zend_class_entry *ce);
     224             : static void ExcimerTimer_free_object(zend_object *object);
     225             : static PHP_METHOD(ExcimerTimer, setEventType);
     226             : static PHP_METHOD(ExcimerTimer, setInterval);
     227             : static PHP_METHOD(ExcimerTimer, setPeriod);
     228             : static PHP_METHOD(ExcimerTimer, setCallback);
     229             : static PHP_METHOD(ExcimerTimer, start);
     230             : static PHP_METHOD(ExcimerTimer, stop);
     231             : static PHP_METHOD(ExcimerTimer, getTime);
     232             : 
     233             : static void ExcimerTimer_start(ExcimerTimer_obj *timer_obj);
     234             : static void ExcimerTimer_stop(ExcimerTimer_obj *timer_obj);
     235             : static void ExcimerTimer_event(zend_long event_count, void *user_data);
     236             : static int ExcimerTimer_set_callback(ExcimerTimer_obj *timer_obj, zval *zp_callback);
     237             : 
     238             : static PHP_FUNCTION(excimer_set_timeout);
     239             : /* }}} */
     240             : 
     241             : static zend_class_entry *ExcimerProfiler_ce;
     242             : static zend_class_entry *ExcimerLog_ce;
     243             : static zend_class_entry *ExcimerLogEntry_ce;
     244             : static zend_class_entry *ExcimerTimer_ce;
     245             : 
     246             : static zend_object_handlers ExcimerProfiler_handlers;
     247             : static zend_object_handlers ExcimerLog_handlers;
     248             : static zend_object_handlers ExcimerLogEntry_handlers;
     249             : static zend_object_handlers ExcimerTimer_handlers;
     250             : 
     251             : /** {{{ arginfo */
     252             : #ifndef ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX
     253             : #define ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, allow_null) \
     254             :         ZEND_BEGIN_ARG_INFO_EX(name, 0, return_reference, required_num_args)
     255             : #endif
     256             : 
     257             : ZEND_BEGIN_ARG_INFO(arginfo_ExcimerProfiler_setPeriod, 0)
     258             :     ZEND_ARG_INFO(0, period)
     259             : ZEND_END_ARG_INFO()
     260             : 
     261             : ZEND_BEGIN_ARG_INFO(arginfo_ExcimerProfiler_setEventType, 0)
     262             :     ZEND_ARG_INFO(0, event_type)
     263             : ZEND_END_ARG_INFO()
     264             : 
     265             : ZEND_BEGIN_ARG_INFO(arginfo_ExcimerProfiler_setMaxDepth, 0)
     266             :     ZEND_ARG_INFO(0, max_depth)
     267             : ZEND_END_ARG_INFO()
     268             : 
     269             : ZEND_BEGIN_ARG_INFO(arginfo_ExcimerProfiler_setFlushCallback, 0)
     270             :     ZEND_ARG_INFO(0, callback)
     271             :     ZEND_ARG_INFO(0, max_samples)
     272             : ZEND_END_ARG_INFO()
     273             : 
     274             : ZEND_BEGIN_ARG_INFO(arginfo_ExcimerProfiler_clearFlushCallback, 0)
     275             : ZEND_END_ARG_INFO()
     276             : 
     277             : ZEND_BEGIN_ARG_INFO(arginfo_ExcimerProfiler_start, 0)
     278             : ZEND_END_ARG_INFO()
     279             : 
     280             : ZEND_BEGIN_ARG_INFO(arginfo_ExcimerProfiler_stop, 0)
     281             : ZEND_END_ARG_INFO()
     282             : 
     283             : #if PHP_VERSION_ID >= 70200
     284             : ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_ExcimerProfiler_getLog, 0, 0, ExcimerLog, 0)
     285             : #else
     286             : ZEND_BEGIN_ARG_INFO(arginfo_ExcimerProfiler_getLog, 0)
     287             : #endif
     288             : ZEND_END_ARG_INFO()
     289             : 
     290             : ZEND_BEGIN_ARG_INFO(arginfo_ExcimerProfiler_flush, 0)
     291             : ZEND_END_ARG_INFO()
     292             : 
     293             : ZEND_BEGIN_ARG_INFO(arginfo_ExcimerLog___construct, 0)
     294             : ZEND_END_ARG_INFO()
     295             : 
     296             : ZEND_BEGIN_ARG_INFO(arginfo_ExcimerLog_formatCollapsed, 0)
     297             : ZEND_END_ARG_INFO()
     298             : 
     299             : ZEND_BEGIN_ARG_INFO(arginfo_ExcimerLog_getSpeedscopeData, 0)
     300             : ZEND_END_ARG_INFO()
     301             : 
     302             : #if PHP_VERSION_ID < 70200
     303             : ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO(arginfo_ExcimerLog_aggregateByFunction, IS_ARRAY, NULL, 0)
     304             : #else
     305             : ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO(arginfo_ExcimerLog_aggregateByFunction, IS_ARRAY, 0)
     306             : #endif
     307             : ZEND_END_ARG_INFO()
     308             : 
     309             : ZEND_BEGIN_ARG_INFO(arginfo_ExcimerLog_getEventCount, 0)
     310             : ZEND_END_ARG_INFO()
     311             : 
     312             : ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_ExcimerLog_current, 0, 0, IS_MIXED, 0)
     313             : ZEND_END_ARG_INFO()
     314             : 
     315             : ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_ExcimerLog_key, 0, 0, IS_MIXED, 0)
     316             : ZEND_END_ARG_INFO()
     317             : 
     318             : ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_ExcimerLog_next, 0, 0, IS_VOID, 0)
     319             : ZEND_END_ARG_INFO()
     320             : 
     321             : ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_ExcimerLog_rewind, 0, 0, IS_VOID, 0)
     322             : ZEND_END_ARG_INFO()
     323             : 
     324             : ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_ExcimerLog_valid, 0, 0, _IS_BOOL, 0)
     325             : ZEND_END_ARG_INFO()
     326             : 
     327             : ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_ExcimerLog_count, 0, 0, IS_LONG, 0)
     328             : ZEND_END_ARG_INFO()
     329             : 
     330             : ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_ExcimerLog_offsetExists, 0, 1, _IS_BOOL, 0)
     331             :     ZEND_ARG_INFO(0, offset)
     332             : ZEND_END_ARG_INFO()
     333             : 
     334             : ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_ExcimerLog_offsetGet, 0, 1, IS_MIXED, 0)
     335             :     ZEND_ARG_INFO(0, offset)
     336             : ZEND_END_ARG_INFO()
     337             : 
     338             : ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_ExcimerLog_offsetSet, 0, 2, IS_VOID, 0)
     339             :     ZEND_ARG_INFO(0, offset)
     340             :     ZEND_ARG_INFO(0, value)
     341             : ZEND_END_ARG_INFO()
     342             : 
     343             : ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_ExcimerLog_offsetUnset, 0, 1, IS_VOID, 0)
     344             :     ZEND_ARG_INFO(0, offset)
     345             : ZEND_END_ARG_INFO()
     346             : 
     347             : ZEND_BEGIN_ARG_INFO(arginfo_ExcimerLogEntry___construct, 0)
     348             : ZEND_END_ARG_INFO()
     349             : 
     350             : ZEND_BEGIN_ARG_INFO(arginfo_ExcimerLogEntry_getTimestamp, 0)
     351             : ZEND_END_ARG_INFO()
     352             : 
     353             : ZEND_BEGIN_ARG_INFO(arginfo_ExcimerLogEntry_getEventCount, 0)
     354             : ZEND_END_ARG_INFO()
     355             : 
     356             : ZEND_BEGIN_ARG_INFO(arginfo_ExcimerLogEntry_getTrace, 0)
     357             : ZEND_END_ARG_INFO()
     358             : 
     359             : ZEND_BEGIN_ARG_INFO(arginfo_ExcimerTimer_setEventType, 0)
     360             :     ZEND_ARG_INFO(0, event_type)
     361             : ZEND_END_ARG_INFO()
     362             : 
     363             : ZEND_BEGIN_ARG_INFO(arginfo_ExcimerTimer_setInterval, 0)
     364             :     ZEND_ARG_INFO(0, interval)
     365             : ZEND_END_ARG_INFO()
     366             : 
     367             : ZEND_BEGIN_ARG_INFO(arginfo_ExcimerTimer_setPeriod, 0)
     368             :     ZEND_ARG_INFO(0, period)
     369             : ZEND_END_ARG_INFO()
     370             : 
     371             : ZEND_BEGIN_ARG_INFO(arginfo_ExcimerTimer_setCallback, 0)
     372             :     ZEND_ARG_INFO(0, callback)
     373             : ZEND_END_ARG_INFO()
     374             : 
     375             : ZEND_BEGIN_ARG_INFO(arginfo_ExcimerTimer_start, 0)
     376             : ZEND_END_ARG_INFO()
     377             : 
     378             : ZEND_BEGIN_ARG_INFO(arginfo_ExcimerTimer_stop, 0)
     379             : ZEND_END_ARG_INFO()
     380             : 
     381             : ZEND_BEGIN_ARG_INFO(arginfo_ExcimerTimer_getTime, 0)
     382             : ZEND_END_ARG_INFO()
     383             : 
     384             : ZEND_BEGIN_ARG_INFO(arginfo_excimer_set_timeout, 0)
     385             :     ZEND_ARG_INFO(0, callback)
     386             :     ZEND_ARG_INFO(0, interval)
     387             : ZEND_END_ARG_INFO()
     388             : 
     389             : /* }}} */
     390             : 
     391             : /** {{{ function entries */
     392             : static const zend_function_entry ExcimerProfiler_methods[] = {
     393             :     PHP_ME(ExcimerProfiler, setPeriod, arginfo_ExcimerProfiler_setPeriod, 0)
     394             :     PHP_ME(ExcimerProfiler, setEventType, arginfo_ExcimerProfiler_setEventType, 0)
     395             :     PHP_ME(ExcimerProfiler, setMaxDepth, arginfo_ExcimerProfiler_setMaxDepth, 0)
     396             :     PHP_ME(ExcimerProfiler, setFlushCallback, arginfo_ExcimerProfiler_setFlushCallback, 0)
     397             :     PHP_ME(ExcimerProfiler, clearFlushCallback, arginfo_ExcimerProfiler_clearFlushCallback, 0)
     398             :     PHP_ME(ExcimerProfiler, start, arginfo_ExcimerProfiler_start, 0)
     399             :     PHP_ME(ExcimerProfiler, stop, arginfo_ExcimerProfiler_stop, 0)
     400             :     PHP_ME(ExcimerProfiler, getLog, arginfo_ExcimerProfiler_getLog, 0)
     401             :     PHP_ME(ExcimerProfiler, flush, arginfo_ExcimerProfiler_flush, 0)
     402             :     PHP_FE_END
     403             : };
     404             : 
     405             : static const zend_function_entry ExcimerLog_methods[] = {
     406             :     PHP_ME(ExcimerLog, __construct, arginfo_ExcimerLog___construct,
     407             :         ZEND_ACC_PRIVATE | ZEND_ACC_FINAL)
     408             :     PHP_ME(ExcimerLog, formatCollapsed, arginfo_ExcimerLog_formatCollapsed, 0)
     409             :     PHP_ME(ExcimerLog, getSpeedscopeData, arginfo_ExcimerLog_getSpeedscopeData, 0)
     410             :     PHP_ME(ExcimerLog, aggregateByFunction, arginfo_ExcimerLog_aggregateByFunction, 0)
     411             :     PHP_ME(ExcimerLog, getEventCount, arginfo_ExcimerLog_getEventCount, 0)
     412             :     PHP_ME(ExcimerLog, current, arginfo_ExcimerLog_current, 0)
     413             :     PHP_ME(ExcimerLog, key, arginfo_ExcimerLog_key, 0)
     414             :     PHP_ME(ExcimerLog, next, arginfo_ExcimerLog_next, 0)
     415             :     PHP_ME(ExcimerLog, rewind, arginfo_ExcimerLog_rewind, 0)
     416             :     PHP_ME(ExcimerLog, valid, arginfo_ExcimerLog_valid, 0)
     417             :     PHP_ME(ExcimerLog, count, arginfo_ExcimerLog_count, 0)
     418             :     PHP_ME(ExcimerLog, offsetExists, arginfo_ExcimerLog_offsetExists, 0)
     419             :     PHP_ME(ExcimerLog, offsetGet, arginfo_ExcimerLog_offsetGet, 0)
     420             :     PHP_ME(ExcimerLog, offsetSet, arginfo_ExcimerLog_offsetSet, 0)
     421             :     PHP_ME(ExcimerLog, offsetUnset, arginfo_ExcimerLog_offsetUnset, 0)
     422             :     PHP_FE_END
     423             : };
     424             : 
     425             : static zend_object_iterator_funcs ExcimerLog_iterator_funcs = {
     426             :     ExcimerLog_iterator_dtor,
     427             :     ExcimerLog_iterator_valid,
     428             :     ExcimerLog_iterator_get_current_data,
     429             :     ExcimerLog_iterator_get_current_key,
     430             :     ExcimerLog_iterator_move_forward,
     431             :     ExcimerLog_iterator_rewind,
     432             :     ExcimerLog_iterator_invalidate_current
     433             : };
     434             : 
     435             : static const zend_function_entry ExcimerLogEntry_methods[] = {
     436             :     PHP_ME(ExcimerLogEntry, __construct, arginfo_ExcimerLogEntry___construct,
     437             :         ZEND_ACC_PRIVATE | ZEND_ACC_FINAL)
     438             :     PHP_ME(ExcimerLogEntry, getTimestamp, arginfo_ExcimerLogEntry_getTimestamp, 0)
     439             :     PHP_ME(ExcimerLogEntry, getEventCount, arginfo_ExcimerLogEntry_getEventCount, 0)
     440             :     PHP_ME(ExcimerLogEntry, getTrace, arginfo_ExcimerLogEntry_getTrace, 0)
     441             :     PHP_FE_END
     442             : };
     443             : 
     444             : static const zend_function_entry ExcimerTimer_methods[] = {
     445             :     PHP_ME(ExcimerTimer, setEventType, arginfo_ExcimerTimer_setEventType, 0)
     446             :     PHP_ME(ExcimerTimer, setInterval, arginfo_ExcimerTimer_setInterval, 0)
     447             :     PHP_ME(ExcimerTimer, setPeriod, arginfo_ExcimerTimer_setPeriod, 0)
     448             :     PHP_ME(ExcimerTimer, setCallback, arginfo_ExcimerTimer_setCallback, 0)
     449             :     PHP_ME(ExcimerTimer, start, arginfo_ExcimerTimer_start, 0)
     450             :     PHP_ME(ExcimerTimer, stop, arginfo_ExcimerTimer_stop, 0)
     451             :     PHP_ME(ExcimerTimer, getTime, arginfo_ExcimerTimer_getTime, 0)
     452             :     PHP_FE_END
     453             : };
     454             : 
     455             : static const zend_function_entry excimer_functions[] = {
     456             :     PHP_FE(excimer_set_timeout, arginfo_excimer_set_timeout)
     457             :     PHP_FE_END
     458             : };
     459             : /* }}} */
     460             : 
     461             : /* {{{ INI Settings */
     462             : PHP_INI_BEGIN()
     463             :     PHP_INI_ENTRY("excimer.default_max_depth", "1000", PHP_INI_ALL, NULL)
     464             : PHP_INI_END()
     465             : /* }}} */
     466             : 
     467         140 : static void *excimer_object_alloc_init(size_t object_size, zend_object_handlers *handlers, zend_class_entry *ce) /* {{{ */
     468             : {
     469             : #if PHP_VERSION_ID < 70300
     470             :     char *intern = ecalloc(1, object_size + zend_object_properties_size(ce));
     471             : #else
     472         140 :     char *intern = zend_object_alloc(object_size, ce);
     473             : #endif
     474         140 :     const size_t header_size = object_size - sizeof(zend_object);
     475         140 :     zend_object *object = (zend_object*)(intern + header_size);
     476         140 :     zend_object_std_init(object, ce);
     477         140 :     object_properties_init(object, ce);
     478         140 :     object->handlers = handlers;
     479         140 :     return intern;
     480             : }
     481             : /* }}} */
     482             : 
     483        1450 : static inline void* excimer_check_object(zend_object *object, size_t offset, const zend_object_handlers *handlers)
     484             : {
     485        1450 :     if (object->handlers != handlers) {
     486           0 :         return NULL;
     487             :     } else {
     488        1450 :         return (void*)((char*)object - offset);
     489             :     }
     490             : }
     491             : 
     492             : /* {{{ PHP_MINIT_FUNCTION
     493             :  */
     494          17 : static PHP_MINIT_FUNCTION(excimer)
     495             : {
     496             :     zend_class_entry ce;
     497             : 
     498          17 :     REGISTER_INI_ENTRIES();
     499             :     
     500          17 :     REGISTER_LONG_CONSTANT("EXCIMER_REAL", EXCIMER_REAL, CONST_CS | CONST_PERSISTENT);
     501             : 
     502             :     // Only define EXCIMER_CPU if the current platform supports it.
     503             :     // This allows application code to detect and gracefully handle a lack of CPU profiling support.
     504             :     #ifdef TIMERLIB_HAVE_CPU_CLOCK
     505          17 :     REGISTER_LONG_CONSTANT("EXCIMER_CPU", EXCIMER_CPU, CONST_CS | CONST_PERSISTENT);
     506             :     #endif
     507             : 
     508             : #define REGISTER_EXCIMER_CLASS(class_name) \
     509             :     INIT_CLASS_ENTRY(ce, #class_name, class_name ## _methods); \
     510             :     class_name ## _ce = zend_register_internal_class(&ce); \
     511             :     class_name ## _ce->create_object = class_name ## _new; \
     512             :     memcpy(&class_name ## _handlers, zend_get_std_object_handlers(), \
     513             :         sizeof(zend_object_handlers)); \
     514             :     class_name ## _handlers.offset = offsetof(class_name ## _obj, std); \
     515             :     class_name ## _handlers.free_obj = class_name ## _free_object;
     516             : 
     517          17 :     REGISTER_EXCIMER_CLASS(ExcimerProfiler);
     518          17 :     ExcimerProfiler_handlers.dtor_obj = ExcimerProfiler_dtor;
     519             : 
     520          17 :     REGISTER_EXCIMER_CLASS(ExcimerLog);
     521          17 :     ExcimerLog_ce->get_iterator = ExcimerLog_get_iterator;
     522          17 :     ExcimerLog_handlers.count_elements = ExcimerLog_count_elements;
     523             : 
     524          17 :     zend_class_implements(ExcimerLog_ce, 1, zend_ce_iterator);
     525             : #if PHP_VERSION_ID >= 70200
     526          17 :     zend_class_implements(ExcimerLog_ce, 1, zend_ce_countable);
     527          17 :     zend_class_implements(ExcimerLog_ce, 1, zend_ce_arrayaccess);
     528             : #elif defined(HAVE_SPL)
     529             :     zend_class_implements(ExcimerLog_ce, 1, spl_ce_Countable);
     530             :     zend_class_implements(ExcimerLog_ce, 1, spl_ce_ArrayAccess);
     531             : #endif
     532             : 
     533          17 :     REGISTER_EXCIMER_CLASS(ExcimerLogEntry);
     534          17 :     REGISTER_EXCIMER_CLASS(ExcimerTimer);
     535             : 
     536             : #undef REGISTER_EXCIMER_CLASS
     537             : 
     538          17 :     excimer_timer_module_init();
     539             : 
     540          17 :     return SUCCESS;
     541             : }
     542             : /* }}} */
     543             : 
     544             : /* {{{ PHP_MSHUTDOWN_FUNCTION
     545             :  */
     546          17 : static PHP_MSHUTDOWN_FUNCTION(excimer)
     547             : {
     548          17 :     UNREGISTER_INI_ENTRIES();
     549          17 :     excimer_timer_module_shutdown();
     550          17 :     return SUCCESS;
     551             : }
     552             : /* }}} */
     553             : 
     554             : /* {{{ PHP_RINIT_FUNCTION
     555             :  */
     556          17 : static PHP_RINIT_FUNCTION(excimer)
     557             : {
     558          17 :     excimer_timer_thread_init();
     559          17 :     return SUCCESS;
     560             : }
     561             : /* }}} */
     562             : 
     563             : /* {{{ ZEND_MODULE_POST_ZEND_DEACTIVATE_D */
     564          17 : static ZEND_MODULE_POST_ZEND_DEACTIVATE_D(excimer)
     565             : {
     566          17 :     excimer_timer_thread_shutdown();
     567          17 :     return SUCCESS;
     568             : }
     569             : /* }}} */
     570             : 
     571             : /* {{{ PHP_MINFO_FUNCTION
     572             :  */
     573           0 : static PHP_MINFO_FUNCTION(excimer)
     574             : {
     575           0 :     php_info_print_table_start();
     576           0 :     php_info_print_table_header(2, "excimer support", "enabled");
     577           0 :     php_info_print_table_row(2, "excimer version", PHP_EXCIMER_VERSION);
     578           0 :     php_info_print_table_end();
     579           0 :     DISPLAY_INI_ENTRIES();
     580           0 : }
     581             : /* }}} */
     582             : 
     583           4 : static zend_object *ExcimerProfiler_new(zend_class_entry *ce) /* {{{ */
     584             : {
     585           4 :     ExcimerProfiler_obj *profiler = EXCIMER_NEW_OBJECT(ExcimerProfiler, ce);
     586             :     ExcimerLog_obj *log_obj;
     587             :     struct timespec now_ts;
     588             :     double initial;
     589             : 
     590           4 :     timerlib_clock_get_time(TIMERLIB_REAL, &now_ts);
     591             : 
     592           4 :     object_init_ex(&profiler->z_log, ExcimerLog_ce);
     593           8 :     log_obj = EXCIMER_OBJ_Z(ExcimerLog, profiler->z_log);
     594             : #if PHP_VERSION_ID < 80600
     595           4 :     log_obj->log.max_depth = INI_INT("excimer.default_max_depth");
     596             : #else
     597             :     log_obj->log.max_depth = zend_ini_long_literal("excimer.default_max_depth");
     598             : #endif
     599           4 :     log_obj->log.epoch = timerlib_timespec_to_ns(&now_ts);
     600             : 
     601           4 :     ZVAL_NULL(&profiler->z_callback);
     602           4 :     profiler->event_type = EXCIMER_REAL;
     603           4 :     profiler->need_reinit = 1;
     604             : 
     605             :     // Stagger start time
     606           4 :     initial = php_mt_rand() * EXCIMER_DEFAULT_PERIOD / UINT32_MAX;
     607           4 :     timerlib_timespec_from_double(&profiler->initial, initial);
     608           4 :     timerlib_timespec_from_double(&profiler->period, EXCIMER_DEFAULT_PERIOD);
     609           4 :     log_obj->log.period = EXCIMER_DEFAULT_PERIOD * EXCIMER_BILLION;
     610             : 
     611           4 :     return &profiler->std;
     612             : }
     613             : /* }}} */
     614             : 
     615           4 : static void ExcimerProfiler_free_object(zend_object *object) /* {{{ */
     616             : {
     617           4 :     ExcimerProfiler_obj *profiler = EXCIMER_OBJ(ExcimerProfiler, object);
     618             : 
     619           4 :     if (profiler->timer.is_valid) {
     620           4 :         excimer_timer_destroy(&profiler->timer);
     621             :     }
     622           4 :     zval_ptr_dtor(&profiler->z_log);
     623           4 :     ZVAL_UNDEF(&profiler->z_log);
     624           4 :     zval_ptr_dtor(&profiler->z_callback);
     625           4 :     ZVAL_UNDEF(&profiler->z_callback);
     626           4 :     zend_object_std_dtor(object);
     627           4 : }
     628             : /* }}} */
     629             : 
     630           4 : static void ExcimerProfiler_dtor(zend_object *object) /* {{{ */
     631             : {
     632           4 :     ExcimerProfiler_obj *profiler = EXCIMER_OBJ(ExcimerProfiler, object);
     633           8 :     ExcimerLog_obj *log_obj = EXCIMER_OBJ_Z(ExcimerLog, profiler->z_log);
     634             :     zval z_old_log;
     635             : 
     636           4 :     if (log_obj->log.entries_size) {
     637           0 :         ExcimerProfiler_flush(profiler, &z_old_log);
     638           0 :         zval_ptr_dtor(&z_old_log);
     639             :     }
     640           4 : }
     641             : /* }}} */
     642             : 
     643             : /* {{{ proto void ExcimerProfiler::setPeriod(float period)
     644             :  */
     645           4 : static PHP_METHOD(ExcimerProfiler, setPeriod)
     646             : {
     647             :     double period, initial;
     648          12 :     ExcimerProfiler_obj *profiler = EXCIMER_OBJ_ZP(ExcimerProfiler, getThis());
     649             : 
     650           4 :     ZEND_PARSE_PARAMETERS_START(1, 1)
     651           8 :         Z_PARAM_DOUBLE(period)
     652           4 :     ZEND_PARSE_PARAMETERS_END();
     653             : 
     654             :     // Stagger start time
     655           4 :     initial = php_mt_rand() * period / UINT32_MAX;
     656             : 
     657           4 :     timerlib_timespec_from_double(&profiler->period, period);
     658           4 :     timerlib_timespec_from_double(&profiler->initial, initial);
     659             : 
     660           4 :     ExcimerLog_obj *log = EXCIMER_OBJ_ZP(ExcimerLog, &profiler->z_log);
     661           4 :     log->log.period = period * EXCIMER_BILLION;
     662             : }
     663             : /* }}} */
     664             : 
     665             : /* {{{ proto void ExcimerProfiler::setEventType(int event_type)
     666             :  */
     667           4 : static PHP_METHOD(ExcimerProfiler, setEventType)
     668             : {
     669             :     zend_long event_type;
     670           8 :     ExcimerProfiler_obj *profiler = EXCIMER_OBJ_ZP(ExcimerProfiler, getThis());
     671             : 
     672           4 :     ZEND_PARSE_PARAMETERS_START(1, 1)
     673           8 :         Z_PARAM_LONG(event_type)
     674           4 :     ZEND_PARSE_PARAMETERS_END();
     675             : 
     676           4 :     if (event_type != EXCIMER_CPU && event_type != EXCIMER_REAL) {
     677           0 :         php_error_docref(NULL, E_WARNING, "Invalid event type");
     678           0 :         return;
     679             :     }
     680             : 
     681           4 :     profiler->event_type = event_type;
     682           4 :     profiler->need_reinit = 1;
     683             : }
     684             : /* }}} */
     685             : 
     686             : /* {{{ proto void ExcimerProfiler::setMaxDepth(int max_depth)
     687             :  */
     688           1 : static PHP_METHOD(ExcimerProfiler, setMaxDepth)
     689             : {
     690             :     zend_long max_depth;
     691             : 
     692           1 :     ZEND_PARSE_PARAMETERS_START(1, 1)
     693           2 :         Z_PARAM_LONG(max_depth)
     694           1 :     ZEND_PARSE_PARAMETERS_END();
     695             : 
     696           2 :     ExcimerProfiler_obj *profiler = EXCIMER_OBJ_ZP(ExcimerProfiler, getThis());
     697           1 :     ExcimerLog_obj *log_obj = EXCIMER_OBJ_ZP(ExcimerLog, &profiler->z_log);
     698           1 :     excimer_log_set_max_depth(&log_obj->log, max_depth);
     699             : }
     700             : /* }}} */
     701             : 
     702             : /* {{{ proto void ExcimerProfiler::setFlushCallback(callable callback, mixed max_samples)
     703             :  */
     704           0 : static PHP_METHOD(ExcimerProfiler, setFlushCallback)
     705             : {
     706             :     zval *z_callback;
     707             :     zend_long max_samples;
     708           0 :     ExcimerProfiler_obj *profiler = EXCIMER_OBJ_ZP(ExcimerProfiler, getThis());
     709             :     char *is_callable_error;
     710             : 
     711           0 :     ZEND_PARSE_PARAMETERS_START(2, 2)
     712           0 :         Z_PARAM_ZVAL(z_callback)
     713           0 :         Z_PARAM_LONG(max_samples)
     714           0 :     ZEND_PARSE_PARAMETERS_END();
     715             : 
     716           0 :     if (!zend_is_callable_ex(z_callback, NULL, 0, NULL, NULL, &is_callable_error)) {
     717           0 :         php_error_docref(NULL, E_WARNING, "flush callback is not callable: %s",
     718             :                 is_callable_error);
     719           0 :         return;
     720             :     }
     721             : 
     722           0 :     ZVAL_COPY(&profiler->z_callback, z_callback);
     723           0 :     profiler->max_samples = max_samples;
     724             : }
     725             : /* }}} */
     726             : 
     727             : /* {{{ proto void ExcimerProfiler::clearFlushCallback()
     728             :  */
     729           0 : static PHP_METHOD(ExcimerProfiler, clearFlushCallback)
     730             : {
     731           0 :     ExcimerProfiler_obj *profiler = EXCIMER_OBJ_ZP(ExcimerProfiler, getThis());
     732           0 :     zval_ptr_dtor(&profiler->z_callback);
     733           0 :     ZVAL_NULL(&profiler->z_callback);
     734           0 :     profiler->max_samples = 0;
     735           0 : }
     736             : /* }}} */
     737             : 
     738             : /* {{{ proto void ExcimerProfiler::start()
     739             :  */
     740           6 : static PHP_METHOD(ExcimerProfiler, start)
     741             : {
     742          12 :     ExcimerProfiler_obj *profiler = EXCIMER_OBJ_ZP(ExcimerProfiler, getThis());
     743             : 
     744           6 :     ZEND_PARSE_PARAMETERS_START(0, 0);
     745           6 :     ZEND_PARSE_PARAMETERS_END();
     746             : 
     747           6 :     if (profiler->timer.is_running) {
     748           0 :         ExcimerProfiler_stop(profiler);
     749             :     }
     750           6 :     ExcimerProfiler_start(profiler);
     751             : }
     752             : /* }}} */
     753             : 
     754             : /* {{{ proto void ExcimerProfiler::stop()
     755             :  */
     756           5 : static PHP_METHOD(ExcimerProfiler, stop)
     757             : {
     758          10 :     ExcimerProfiler_obj *profiler = EXCIMER_OBJ_ZP(ExcimerProfiler, getThis());
     759             : 
     760           5 :     ZEND_PARSE_PARAMETERS_START(0, 0);
     761           5 :     ZEND_PARSE_PARAMETERS_END();
     762             : 
     763           5 :     ExcimerProfiler_stop(profiler);
     764             : }
     765             : /* }}} */
     766             : 
     767             : /* {{{ proto ExcimerLog ExcimerProfiler::getLog()
     768             :  */
     769          40 : static PHP_METHOD(ExcimerProfiler, getLog)
     770             : {
     771          80 :     ExcimerProfiler_obj * profiler = EXCIMER_OBJ_ZP(ExcimerProfiler, getThis());
     772             : 
     773          40 :     ZEND_PARSE_PARAMETERS_START(0, 0);
     774          40 :     ZEND_PARSE_PARAMETERS_END();
     775             : 
     776         120 :     RETURN_ZVAL(&profiler->z_log, 1, 0);
     777             : }
     778             : /* }}} */
     779             : 
     780             : /* {{{ proto ExcimerLog ExcimerProfiler::flush() */
     781           4 : static PHP_METHOD(ExcimerProfiler, flush)
     782             : {
     783           8 :     ExcimerProfiler_obj *profiler = EXCIMER_OBJ_ZP(ExcimerProfiler, getThis());
     784             : 
     785           4 :     ZEND_PARSE_PARAMETERS_START(0, 0);
     786           4 :     ZEND_PARSE_PARAMETERS_END();
     787             : 
     788           4 :     ExcimerProfiler_flush(profiler, return_value);
     789             : }
     790             : /* }}} */
     791             : 
     792           6 : static void ExcimerProfiler_start(ExcimerProfiler_obj *profiler) /* {{{ */
     793             : {
     794           6 :     if (profiler->need_reinit || !profiler->timer.is_valid) {
     795           4 :         if (profiler->timer.is_valid) {
     796           0 :             excimer_timer_destroy(&profiler->timer);
     797             :         }
     798           4 :         if (excimer_timer_init(&profiler->timer,
     799           4 :             profiler->event_type,
     800             :             ExcimerProfiler_event,
     801             :             (void*)profiler) == FAILURE)
     802             :         {
     803             :             /* Error message already sent */
     804           0 :             return;
     805             :         }
     806           4 :         profiler->need_reinit = 0;
     807             :     }
     808           6 :     excimer_timer_start(&profiler->timer,
     809             :             &profiler->period,
     810             :             &profiler->initial);
     811             : }
     812             : /* }}} */
     813             : 
     814           5 : static void ExcimerProfiler_stop(ExcimerProfiler_obj *profiler) /* {{{ */
     815             : {
     816           5 :     if (profiler->timer.is_valid) {
     817           5 :         excimer_timer_stop(&profiler->timer);
     818             :     }
     819           5 : }
     820             : /* }}} */
     821             : 
     822          62 : static void ExcimerProfiler_event(zend_long event_count, void *user_data) /* {{{ */
     823             : {
     824             :     uint64_t now_ns;
     825             :     struct timespec now_ts;
     826          62 :     ExcimerProfiler_obj *profiler = (ExcimerProfiler_obj*)user_data;
     827          62 :     ExcimerLog_obj *log_obj = EXCIMER_OBJ_ZP(ExcimerLog, &profiler->z_log);
     828             :     excimer_log *log;
     829             : 
     830          62 :     log = &log_obj->log;
     831             : 
     832          62 :     timerlib_clock_get_time(TIMERLIB_REAL, &now_ts);
     833          62 :     now_ns = timerlib_timespec_to_ns(&now_ts);
     834             : 
     835          62 :     excimer_log_add(log, EG(current_execute_data), event_count, now_ns);
     836             : 
     837          62 :     if (profiler->max_samples && log->entries_size >= profiler->max_samples) {
     838             :         zval z_old_log;
     839           0 :         ExcimerProfiler_flush(profiler, &z_old_log);
     840           0 :         zval_ptr_dtor(&z_old_log);
     841             :     }
     842          62 : }
     843             : /* }}} */
     844             : 
     845           4 : static void ExcimerProfiler_flush(ExcimerProfiler_obj *profiler, zval *zp_old_log) /* {{{ */
     846             : {
     847           4 :     ExcimerLog_obj *log_obj = EXCIMER_OBJ_ZP(ExcimerLog, &profiler->z_log);
     848           4 :     excimer_log *log = &log_obj->log;
     849             : 
     850             :     zend_fcall_info fci;
     851             :     zend_fcall_info_cache fcc;
     852           4 :     char *is_callable_error = NULL;
     853             :     zval retval;
     854             :     int status;
     855             : 
     856             :     /* Rotate the log */
     857           4 :     ZVAL_COPY(zp_old_log, &profiler->z_log);
     858           4 :     Z_DELREF(profiler->z_log);
     859           4 :     object_init_ex(&profiler->z_log, ExcimerLog_ce);
     860           4 :     excimer_log_copy_options(&EXCIMER_OBJ_ZP(ExcimerLog, &profiler->z_log)->log, log);
     861             : 
     862           8 :     if (Z_ISNULL(profiler->z_callback)) {
     863           4 :         return;
     864             :     }
     865             : 
     866             :     /* Prepare to call the flush callback */
     867           0 :     if (zend_fcall_info_init(&profiler->z_callback, 0, &fci, &fcc, NULL,
     868             :         &is_callable_error) != SUCCESS)
     869             :     {
     870           0 :         php_error(E_WARNING, "ExcimerProfiler callback is not callable (during event): %s",
     871             :             is_callable_error);
     872           0 :         ExcimerProfiler_stop(profiler);
     873           0 :         return;
     874             :     }
     875             : 
     876           0 :     fci.retval = &retval;
     877             : 
     878             :     /* Call it */
     879           0 :     zend_fcall_info_argn(&fci, 1, zp_old_log);
     880           0 :     status = zend_call_function(&fci, &fcc);
     881           0 :     if (status == SUCCESS) {
     882           0 :         zval_ptr_dtor(&retval);
     883             :     }
     884           0 :     zend_fcall_info_args_clear(&fci, 1);
     885             : }
     886             : /* }}} */
     887             : 
     888           8 : static zend_object *ExcimerLog_new(zend_class_entry *ce) /* {{{ */
     889             : {
     890           8 :     ExcimerLog_obj *log_obj = EXCIMER_NEW_OBJECT(ExcimerLog, ce);
     891           8 :     excimer_log_init(&log_obj->log);
     892             :     /* Lazy-initialise z_current to minimise circular references */
     893           8 :     ZVAL_NULL(&log_obj->z_current);
     894           8 :     log_obj->iter_entry_index = 0;
     895           8 :     return &log_obj->std;
     896             : }
     897             : /* }}} */
     898             : 
     899           8 : static void ExcimerLog_free_object(zend_object *object) /* {{{ */
     900             : {
     901           8 :     ExcimerLog_obj *log_obj = EXCIMER_OBJ(ExcimerLog, object);
     902           8 :     excimer_log_destroy(&log_obj->log);
     903           8 :     zval_ptr_dtor(&log_obj->z_current);
     904           8 :     zend_object_std_dtor(object);
     905           8 : }
     906             : /* }}} */
     907             : 
     908             : /* {{{ ExcimerLog_get_iterator */
     909           2 : static zend_object_iterator *ExcimerLog_get_iterator(
     910             :     zend_class_entry *ce, zval *zp_log, int by_ref)
     911             : {
     912             :     ExcimerLog_iterator *iterator;
     913             : 
     914           2 :     if (by_ref) {
     915           0 :         zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0);
     916           0 :         return NULL;
     917             :     }
     918             : 
     919           2 :     iterator = emalloc(sizeof(ExcimerLog_iterator));
     920           2 :     zend_iterator_init((zend_object_iterator*)iterator);
     921             : 
     922           2 :     ZVAL_COPY(&iterator->intern.it.data, zp_log);
     923             : 
     924           2 :     iterator->intern.it.funcs = &ExcimerLog_iterator_funcs;
     925           2 :     iterator->intern.ce = ce;
     926           2 :     iterator->index = 0;
     927           2 :     ZVAL_NULL(&iterator->z_current);
     928             : 
     929           2 :     return &iterator->intern.it;
     930             : }
     931             : /* }}} */
     932             : 
     933           2 : static void ExcimerLog_iterator_dtor(zend_object_iterator *iter) /* {{{ */
     934             : {
     935           2 :     ExcimerLog_iterator *iterator = (ExcimerLog_iterator*)iter;
     936             : 
     937           2 :     zval_ptr_dtor(&iterator->z_current);
     938           2 :     ZVAL_UNDEF(&iterator->z_current);
     939           2 :     zval_ptr_dtor(&iterator->intern.it.data);
     940           2 :     ZVAL_UNDEF(&iterator->intern.it.data);
     941           2 : }
     942             : /* }}} */
     943             : 
     944          62 : static int ExcimerLog_iterator_valid(zend_object_iterator *iter) /* {{{ */
     945             : {
     946          62 :     ExcimerLog_iterator *iterator = (ExcimerLog_iterator*)iter;
     947         124 :     ExcimerLog_obj *log_obj = EXCIMER_OBJ_Z(ExcimerLog, iterator->intern.it.data);
     948             : 
     949          62 :     if (iterator->index < log_obj->log.entries_size) {
     950          60 :         return SUCCESS;
     951             :     } else {
     952           2 :         return FAILURE;
     953             :     }
     954             : }
     955             : /* }}} */
     956             : 
     957          60 : static zval *ExcimerLog_iterator_get_current_data(zend_object_iterator *iter) /* {{{ */
     958             : {
     959          60 :     ExcimerLog_iterator *iterator = (ExcimerLog_iterator*)iter;
     960         120 :     ExcimerLog_obj *log_obj = EXCIMER_OBJ_Z(ExcimerLog, iterator->intern.it.data);
     961             : 
     962         120 :     if (Z_ISNULL(iterator->z_current)) {
     963          60 :         if (iterator->index < log_obj->log.entries_size) {
     964          60 :             ExcimerLog_init_entry(&iterator->z_current, &iterator->intern.it.data, iterator->index);
     965             :         } else {
     966           0 :             return NULL;
     967             :         }
     968             :     }
     969          60 :     return &iterator->z_current;
     970             : }
     971             : /* }}} */
     972             : 
     973           0 : static void ExcimerLog_iterator_get_current_key(zend_object_iterator *iter, zval *key) /* {{{ */
     974             : {
     975           0 :     ExcimerLog_iterator *iterator = (ExcimerLog_iterator*)iter;
     976           0 :     ExcimerLog_obj *log_obj = EXCIMER_OBJ_Z(ExcimerLog, iterator->intern.it.data);
     977             : 
     978           0 :     if (iterator->index < log_obj->log.entries_size) {
     979           0 :         ZVAL_LONG(key, iterator->index);
     980             :     } else {
     981           0 :         ZVAL_NULL(key);
     982             :     }
     983           0 : }
     984             : /* }}} */
     985             : 
     986          60 : static void ExcimerLog_iterator_move_forward(zend_object_iterator *iter) /* {{{ */
     987             : {
     988          60 :     ExcimerLog_iterator *iterator = (ExcimerLog_iterator*)iter;
     989         120 :     ExcimerLog_obj *log_obj = EXCIMER_OBJ_Z(ExcimerLog, iterator->intern.it.data);
     990             : 
     991          60 :     zval_ptr_dtor(&iterator->z_current);
     992          60 :     ZVAL_NULL(&iterator->z_current);
     993             : 
     994          60 :     if (iterator->index < log_obj->log.entries_size) {
     995          60 :         iterator->index++;
     996             :     }
     997          60 : }
     998             : /* }}} */
     999             : 
    1000           2 : static void ExcimerLog_iterator_rewind(zend_object_iterator *iter) /* {{{ */
    1001             : {
    1002           2 :     ExcimerLog_iterator *iterator = (ExcimerLog_iterator*)iter;
    1003             : 
    1004           2 :     zval_ptr_dtor(&iterator->z_current);
    1005           2 :     ZVAL_NULL(&iterator->z_current);
    1006           2 :     iterator->index = 0;
    1007           2 : }
    1008             : /* }}} */
    1009             : 
    1010           0 : static void ExcimerLog_iterator_invalidate_current(zend_object_iterator *iter) /* {{{ */
    1011             : {
    1012           0 :     ExcimerLog_iterator *iterator = (ExcimerLog_iterator*)iter;
    1013             : 
    1014           0 :     zval_ptr_dtor(&iterator->z_current);
    1015           0 :     ZVAL_NULL(&iterator->z_current);
    1016           0 : }
    1017             : /* }}} */
    1018             : 
    1019          91 : static void ExcimerLog_init_entry(zval *zp_dest, zval *zp_log, zend_long index) /* {{{ */
    1020             : {
    1021          91 :     ExcimerLog_obj *log_obj = EXCIMER_OBJ_ZP(ExcimerLog, zp_log);
    1022          91 :     excimer_log_entry *entry = excimer_log_get_entry(&log_obj->log, index);
    1023             :     ExcimerLogEntry_obj *entry_obj;
    1024             : 
    1025          91 :     if (entry) {
    1026          91 :         object_init_ex(zp_dest, ExcimerLogEntry_ce);
    1027          91 :         entry_obj = EXCIMER_OBJ_ZP(ExcimerLogEntry, zp_dest);
    1028          91 :         ZVAL_COPY(&entry_obj->z_log, zp_log);
    1029          91 :         entry_obj->index = index;
    1030             :     } else {
    1031           0 :         ZVAL_NULL(zp_dest);
    1032             :     }
    1033          91 : }
    1034             : /* }}} */
    1035             : 
    1036             : #if PHP_VERSION_ID < 80000
    1037             : static int ExcimerLog_count_elements(zval *zp_log, zend_long *lp_count) /* {{{ */
    1038             : {
    1039             :     ExcimerLog_obj *log_obj = EXCIMER_OBJ_ZP(ExcimerLog, zp_log);
    1040             :     *lp_count = log_obj->log.entries_size;
    1041             :     return SUCCESS;
    1042             : }
    1043             : /* }}} */
    1044             : #else
    1045          41 : static int ExcimerLog_count_elements(zend_object *object, zend_long *lp_count) /* {{{ */
    1046             : {
    1047          41 :     ExcimerLog_obj *log_obj = EXCIMER_OBJ(ExcimerLog, object);
    1048          41 :     *lp_count = log_obj->log.entries_size;
    1049          41 :     return SUCCESS;
    1050             : }
    1051             : /* }}} */
    1052             : #endif
    1053             : 
    1054             : /* {{{ proto void ExcimerLog::__construct()
    1055             :  */
    1056           0 : static PHP_METHOD(ExcimerLog, __construct)
    1057             : {
    1058           0 :     php_error_docref(NULL, E_ERROR, "ExcimerLog cannot be constructed directly");
    1059           0 : }
    1060             : /* }}} */
    1061             : 
    1062             : /* {{{ proto string ExcimerLog::formatCollapsed()
    1063             :  */
    1064           2 : static PHP_METHOD(ExcimerLog, formatCollapsed)
    1065             : {
    1066           4 :     ExcimerLog_obj *log_obj = EXCIMER_OBJ_ZP(ExcimerLog, getThis());
    1067           4 :     RETURN_STR(excimer_log_format_collapsed(&log_obj->log));
    1068             : }
    1069             : /* }}} */
    1070             : 
    1071             : /* {{{ proto string ExcimerLog::getSpeedscopeData()
    1072             :  */
    1073           1 : static PHP_METHOD(ExcimerLog, getSpeedscopeData)
    1074             : {
    1075           2 :     ExcimerLog_obj *log_obj = EXCIMER_OBJ_ZP(ExcimerLog, getThis());
    1076           1 :     excimer_log_get_speedscope_data(&log_obj->log, return_value);
    1077           1 : }
    1078             : /* }}} */
    1079             : 
    1080             : /* {{{ proto string ExcimerLog::aggregateByFunction()
    1081             :  */
    1082           1 : static PHP_METHOD(ExcimerLog, aggregateByFunction)
    1083             : {
    1084           2 :     ExcimerLog_obj *log_obj = EXCIMER_OBJ_ZP(ExcimerLog, getThis());
    1085           1 :     RETURN_ARR(excimer_log_aggr_by_func(&log_obj->log));
    1086             : }
    1087             : /* }}} */
    1088             : 
    1089             : /* {{{ proto string ExcimerLog::getEventCount()
    1090             :  */
    1091           1 : static PHP_METHOD(ExcimerLog, getEventCount)
    1092             : {
    1093           2 :     ExcimerLog_obj *log_obj = EXCIMER_OBJ_ZP(ExcimerLog, getThis());
    1094           1 :     RETURN_LONG(log_obj->log.event_count);
    1095             : }
    1096             : /* }}} */
    1097             : 
    1098             : /* {{{ proto array ExcimerLog::current()
    1099             :  */
    1100          30 : static PHP_METHOD(ExcimerLog, current)
    1101             : {
    1102          60 :     ExcimerLog_obj * log_obj = EXCIMER_OBJ_ZP(ExcimerLog, getThis());
    1103             : 
    1104          30 :     ZEND_PARSE_PARAMETERS_START(0, 0);
    1105          30 :     ZEND_PARSE_PARAMETERS_END();
    1106             : 
    1107          60 :     if (Z_ISNULL(log_obj->z_current) && log_obj->iter_entry_index < log_obj->log.entries_size) {
    1108          60 :         ExcimerLog_init_entry(&log_obj->z_current, getThis(), log_obj->iter_entry_index);
    1109             :     }
    1110             : 
    1111          90 :     RETURN_ZVAL(&log_obj->z_current, 1, 0);
    1112             : }
    1113             : /* }}} */
    1114             : 
    1115             : /* {{{ proto int ExcimerLog::key()
    1116             :  */
    1117           0 : static PHP_METHOD(ExcimerLog, key)
    1118             : {
    1119           0 :     ExcimerLog_obj * log_obj = EXCIMER_OBJ_ZP(ExcimerLog, getThis());
    1120             : 
    1121           0 :     ZEND_PARSE_PARAMETERS_START(0, 0);
    1122           0 :     ZEND_PARSE_PARAMETERS_END();
    1123             : 
    1124           0 :     if (log_obj->iter_entry_index < log_obj->log.entries_size) {
    1125           0 :         RETURN_LONG(log_obj->iter_entry_index);
    1126             :     } else {
    1127           0 :         RETURN_NULL();
    1128             :     }
    1129             : }
    1130             : /* }}} */
    1131             : 
    1132             : /* {{{ proto void ExcimerLog::next()
    1133             :  */
    1134          30 : static PHP_METHOD(ExcimerLog, next)
    1135             : {
    1136          60 :     ExcimerLog_obj * log_obj = EXCIMER_OBJ_ZP(ExcimerLog, getThis());
    1137             : 
    1138          30 :     ZEND_PARSE_PARAMETERS_START(0, 0);
    1139          30 :     ZEND_PARSE_PARAMETERS_END();
    1140             : 
    1141          30 :     zval_ptr_dtor(&log_obj->z_current);
    1142          30 :     ZVAL_NULL(&log_obj->z_current);
    1143          30 :     if (log_obj->iter_entry_index < log_obj->log.entries_size) {
    1144          30 :         log_obj->iter_entry_index++;
    1145             :     }
    1146             : }
    1147             : /* }}} */
    1148             : 
    1149             : /* {{{ proto void ExcimerLog::rewind()
    1150             :  */
    1151           1 : static PHP_METHOD(ExcimerLog, rewind)
    1152             : {
    1153           2 :     ExcimerLog_obj * log_obj = EXCIMER_OBJ_ZP(ExcimerLog, getThis());
    1154             : 
    1155           1 :     ZEND_PARSE_PARAMETERS_START(0, 0);
    1156           1 :     ZEND_PARSE_PARAMETERS_END();
    1157             : 
    1158           1 :     log_obj->iter_entry_index = 0;
    1159           1 :     zval_ptr_dtor(&log_obj->z_current);
    1160           1 :     ZVAL_NULL(&log_obj->z_current);
    1161             : }
    1162             : /* }}} */
    1163             : 
    1164             : /* {{{ proto bool ExcimerLog::valid()
    1165             :  */
    1166          31 : static PHP_METHOD(ExcimerLog, valid)
    1167             : {
    1168          62 :     ExcimerLog_obj * log_obj = EXCIMER_OBJ_ZP(ExcimerLog, getThis());
    1169             : 
    1170          31 :     ZEND_PARSE_PARAMETERS_START(0, 0);
    1171          31 :     ZEND_PARSE_PARAMETERS_END();
    1172             : 
    1173          31 :     if (log_obj->iter_entry_index < log_obj->log.entries_size) {
    1174          30 :         RETURN_TRUE;
    1175             :     } else {
    1176           1 :         RETURN_FALSE;
    1177             :     }
    1178             : }
    1179             : /* }}} */
    1180             : 
    1181             : /* {{{ proto int ExcimerLog::count()
    1182             :  */
    1183           2 : static PHP_METHOD(ExcimerLog, count)
    1184             : {
    1185           4 :     ExcimerLog_obj *log_obj = EXCIMER_OBJ_ZP(ExcimerLog, getThis());
    1186             : 
    1187           2 :     ZEND_PARSE_PARAMETERS_START(0, 0);
    1188           2 :     ZEND_PARSE_PARAMETERS_END();
    1189             : 
    1190           2 :     RETURN_LONG(log_obj->log.entries_size);
    1191             : }
    1192             : /* }}} */
    1193             : 
    1194             : /* {{{ proto bool ExcimerLog::offsetExists(mixed offset) */
    1195           1 : static PHP_METHOD(ExcimerLog, offsetExists)
    1196             : {
    1197           2 :     ExcimerLog_obj *log_obj = EXCIMER_OBJ_ZP(ExcimerLog, getThis());
    1198             :     zend_long offset;
    1199             : 
    1200           1 :     ZEND_PARSE_PARAMETERS_START(1, 1);
    1201           2 :         Z_PARAM_LONG(offset)
    1202           1 :     ZEND_PARSE_PARAMETERS_END();
    1203             : 
    1204           1 :     if (offset >= 0 && offset < log_obj->log.entries_size) {
    1205           1 :         RETURN_TRUE;
    1206             :     } else {
    1207           0 :         RETURN_FALSE;
    1208             :     }
    1209             : }
    1210             : /* }}} */
    1211             : 
    1212             : /* {{{ proto mixed ExcimerLog::offsetGet(mixed offset) */
    1213           1 : static PHP_METHOD(ExcimerLog, offsetGet)
    1214             : {
    1215           2 :     ExcimerLog_obj *log_obj = EXCIMER_OBJ_ZP(ExcimerLog, getThis());
    1216             :     zend_long offset;
    1217             : 
    1218           1 :     ZEND_PARSE_PARAMETERS_START(1, 1);
    1219           2 :         Z_PARAM_LONG(offset)
    1220           1 :     ZEND_PARSE_PARAMETERS_END();
    1221             : 
    1222           1 :     if (offset < 0 || offset >= log_obj->log.entries_size) {
    1223           0 :         RETURN_NULL();
    1224             :     }
    1225             : 
    1226           2 :     ExcimerLog_init_entry(return_value, getThis(), offset);
    1227             : }
    1228             : /* }}} */
    1229             : 
    1230             : /* {{{ proto void ExcimerLog::offsetSet(mixed offset, mixed value) */
    1231           0 : static PHP_METHOD(ExcimerLog, offsetSet)
    1232             : {
    1233           0 :     php_error_docref(NULL, E_WARNING, "ExcimerLog cannot be modified");
    1234           0 : }
    1235             : /* }}} */
    1236             : 
    1237             : /* {{{ proto void ExcimerLog::offsetUnset(mixed offset) */
    1238           0 : static PHP_METHOD(ExcimerLog, offsetUnset)
    1239             : {
    1240           0 :     php_error_docref(NULL, E_WARNING, "ExcimerLog cannot be modified");
    1241           0 : }
    1242             : /* }}} */
    1243             : 
    1244          91 : static zend_object *ExcimerLogEntry_new(zend_class_entry *ce) /* {{{ */
    1245             : {
    1246          91 :     ExcimerLogEntry_obj *entry_obj = EXCIMER_NEW_OBJECT(ExcimerLogEntry, ce);
    1247          91 :     ZVAL_NULL(&entry_obj->z_log);
    1248          91 :     entry_obj->index = 0;
    1249          91 :     return &entry_obj->std;
    1250             : }
    1251             : /* }}} */
    1252             : 
    1253          91 : static void ExcimerLogEntry_free_object(zend_object *object) /* {{{ */
    1254             : {
    1255          91 :     ExcimerLogEntry_obj *entry_obj = EXCIMER_OBJ(ExcimerLogEntry, object);
    1256          91 :     zval_ptr_dtor(&entry_obj->z_log);
    1257          91 :     ZVAL_UNDEF(&entry_obj->z_log);
    1258          91 :     zend_object_std_dtor(object);
    1259          91 : }
    1260             : /* }}} */
    1261             : 
    1262             : /* {{{ proto void ExcimerLogEntry::__construct()
    1263             :  */
    1264           0 : static PHP_METHOD(ExcimerLogEntry, __construct)
    1265             : {
    1266           0 :     php_error_docref(NULL, E_ERROR, "ExcimerLogEntry cannot be constructed directly");
    1267           0 : }
    1268             : /* }}} */
    1269             : 
    1270             : /* {{{ proto float ExcimerLogEntry::getTimestamp()
    1271             :  */
    1272         118 : static PHP_METHOD(ExcimerLogEntry, getTimestamp)
    1273             : {
    1274         236 :     ExcimerLogEntry_obj *entry_obj = EXCIMER_OBJ_ZP(ExcimerLogEntry, getThis());
    1275         118 :     ExcimerLog_obj *log_obj = EXCIMER_OBJ_ZP(ExcimerLog, &entry_obj->z_log);
    1276         118 :     excimer_log_entry *entry = excimer_log_get_entry(&log_obj->log, entry_obj->index);
    1277             : 
    1278         118 :     ZEND_PARSE_PARAMETERS_START(0, 0);
    1279         118 :     ZEND_PARSE_PARAMETERS_END();
    1280             : 
    1281         118 :     RETURN_DOUBLE((entry->timestamp - log_obj->log.epoch) / 1e9);
    1282             : }
    1283             : /* }}} */
    1284             : 
    1285             : /* {{{ proto float ExcimerLogEntry::getEventCount()
    1286             :  */
    1287          60 : static PHP_METHOD(ExcimerLogEntry, getEventCount)
    1288             : {
    1289         120 :     ExcimerLogEntry_obj *entry_obj = EXCIMER_OBJ_ZP(ExcimerLogEntry, getThis());
    1290          60 :     ExcimerLog_obj *log_obj = EXCIMER_OBJ_ZP(ExcimerLog, &entry_obj->z_log);
    1291          60 :     excimer_log_entry *entry = excimer_log_get_entry(&log_obj->log, entry_obj->index);
    1292             : 
    1293          60 :     ZEND_PARSE_PARAMETERS_START(0, 0);
    1294          60 :     ZEND_PARSE_PARAMETERS_END();
    1295             : 
    1296          60 :     RETURN_LONG(entry->event_count);
    1297             : }
    1298             : /* }}} */
    1299             : 
    1300             : /* {{{ proto array ExcimerLogEntry::getTrace()
    1301             :  */
    1302          91 : static PHP_METHOD(ExcimerLogEntry, getTrace)
    1303             : {
    1304         182 :     ExcimerLogEntry_obj *entry_obj = EXCIMER_OBJ_ZP(ExcimerLogEntry, getThis());
    1305          91 :     ExcimerLog_obj *log_obj = EXCIMER_OBJ_ZP(ExcimerLog, &entry_obj->z_log);
    1306          91 :     excimer_log_entry *entry = excimer_log_get_entry(&log_obj->log, entry_obj->index);
    1307             : 
    1308          91 :     ZEND_PARSE_PARAMETERS_START(0, 0);
    1309          91 :     ZEND_PARSE_PARAMETERS_END();
    1310             : 
    1311          91 :     RETURN_ARR(excimer_log_trace_to_array(&log_obj->log, entry->frame_index));
    1312             : }
    1313             : /* }}} */
    1314             : 
    1315          37 : static zend_object *ExcimerTimer_new(zend_class_entry *ce) /* {{{ */
    1316             : {
    1317          37 :     ExcimerTimer_obj *timer_obj = EXCIMER_NEW_OBJECT(ExcimerTimer, ce);
    1318          37 :     ZVAL_UNDEF(&timer_obj->z_callback);
    1319          37 :     timer_obj->event_type = EXCIMER_REAL;
    1320          37 :     timer_obj->need_reinit = 1;
    1321          37 :     return &timer_obj->std;
    1322             : }
    1323             : /* }}} */
    1324             : 
    1325          37 : static void ExcimerTimer_free_object(zend_object *object) /* {{{ */
    1326             : {
    1327          37 :     ExcimerTimer_obj *timer_obj = EXCIMER_OBJ(ExcimerTimer, object);
    1328          37 :     if (timer_obj->timer.is_valid) {
    1329          37 :         excimer_timer_destroy(&timer_obj->timer);
    1330             :     }
    1331          37 :     zval_ptr_dtor(&timer_obj->z_callback);
    1332          37 :     ZVAL_UNDEF(&timer_obj->z_callback);
    1333          37 : }
    1334             : /* }}} */
    1335             : 
    1336             : /* {{{ proto void ExcimerTimer::setEventType(int event_type)
    1337             :  */
    1338           2 : static PHP_METHOD(ExcimerTimer, setEventType)
    1339             : {
    1340           4 :     ExcimerTimer_obj *timer_obj = EXCIMER_OBJ_ZP(ExcimerTimer, getThis());
    1341             :     zend_long event_type;
    1342             : 
    1343           2 :     ZEND_PARSE_PARAMETERS_START(1, 1)
    1344           4 :         Z_PARAM_LONG(event_type)
    1345           2 :     ZEND_PARSE_PARAMETERS_END();
    1346             : 
    1347           2 :     if (event_type != EXCIMER_CPU && event_type != EXCIMER_REAL) {
    1348           0 :         php_error_docref(NULL, E_WARNING, "Invalid event type");
    1349           0 :         return;
    1350             :     }
    1351             : 
    1352           2 :     timer_obj->event_type = event_type;
    1353           2 :     timer_obj->need_reinit = 1;
    1354             : }
    1355             : /* }}} */
    1356             : 
    1357             : /* {{{ proto void ExcimerTimer::setInterval(float interval)
    1358             :  */
    1359          24 : static PHP_METHOD(ExcimerTimer, setInterval)
    1360             : {
    1361          48 :     ExcimerTimer_obj *timer_obj = EXCIMER_OBJ_ZP(ExcimerTimer, getThis());
    1362             :     double initial;
    1363             : 
    1364          24 :     ZEND_PARSE_PARAMETERS_START(1, 1)
    1365          48 :         Z_PARAM_DOUBLE(initial)
    1366          24 :     ZEND_PARSE_PARAMETERS_END();
    1367             : 
    1368          24 :     timerlib_timespec_from_double(&timer_obj->initial, initial);
    1369             : }
    1370             : /* }}} */
    1371             : 
    1372             : /* {{{ proto void ExcimerTimer::setPeriod(float period)
    1373             :  */
    1374          13 : static PHP_METHOD(ExcimerTimer, setPeriod)
    1375             : {
    1376          26 :     ExcimerTimer_obj *timer_obj = EXCIMER_OBJ_ZP(ExcimerTimer, getThis());
    1377             :     double period;
    1378             : 
    1379          13 :     ZEND_PARSE_PARAMETERS_START(1, 1)
    1380          26 :         Z_PARAM_DOUBLE(period)
    1381          13 :     ZEND_PARSE_PARAMETERS_END();
    1382             : 
    1383          13 :     timerlib_timespec_from_double(&timer_obj->period, period);
    1384             : }
    1385             : /* }}} */
    1386             : 
    1387             : /* {{{ proto void ExcimerTimer::setCallback(callback callback)
    1388             :  */
    1389          34 : static PHP_METHOD(ExcimerTimer, setCallback)
    1390             : {
    1391          68 :     ExcimerTimer_obj *timer_obj = EXCIMER_OBJ_ZP(ExcimerTimer, getThis());
    1392             :     zval *zp_callback;
    1393             : 
    1394          34 :     ZEND_PARSE_PARAMETERS_START(1, 1)
    1395          34 :         Z_PARAM_ZVAL(zp_callback)
    1396          34 :     ZEND_PARSE_PARAMETERS_END();
    1397             : 
    1398          68 :     if (Z_TYPE_P(zp_callback) == IS_NULL) {
    1399           0 :         zval_ptr_dtor(&timer_obj->z_callback);
    1400           0 :         ZVAL_NULL(&timer_obj->z_callback);
    1401             :     } else {
    1402          34 :         ExcimerTimer_set_callback(timer_obj, zp_callback);
    1403             :     }
    1404             : }
    1405             : /* }}} */
    1406             : 
    1407          35 : static int ExcimerTimer_set_callback(ExcimerTimer_obj *timer_obj, zval *zp_callback) /* {{{ */
    1408             : {
    1409             :     char *is_callable_error;
    1410             : 
    1411          35 :     if (!zend_is_callable_ex(zp_callback, NULL, 0, NULL, NULL, &is_callable_error)) {
    1412           0 :         php_error_docref(NULL, E_WARNING, "timer callback is not callable: %s",
    1413             :                 is_callable_error);
    1414           0 :         return FAILURE;
    1415             :     }
    1416             : 
    1417          35 :     zval_ptr_dtor(&timer_obj->z_callback);
    1418          35 :     ZVAL_COPY(&timer_obj->z_callback, zp_callback);
    1419          35 :     return SUCCESS;
    1420             : }
    1421             : /* }}} */
    1422             : 
    1423             : /* {{{ proto void ExcimerTimer::start()
    1424             :  */
    1425          36 : static PHP_METHOD(ExcimerTimer, start)
    1426             : {
    1427          72 :     ExcimerTimer_obj *timer_obj = EXCIMER_OBJ_ZP(ExcimerTimer, getThis());
    1428             : 
    1429          36 :     ZEND_PARSE_PARAMETERS_START(0, 0);
    1430          36 :     ZEND_PARSE_PARAMETERS_END();
    1431             : 
    1432          36 :     if (timer_obj->timer.is_running) {
    1433           0 :         ExcimerTimer_stop(timer_obj);
    1434             :     }
    1435          36 :     ExcimerTimer_start(timer_obj);
    1436             : }
    1437             : /* }}} */
    1438             : 
    1439             : /* {{{ proto void ExcimerTimer::stop()
    1440             :  */
    1441           2 : static PHP_METHOD(ExcimerTimer, stop)
    1442             : {
    1443           4 :     ExcimerTimer_obj *timer_obj = EXCIMER_OBJ_ZP(ExcimerTimer, getThis());
    1444             : 
    1445           2 :     ZEND_PARSE_PARAMETERS_START(0, 0);
    1446           2 :     ZEND_PARSE_PARAMETERS_END();
    1447             : 
    1448           2 :     ExcimerTimer_stop(timer_obj);
    1449             : }
    1450             : /* }}} */
    1451             : 
    1452             : /* {{{ proto float ExcimerTimer::getTime()
    1453             :  */
    1454           3 : static PHP_METHOD(ExcimerTimer, getTime)
    1455             : {
    1456           6 :     ExcimerTimer_obj *timer_obj = EXCIMER_OBJ_ZP(ExcimerTimer, getThis());
    1457             :     struct timespec ts;
    1458             : 
    1459           3 :     ZEND_PARSE_PARAMETERS_START(0, 0);
    1460           3 :     ZEND_PARSE_PARAMETERS_END();
    1461             : 
    1462           3 :     excimer_timer_get_time(&timer_obj->timer, &ts);
    1463           3 :     RETURN_DOUBLE(timerlib_timespec_to_double(&ts));
    1464             : }
    1465             : /* }}} */
    1466             : 
    1467          37 : static void ExcimerTimer_start(ExcimerTimer_obj *timer_obj) /* {{{ */
    1468             : {
    1469          37 :     if (timer_obj->need_reinit || !timer_obj->timer.is_valid) {
    1470          37 :         if (timer_obj->timer.is_valid) {
    1471           0 :             excimer_timer_destroy(&timer_obj->timer);
    1472             :         }
    1473          37 :         if (excimer_timer_init(&timer_obj->timer,
    1474          37 :             timer_obj->event_type,
    1475             :             ExcimerTimer_event,
    1476             :             (void*)timer_obj) == FAILURE)
    1477             :         {
    1478             :             /* Error message already sent */
    1479           0 :             return;
    1480             :         }
    1481          37 :         timer_obj->need_reinit = 0;
    1482             :     }
    1483          37 :     excimer_timer_start(&timer_obj->timer,
    1484             :             &timer_obj->period,
    1485             :             &timer_obj->initial);
    1486             : }
    1487             : /* }}} */
    1488             : 
    1489           2 : static void ExcimerTimer_stop(ExcimerTimer_obj *timer_obj) /* {{{ */
    1490             : {
    1491           2 :     if (timer_obj->timer.is_valid) {
    1492           2 :         excimer_timer_stop(&timer_obj->timer);
    1493             :     }
    1494           2 : }
    1495             : /* }}} */
    1496             : 
    1497          26 : static void ExcimerTimer_event(zend_long event_count, void *user_data) /* {{{ */
    1498             : {
    1499          26 :     ExcimerTimer_obj *timer_obj = (ExcimerTimer_obj*)user_data;
    1500             :     zend_fcall_info fci;
    1501             :     zend_fcall_info_cache fcc;
    1502             :     zval retval;
    1503             :     zval z_event_count;
    1504             :     char *is_callable_error;
    1505             : 
    1506          78 :     if (Z_ISNULL(timer_obj->z_callback) || Z_ISUNDEF(timer_obj->z_callback)) {
    1507           0 :         return;
    1508             :     }
    1509             : 
    1510          26 :     if (zend_fcall_info_init(&timer_obj->z_callback, 0, &fci, &fcc, NULL,
    1511             :         &is_callable_error) != SUCCESS)
    1512             :     {
    1513           0 :         php_error(E_WARNING, "ExcimerTimer callback is not callable (during event): %s",
    1514             :             is_callable_error);
    1515           0 :         ExcimerTimer_stop(timer_obj);
    1516           0 :         return;
    1517             :     }
    1518             : 
    1519          26 :     fci.retval = &retval;
    1520          26 :     ZVAL_LONG(&z_event_count, event_count);
    1521             : 
    1522          26 :     zend_fcall_info_argn(&fci, 1, &z_event_count);
    1523          26 :     if (zend_call_function(&fci, &fcc) == SUCCESS) {
    1524          26 :         zval_ptr_dtor(&retval);
    1525             :     }
    1526          26 :     zend_fcall_info_args_clear(&fci, 1);
    1527             : }
    1528             : /* }}} */
    1529             : 
    1530             : /* {{{ proto ExcimerTimer excimer_set_timeout(callable callback, float interval)
    1531             :  */
    1532           1 : PHP_FUNCTION(excimer_set_timeout)
    1533             : {
    1534             :     ExcimerTimer_obj *timer_obj;
    1535             :     zval * zp_callback;
    1536             :     double initial;
    1537             : 
    1538           1 :     ZEND_PARSE_PARAMETERS_START(2, 2)
    1539           1 :         Z_PARAM_ZVAL(zp_callback)
    1540           2 :         Z_PARAM_DOUBLE(initial)
    1541           1 :     ZEND_PARSE_PARAMETERS_END();
    1542             : 
    1543           1 :     object_init_ex(return_value, ExcimerTimer_ce);
    1544           1 :     timer_obj = EXCIMER_OBJ_ZP(ExcimerTimer, return_value);
    1545           1 :     if (ExcimerTimer_set_callback(timer_obj, zp_callback) == FAILURE) {
    1546           0 :         zval_ptr_dtor(return_value);
    1547           0 :         ZVAL_NULL(return_value);
    1548             :     }
    1549             : 
    1550           1 :     timerlib_timespec_from_double(&timer_obj->initial, initial);
    1551           1 :     ExcimerTimer_start(timer_obj);
    1552             : }
    1553             : /* }}} */
    1554             : 
    1555             : static const zend_module_dep excimer_deps[] = {
    1556             : #if PHP_VERSION_ID < 70200
    1557             :     ZEND_MOD_REQUIRED("spl")
    1558             : #endif
    1559             :     ZEND_MOD_END
    1560             : };
    1561             : 
    1562             : /* {{{ excimer_module_entry */
    1563             : zend_module_entry excimer_module_entry = {
    1564             :     STANDARD_MODULE_HEADER_EX,
    1565             :     NULL,
    1566             :     excimer_deps,
    1567             :     "excimer",
    1568             :     excimer_functions,
    1569             :     PHP_MINIT(excimer),
    1570             :     PHP_MSHUTDOWN(excimer),
    1571             :     PHP_RINIT(excimer),
    1572             :     NULL, /* RSHUTDOWN */
    1573             :     PHP_MINFO(excimer),
    1574             :     PHP_EXCIMER_VERSION,
    1575             :     NO_MODULE_GLOBALS,
    1576             :     ZEND_MODULE_POST_ZEND_DEACTIVATE_N(excimer),
    1577             :     STANDARD_MODULE_PROPERTIES_EX
    1578             : };
    1579             : /* }}} */
    1580             : 
    1581             : #ifdef COMPILE_DL_EXCIMER
    1582             : #ifdef ZTS
    1583             : ZEND_TSRMLS_CACHE_DEFINE()
    1584             : #endif
    1585          17 : ZEND_GET_MODULE(excimer)
    1586             : #endif
    1587             : 
    1588             : /*
    1589             :  * Local variables:
    1590             :  * tab-width: 4
    1591             :  * c-basic-offset: 4
    1592             :  * End:
    1593             :  * vim600: noet sw=4 ts=4 fdm=marker
    1594             :  * vim<600: noet sw=4 ts=4
    1595             :  */

Generated by: LCOV version 1.16