Skip to content

Export zend_accel_shared_globals #15543

New issue

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

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

Already on GitHub? Sign in to your account

Merged

Conversation

realFlowControl
Copy link
Contributor

Hey folks 👋

I could not find any other way to access this information from an extension:

typedef struct _zend_accel_shared_globals {
/* Cache Data Structures */
zend_ulong hits;
zend_ulong misses;
zend_ulong blacklist_misses;
zend_ulong oom_restarts; /* number of restarts because of out of memory */
zend_ulong hash_restarts; /* number of restarts because of hash overflow */
zend_ulong manual_restarts; /* number of restarts scheduled by opcache_reset() */
zend_accel_hash hash; /* hash table for cached scripts */
size_t map_ptr_last;
/* Directives & Maintenance */
time_t start_time;
time_t last_restart_time;
time_t force_restart_time;
bool accelerator_enabled;
bool restart_pending;
zend_accel_restart_reason restart_reason;
bool cache_status_before_restart;
#ifdef ZEND_WIN32
LONGLONG mem_usage;
LONGLONG restart_in;
#endif
bool restart_in_progress;
bool jit_counters_stopped;
/* Preloading */
zend_persistent_script *preload_script;
zend_persistent_script **saved_scripts;
/* uninitialized HashTable Support */
uint32_t uninitialized_bucket[-HT_MIN_MASK];
/* Tracing JIT */
void *jit_traces;
const void **jit_exit_groups;
/* Interned Strings Support (must be the last element) */
ZEND_SET_ALIGNED(ZEND_STRING_TABLE_POS_ALIGNMENT, zend_string_table interned_strings);
} zend_accel_shared_globals;

There is a user land function opcache_get_status() but this does way more, especially unnecessary things when not using the data in PHP, but in another C/Rust extension.

I'd like to add a bit more observability to the OPcache. IIRC @arnaud-lb is working on OPcache, is that right?

@cmb69
Copy link
Member

cmb69 commented Aug 22, 2024

Did you try (zend_accel_shared_globals *) ZSMMG(app_shared_globals)?

@bwoebi
Copy link
Member

bwoebi commented Aug 22, 2024

@cmb69 ZSMMG() isn't shared either. But yes, it would make more sense to expose that one instead.

@bwoebi
Copy link
Member

bwoebi commented Aug 22, 2024

@realFlowControl Any reason you create an extra function instead of just exporting the global?

@cmb69
Copy link
Member

cmb69 commented Aug 22, 2024

Hmm,

extern zend_accel_shared_globals *accel_shared_globals;

@realFlowControl
Copy link
Contributor Author

That is because it is declared here, but defined somewhere else. The definition is here:

zend_accel_shared_globals *accel_shared_globals = NULL;

@cmb69
Copy link
Member

cmb69 commented Aug 22, 2024

That is because it is declared here, but defined somewhere else. The definition is here:

Wouldn't ZEND_EXT_API extern zend_accel_shared_globals *accel_shared_globals; do?

@realFlowControl realFlowControl force-pushed the florian/opcache-shared-globals-api branch 2 times, most recently from 776f6ae to 099b6b9 Compare August 22, 2024 14:54
@realFlowControl
Copy link
Contributor Author

realFlowControl commented Aug 22, 2024

Jup, thanks @bwoebi and @cmb69. I updated the PR.

    zend_smm_shared_globals **smm_shared_globals;
    smm_shared_globals = dlsym(opcache_handle, "smm_shared_globals");
    zend_accel_shared_globals * accel_shared_globals = (zend_accel_shared_globals *)(*smm_shared_globals)->app_shared_globals;
    if (accel_shared_globals)
        printf("restart pending: %d\n", accel_shared_globals->restart_pending);

Is working and gets me the data I need 😄

Update: looks like Windows want's the ZEND_EXT_API in the zend_shared_alloc.h as well

@realFlowControl realFlowControl marked this pull request as ready for review August 22, 2024 15:00
@realFlowControl realFlowControl force-pushed the florian/opcache-shared-globals-api branch from 099b6b9 to a97730d Compare August 22, 2024 15:29
@arnaud-lb
Copy link
Member

Looks good to me, but could we possibly expose more information via opcache_get_status() instead?

