-
Notifications
You must be signed in to change notification settings - Fork 7.9k
feat: macOS support for Zend Max Execution timers #13468
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
base: master
Are you sure you want to change the base?
Changes from all commits
2afb398
83e8722
aa7813d
0abb235
5359294
54b4daf
956a611
6121a12
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,32 +16,129 @@ | |
|
||
#ifdef ZEND_MAX_EXECUTION_TIMERS | ||
|
||
# ifdef __APPLE__ | ||
|
||
#include <dispatch/dispatch.h> | ||
# ifdef ZTS | ||
#include <pthread.h> | ||
# else | ||
#include "zend_execute.h" | ||
# endif | ||
|
||
#include "zend.h" | ||
#include "zend_globals.h" | ||
|
||
// macOS doesn't support timer_create(), fallback to Grand Central Dispatch | ||
|
||
static inline void zend_max_execution_timer_handler(void *arg) | ||
{ | ||
#ifdef ZTS | ||
pthread_t *tid = (pthread_t *) arg; | ||
pthread_kill(*tid, ZEND_MAX_EXECUTION_TIMERS_SIGNAL); | ||
#else | ||
zend_timeout_handler(); | ||
#endif | ||
} | ||
|
||
static inline void zend_max_execution_timer_cancel(void *arg) | ||
{ | ||
free(arg); | ||
} | ||
|
||
ZEND_API void zend_max_execution_timer_init(void) /* {{{ */ | ||
{ | ||
pid_t pid = getpid(); | ||
|
||
if (EG(pid) == pid) { | ||
return; | ||
} | ||
|
||
dispatch_queue_global_t queue = dispatch_get_global_queue(QOS_CLASS_UTILITY, 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. quick question: did you try with using your own queue or at least, does it make any meaningful difference in your opinion e.g. dispatch_queue_attr_t attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_UTILITY, 0);
dispatch_queue_t queue = dispatch_queue_create("net.php.zend_max_execution_timer", attr); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to the docs, this queue looks adapted to our use case, but I'm not a specialist in Mac specifics. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ACK. Was wondering what is best for, at least, ZTS context. |
||
EG(max_execution_timer_source) = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); | ||
if (EG(max_execution_timer_source) == NULL) { | ||
zend_strerror_noreturn(E_ERROR, errno, "Could not create dispatch source"); | ||
} | ||
|
||
EG(pid) = pid; | ||
EG(max_execution_timer_suspended) = 1; | ||
|
||
# ifdef ZTS | ||
pthread_t tid = pthread_self(); | ||
pthread_t *ptid = malloc(sizeof(pthread_t)); | ||
memcpy(ptid, &tid, sizeof(pthread_t)); | ||
dispatch_set_context(EG(max_execution_timer_source), ptid); | ||
#else | ||
//dispatch_set_context(EG(max_execution_timer_source), NULL); | ||
# endif | ||
|
||
dispatch_source_set_event_handler_f(EG(max_execution_timer_source), zend_max_execution_timer_handler); | ||
dispatch_source_set_cancel_handler_f(EG(max_execution_timer_source), zend_max_execution_timer_cancel); | ||
} /* }}} */ | ||
|
||
void zend_max_execution_timer_settime(zend_long seconds) /* {{{ */ | ||
{ | ||
if (seconds == 0) { | ||
if (!EG(max_execution_timer_suspended)) { | ||
dispatch_suspend(EG(max_execution_timer_source)); | ||
EG(max_execution_timer_suspended) = 1; | ||
} | ||
|
||
return; | ||
} | ||
|
||
dispatch_source_set_timer( | ||
EG(max_execution_timer_source), | ||
dispatch_time(DISPATCH_TIME_NOW, seconds * NSEC_PER_SEC), | ||
seconds * NSEC_PER_SEC, | ||
0 | ||
Comment on lines
+91
to
+93
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we disable recurrence of the timer? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, this seems not possible: https://developer.apple.com/documentation/dispatch/1385606-dispatch_source_set_timer |
||
); | ||
if (EG(max_execution_timer_suspended)) { | ||
dispatch_resume(EG(max_execution_timer_source)); | ||
EG(max_execution_timer_suspended) = 0; | ||
} | ||
} /* }}} */ | ||
|
||
void zend_max_execution_timer_shutdown(void) /* {{{ */ | ||
{ | ||
/* Don't try to delete a timer created before a call to fork() */ | ||
if (EG(pid) != getpid()) { | ||
return; | ||
} | ||
|
||
EG(pid) = 0; | ||
|
||
dispatch_source_cancel(EG(max_execution_timer_source)); | ||
//dispatch_release(EG(max_execution_timer_source)); | ||
} /* }}} */ | ||
|
||
# else | ||
|
||
#include <stdio.h> | ||
#include <signal.h> | ||
#include <time.h> | ||
#include <unistd.h> | ||
#include <errno.h> | ||
#include <sys/syscall.h> | ||
#include <sys/types.h> | ||
# ifdef __FreeBSD__ | ||
# include <pthread_np.h> | ||
# endif | ||
# ifdef __FreeBSD__ | ||
# include <pthread_np.h> | ||
# endif | ||
|
||
#include "zend.h" | ||
#include "zend_globals.h" | ||
|
||
// Musl Libc defines this macro, glibc does not | ||
// According to "man 2 timer_create" this field should always be available, but it's not: https://sourceware.org/bugzilla/show_bug.cgi?id=27417 | ||
# ifndef sigev_notify_thread_id | ||
# define sigev_notify_thread_id _sigev_un._tid | ||
# endif | ||
# ifndef sigev_notify_thread_id | ||
# define sigev_notify_thread_id _sigev_un._tid | ||
# endif | ||
|
||
// FreeBSD doesn't support CLOCK_BOOTTIME | ||
# ifdef __FreeBSD__ | ||
# define ZEND_MAX_EXECUTION_TIMERS_CLOCK CLOCK_MONOTONIC | ||
# else | ||
# define ZEND_MAX_EXECUTION_TIMERS_CLOCK CLOCK_BOOTTIME | ||
# endif | ||
# ifdef __FreeBSD__ | ||
# define ZEND_MAX_EXECUTION_TIMERS_CLOCK CLOCK_MONOTONIC | ||
# else | ||
# define ZEND_MAX_EXECUTION_TIMERS_CLOCK CLOCK_BOOTTIME | ||
# endif | ||
|
||
ZEND_API void zend_max_execution_timer_init(void) /* {{{ */ | ||
{ | ||
|
@@ -54,12 +151,12 @@ ZEND_API void zend_max_execution_timer_init(void) /* {{{ */ | |
struct sigevent sev; | ||
sev.sigev_notify = SIGEV_THREAD_ID; | ||
sev.sigev_value.sival_ptr = &EG(max_execution_timer_timer); | ||
sev.sigev_signo = SIGRTMIN; | ||
# ifdef __FreeBSD__ | ||
sev.sigev_signo = ZEND_MAX_EXECUTION_TIMERS_SIGNAL; | ||
# ifdef __FreeBSD__ | ||
sev.sigev_notify_thread_id = pthread_getthreadid_np(); | ||
# else | ||
# else | ||
sev.sigev_notify_thread_id = (pid_t) syscall(SYS_gettid); | ||
# endif | ||
# endif | ||
|
||
// Measure wall time instead of CPU time as originally planned now that it is possible https://github.com/php/php-src/pull/6504#issuecomment-1370303727 | ||
if (timer_create(ZEND_MAX_EXECUTION_TIMERS_CLOCK, &sev, &EG(max_execution_timer_timer)) != 0) { | ||
|
@@ -68,9 +165,9 @@ ZEND_API void zend_max_execution_timer_init(void) /* {{{ */ | |
|
||
EG(pid) = pid; | ||
|
||
# ifdef MAX_EXECUTION_TIMERS_DEBUG | ||
# ifdef MAX_EXECUTION_TIMERS_DEBUG | ||
fprintf(stderr, "Timer %#jx created on thread %d\n", (uintmax_t) EG(max_execution_timer_timer), sev.sigev_notify_thread_id); | ||
# endif | ||
# endif | ||
|
||
sigaction(sev.sigev_signo, NULL, &EG(oldact)); | ||
} | ||
|
@@ -89,9 +186,9 @@ void zend_max_execution_timer_settime(zend_long seconds) /* {{{ }*/ | |
its.it_value.tv_sec = seconds; | ||
its.it_value.tv_nsec = its.it_interval.tv_sec = its.it_interval.tv_nsec = 0; | ||
|
||
# ifdef MAX_EXECUTION_TIMERS_DEBUG | ||
# ifdef MAX_EXECUTION_TIMERS_DEBUG | ||
fprintf(stderr, "Setting timer %#jx on thread %d (%ld seconds)...\n", (uintmax_t) timer, (pid_t) syscall(SYS_gettid), seconds); | ||
# endif | ||
# endif | ||
|
||
if (timer_settime(timer, 0, &its, NULL) != 0) { | ||
zend_strerror_noreturn(E_ERROR, errno, "Could not set timer"); | ||
|
@@ -110,9 +207,9 @@ void zend_max_execution_timer_shutdown(void) /* {{{ */ | |
|
||
timer_t timer = EG(max_execution_timer_timer); | ||
|
||
# ifdef MAX_EXECUTION_TIMERS_DEBUG | ||
# ifdef MAX_EXECUTION_TIMERS_DEBUG | ||
fprintf(stderr, "Deleting timer %#jx on thread %d...\n", (uintmax_t) timer, (pid_t) syscall(SYS_gettid)); | ||
# endif | ||
# endif | ||
|
||
int err = timer_delete(timer); | ||
if (err != 0) { | ||
|
@@ -121,4 +218,5 @@ void zend_max_execution_timer_shutdown(void) /* {{{ */ | |
} | ||
/* }}}} */ | ||
|
||
# endif | ||
#endif |
Uh oh!
There was an error while loading. Please reload this page.