weerok

FreeBSD free RAM memory status

Nov 23rd, 2012
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6.71 KB | None | 0 0
  1. #!/usr/bin/perl
  2. ##
  3. ##  freebsd-memory -- List Total System Memory Usage
  4. ##  Copyright (c) 2003-2004 Ralf S. Engelschall <rse@engelschall.com>
  5. ##  
  6. ##  Redistribution and use in source and binary forms, with or without
  7. ##  modification, are permitted provided that the following conditions
  8. ##  are met:
  9. ##  1. Redistributions of source code must retain the above copyright
  10. ##     notice, this list of conditions and the following disclaimer.
  11. ##  2. Redistributions in binary form must reproduce the above copyright
  12. ##     notice, this list of conditions and the following disclaimer in the
  13. ##     documentation and/or other materials provided with the distribution.
  14. ##  
  15. ##  THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. ##  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. ##  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ##  ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
  19. ##  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. ##  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. ##  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. ##  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. ##  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. ##  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. ##  SUCH DAMAGE.
  26. ##
  27.  
  28. #   query the system through the generic sysctl(8) interface
  29. #   (this does not require special priviledges)
  30. my $sysctl = {};
  31. my $sysctl_output = `/sbin/sysctl -a`;
  32. foreach my $line (split(/\n/, $sysctl_output)) {
  33.     if ($line =~ m/^([^:]+):\s+(.+)\s*$/s) {
  34.         $sysctl->{$1} = $2;
  35.     }
  36. }
  37.  
  38. #   round the physical memory size to the next power of two which is
  39. #   reasonable for memory cards. We do this by first determining the
  40. #   guessed memory card size under the assumption that usual computer
  41. #   hardware has an average of a maximally eight memory cards installed
  42. #   and those are usually of equal size.
  43. sub mem_rounded {
  44.     my ($mem_size) = @_;
  45.     my $chip_size  = 1;
  46.     my $chip_guess = ($mem_size / 8) - 1;
  47.     while ($chip_guess != 0) {
  48.         $chip_guess >>= 1;
  49.         $chip_size  <<= 1;
  50.     }
  51.     my $mem_round = (int($mem_size / $chip_size) + 1) * $chip_size;
  52.     return $mem_round;
  53. }
  54.  
  55. #   determine the individual known information
  56. #   NOTICE: forget hw.usermem, it is just (hw.physmem - vm.stats.vm.v_wire_count).
  57. #   NOTICE: forget vm.stats.misc.zero_page_count, it is just the subset of
  58. #           vm.stats.vm.v_free_count which is already pre-zeroed.
  59. my $mem_hw        = &mem_rounded($sysctl->{"hw.physmem"});
  60. my $mem_phys      = $sysctl->{"hw.physmem"};
  61. my $mem_all       = $sysctl->{"vm.stats.vm.v_page_count"}      * $sysctl->{"hw.pagesize"};
  62. my $mem_wire      = $sysctl->{"vm.stats.vm.v_wire_count"}      * $sysctl->{"hw.pagesize"};
  63. my $mem_active    = $sysctl->{"vm.stats.vm.v_active_count"}    * $sysctl->{"hw.pagesize"};
  64. my $mem_inactive  = $sysctl->{"vm.stats.vm.v_inactive_count"}  * $sysctl->{"hw.pagesize"};
  65. my $mem_cache     = $sysctl->{"vm.stats.vm.v_cache_count"}     * $sysctl->{"hw.pagesize"};
  66. my $mem_free      = $sysctl->{"vm.stats.vm.v_free_count"}      * $sysctl->{"hw.pagesize"};
  67.  
  68. #   determine the individual unknown information
  69. my $mem_gap_vm    = $mem_all - ($mem_wire + $mem_active + $mem_inactive + $mem_cache + $mem_free);
  70. my $mem_gap_sys   = $mem_phys - $mem_all;
  71. my $mem_gap_hw    = $mem_hw   - $mem_phys;
  72.  
  73. #   determine logical summary information
  74. my $mem_total = $mem_hw;
  75. my $mem_avail = $mem_inactive + $mem_cache + $mem_free;
  76. my $mem_used  = $mem_total - $mem_avail;
  77.  
  78. #   information annotations
  79. my $info = {
  80.     "mem_wire"     => 'Wired: disabled for paging out',
  81.     "mem_active"   => 'Active: recently referenced',
  82.     "mem_inactive" => 'Inactive: recently not referenced',
  83.     "mem_cache"    => 'Cached: almost avail. for allocation',
  84.     "mem_free"     => 'Free: fully available for allocation',
  85.     "mem_gap_vm"   => 'Memory gap: UNKNOWN',
  86.     "mem_all"      => 'Total real memory managed',
  87.     "mem_gap_sys"  => 'Memory gap: Kernel?!',
  88.     "mem_phys"     => 'Total real memory available',
  89.     "mem_gap_hw"   => 'Memory gap: Segment Mappings?!',
  90.     "mem_hw"       => 'Total real memory installed',
  91.     "mem_used"     => 'Logically used memory',
  92.     "mem_avail"    => 'Logically available memory',
  93.     "mem_total"    => 'Logically total memory',
  94. };
  95.  
  96. #   print system results
  97. printf("SYSTEM MEMORY INFORMATION:\n");
  98. printf("mem_wire:      %12d (%7dMB) [%3d%%] %s\n", $mem_wire,     $mem_wire     / (1024*1024), ($mem_wire     / $mem_all) * 100, $info->{"mem_wire"});
  99. printf("mem_active:  + %12d (%7dMB) [%3d%%] %s\n", $mem_active,   $mem_active   / (1024*1024), ($mem_active   / $mem_all) * 100, $info->{"mem_active"});
  100. printf("mem_inactive:+ %12d (%7dMB) [%3d%%] %s\n", $mem_inactive, $mem_inactive / (1024*1024), ($mem_inactive / $mem_all) * 100, $info->{"mem_inactive"});
  101. printf("mem_cache:   + %12d (%7dMB) [%3d%%] %s\n", $mem_cache,    $mem_cache    / (1024*1024), ($mem_cache    / $mem_all) * 100, $info->{"mem_cache"});
  102. printf("mem_free:    + %12d (%7dMB) [%3d%%] %s\n", $mem_free,     $mem_free     / (1024*1024), ($mem_free     / $mem_all) * 100, $info->{"mem_free"});
  103. printf("mem_gap_vm:  + %12d (%7dMB) [%3d%%] %s\n", $mem_gap_vm,   $mem_gap_vm   / (1024*1024), ($mem_gap_vm   / $mem_all) * 100, $info->{"mem_gap_vm"});
  104. printf("-------------- ------------ ----------- ------\n");
  105. printf("mem_all:     = %12d (%7dMB) [100%%] %s\n", $mem_all,      $mem_all      / (1024*1024), $info->{"mem_all"});
  106. printf("mem_gap_sys: + %12d (%7dMB)        %s\n",  $mem_gap_sys,  $mem_gap_sys  / (1024*1024), $info->{"mem_gap_sys"});
  107. printf("-------------- ------------ -----------\n");
  108. printf("mem_phys:    = %12d (%7dMB)        %s\n",  $mem_phys,     $mem_phys     / (1024*1024), $info->{"mem_phys"});
  109. printf("mem_gap_hw:  + %12d (%7dMB)        %s\n",  $mem_gap_hw,   $mem_gap_hw   / (1024*1024), $info->{"mem_gap_hw"});    
  110. printf("-------------- ------------ -----------\n");
  111. printf("mem_hw:      = %12d (%7dMB)        %s\n",  $mem_hw,       $mem_hw       / (1024*1024), $info->{"mem_hw"});    
  112.  
  113. #   print logical results
  114. printf("\n");
  115. printf("SYSTEM MEMORY SUMMARY:\n");
  116. printf("mem_used:      %12d (%7dMB) [%3d%%] %s\n", $mem_used,  $mem_used  / (1024*1024), ($mem_used  / $mem_total) * 100, $info->{"mem_used"});
  117. printf("mem_avail:   + %12d (%7dMB) [%3d%%] %s\n", $mem_avail, $mem_avail / (1024*1024), ($mem_avail / $mem_total) * 100, $info->{"mem_avail"});
  118. printf("-------------- ------------ ----------- ------\n");
  119. printf("mem_total:   = %12d (%7dMB) [100%%] %s\n", $mem_total, $mem_total / (1024*1024), $info->{"mem_total"});
Add Comment
Please, Sign In to add comment