Index: index.php
===================================================================
RCS file: /cvs/drupal/drupal/index.php,v
retrieving revision 1.94
diff -u -F^f -r1.94 index.php
--- index.php	26 Dec 2007 08:46:48 -0000	1.94
+++ index.php	23 Oct 2008 02:21:36 -0000
@@ -12,6 +12,10 @@
  * See COPYRIGHT.txt and LICENSE.txt.
  */
 
+include_once './includes/memory.inc';
+memory_put('total');
+
+memory_put('bootstrap');
 require_once './includes/bootstrap.inc';
 drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
 
@@ -37,3 +41,7 @@
 }
 
 drupal_page_footer();
+
+memory_put('total');
+memory_shutdown();
+
Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.756.2.30
diff -u -F^f -r1.756.2.30 common.inc
--- includes/common.inc	22 Oct 2008 19:36:25 -0000	1.756.2.30
+++ includes/common.inc	23 Oct 2008 02:21:38 -0000
@@ -2465,6 +2465,7 @@ function _drupal_bootstrap_full() {
   module_load_all();
   // Let all modules take action before menu system handles the request
   // We do not want this while running update.php.
+  memory_put('bootstrap');
   if (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update') {
     module_invoke_all('init');
   }
Index: includes/memory.inc
===================================================================
RCS file: includes/memory.inc
diff -N includes/memory.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ includes/memory.inc	23 Oct 2008 02:21:38 -0000
@@ -0,0 +1,93 @@
+<?php
+
+$mem = array();
+
+function memory_put($realm = NULL, $item = NULL) {
+  global $mem;
+
+  if (!isset($realm)) {
+    // Nothing to do
+    return;
+  }
+
+  if (!isset($item)) {
+    if (isset($mem[$realm])) {
+      // Has a value, so calculate the memory usage
+      $mem[$realm] = memory_get_usage() - $mem[$realm];
+    }
+    else {
+      // Not set before, set it now
+      $mem[$realm] = memory_get_usage();
+    }
+  }
+  else {
+    if (isset($mem[$realm][$item])) {
+      $mem[$realm][$item] = memory_get_usage() - $mem[$realm][$item];
+    }
+    else {
+      $mem[$realm][$item] = memory_get_usage();
+    }
+  }
+}
+
+function memory_get($realm = NULL, $item = NULL) {
+  global $mem;
+  
+  if (!$realm) {
+    // Nothing to do
+    return 0;
+  }
+
+  if (!$item) {
+    return $mem[$realm];
+  }
+  else {
+    return $mem[$realm][$item];
+  }
+}
+
+function memory_get_file() {
+  static $memory_file;
+
+  if ($memory_file) {
+    return $memory_file;
+  }
+
+  $tmp_dir = variable_get('file_directory_temp', '/tmp');
+  $memory_file = fopen($tmp_dir . '/memory.csv', 'a+');
+
+  // Output the path of the page, since different pages can have different memory
+  memory_write(':==' . date('c') . '===  Page: ' . $_GET['q']);
+  return $memory_file;
+}
+
+function memory_write($string = '') {
+  // Add the following line to settings.php to enable memory profiling
+  //   $conf['memory_profiling'] = 1;
+  if (variable_get('memory_profiling', 0)) {
+    $file = memory_get_file();
+    fwrite($file, $string . "\n");
+  }
+}
+
+function memory_shutdown() {
+  $realm_data = array();
+
+  foreach(array('code', 'blocks') as $realm) {
+    $realm_data[$realm] = 0;
+    foreach(memory_get($realm) as $item => $bytes) {
+      $realm_data[$realm] += $bytes;
+      memory_write($realm . '-' . $item . ',' . $bytes);
+    }
+  }
+
+  $drupal = memory_get('total');
+  $data = $drupal - $realm_data['code'];
+
+  memory_write('Total code,'     . $realm_data['code']);
+  memory_write('Bootstrap code,' . memory_get('bootstrap'));
+  memory_write('Block data,'     . $realm_data['blocks']);
+  memory_write('Total data,'     . $data);
+  memory_write('Drupal,'         . $drupal);
+  memory_write('PHP,'            . memory_get_peak_usage());
+}
Index: includes/module.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/module.inc,v
retrieving revision 1.115
diff -u -F^f -r1.115 module.inc
--- includes/module.inc	27 Dec 2007 12:31:05 -0000	1.115
+++ includes/module.inc	23 Oct 2008 02:21:38 -0000
@@ -11,7 +11,9 @@
  */
 function module_load_all() {
   foreach (module_list(TRUE, FALSE) as $module) {
+    memory_put('code', $module);
     drupal_load('module', $module);
+    memory_put('code', $module);
   }
 }
 
Index: includes/theme.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/theme.inc,v
retrieving revision 1.415.2.13
diff -u -F^f -r1.415.2.13 theme.inc
--- includes/theme.inc	16 Oct 2008 13:50:59 -0000	1.415.2.13
+++ includes/theme.inc	23 Oct 2008 02:21:39 -0000
@@ -168,12 +168,16 @@ function _init_theme($theme, $base_theme
     foreach ($base_theme as $base) {
       // Include the theme file or the engine.
       if (!empty($base->owner)) {
+        memory_put('code', $base->owner);
         include_once './'. $base->owner;
+        memory_put('code', $base->owner);
       }
     }
     // and our theme gets one too.
     if (!empty($theme->owner)) {
+      memory_put('code', $theme->owner);
       include_once './'. $theme->owner;
+      memory_put('code', $theme->owner);
     }
   }
 
@@ -280,10 +284,14 @@ function _theme_process_registry(&$cache
       // files can prevent them from getting registered.
       if (isset($info['file']) && !isset($info['path'])) {
         $result[$hook]['file'] = $path .'/'. $info['file'];
+        memory_put('code', $result[$hook]['file']);
         include_once($result[$hook]['file']);
+        memory_put('code', $result[$hook]['file']);
       }
       elseif (isset($info['file']) && isset($info['path'])) {
+        memory_put('code', $info['path'] .'/'. $info['file']);
         include_once($info['path'] .'/'. $info['file']);
+        memory_put('code', $info['path'] .'/'. $info['file']);
       }
 
       if (isset($info['template']) && !isset($info['path'])) {
@@ -590,7 +598,9 @@ function theme() {
     if (isset($info['path'])) {
       $include_file = $info['path'] .'/'. $include_file;
     }
+    memory_put('code', $include_file);
     include_once($include_file);
+    memory_put('code', $include_file);
   }
   if (isset($info['function'])) {
     // The theme call is a function.
@@ -976,7 +986,9 @@ function theme_get_setting($setting_name
 function theme_render_template($template_file, $variables) {
   extract($variables, EXTR_SKIP);  // Extract the variables to a local namespace
   ob_start();                      // Start output buffering
+  memory_put('code', $template_file);
   include "./$template_file";      // Include the template file
+  memory_put('code', $template_file);
   $contents = ob_get_contents();   // Get the contents of the buffer
   ob_end_clean();                  // End buffering and discard
   return $contents;                // Return the contents
@@ -1532,6 +1544,7 @@ function theme_closure($main = 0) {
  *   A string containing the themed blocks for this region.
  */
 function theme_blocks($region) {
+  memory_put('blocks', $region);
   $output = '';
 
   if ($list = block_list($region)) {
@@ -1543,6 +1556,7 @@ function theme_blocks($region) {
 
   // Add any content assigned to this region through drupal_set_content() calls.
   $output .= drupal_get_content($region);
+  memory_put('blocks', $region);
 
   return $output;
 }
Index: themes/engines/phptemplate/phptemplate.engine
===================================================================
RCS file: /cvs/drupal/drupal/themes/engines/phptemplate/phptemplate.engine,v
retrieving revision 1.69
diff -u -F^f -r1.69 phptemplate.engine
--- themes/engines/phptemplate/phptemplate.engine	2 Oct 2007 16:19:23 -0000	1.69
+++ themes/engines/phptemplate/phptemplate.engine	23 Oct 2008 02:21:39 -0000
@@ -9,7 +9,9 @@
 function phptemplate_init($template) {
   $file = dirname($template->filename) .'/template.php';
   if (file_exists($file)) {
+    memory_put('code', $file);
     include_once "./$file";
+    memory_put('code', $file);
   }
 }
 

