Sha256: 145675fb3298401573fea6c413e46a70f3154925fcfe4a7fd77ee12dfd819466

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

Stored size: 1.43 KB

Contents

module NewRelic::Agent::Samplers
  class CpuSampler < NewRelic::Agent::Sampler
    attr_reader :last_time
    def initialize
      super :cpu
      poll
    end
    def user_util_stats
      @userutil ||= stats_engine.get_stats("CPU/User/Utilization", false)
    end
    def system_util_stats
      @systemutil ||= stats_engine.get_stats("CPU/System/Utilization", false)
    end
    def usertime_stats
      @usertime ||= stats_engine.get_stats("CPU/User Time", false)
    end
    def systemtime_stats
      @systemtime ||= stats_engine.get_stats("CPU/System Time", false)
    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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
newrelic_rpm-2.9.2 lib/new_relic/agent/samplers/cpu_sampler.rb
newrelic_rpm-2.9.3 lib/new_relic/agent/samplers/cpu_sampler.rb