diff options
author | Dmitry Stogov <dmitry@php.net> | 2006-07-25 13:40:05 +0000 |
---|---|---|
committer | Dmitry Stogov <dmitry@php.net> | 2006-07-25 13:40:05 +0000 |
commit | 356facf4afc9c3cea3d6648923f69daba9e1ec82 (patch) | |
tree | c7d3fc15dade605244f2df6b0b4fe3bec217d5c9 | |
parent | 0a95528fc042143f00567deb54e7174e8881204f (diff) | |
download | php-git-356facf4afc9c3cea3d6648923f69daba9e1ec82.tar.gz |
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.
-rw-r--r-- | NEWS | 4 | ||||
-rw-r--r-- | Zend/zend_alloc.c | 83 | ||||
-rw-r--r-- | Zend/zend_alloc.h | 4 | ||||
-rw-r--r-- | ext/standard/var.c | 15 | ||||
-rw-r--r-- | sapi/apache/mod_php5.c | 2 | ||||
-rw-r--r-- | sapi/apache2filter/sapi_apache2.c | 2 | ||||
-rw-r--r-- | sapi/apache2handler/sapi_apache2.c | 2 | ||||
-rw-r--r-- | sapi/apache_hooks/mod_php5.c | 2 |
8 files changed, 84 insertions, 30 deletions
@@ -1,6 +1,10 @@ PHP NEWS ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ?? Aug 2006, PHP 5.2.0RC2 +- 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. (Dmitry) + - Moved extensions to PECL: . ext/filepro (Derick, Tony) . ext/hwapi (Derick, Tony) diff --git a/Zend/zend_alloc.c b/Zend/zend_alloc.c index 1ebe1012f7..1a04bc3953 100644 --- a/Zend/zend_alloc.c +++ b/Zend/zend_alloc.c @@ -312,10 +312,12 @@ struct _zend_mm_heap { size_t block_size; zend_mm_segment *segments_list; zend_mm_storage *storage; - size_t size; + size_t real_size; #if MEMORY_LIMIT - size_t peak; + size_t real_peak; size_t limit; + size_t size; + size_t peak; #endif #if ZEND_USE_MALLOC_MM 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; } } - heap->size -= segment->size; + heap->real_size -= segment->size; 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; #endif - heap->size = 0; + heap->real_size = 0; #if MEMORY_LIMIT - heap->peak = 0; + heap->real_peak = 0; heap->limit = 1<<30; + heap->size = 0; + heap->peak = 0; #endif heap->overflow = 0; @@ -996,8 +1000,10 @@ ZEND_API void zend_mm_shutdown(zend_mm_heap *heap, int full_shutdown, int silent } else { heap->segments_list = NULL; zend_mm_init(heap); - heap->size = 0; + heap->real_size = 0; #if MEMORY_LIMIT + heap->real_peak = 0; + heap->size = 0; heap->peak = 0; #endif heap->overflow = 0; @@ -1175,7 +1181,7 @@ zend_mm_finished_searching_for_block: #if MEMORY_LIMIT - if (heap->size + segment_size > heap->limit) { + if (heap->real_size + segment_size > heap->limit) { /* Memory limit overflow */ #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); @@ -1196,17 +1202,17 @@ zend_mm_finished_searching_for_block: #endif HANDLE_UNBLOCK_INTERRUPTIONS(); #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 - 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 return NULL; } - heap->size += segment_size; + heap->real_size += segment_size; #if MEMORY_LIMIT - if (heap->size > heap->peak) { - heap->peak = heap->size; + if (heap->real_size > heap->real_peak) { + heap->real_peak = heap->real_size; } #endif @@ -1242,6 +1248,14 @@ zend_mm_finished_searching_for_block: # endif ZEND_MM_SET_END_MAGIC(best_fit); #endif + +#if MEMORY_LIMIT + heap->size += true_size; + if (heap->peak < heap->size) { + heap->peak = heap->size; + } +#endif + HANDLE_UNBLOCK_INTERRUPTIONS(); 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 HANDLE_BLOCK_INTERRUPTIONS(); + +#if MEMORY_LIMIT + heap->size -= size; +#endif + if (ZEND_MM_PREV_BLOCK_IS_FREE(mm_block)) { next_block = ZEND_MM_NEXT_BLOCK(mm_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); 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)) { 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); #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(); #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); @@ -1453,16 +1480,16 @@ realloc_segment: if (!segment) { HANDLE_UNBLOCK_INTERRUPTIONS(); #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 - 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 return NULL; } - heap->size += segment_size - segment->size; + heap->real_size += segment_size - segment->size; #if MEMORY_LIMIT - if (heap->size > heap->peak) { - heap->peak = heap->size; + if (heap->real_size > heap->real_peak) { + heap->real_peak = heap->real_size; } #endif segment->size = segment_size; @@ -1736,15 +1763,27 @@ ZEND_API int zend_set_memory_limit(unsigned int memory_limit) #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 -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 diff --git a/Zend/zend_alloc.h b/Zend/zend_alloc.h index 6a0851c98f..c8eecb99e9 100644 --- a/Zend/zend_alloc.h +++ b/Zend/zend_alloc.h @@ -108,8 +108,8 @@ void zend_debug_alloc_output(char *format, ...); #endif #if MEMORY_LIMIT -ZEND_API size_t zend_memory_usage(TSRMLS_D); -ZEND_API size_t zend_memory_peak_usage(TSRMLS_D); +ZEND_API size_t zend_memory_usage(int real_usage TSRMLS_DC); +ZEND_API size_t zend_memory_peak_usage(int real_usage TSRMLS_DC); #endif END_EXTERN_C() diff --git a/ext/standard/var.c b/ext/standard/var.c index f88d4ff39d..bfc61a0ea4 100644 --- a/ext/standard/var.c +++ b/ext/standard/var.c @@ -912,14 +912,25 @@ PHP_FUNCTION(unserialize) /* {{{ proto int memory_get_usage() Returns the allocated by PHP memory */ 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() Returns the peak allocated by PHP memory */ 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 diff --git a/sapi/apache/mod_php5.c b/sapi/apache/mod_php5.c index e661fdb4b0..12ae84b89e 100644 --- a/sapi/apache/mod_php5.c +++ b/sapi/apache/mod_php5.c @@ -684,7 +684,7 @@ static int send_parsed_php(request_rec * r) char *mem_usage; 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); } #endif diff --git a/sapi/apache2filter/sapi_apache2.c b/sapi/apache2filter/sapi_apache2.c index 4e7c61411c..453a8e51b9 100644 --- a/sapi/apache2filter/sapi_apache2.c +++ b/sapi/apache2filter/sapi_apache2.c @@ -526,7 +526,7 @@ static int php_output_filter(ap_filter_t *f, apr_bucket_brigade *bb) { 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); } #endif diff --git a/sapi/apache2handler/sapi_apache2.c b/sapi/apache2handler/sapi_apache2.c index f2c4956e8b..bed3a5a973 100644 --- a/sapi/apache2handler/sapi_apache2.c +++ b/sapi/apache2handler/sapi_apache2.c @@ -598,7 +598,7 @@ zend_first_try { { 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); } #endif diff --git a/sapi/apache_hooks/mod_php5.c b/sapi/apache_hooks/mod_php5.c index 5ef0181e7d..fcdb4d44f7 100644 --- a/sapi/apache_hooks/mod_php5.c +++ b/sapi/apache_hooks/mod_php5.c @@ -742,7 +742,7 @@ static int send_parsed_php(request_rec * r) char *mem_usage; 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); } #endif |