Skip to content
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

Added: [zend_]memory_reset_peak_usage() #8151

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ PHP NEWS
. Finished AVIF support in getimagesize(). (Yannis Guyon)
. Fixed bug GH-7847 (stripos with large haystack has bad performance).
(ilutov)
. New function memory_reset_peak_usage(). (Patrick Allaert)
patrickallaert marked this conversation as resolved.
Show resolved Hide resolved

- Zip:
. add ZipArchive::clearError() method
Expand Down
4 changes: 4 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ PHP 8.2 UPGRADE NOTES
6. New Functions
========================================

- Standard:
. The peak memory usage can now be reset to the current usage thanks to
memory_reset_peak_usage().

========================================
7. New Classes and Interfaces
========================================
Expand Down
23 changes: 23 additions & 0 deletions Zend/tests/memory_reset_peak_usage.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
void memory_reset_peak_usage();
--SKIPIF--
<?php
if (getenv("USE_ZEND_ALLOC") === "0") {
die("skip Zend MM disabled");
}
?>
--INI--
memory_limit=-1
--FILE--
<?php
$array0 = range(1, 1024 * 1024);
$m0 = memory_get_peak_usage();
$array1 = range(1, 1024 * 1024);
var_dump(($m1 = memory_get_peak_usage()) > $m0);
unset($array0, $array1);
memory_reset_peak_usage();
var_dump(memory_get_peak_usage() < $m1);
?>
--EXPECT--
bool(true)
bool(true)
8 changes: 8 additions & 0 deletions Zend/zend_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2724,6 +2724,14 @@ ZEND_API size_t zend_memory_peak_usage(bool real_usage)
return 0;
}

ZEND_API void zend_memory_reset_peak_usage(void)
{
#if ZEND_MM_STAT
AG(mm_heap)->real_peak = AG(mm_heap)->real_size;
AG(mm_heap)->peak = AG(mm_heap)->size;
#endif
}

ZEND_API void shutdown_memory_manager(bool silent, bool full_shutdown)
{
zend_mm_shutdown(AG(mm_heap), full_shutdown, silent);
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ ZEND_API bool is_zend_ptr(const void *ptr);

ZEND_API size_t zend_memory_usage(bool real_usage);
ZEND_API size_t zend_memory_peak_usage(bool real_usage);
ZEND_API void zend_memory_reset_peak_usage(void);

/* fast cache for HashTables */
#define ALLOC_HASHTABLE(ht) \
Expand Down
5 changes: 5 additions & 0 deletions ext/standard/basic_functions_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -2175,6 +2175,9 @@ ZEND_END_ARG_INFO()

#define arginfo_memory_get_peak_usage arginfo_memory_get_usage

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_memory_reset_peak_usage, 0, 0, IS_VOID, 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

This change looks suspicious to me: the stub file from which this file is generated is not modified. I suspect that this was changed manually instead of being generated, which would then be lost on next run of the generator.

Copy link
Contributor

Choose a reason for hiding this comment

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

And you were so right! I'm going to fix this.

Copy link
Contributor

Choose a reason for hiding this comment

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

can git add . -N && git diff --exit-code be added to CI to check if all generated files are in sync after build is run?

Copy link
Member

Choose a reason for hiding this comment

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

That would make sense. Would also prevent adding malicious code to generated files like zend_vm_execute.h.

ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_version_compare, 0, 2, MAY_BE_LONG|MAY_BE_BOOL)
ZEND_ARG_TYPE_INFO(0, version1, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, version2, IS_STRING, 0)
Expand Down Expand Up @@ -2824,6 +2827,7 @@ ZEND_FUNCTION(serialize);
ZEND_FUNCTION(unserialize);
ZEND_FUNCTION(memory_get_usage);
ZEND_FUNCTION(memory_get_peak_usage);
ZEND_FUNCTION(memory_reset_peak_usage);
ZEND_FUNCTION(version_compare);
#if defined(PHP_WIN32)
ZEND_FUNCTION(sapi_windows_cp_set);
Expand Down Expand Up @@ -3479,6 +3483,7 @@ static const zend_function_entry ext_functions[] = {
ZEND_FE(unserialize, arginfo_unserialize)
ZEND_FE(memory_get_usage, arginfo_memory_get_usage)
ZEND_FE(memory_get_peak_usage, arginfo_memory_get_peak_usage)
ZEND_FE(memory_reset_peak_usage, arginfo_memory_reset_peak_usage)
ZEND_SUPPORTS_COMPILE_TIME_EVAL_FE(version_compare, arginfo_version_compare)
#if defined(PHP_WIN32)
ZEND_FE(sapi_windows_cp_set, arginfo_sapi_windows_cp_set)
Expand Down
8 changes: 8 additions & 0 deletions ext/standard/var.c
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,14 @@ PHP_FUNCTION(memory_get_peak_usage) {
}
/* }}} */

/* {{{ Resets the peak PHP memory usage */
PHP_FUNCTION(memory_reset_peak_usage) {
ZEND_PARSE_PARAMETERS_NONE();

zend_memory_reset_peak_usage();
}
/* }}} */

PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("unserialize_max_depth", "4096", PHP_INI_ALL, OnUpdateLong, unserialize_max_depth, php_basic_globals, basic_globals)
PHP_INI_END()
Expand Down