Sha256: 877fccab86d0440d437b31be01a4c3268bcb01841d968d0f7eac6a2c960a8415

Contents?: true

Size: 1.96 KB

Versions: 3

Compression:

Stored size: 1.96 KB

Contents

# encoding: utf-8
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.

require 'new_relic/agent/sampler'

module NewRelic
  module Agent
    module Samplers
      class CpuSampler < NewRelic::Agent::Sampler
        attr_reader :last_time
        def initialize
          super :cpu
          poll
        end

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

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

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

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

        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

        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

            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 num_processors.

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
newrelic_rpm-3.6.0.83 lib/new_relic/agent/samplers/cpu_sampler.rb
newrelic_rpm-3.6.0.78 lib/new_relic/agent/samplers/cpu_sampler.rb
newrelic_rpm-3.6.0.74.beta lib/new_relic/agent/samplers/cpu_sampler.rb