Class: NewRelic::Agent::Samplers::MemorySampler

Inherits:
NewRelic::Agent::Sampler show all
Defined in:
lib/new_relic/agent/samplers/memory_sampler.rb

Defined Under Namespace

Classes: Base, JavaHeapSampler, ProcStatus, ShellPS

Instance Attribute Summary collapse

Attributes inherited from NewRelic::Agent::Sampler

#id, #stats_engine

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMemorySampler

Returns a new instance of MemorySampler.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/new_relic/agent/samplers/memory_sampler.rb', line 6

def initialize
  super :memory
  
  # macos, linux, solaris
  if defined? JRuby
    @sampler = JavaHeapSampler.new
  elsif platform =~ /linux/
    @sampler = ProcStatus.new
    if !@sampler.can_run?
      NewRelic::Agent.instance.log.warn "Error attempting to use /proc/$$/status file for reading memory. Using ps command instead."
      @sampler = ShellPS.new("ps -o rsz")
    else
      NewRelic::Agent.instance.log.info "Using /proc/$$/status for reading process memory."
    end
  elsif platform =~ /darwin9/ # 10.5
    @sampler = ShellPS.new("ps -o rsz")
  elsif platform =~ /darwin10/ # 10.6
    @sampler = ShellPS.new("ps -o rss")
  elsif platform =~ /freebsd/
    @sampler = ShellPS.new("ps -o rss")
  elsif platform =~ /solaris/
    @sampler = ShellPS.new("/usr/bin/ps -o rss -p")
  end
  
  raise "Unsupported platform for getting memory: #{platform}" if @sampler.nil?
  raise "Unable to run #{@sampler}" unless @sampler.can_run?
end

Instance Attribute Details

#samplerObject

Returns the value of attribute sampler.



4
5
6
# File 'lib/new_relic/agent/samplers/memory_sampler.rb', line 4

def sampler
  @sampler
end

Class Method Details

.platformObject



37
38
39
40
41
42
43
# File 'lib/new_relic/agent/samplers/memory_sampler.rb', line 37

def self.platform
  if RUBY_PLATFORM =~ /java/
    %x[uname -s].downcase
  else
    RUBY_PLATFORM.downcase
  end
end

.supported_on_this_platform?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/new_relic/agent/samplers/memory_sampler.rb', line 33

def self.supported_on_this_platform?
  defined?(JRuby) or platform =~ /linux|darwin9|darwin10|freebsd|solaris/
end

Instance Method Details

#platformObject



44
45
46
# File 'lib/new_relic/agent/samplers/memory_sampler.rb', line 44

def platform
  NewRelic::Agent::Samplers::MemorySampler.platform
end

#pollObject



51
52
53
54
55
# File 'lib/new_relic/agent/samplers/memory_sampler.rb', line 51

def poll
  sample = @sampler.get_sample
  stats.record_data_point sample if sample
  stats
end

#statsObject



48
49
50
# File 'lib/new_relic/agent/samplers/memory_sampler.rb', line 48

def stats
  stats_engine.get_stats("Memory/Physical", false) 
end