class NewRelic::Agent::Samplers::MemorySampler

Attributes

sampler[RW]

Public Class Methods

new() click to toggle source
# File lib/new_relic/agent/samplers/memory_sampler.rb, line 16
def initialize
  @sampler = nil

  # macos, linux, solaris
  if defined? JRuby
    @sampler = JavaHeapSampler.new
  elsif platform.include?('linux')
    @sampler = ProcStatus.new
    if !@sampler.can_run?
      ::NewRelic::Agent.logger.debug("Error attempting to use /proc/#{$$}/status file for reading memory. Using ps command instead.")
      @sampler = ShellPS.new('ps -o rsz')
    else
      ::NewRelic::Agent.logger.debug("Using /proc/#{$$}/status for reading process memory.")
    end
  elsif platform.include?('darwin9') # 10.5
    @sampler = ShellPS.new('ps -o rsz')
  elsif /darwin(1|2)\d+/.match?(platform) # >= 10.6
    @sampler = ShellPS.new('ps -o rss')
  elsif platform.include?('freebsd')
    @sampler = ShellPS.new('ps -o rss')
  elsif platform.include?('solaris')
    @sampler = ShellPS.new('/usr/bin/ps -o rss -p')
  end

  raise Unsupported, "Unsupported platform for getting memory: #{platform}" if @sampler.nil?
  raise Unsupported, "Unable to run #{@sampler}" unless @sampler.can_run?
end
platform() click to toggle source
# File lib/new_relic/agent/samplers/memory_sampler.rb, line 48
def self.platform
  if RUBY_PLATFORM.include?('java')
    begin
      NewRelic::Helper.run_command('uname -s').downcase
    rescue NewRelic::CommandRunFailedError, NewRelic::CommandExecutableNotFoundError
      'unknown'
    end
  else
    RUBY_PLATFORM.downcase
  end
end
supported_on_this_platform?() click to toggle source
# File lib/new_relic/agent/samplers/memory_sampler.rb, line 44
def self.supported_on_this_platform?
  defined?(JRuby) or platform =~ /linux|darwin|freebsd|solaris/
end

Public Instance Methods

platform() click to toggle source
# File lib/new_relic/agent/samplers/memory_sampler.rb, line 60
def platform
  NewRelic::Agent::Samplers::MemorySampler.platform
end
poll() click to toggle source
# File lib/new_relic/agent/samplers/memory_sampler.rb, line 64
def poll
  sample = @sampler.get_sample
  if sample
    NewRelic::Agent.record_metric('Memory/Physical', sample)
  end
end