Skip to content

Commit

Permalink
Merge pull request tideways#53 from tideways/memory-profiling-hook
Browse files Browse the repository at this point in the history
Fix to, correctly sum up exclusive and inclusive memory values.
  • Loading branch information
beberlei authored Jun 11, 2018
2 parents 13347e1 + eb22792 commit 73990a8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 23 deletions.
3 changes: 3 additions & 0 deletions php_tideways_xhprof.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ ZEND_BEGIN_MODULE_GLOBALS(tideways_xhprof)
zend_ulong function_hash_counters[TIDEWAYS_XHPROF_CALLGRAPH_COUNTER_SIZE];
xhprof_callgraph_bucket* callgraph_buckets[TIDEWAYS_XHPROF_CALLGRAPH_SLOTS];
zend_long flags;
long int num_alloc;
long int num_free;
long int amount_alloc;
ZEND_END_MODULE_GLOBALS(tideways_xhprof)

#if defined(__GNUC__) && __GNUC__ >= 4
Expand Down
25 changes: 8 additions & 17 deletions tracing.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,8 @@ void tracing_request_shutdown()

void *tideways_malloc (size_t size)
{
xhprof_frame_t *current_frame = TXRG(callgraph_frames);
if (current_frame) {
current_frame->num_alloc += 1;
current_frame->amount_alloc += size;
}
TXRG(num_alloc) += 1;
TXRG(amount_alloc) += size;

if (_zend_malloc) {
return _zend_malloc(size);
Expand All @@ -326,10 +323,7 @@ void *tideways_malloc (size_t size)

void tideways_free (void *ptr)
{
xhprof_frame_t *current_frame = TXRG(callgraph_frames);
if (current_frame) {
current_frame->num_free += 1;
}
TXRG(num_free) += 1;

if (_zend_free) {
return _zend_free(ptr);
Expand All @@ -341,17 +335,14 @@ void tideways_free (void *ptr)

void *tideways_realloc (void *ptr, size_t size)
{
xhprof_frame_t *current_frame = TXRG(callgraph_frames);
if (current_frame) {
current_frame->num_alloc += 1;
current_frame->num_free += 1;
current_frame->amount_alloc += size;
}

TXRG(num_alloc) += 1;
TXRG(num_free) += 1;
TXRG(amount_alloc) += size;

if (_zend_realloc) {
return _zend_realloc(ptr, size);
}

zend_mm_heap *heap = zend_mm_get_heap();
return zend_mm_realloc(heap, ptr, size);
}
}
12 changes: 6 additions & 6 deletions tracing.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ zend_always_inline static int tracing_enter_frame_callgraph(zend_string *root_sy
current_frame->mu_start = zend_memory_usage(0 TSRMLS_CC);
}

current_frame->num_alloc = 0;
current_frame->num_free = 0;
current_frame->amount_alloc = 0;
current_frame->num_alloc = TXRG(num_alloc);
current_frame->num_free = TXRG(num_free);
current_frame->amount_alloc = TXRG(amount_alloc);

/* We only need to compute the hash for the function name,
* that should be "good" enough, we sort into 1024 buckets only anyways */
Expand Down Expand Up @@ -202,9 +202,9 @@ zend_always_inline static void tracing_exit_frame_callgraph(TSRMLS_D)
bucket->count++;
bucket->wall_time += duration;

bucket->num_alloc += current_frame->num_alloc;
bucket->num_free += current_frame->num_free;
bucket->amount_alloc += current_frame->amount_alloc;
bucket->num_alloc += TXRG(num_alloc) - current_frame->num_alloc;
bucket->num_free += TXRG(num_free) - current_frame->num_free;
bucket->amount_alloc += TXRG(amount_alloc) - current_frame->amount_alloc;

if (TXRG(flags) & TIDEWAYS_XHPROF_FLAGS_CPU) {
bucket->cpu_time += (cpu_timer() - current_frame->cpu_start);
Expand Down

0 comments on commit 73990a8

Please sign in to comment.