mirror of
https://github.com/php/php-src.git
synced 2025-08-18 15:08:55 +02:00
Changed memory_get_usage() and memory_get_peak_usage(). Optional boolean argument allows get memory size allocated by emalloc() (by default) or real size of memory allocated from system.
This commit is contained in:
parent
e05efeaff5
commit
61d39cf1c1
7 changed files with 80 additions and 30 deletions
|
@ -312,10 +312,12 @@ struct _zend_mm_heap {
|
||||||
size_t block_size;
|
size_t block_size;
|
||||||
zend_mm_segment *segments_list;
|
zend_mm_segment *segments_list;
|
||||||
zend_mm_storage *storage;
|
zend_mm_storage *storage;
|
||||||
size_t size;
|
size_t real_size;
|
||||||
#if MEMORY_LIMIT
|
#if MEMORY_LIMIT
|
||||||
size_t peak;
|
size_t real_peak;
|
||||||
size_t limit;
|
size_t limit;
|
||||||
|
size_t size;
|
||||||
|
size_t peak;
|
||||||
#endif
|
#endif
|
||||||
#if ZEND_USE_MALLOC_MM
|
#if ZEND_USE_MALLOC_MM
|
||||||
int use_zend_alloc;
|
int use_zend_alloc;
|
||||||
|
@ -514,7 +516,7 @@ static void zend_mm_del_segment(zend_mm_heap *heap, zend_mm_segment *segment)
|
||||||
p = p->next_segment;
|
p = p->next_segment;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
heap->size -= segment->size;
|
heap->real_size -= segment->size;
|
||||||
ZEND_MM_STORAGE_FREE(segment);
|
ZEND_MM_STORAGE_FREE(segment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -595,10 +597,12 @@ ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_mem_handlers *handlers,
|
||||||
heap->use_zend_alloc = 1;
|
heap->use_zend_alloc = 1;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
heap->size = 0;
|
heap->real_size = 0;
|
||||||
#if MEMORY_LIMIT
|
#if MEMORY_LIMIT
|
||||||
heap->peak = 0;
|
heap->real_peak = 0;
|
||||||
heap->limit = 1<<30;
|
heap->limit = 1<<30;
|
||||||
|
heap->size = 0;
|
||||||
|
heap->peak = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
heap->overflow = 0;
|
heap->overflow = 0;
|
||||||
|
@ -996,8 +1000,10 @@ ZEND_API void zend_mm_shutdown(zend_mm_heap *heap, int full_shutdown, int silent
|
||||||
} else {
|
} else {
|
||||||
heap->segments_list = NULL;
|
heap->segments_list = NULL;
|
||||||
zend_mm_init(heap);
|
zend_mm_init(heap);
|
||||||
heap->size = 0;
|
heap->real_size = 0;
|
||||||
#if MEMORY_LIMIT
|
#if MEMORY_LIMIT
|
||||||
|
heap->real_peak = 0;
|
||||||
|
heap->size = 0;
|
||||||
heap->peak = 0;
|
heap->peak = 0;
|
||||||
#endif
|
#endif
|
||||||
heap->overflow = 0;
|
heap->overflow = 0;
|
||||||
|
@ -1175,7 +1181,7 @@ zend_mm_finished_searching_for_block:
|
||||||
|
|
||||||
|
|
||||||
#if MEMORY_LIMIT
|
#if MEMORY_LIMIT
|
||||||
if (heap->size + segment_size > heap->limit) {
|
if (heap->real_size + segment_size > heap->limit) {
|
||||||
/* Memory limit overflow */
|
/* Memory limit overflow */
|
||||||
#if ZEND_DEBUG
|
#if ZEND_DEBUG
|
||||||
zend_mm_safe_error(heap, "Allowed memory size of %d bytes exhausted at %s:%d (tried to allocate %d bytes)", heap->limit, __zend_filename, __zend_lineno, size);
|
zend_mm_safe_error(heap, "Allowed memory size of %d bytes exhausted at %s:%d (tried to allocate %d bytes)", heap->limit, __zend_filename, __zend_lineno, size);
|
||||||
|
@ -1196,17 +1202,17 @@ zend_mm_finished_searching_for_block:
|
||||||
#endif
|
#endif
|
||||||
HANDLE_UNBLOCK_INTERRUPTIONS();
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
||||||
#if ZEND_DEBUG
|
#if ZEND_DEBUG
|
||||||
zend_mm_safe_error(heap, "Out of memory (allocated %d) at %s:%d (tried to allocate %d bytes)", heap->size, __zend_filename, __zend_lineno, size);
|
zend_mm_safe_error(heap, "Out of memory (allocated %d) at %s:%d (tried to allocate %d bytes)", heap->real_size, __zend_filename, __zend_lineno, size);
|
||||||
#else
|
#else
|
||||||
zend_mm_safe_error(heap, "Out of memory (allocated %d) (tried to allocate %d bytes)", heap->size, size);
|
zend_mm_safe_error(heap, "Out of memory (allocated %d) (tried to allocate %d bytes)", heap->real_size, size);
|
||||||
#endif
|
#endif
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
heap->size += segment_size;
|
heap->real_size += segment_size;
|
||||||
#if MEMORY_LIMIT
|
#if MEMORY_LIMIT
|
||||||
if (heap->size > heap->peak) {
|
if (heap->real_size > heap->real_peak) {
|
||||||
heap->peak = heap->size;
|
heap->real_peak = heap->real_size;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -1242,6 +1248,14 @@ zend_mm_finished_searching_for_block:
|
||||||
# endif
|
# endif
|
||||||
ZEND_MM_SET_END_MAGIC(best_fit);
|
ZEND_MM_SET_END_MAGIC(best_fit);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if MEMORY_LIMIT
|
||||||
|
heap->size += true_size;
|
||||||
|
if (heap->peak < heap->size) {
|
||||||
|
heap->peak = heap->size;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
HANDLE_UNBLOCK_INTERRUPTIONS();
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
||||||
|
|
||||||
return ZEND_MM_DATA_OF(best_fit);
|
return ZEND_MM_DATA_OF(best_fit);
|
||||||
|
@ -1278,6 +1292,11 @@ static void _zend_mm_free_int(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
HANDLE_BLOCK_INTERRUPTIONS();
|
HANDLE_BLOCK_INTERRUPTIONS();
|
||||||
|
|
||||||
|
#if MEMORY_LIMIT
|
||||||
|
heap->size -= size;
|
||||||
|
#endif
|
||||||
|
|
||||||
if (ZEND_MM_PREV_BLOCK_IS_FREE(mm_block)) {
|
if (ZEND_MM_PREV_BLOCK_IS_FREE(mm_block)) {
|
||||||
next_block = ZEND_MM_NEXT_BLOCK(mm_block);
|
next_block = ZEND_MM_NEXT_BLOCK(mm_block);
|
||||||
if (ZEND_MM_IS_FREE_BLOCK(next_block)) {
|
if (ZEND_MM_IS_FREE_BLOCK(next_block)) {
|
||||||
|
@ -1349,6 +1368,14 @@ static void *_zend_mm_realloc_int(zend_mm_heap *heap, void *p, size_t size ZEND_
|
||||||
}
|
}
|
||||||
mm_block = ZEND_MM_HEADER_OF(p);
|
mm_block = ZEND_MM_HEADER_OF(p);
|
||||||
true_size = ZEND_MM_TRUE_SIZE(size);
|
true_size = ZEND_MM_TRUE_SIZE(size);
|
||||||
|
|
||||||
|
#if MEMORY_LIMIT
|
||||||
|
heap->size = heap->size + true_size - ZEND_MM_BLOCK_SIZE(mm_block);
|
||||||
|
if (heap->peak < heap->size) {
|
||||||
|
heap->peak = heap->size;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if (true_size <= ZEND_MM_BLOCK_SIZE(mm_block)) {
|
if (true_size <= ZEND_MM_BLOCK_SIZE(mm_block)) {
|
||||||
size_t remaining_size = ZEND_MM_BLOCK_SIZE(mm_block) - true_size;
|
size_t remaining_size = ZEND_MM_BLOCK_SIZE(mm_block) - true_size;
|
||||||
|
|
||||||
|
@ -1439,7 +1466,7 @@ realloc_segment:
|
||||||
|
|
||||||
segment_copy = (zend_mm_segment *) ((char *)mm_block - ZEND_MM_ALIGNED_SEGMENT_SIZE);
|
segment_copy = (zend_mm_segment *) ((char *)mm_block - ZEND_MM_ALIGNED_SEGMENT_SIZE);
|
||||||
#if MEMORY_LIMIT
|
#if MEMORY_LIMIT
|
||||||
if (heap->size + segment_size - segment_copy->size > heap->limit) {
|
if (heap->real_size + segment_size - segment_copy->size > heap->limit) {
|
||||||
HANDLE_UNBLOCK_INTERRUPTIONS();
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
||||||
#if ZEND_DEBUG
|
#if ZEND_DEBUG
|
||||||
zend_mm_safe_error(heap, "Allowed memory size of %d bytes exhausted at %s:%d (tried to allocate %d bytes)", heap->limit, __zend_filename, __zend_lineno, size);
|
zend_mm_safe_error(heap, "Allowed memory size of %d bytes exhausted at %s:%d (tried to allocate %d bytes)", heap->limit, __zend_filename, __zend_lineno, size);
|
||||||
|
@ -1453,16 +1480,16 @@ realloc_segment:
|
||||||
if (!segment) {
|
if (!segment) {
|
||||||
HANDLE_UNBLOCK_INTERRUPTIONS();
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
||||||
#if ZEND_DEBUG
|
#if ZEND_DEBUG
|
||||||
zend_mm_safe_error(heap, "Out of memory (allocated %d) at %s:%d (tried to allocate %d bytes)", heap->size, __zend_filename, __zend_lineno, size);
|
zend_mm_safe_error(heap, "Out of memory (allocated %d) at %s:%d (tried to allocate %d bytes)", heap->real_size, __zend_filename, __zend_lineno, size);
|
||||||
#else
|
#else
|
||||||
zend_mm_safe_error(heap, "Out of memory (allocated %d) (tried to allocate %d bytes)", heap->size, size);
|
zend_mm_safe_error(heap, "Out of memory (allocated %d) (tried to allocate %d bytes)", heap->real_size, size);
|
||||||
#endif
|
#endif
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
heap->size += segment_size - segment->size;
|
heap->real_size += segment_size - segment->size;
|
||||||
#if MEMORY_LIMIT
|
#if MEMORY_LIMIT
|
||||||
if (heap->size > heap->peak) {
|
if (heap->real_size > heap->real_peak) {
|
||||||
heap->peak = heap->size;
|
heap->real_peak = heap->real_size;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
segment->size = segment_size;
|
segment->size = segment_size;
|
||||||
|
@ -1778,15 +1805,27 @@ ZEND_API int zend_set_memory_limit(unsigned int memory_limit)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
ZEND_API size_t zend_memory_usage(TSRMLS_D)
|
ZEND_API size_t zend_memory_usage(int real_usage TSRMLS_DC)
|
||||||
{
|
{
|
||||||
return AG(mm_heap)->size;
|
if (real_usage) {
|
||||||
|
return AG(mm_heap)->real_size;
|
||||||
|
} else {
|
||||||
|
#if MEMORY_LIMIT
|
||||||
|
return AG(mm_heap)->size;
|
||||||
|
#else
|
||||||
|
return AG(mm_heap)->real_size;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#if MEMORY_LIMIT
|
#if MEMORY_LIMIT
|
||||||
ZEND_API size_t zend_memory_peak_usage(TSRMLS_D)
|
ZEND_API size_t zend_memory_peak_usage(int real_usage TSRMLS_DC)
|
||||||
{
|
{
|
||||||
return AG(mm_heap)->peak;
|
if (real_usage) {
|
||||||
|
return AG(mm_heap)->real_peak;
|
||||||
|
} else {
|
||||||
|
return AG(mm_heap)->peak;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -119,8 +119,8 @@ void zend_debug_alloc_output(char *format, ...);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if MEMORY_LIMIT
|
#if MEMORY_LIMIT
|
||||||
ZEND_API size_t zend_memory_usage(TSRMLS_D);
|
ZEND_API size_t zend_memory_usage(int real_usage TSRMLS_DC);
|
||||||
ZEND_API size_t zend_memory_peak_usage(TSRMLS_D);
|
ZEND_API size_t zend_memory_peak_usage(int real_usage TSRMLS_DC);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
END_EXTERN_C()
|
END_EXTERN_C()
|
||||||
|
|
|
@ -1152,14 +1152,25 @@ PHP_FUNCTION(unserialize)
|
||||||
/* {{{ proto int memory_get_usage()
|
/* {{{ proto int memory_get_usage()
|
||||||
Returns the allocated by PHP memory */
|
Returns the allocated by PHP memory */
|
||||||
PHP_FUNCTION(memory_get_usage) {
|
PHP_FUNCTION(memory_get_usage) {
|
||||||
|
zend_bool real_usage = 0;
|
||||||
|
|
||||||
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &real_usage) == FAILURE) {
|
||||||
|
RETURN_FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
RETURN_LONG(zend_memory_usage(TSRMLS_C));
|
RETURN_LONG(zend_memory_usage(real_usage TSRMLS_CC));
|
||||||
}
|
}
|
||||||
/* }}} */
|
/* }}} */
|
||||||
/* {{{ proto int memory_get_peak_usage()
|
/* {{{ proto int memory_get_peak_usage()
|
||||||
Returns the peak allocated by PHP memory */
|
Returns the peak allocated by PHP memory */
|
||||||
PHP_FUNCTION(memory_get_peak_usage) {
|
PHP_FUNCTION(memory_get_peak_usage) {
|
||||||
RETURN_LONG(zend_memory_peak_usage(TSRMLS_C));
|
zend_bool real_usage = 0;
|
||||||
|
|
||||||
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &real_usage) == FAILURE) {
|
||||||
|
RETURN_FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
RETURN_LONG(zend_memory_peak_usage(real_usage TSRMLS_CC));
|
||||||
}
|
}
|
||||||
/* }}} */
|
/* }}} */
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -669,7 +669,7 @@ static int send_parsed_php(request_rec * r)
|
||||||
char *mem_usage;
|
char *mem_usage;
|
||||||
TSRMLS_FETCH();
|
TSRMLS_FETCH();
|
||||||
|
|
||||||
mem_usage = ap_psprintf(r->pool, "%u", zend_memory_peak_usage(TSRMLS_C));
|
mem_usage = ap_psprintf(r->pool, "%u", zend_memory_peak_usage(1 TSRMLS_CC));
|
||||||
ap_table_setn(r->notes, "mod_php_memory_usage", mem_usage);
|
ap_table_setn(r->notes, "mod_php_memory_usage", mem_usage);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -521,7 +521,7 @@ static int php_output_filter(ap_filter_t *f, apr_bucket_brigade *bb)
|
||||||
{
|
{
|
||||||
char *mem_usage;
|
char *mem_usage;
|
||||||
|
|
||||||
mem_usage = apr_psprintf(ctx->r->pool, "%u", zend_memory_peak_usage(TSRMLS_C));
|
mem_usage = apr_psprintf(ctx->r->pool, "%u", zend_memory_peak_usage(1 TSRMLS_CC));
|
||||||
apr_table_set(ctx->r->notes, "mod_php_memory_usage", mem_usage);
|
apr_table_set(ctx->r->notes, "mod_php_memory_usage", mem_usage);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -593,7 +593,7 @@ zend_first_try {
|
||||||
{
|
{
|
||||||
char *mem_usage;
|
char *mem_usage;
|
||||||
|
|
||||||
mem_usage = apr_psprintf(ctx->r->pool, "%u", zend_memory_peak_usage(TSRMLS_C));
|
mem_usage = apr_psprintf(ctx->r->pool, "%u", zend_memory_peak_usage(1 TSRMLS_CC));
|
||||||
apr_table_set(r->notes, "mod_php_memory_usage", mem_usage);
|
apr_table_set(r->notes, "mod_php_memory_usage", mem_usage);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -727,7 +727,7 @@ static int send_parsed_php(request_rec * r)
|
||||||
char *mem_usage;
|
char *mem_usage;
|
||||||
TSRMLS_FETCH();
|
TSRMLS_FETCH();
|
||||||
|
|
||||||
mem_usage = ap_psprintf(r->pool, "%u", zend_memory_peak_usage(TSRMLS_C));
|
mem_usage = ap_psprintf(r->pool, "%u", zend_memory_peak_usage(1 TSRMLS_CC));
|
||||||
ap_table_setn(r->notes, "mod_php_memory_usage", mem_usage);
|
ap_table_setn(r->notes, "mod_php_memory_usage", mem_usage);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue