icon Get the most out of Surmunity, read our tips here! Need an interesting blog to read? You've got to read the Surpass Blog! | Welcome! Please register to access all of our features.
Old July 26th, 2007, 2:59 AM   #1 (permalink)
Bow before Surpass!
Super #1
 
Joined in Sep 2004
1,542 posts
Gave thanks: 91
Thanked 49 times
PHP Memory Checker

I'm trying to do a "if" statement where the PHP checks on the server's memory usage and performs actions based on the amount.

For example, if it could find the usage of memory in percentage values, then I'd have an if statement tha would...

PHP Code:
if (memory => low) { 
/* show content */ 
} elseif (memory => medium) { 
/* show limited content */ 
} elseif (memory =< high) { 
/* block access */ 

So, would anyone know the right type of method to find the memory usage and then create a similar if statement?

Thank you
__________________
GamingHybrid is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old July 26th, 2007, 7:31 AM   #2 (permalink)
Surpass Fan
Excelling Contributor
 
cowboy's Avatar
 
Joined in Nov 2005
Lives in Colorado
Hosted on DEDI
934 posts
Gave thanks: 2
Thanked 94 times
Memory is physical and swap, so you need to determine the criteria you wish to compare when you indicate values like low, medium, high. I graph my server memory and keep my eye on a constant System, currently around 40%, and a very low swap file.

If I were to use an if() statement to determine tasks I suppose I would use the swap file as the deciding factor: low < 5%; high > 70%.



You will find memory usage in the file meminfo in the proc directory.

Sample:
Code:
MemTotal:       451100 kB
MemFree:         21956 kB
Buffers:         76644 kB
Cached:         170976 kB
SwapCached:      19332 kB
Active:         244424 kB
Inactive:       127024 kB
HighTotal:           0 kB
HighFree:            0 kB
LowTotal:       451100 kB
LowFree:         21956 kB
SwapTotal:     2048276 kB
SwapFree:      1962216 kB
Dirty:            1216 kB
Writeback:           0 kB
Mapped:         139048 kB
Slab:            50756 kB
Committed_AS:   443792 kB
PageTables:       2240 kB
VmallocTotal:   565240 kB
VmallocUsed:      3644 kB
VmallocChunk:   561348 kB
HugePages_Total:     0
HugePages_Free:      0
Hugepagesize:     4096 kB
and the size of the swap files in the file swaps in the proc directory.

Sample:
Code:
Filename    Type  Size Used Priority
/dev/hda3                               partition 2048276 86060 -1
Filename    Type  Size Used Priority
/dev/hda3                               partition 2048276 86060 -1
Filename    Type  Size Used Priority
/dev/hda3                               partition 2048276 86060 -1
Filename    Type  Size Used Priority
/dev/hda3                               partition 2048276 86060 -1
Filename    Type  Size Used Priority
/dev/hda3                               partition 2048276 86060 -1
Sample Code function to yield Physical memory (System, Buffered, & Cached) and Swap (Total & Available):

PHP Code:
<?
  
function memory () {
    if (
$fd fopen('/proc/meminfo''r')) {
      
$results['ram'] = array();
      
$results['swap'] = array();
      
$results['devswap'] = array();
      while (
$buf fgets($fd4096)) {
        if (
preg_match('/^MemTotal:\s+(.*)\s*kB/i'$buf$ar_buf)) {
          
$results['ram']['total'] = $ar_buf[1];
        } else if (
preg_match('/^MemFree:\s+(.*)\s*kB/i'$buf$ar_buf)) {
          
$results['ram']['t_free'] = $ar_buf[1];
        } else if (
preg_match('/^Cached:\s+(.*)\s*kB/i'$buf$ar_buf)) {
          
$results['ram']['cached'] = $ar_buf[1];
        } else if (
preg_match('/^Buffers:\s+(.*)\s*kB/i'$buf$ar_buf)) {
          
$results['ram']['buffers'] = $ar_buf[1];
        } else if (
preg_match('/^SwapTotal:\s+(.*)\s*kB/i'$buf$ar_buf)) {
          
$results['swap']['total'] = $ar_buf[1];
        } else if (
preg_match('/^SwapFree:\s+(.*)\s*kB/i'$buf$ar_buf)) {
          
$results['swap']['free'] = $ar_buf[1];
        } 
      } 
      
fclose($fd);
      
$results['ram']['t_used'] = $results['ram']['total'] - $results['ram']['t_free'];
      
$results['ram']['percent'] = round(($results['ram']['t_used'] * 100) / $results['ram']['total']);
      
$results['swap']['used'] = $results['swap']['total'] - $results['swap']['free'];
      
$results['swap']['percent'] = round(($results['swap']['used'] * 100) / $results['swap']['total']);
 
      
// values for splitting memory usage
      
if (isset($results['ram']['cached']) && isset($results['ram']['buffers'])) {
        
$results['ram']['app'] = $results['ram']['t_used'] - $results['ram']['cached'] - $results['ram']['buffers'];
 
$results['ram']['app_percent'] = round(($results['ram']['app'] * 100) / $results['ram']['total']);
 
$results['ram']['buffers_percent'] = round(($results['ram']['buffers'] * 100) / $results['ram']['total']);
 
$results['ram']['cached_percent'] = round(($results['ram']['cached'] * 100) / $results['ram']['total']);
      }
      
$swaps file ('/proc/swaps');
      for (
$i 1$i < (sizeof($swaps)); $i++) {
        
$ar_buf preg_split('/\s+/'$swaps[$i], 6);
        
$results['devswap'][$i 1] = array();
        
$results['devswap'][$i 1]['dev'] = $ar_buf[0];
        
$results['devswap'][$i 1]['total'] = $ar_buf[2];
        
$results['devswap'][$i 1]['used'] = $ar_buf[3];
        
$results['devswap'][$i 1]['free'] = ($results['devswap'][$i 1]['total'] - $results['devswap'][$i 1]['used']);
        
$results['devswap'][$i 1]['percent'] = round(($ar_buf[3] * 100) / $ar_buf[2]);
      } 
    } else {
      
$results['ram'] = array();
      
$results['swap'] = array();
      
$results['devswap'] = array();
    }
    return 
$results;
  } 
?>
__________________
Where would you be if you were at the highest court in the land (US)?
cowboy is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old July 27th, 2007, 3:53 PM   #3 (permalink)
Bow before Surpass!
Super #1
 
Joined in Sep 2004
1,542 posts
Gave thanks: 91
Thanked 49 times
You're my hero Cowboy! Thanks for this! I'll try it soon and see how it goes, also thanks for the information on about memory.

I'd click "thanks" if there the feature was still there... :P
__________________
GamingHybrid is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On