Class: PhusionPassenger::AdminTools::MemoryStats

Class PhusionPassenger::AdminTools::MemoryStats |
In: | lib/phusion_passenger/admin_tools/memory_stats.rb
Parent: | Object

Methods

- apache_processes
- nginx_processes
- passenger_processes
- platform_provides_private_dirty_rss_information?
- root_privileges_required_for_private_dirty_rss?
- should_show_private_dirty_rss?
- sum_memory_usage
- system_ram_usage

Classes and Modules
Class PhusionPassenger::AdminTools::MemoryStats::Process
Public Instance methods
apache_processes()

Returns a list of Apache processes, which may be the empty list if Apache is not running. If the Apache executable name is unknown then nil will be returned.

[ show source ]

        # File lib/phusion_passenger/admin_tools/memory_stats.rb, line 64
    64:         def apache_processes
    65:                 @apache_processes ||= begin
    66:                         if PlatformInfo.httpd
    67:                                 processes = list_processes(:exe => PlatformInfo.httpd)
    68:                                 if processes.empty?
    69:                                         # On some Linux distros, the Apache worker processes
    70:                                         # are called "httpd.worker"
    71:                                         processes = list_processes(:exe => "#{PlatformInfo.httpd}.worker")
    72:                                 end
    73:                                 processes
    74:                         else
    75:                                 nil
    76:                         end
    77:                 end
    78:         end

nginx_processes()

Returns a list of Nginx processes, which may be the empty list if Nginx is not running.

[ show source ]

        # File lib/phusion_passenger/admin_tools/memory_stats.rb, line 82
    82:         def nginx_processes
    83:                 @nginx_processes ||= list_processes(:exe => "nginx")
    84:         end

passenger_processes()

Returns a list of Phusion Passenger processes, which may be the empty list if Phusion Passenger is not running.

[ show source ]

        # File lib/phusion_passenger/admin_tools/memory_stats.rb, line 88
    88:         def passenger_processes
    89:                 @passenger_processes ||= list_processes(:match =>
    90:                         /((^| )Passenger |(^| )Rails:|(^| )Rack:|PassengerHelperAgent|PassengerWatchdog|PassengerLoggingAgent)/)
    91:         end

platform_provides_private_dirty_rss_information?()

[ show source ]

         # File lib/phusion_passenger/admin_tools/memory_stats.rb, line 117
    117:         def platform_provides_private_dirty_rss_information?
    118:                 return ruby_platform =~ /linux/
    119:         end

root_privileges_required_for_private_dirty_rss?()

Returns whether root privileges are required in order to measure private dirty RSS. Only meaningful if #platform_provides_private_dirty_rss_information? returns true.

[ show source ]

         # File lib/phusion_passenger/admin_tools/memory_stats.rb, line 123
    123:         def root_privileges_required_for_private_dirty_rss?
    124:                 all_processes = (apache_processes || []) + nginx_processes + passenger_processes
    125:                 return all_processes.any?{ |p| p.private_dirty_rss.nil? }
    126:         end

should_show_private_dirty_rss?()

[ show source ]

         # File lib/phusion_passenger/admin_tools/memory_stats.rb, line 128
    128:         def should_show_private_dirty_rss?
    129:                 return platform_provides_private_dirty_rss_information? &&
    130:                         (::Process.euid == 0 || root_privileges_required_for_private_dirty_rss?)
    131:         end

sum_memory_usage(processes)

Returns the sum of the memory usages of all given processes. Returns a pair [usage, accurate]. `usage` is the summed memory usage in KB, and `accurate` indicates whether this sum is accurate. This may be false if some process‘s memory usage cannot be determined.

[ show source ]

         # File lib/phusion_passenger/admin_tools/memory_stats.rb, line 97
     97:         def sum_memory_usage(processes)
     98:                 total = 0
     99:                 if should_show_private_dirty_rss?
    100:                         accurate = true
    101:                         processes.each do |p|
    102:                                 if p.private_dirty_rss.is_a?(Numeric)
    103:                                         total += p.private_dirty_rss
    104:                                 else
    105:                                         accurate = true
    106:                                 end
    107:                         end
    108:                         return [total, accurate]
    109:                 else
    110:                         processes.each do |p|
    111:                                 total += p.rss
    112:                         end
    113:                         return [total, true]
    114:                 end
    115:         end

system_ram_usage()

Determine the system‘s RAM usage, not including swap. Returns a tuple [total, used] where both numbers are in KB, or nil if the system‘s RAM usage cannot be determined.

[ show source ]

         # File lib/phusion_passenger/admin_tools/memory_stats.rb, line 136
    136:         def system_ram_usage
    137:                 @total_system_ram ||= begin
    138:                         case ruby_platform
    139:                         when /linux/
    140:                                 free_text = `free -k`
    141:
    142:                                 free_text =~ %r{Mem:(.+)$}
    143:                                 line = $1.strip
    144:                                 total = line.split(/ +/).first.to_i
    145:
    146:                                 free_text =~ %r{buffers/cache:(.+)$}
    147:                                 line = $1.strip
    148:                                 used = line.split(/ +/).first.to_i
    149:
    150:                                 [total, used]
    151:                         when /darwin/
    152:                                 vm_stat = `vm_stat`
    153:                                 vm_stat =~ /page size of (\d+) bytes/
    154:                                 page_size = $1
    155:                                 vm_stat =~ /Pages free: +(\d+)/
    156:                                 free = $1
    157:                                 vm_stat =~ /Pages active: +(\d+)/
    158:                                 active = $1
    159:                                 vm_stat =~ /Pages inactive: +(\d+)/
    160:                                 inactive = $1
    161:                                 vm_stat =~ /Pages wired down: +(\d+)/
    162:                                 wired = $1
    163:
    164:                                 if page_size && free && active && inactive && wired
    165:                                         page_size = page_size.to_i
    166:                                         free = free.to_i * page_size / 1024
    167:                                         active = active.to_i * page_size / 1024
    168:                                         inactive = inactive.to_i * page_size / 1024
    169:                                         wired = wired.to_i * page_size / 1024
    170:
    171:                                         used = active + wired
    172:                                         [free + inactive + used, used]
    173:                                 else
    174:                                         nil
    175:                                 end
    176:                         else
    177:                                 `top` =~ /(\d+)(K|M) Active, (\d+)(K|M) Inact, (\d+)(K|M) Wired,.*?(\d+)(K|M) Free/
    178:                                 if $1 && $2 && $3 && $4 && $5 && $6 && $7 && $8
    179:                                         to_kb = lambda do |number, unit|
    180:                                                 if unit == 'K'
    181:                                                         number.to_i
    182:                                                 else
    183:                                                         number.to_i * 1024
    184:                                                 end
    185:                                         end
    186:
    187:                                         active = to_kb.call($1, $2)
    188:                                         inactive = to_kb.call($3, $4)
    189:                                         wired = to_kb.call($5, $6)
    190:                                         free = to_kb.call($7, $8)
    191:
    192:                                         used = active + wired
    193:                                         [free + inactive + used, used]
    194:                                 else
    195:                                         nil
    196:                                 end
    197:                         end
    198:                 end
    199:         end