@realFlowControl
Copy link
Contributor Author

realFlowControl commented Aug 25, 2024

Hey @arnaud-lb,
we could expose more via opcache_get_status() as well, in my use case I see two problems:

  • I need that data in an extension, this function prepares data for user land, which just adds overhead not needed in this case
  • calling this function from outside a request phase is doable, but tricky

Actually I'd like to add an observer API to the opcache so that you are able to observe certain events when they happen, but I won't be able to in time for 8.4.
Would you be interested to talk about and target something for 8.5/9.0?
In the mean time, this would help a lot for extensions that need to collect some internal opcache data.

Copy link
Member

@dstogov dstogov left a comment

Choose a reason for hiding this comment

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

I'm against this change. This is not an universal solution.
It won't work on Windows, except you link your observer with opcache, but then it won't wok without opcache.

@bwoebi
Copy link
Member

bwoebi commented Aug 26, 2024

@dstogov Why won't it work on Windows to use DL_FETCH_SYMBOL() there?

@dstogov
Copy link
Member

dstogov commented Aug 26, 2024

@dstogov Why won't it work on Windows to use DL_FETCH_SYMBOL() there?

I didn't try DL_FETCH_SYMBOL().
Transparent linking of public symbols between different DSO definitely doesn't work as on Linux.

@bwoebi
Copy link
Member

bwoebi commented Aug 26, 2024

@dstogov We've definitely been successfully using DL_FETCH_SYMBOL on windows with the other ZEND_EXT_API symbols opcache exports (jit functions).

So at least under that aspect it would be an universal solution.

Copy link
Member

@dstogov dstogov left a comment

Choose a reason for hiding this comment

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

OK. I don't care.

@bwoebi bwoebi merged commit b9b317a into php:master Aug 26, 2024
10 checks passed
@realFlowControl realFlowControl deleted the florian/opcache-shared-globals-api branch August 26, 2024 13:01
MaxKellermann added a commit to CM4all/php-src that referenced this pull request Sep 3, 2024
This reverts commit b9b317a.  We
don't need it and the use of ZEND_EXT_API would require us to include
ZendAccelerator.h.
MaxKellermann added a commit to CM4all/php-src that referenced this pull request Sep 12, 2024
This reverts commit b9b317a.  We
don't need it and the use of ZEND_EXT_API would require us to include
ZendAccelerator.h.
MaxKellermann added a commit to CM4all/php-src that referenced this pull request Nov 21, 2024
This reverts commit b9b317a.  We
don't need it and the use of ZEND_EXT_API would require us to include
ZendAccelerator.h.
MaxKellermann added a commit to CM4all/php-src that referenced this pull request Jan 7, 2025
This reverts commit b9b317a.  We
don't need it and the use of ZEND_EXT_API would require us to include
ZendAccelerator.h.
MaxKellermann added a commit to CM4all/php-src that referenced this pull request Jan 24, 2025
This reverts commit b9b317a.  We
don't need it and the use of ZEND_EXT_API would require us to include
ZendAccelerator.h.
MaxKellermann added a commit to CM4all/php-src that referenced this pull request Feb 11, 2025
This reverts commit b9b317a.  We
don't need it and the use of ZEND_EXT_API would require us to include
ZendAccelerator.h.
MaxKellermann added a commit to CM4all/php-src that referenced this pull request Mar 12, 2025
This reverts commit b9b317a.  We
don't need it and the use of ZEND_EXT_API would require us to include
ZendAccelerator.h.
MaxKellermann added a commit to CM4all/php-src that referenced this pull request Apr 9, 2025
This reverts commit b9b317a.  We
don't need it and the use of ZEND_EXT_API would require us to include
ZendAccelerator.h.
MaxKellermann added a commit to CM4all/php-src that referenced this pull request May 7, 2025
This reverts commit b9b317a.  We
don't need it and the use of ZEND_EXT_API would require us to include
ZendAccelerator.h.
MaxKellermann added a commit to CM4all/php-src that referenced this pull request Jun 6, 2025
This reverts commit b9b317a.  We
don't need it and the use of ZEND_EXT_API would require us to include
ZendAccelerator.h.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants