Smarty
[ class tree: Smarty ] [ index: Smarty ] [ all elements ]

Source for file function.memusage.php

Documentation is available at function.memusage.php

  1. <?php
  2. /**
  3.  * Smarty plugin
  4.  * @package Smarty
  5.  * @subpackage plugins
  6.  */
  7.  
  8. /**
  9.  * smarty_function_memusage
  10.  */
  11. function smarty_function_memusage($params, &$gBitSmarty) {
  12.     if( function_exists( 'memory_get_usage' ) ) {
  13.         $memusage = memory_get_usage();
  14.     } else {
  15.         // If its Windows
  16.         // Tested on Win XP Pro SP2. Should work on Win 2003 Server too
  17.         // Doesn't work for 2000
  18.         // If you need it to work for 2000 look at http://us2.php.net/manual/en/function.memory-get-usage.php#54642
  19.         if( substr( PHP_OS, 0, 3 ) == 'WIN' ) {
  20.             if( substr( PHP_OS, 0, 3 ) == 'WIN' ) {
  21.                 $output = array();
  22.             }
  23.             exec( 'tasklist /FI "PID eq ' . getmypid() . '" /FO LIST', $output );
  24.             $memusage = preg_replace( '/[\D]/', '', $output[5] ) * 1024;
  25.         } else {
  26.             // We now assume the OS is UNIX
  27.             // Tested on Mac OS X 10.4.6 and Linux Red Hat Enterprise 4
  28.             // This should work on most UNIX systems
  29.             $pid = getmypid();
  30.             exec( "ps -eo%mem,rss,pid | grep $pid", $output );
  31.             $output = explode( " ", trim( $output[0] ) );
  32.             // rss is given in 1024 byte units
  33.             $memusage = $output[1] * 1024;
  34.         }
  35.     }
  36.  
  37.     if( $memusage > 0 ) {
  38.         $memunit="B";
  39.         if( $memusage > 1024 ) {
  40.             $memusage = $memusage/1024;
  41.             $memunit = "kB";
  42.         }
  43.         if( $memusage>1024 ) {
  44.             $memusage=$memusage/1024;
  45.             $memunit="MB";
  46.         }
  47.         if( $memusage>1024 ) {
  48.             $memusage=$memusage/1024;
  49.             $memunit="GB";
  50.         }
  51.         print( number_format( $memusage, 2 ).$memunit );
  52.     }
  53. }
  54.  
  55. ?>

Documentation generated on Wed, 29 Jul 2015 13:56:39 +0000 by phpDocumentor 1.5.0-lsces