Class: NewRelic::Agent::Samplers::CpuSampler

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

Instance Attribute Summary collapse

Attributes inherited from NewRelic::Agent::Sampler

#id, #stats_engine

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NewRelic::Agent::Sampler

inherited, sampler_classes, use_harvest_sampler?

Constructor Details

#initializeCpuSampler

Returns a new instance of CpuSampler.



8
9
10
11
# File 'lib/new_relic/agent/samplers/cpu_sampler.rb', line 8

def initialize
  super :cpu
  poll
end

Instance Attribute Details

#last_timeObject (readonly)

Returns the value of attribute last_time.



7
8
9
# File 'lib/new_relic/agent/samplers/cpu_sampler.rb', line 7

def last_time
  @last_time
end

Class Method Details

.supported_on_this_platform?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/new_relic/agent/samplers/cpu_sampler.rb', line 26

def self.supported_on_this_platform?
  # Process.times on JRuby reports wall clock elapsed time,
  # not actual cpu time used, so we cannot use this sampler there.
  not defined?(JRuby)
end

Instance Method Details

#pollObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/new_relic/agent/samplers/cpu_sampler.rb', line 32

def poll
  now = Time.now
  t = Process.times
  if @last_time
    elapsed = now - @last_time
    return if elapsed < 1 # Causing some kind of math underflow
    num_processors = NewRelic::Control.instance.local_env.processors || 1
    usertime = t.utime - @last_utime
    systemtime = t.stime - @last_stime

    systemtime_stats.record_data_point(systemtime) if systemtime >= 0
    usertime_stats.record_data_point(usertime) if usertime >= 0

    # Calculate the true utilization by taking cpu times and dividing by
    # elapsed time X num_processors.
    user_util_stats.record_data_point usertime / (elapsed * num_processors)
    system_util_stats.record_data_point systemtime / (elapsed * num_processors)
  end
  @last_utime = t.utime
  @last_stime = t.stime
  @last_time = now
end

#system_util_statsObject



16
17
18
# File 'lib/new_relic/agent/samplers/cpu_sampler.rb', line 16

def system_util_stats
  stats_engine.get_stats_no_scope("CPU/System/Utilization")
end

#systemtime_statsObject



22
23
24
# File 'lib/new_relic/agent/samplers/cpu_sampler.rb', line 22

def systemtime_stats
  stats_engine.get_stats_no_scope("CPU/System Time")
end

#user_util_statsObject



13
14
15
# File 'lib/new_relic/agent/samplers/cpu_sampler.rb', line 13

def user_util_stats
  stats_engine.get_stats_no_scope("CPU/User/Utilization")
end

#usertime_statsObject



19
20
21
# File 'lib/new_relic/agent/samplers/cpu_sampler.rb', line 19

def usertime_stats
  stats_engine.get_stats_no_scope("CPU/User Time")
end