Sha256: f5c274733c6b798edf00b80f5e9982821311b0e13c4dd5e1e3dab8bc92440626

Contents?: true

Size: 1.49 KB

Versions: 23

Compression:

Stored size: 1.49 KB

Contents

module NewRelic
  module Agent
    module Samplers
  class CpuSampler < NewRelic::Agent::Sampler
    attr_reader :last_time
    def initialize
      super :cpu
      poll
    end
    
    def user_util_stats
      stats_engine.get_stats_no_scope("CPU/User/Utilization")
    end
    def system_util_stats
      stats_engine.get_stats_no_scope("CPU/System/Utilization")
    end
    def usertime_stats
      stats_engine.get_stats_no_scope("CPU/User Time")
    end
    def systemtime_stats
      stats_engine.get_stats_no_scope("CPU/System Time")
    end
    
    def self.supported_on_this_platform?
      not defined?(JRuby)
    end
    
    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
  end
end
end
end

Version data entries

23 entries across 23 versions & 2 rubygems

Version Path
onyx_newrelic_rpm-2.12.5 lib/new_relic/agent/samplers/cpu_sampler.rb
newrelic_rpm-2.13.0.beta5 lib/new_relic/agent/samplers/cpu_sampler.rb
newrelic_rpm-2.13.0.beta4 lib/new_relic/agent/samplers/cpu_sampler.rb
newrelic_rpm-2.13.0.beta3 lib/new_relic/agent/samplers/cpu_sampler.rb
newrelic_rpm-2.12.3 lib/new_relic/agent/samplers/cpu_sampler.rb
newrelic_rpm-2.12.2 lib/new_relic/agent/samplers/cpu_sampler.rb
newrelic_rpm-2.12.2.beta2 lib/new_relic/agent/samplers/cpu_sampler.rb
newrelic_rpm-2.12.2.beta lib/new_relic/agent/samplers/cpu_sampler.rb
newrelic_rpm-2.12.1 lib/new_relic/agent/samplers/cpu_sampler.rb
newrelic_rpm-2.12.1.alpha lib/new_relic/agent/samplers/cpu_sampler.rb
newrelic_rpm-2.12.0 lib/new_relic/agent/samplers/cpu_sampler.rb
newrelic_rpm-2.11.3 lib/new_relic/agent/samplers/cpu_sampler.rb
newrelic_rpm-2.11.2 lib/new_relic/agent/samplers/cpu_sampler.rb
newrelic_rpm-2.11.2.beta2 lib/new_relic/agent/samplers/cpu_sampler.rb
newrelic_rpm-2.11.2.beta lib/new_relic/agent/samplers/cpu_sampler.rb
newrelic_rpm-2.11.1 lib/new_relic/agent/samplers/cpu_sampler.rb
newrelic_rpm-2.11.0.beta2 lib/new_relic/agent/samplers/cpu_sampler.rb
newrelic_rpm-2.11.0.beta lib/new_relic/agent/samplers/cpu_sampler.rb
newrelic_rpm-2.10.8 lib/new_relic/agent/samplers/cpu_sampler.rb
newrelic_rpm-2.10.6 lib/new_relic/agent/samplers/cpu_sampler.rb