-
Notifications
You must be signed in to change notification settings - Fork 79
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
Implement auto dump on out-of-memory #123
Open
tortis
wants to merge
2
commits into
BitOne:master
Choose a base branch
from
tortis:oom-dump
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,39 @@ | ||
#ifndef PHP_MEMINFO_H | ||
#define PHP_MEMINFO_H 1 | ||
|
||
#include "php.h" | ||
|
||
extern zend_module_entry meminfo_module_entry; | ||
#define phpext_meminfo_ptr &meminfo_module_entry | ||
|
||
#define MEMINFO_NAME "PHP Meminfo" | ||
#define MEMINFO_VERSION "2.0.0-beta1" | ||
#define MEMINFO_AUTHOR "Benoit Jacquemont" | ||
#define MEMINFO_COPYRIGHT "Copyright (c) 2010-2021 by Benoit Jacquemont & contributors" | ||
#define MEMINFO_COPYRIGHT "Copyright (c) 2010-2021 by Benoit Jacquemont & contributors" | ||
#define MEMINFO_COPYRIGHT_SHORT "Copyright (c) 2010-2021" | ||
|
||
ZEND_BEGIN_MODULE_GLOBALS(meminfo) | ||
zend_bool dump_on_limit; | ||
ZEND_END_MODULE_GLOBALS(meminfo) | ||
|
||
static ZEND_DECLARE_MODULE_GLOBALS(meminfo) | ||
#define MEMINFO_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(meminfo, v) | ||
|
||
PHP_FUNCTION(meminfo_dump); | ||
PHP_MSHUTDOWN_FUNCTION(meminfo); | ||
PHP_MINIT_FUNCTION(meminfo); | ||
PHP_MINFO_FUNCTION(meminfo); | ||
PHP_GINIT_FUNCTION(meminfo); | ||
|
||
PHP_INI_BEGIN() | ||
STD_PHP_INI_ENTRY("meminfo.dump_on_limit", "Off", PHP_INI_ALL, OnUpdateBool, dump_on_limit, zend_meminfo_globals, meminfo_globals) | ||
PHP_INI_ENTRY("meminfo.dump_dir", "/tmp", PHP_INI_ALL, NULL) | ||
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. @BitOne Do you think we can just let this default to "/tmp" for now? It might not be the best for Windows, but I think most users will probably set the dump_dir if they are going to use this anyway right? |
||
PHP_INI_END() | ||
|
||
zend_ulong meminfo_get_element_size(zval* z); | ||
|
||
// Functions to browse memory parts to record item | ||
void perform_dump(php_stream* stream); | ||
void meminfo_browse_exec_frames(php_stream *stream, HashTable *visited_items, int *first_element); | ||
void meminfo_browse_class_static_members(php_stream *stream, HashTable *visited_items, int *first_element); | ||
|
||
|
@@ -28,6 +47,24 @@ void meminfo_build_frame_label(char * frame_label, int frame_label_len, zend_exe | |
|
||
zend_string * meminfo_escape_for_json(const char *s); | ||
|
||
static zend_bool should_autodump(int error_type, const char* message); | ||
|
||
// Function pointer to original error handler | ||
// See https://www.phpinternalsbook.com/php7/extensions_design/hooks.html | ||
#if PHP_VERSION_ID < 70200 /* PHP 7.1 */ | ||
static void (*original_zend_error_cb)(int type, const char* error_filename, const uint error_lineno, const char* format, va_list args); | ||
#define MEMINFO_ZEND_ERROR_CB_ARGS_PASSTHRU type, error_filename, error_lineno, format, args | ||
#elif PHP_VERSION_ID < 80000 /* PHP 7.2 - 7.4 */ | ||
static void (*original_zend_error_cb)(int type, const char* error_filename, const uint32_t error_lineno, const char* format, va_list args); | ||
#define MEMINFO_ZEND_ERROR_CB_ARGS_PASSTHRU type, error_filename, error_lineno, format, args | ||
#elif PHP_VERSION_ID < 80100 /* PHP 8.0 */ | ||
static void (*original_zend_error_cb)(int type, const char* error_filename, const uint32_t error_lineno, zend_string* message); | ||
#define MEMINFO_ZEND_ERROR_CB_ARGS_PASSTHRU type, error_filename, error_lineno, message | ||
#else /* PHP 8.1 */ | ||
static void (*original_zend_error_cb)(int type, zend_string* error_filename, const uint32_t error_lineno, zend_string* message); | ||
#define MEMINFO_ZEND_ERROR_CB_ARGS_PASSTHRU type, error_filename, error_lineno, message | ||
#endif | ||
|
||
extern zend_module_entry meminfo_entry; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--TEST-- | ||
Trigger PHP OOM | ||
--FILE-- | ||
<?php | ||
|
||
ini_set("meminfo.dump_on_limit", true); | ||
ini_set("meminfo.dump_dir", __DIR__); | ||
|
||
$things = []; | ||
for ($i = 0; $i < 3000; $i++) { | ||
$things []= str_repeat("*", rand(50000, 100000)); | ||
} | ||
--EXPECT-- | ||
--XFAIL-- | ||
This test triggers an OOM error which will write a heap dump into the test | ||
directory. The test dump-oom-confirm.phpt will verify if the heap dump was | ||
written. This test is prefixed with 00 to ensure that it runs before the | ||
confirm test. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--TEST-- | ||
Confirm heap dump on OOM | ||
--FILE-- | ||
<?php | ||
|
||
$files = glob(__DIR__ . '/php_heap*.json'); | ||
echo count($files); | ||
--EXPECT-- | ||
1 | ||
--CLEAN-- | ||
<?php | ||
|
||
$files = glob(__DIR__ . '/php_heap*.json'); | ||
foreach($files as $file) { | ||
if(is_file($file)) { | ||
unlink($file); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there no other way to check for memory exhaustion error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found this technique in memprof when I was researching how to do it. Unfortunately I didn't find any other options.