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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NewRelic::Agent::Sampler

inherited, sampler_classes

Constructor Details

#initializeCpuSampler

Returns a new instance of CpuSampler.



12
13
14
15
16
17
18
19
20
# File 'lib/new_relic/agent/samplers/cpu_sampler.rb', line 12

def initialize
  super :cpu
  @processor_count = NewRelic::Agent::SystemInfo.processor_count
  if @processor_count.nil?
    NewRelic::Agent.logger.warn("Failed to determine processor count, assuming 1")
    @processor_count = 1
  end
  poll
end

Instance Attribute Details

#last_timeObject (readonly)

Returns the value of attribute last_time.



11
12
13
# File 'lib/new_relic/agent/samplers/cpu_sampler.rb', line 11

def last_time
  @last_time
end

Class Method Details

.supported_on_this_platform?Boolean

Returns:

  • (Boolean)


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

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/new_relic/agent/samplers/cpu_sampler.rb', line 44

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

    usertime = t.utime - @last_utime
    systemtime = t.stime - @last_stime

    record_systemtime(systemtime) if systemtime >= 0
    record_usertime(usertime) if usertime >= 0

    # Calculate the true utilization by taking cpu times and dividing by
    # elapsed time X processor_count.

    record_user_util(usertime / (elapsed * @processor_count))
    record_system_util(systemtime / (elapsed * @processor_count))
  end
  @last_utime = t.utime
  @last_stime = t.stime
  @last_time = now
end

#record_system_util(value) ⇒ Object



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

def record_system_util(value)
  NewRelic::Agent.record_metric("CPU/System/Utilization", value)
end

#record_systemtime(value) ⇒ Object



34
35
36
# File 'lib/new_relic/agent/samplers/cpu_sampler.rb', line 34

def record_systemtime(value)
  NewRelic::Agent.record_metric("CPU/System Time", value)
end

#record_user_util(value) ⇒ Object



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

def record_user_util(value)
  NewRelic::Agent.record_metric("CPU/User/Utilization", value)
end

#record_usertime(value) ⇒ Object



30
31
32
# File 'lib/new_relic/agent/samplers/cpu_sampler.rb', line 30

def record_usertime(value)
  NewRelic::Agent.record_metric("CPU/User Time", value)
end