Index: index.php
===================================================================
RCS file: /cvs/drupal/drupal/index.php,v
retrieving revision 1.91
diff -u -F^f -r1.91 index.php
--- index.php	12 Dec 2006 09:32:18 -0000	1.91
+++ index.php	23 Oct 2008 02:21:41 -0000
@@ -9,6 +9,10 @@
  * prints the appropriate page.
  */
 
+include_once './includes/memory.inc';
+memory_put('total');
+
+memory_put('bootstrap');
 require_once './includes/bootstrap.inc';
 drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
 
@@ -34,4 +38,7 @@
 
 }
 
-drupal_page_footer();
\ No newline at end of file
+drupal_page_footer();
+
+memory_put('total');
+memory_shutdown();
Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.611.2.20
diff -u -F^f -r1.611.2.20 common.inc
--- includes/common.inc	9 Jul 2008 19:34:30 -0000	1.611.2.20
+++ includes/common.inc	23 Oct 2008 02:21:42 -0000
@@ -1867,6 +1867,7 @@ function _drupal_bootstrap_full() {
   // Initialize the localization system.  Depends on i18n.module being loaded already.
   $locale = locale_initialize();
   // Let all modules take action before menu system handles the reqest
+  memory_put('bootstrap');
   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:42 -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.93.2.2
diff -u -F^f -r1.93.2.2 module.inc
--- includes/module.inc	21 Jul 2007 00:54:18 -0000	1.93.2.2
+++ includes/module.inc	23 Oct 2008 02:21:42 -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.337.2.6
diff -u -F^f -r1.337.2.6 theme.inc
--- includes/theme.inc	1 Oct 2008 22:48:47 -0000	1.337.2.6
+++ includes/theme.inc	23 Oct 2008 02:21:43 -0000
@@ -31,7 +31,6 @@
  */
 function init_theme() {
   global $theme, $user, $custom_theme, $theme_engine, $theme_key;
-
   // If $theme is already set, assume the others are set, too, and do nothing
   if (isset($theme)) {
     return;
@@ -72,11 +71,15 @@ function init_theme() {
 
   if (strpos($themes[$theme]->filename, '.theme')) {
    // file is a theme; include it
+   memory_put('code', $themes[$theme]->filename);
    include_once './' . $themes[$theme]->filename;
+   memory_put('code', $themes[$theme]->filename);
   }
   elseif (strpos($themes[$theme]->description, '.engine')) {
     // file is a template; include its engine
+    memory_put('code', $themes[$theme]->description);
     include_once './' . $themes[$theme]->description;
+    memory_put('code', $themes[$theme]->description);
     $theme_engine = basename($themes[$theme]->description, '.engine');
     if (function_exists($theme_engine .'_init')) {
       call_user_func($theme_engine .'_init', $themes[$theme]);
@@ -1016,6 +1019,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)) {
@@ -1028,6 +1032,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.54.2.4
diff -u -F^f -r1.54.2.4 phptemplate.engine
--- themes/engines/phptemplate/phptemplate.engine	13 Aug 2008 18:47:17 -0000	1.54.2.4
+++ themes/engines/phptemplate/phptemplate.engine	23 Oct 2008 02:21:43 -0000
@@ -9,7 +9,9 @@
 function phptemplate_init($template) {
   $file = dirname($template->filename) . '/template.php';
   if (file_exists($file)) {
-   include_once "./$file";
+     memory_put('code', $file);
+     include_once "./$file";
+     memory_put('code', $file);
   }
 }
 
@@ -409,7 +411,10 @@ function _phptemplate_default($hook, $va
 function _phptemplate_render($file, $variables) {
   extract($variables, EXTR_SKIP);  // Extract the variables to a local namespace
   ob_start();                      // Start output buffering
+  
+  memory_put('code', $file);
   include "./$file";               // Include the file
+  memory_put('code', $file);
   $contents = ob_get_contents();   // Get the contents of the buffer
   ob_end_clean();                  // End buffering and discard
   return $contents;                // Return the contents
