Memory usage tracking
Reg - July 8, 2008 - 10:14
| Project: | Trace |
| Version: | 5.x-1.0 |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | needs work |
Jump to:
Description
With only a few lines of code your module can be very useful for finding memory issues in modules. I thought you might like to add this sometime as there is no other module anywhere that I can find that will help track memory problems.
Here's all I did to your module to see what was causing my memory problems:
trace.module:
changed definition to this:
define('TRACE_FORMAT', "#%s %s [%11s][%11s] [%s] ");
trace_api.inc:
/**
* Implements hook_trace().
*/
function trace_trace($type, $msg, $time = NULL) {
if (TRACE_OUTPUT) {
// Delegate to the actual selected output driver:
$driver = 'trace_' . TRACE_OUTPUT . '_trace';
if (!function_exists($driver)) {
require_once TRACE_PATH . '/drivers/' . TRACE_OUTPUT . '.inc';
}
$scriptMem = memory_get_usage(true); # added this line
$systemMem = memory_get_usage(false); # added this line
return $driver($type, $msg, $time, $scriptMem, $systemMem); # modified this line
}
}
file.inc:
function trace_file_trace($type, $msg, $time = NULL, $scriptMem = NULL, $realMem = NULL) { #modified this line
$type = sprintf('%-7s', strtoupper($type));
$time = _trace_format_timedelta($time);
$header = sprintf(TRACE_FORMAT, TRACE_ID, $time, number_format($scriptMem), number_format($realMem), $type) . ' '; #modified this line
if (!is_array($msg)) {
$output = $header . $msg;
}
else {
$output = array($header . array_shift($msg));
foreach ($msg as $line) {
$output[] = str_repeat(' ', strlen($header)) . $line;
}
$output = implode("\n", $output);
}
if (($file = fopen(TRACE_FILE, 'ab'))) {
fwrite($file, $output . "\n");
fclose($file);
}
}
And now I get an output like this:
#997e3fc0 T+0.110184 [ 13,893,632][ 13,034,680] [HOOK ] hook_init: cacheexclude, content, globalredirect, httpbl, memcache_admin, tokenI'm sure you could do some nifty things to make it better than my quick hack like opt to show only the used or real memory since they only seem to be bytes different, show memory in kB or MB to shorten the line. It would really be nice to see the memory usage after each call to each module on each line.
Anyway, I thought this would be dead easy useful additional feature to your module.

#1
Hello,
Your post is one year old, can you tell me if i can use this code to find what is the problem with my high traffic website that suddenly has memory problems on a 4000MB VPS ?
No more visitors than before but suddenly thousands of errors everyday, what's wrong ?
Thanks a lot !
#2
Whether this would help with your sudden problems or not I have no idea. If you are sure that the problem revolves around memory then it will certainly give you more information for a diagnostic - if it still works in the current code, you may have to tweak it. However, VPS is a fancy name for a beefier (or less sliced up) shared server so your problems could be just about anything unless you have already determined to some extent what's going on.
For example, when I added a second Drupal installation to my server I suddenly had problems that neither site on their own had. After much head scratching (and some ignoring) I finally tracked it down to both sites using memcache. Once I prefixed each site differently for memcache usage all the problems disappeared. Your problem could be like that, or a subtle system change by the ISP that only affects involved Drupal installations... just about anything.
#3
If anybody wants to roll up this feature as a patch for the 6.x branch, would certainly incorporate it.