Sha256: 6bc6a5d83f12aac73e79f76df1ba2c288ebd7462ce269b9829e52e8a497cb5ee

Contents?: true

Size: 1.64 KB

Versions: 11

Compression:

Stored size: 1.64 KB

Contents

# encoding: utf-8

require 'one_apm/collector/sampler'

module OneApm
  module Collector
    module Samplers
      class CpuSampler < OneApm::Collector::Sampler
        attr_reader :last_time

        named :cpu

        def initialize
          @processor_count = OneApm::Support::SystemInfo.num_logical_processors
          poll
        end

        def set_poller poller
          @poller = poller
        end
        
        def poll
          now = Time.now
          @poller.pid = Process.pid if @poller
          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

            if systemtime >= 0
              OneApm::Manager.record_metric("CPU/System Time", systemtime)
              OneApm::Manager.record_metric("CPU/System/Utilization", systemtime / (elapsed * @processor_count))
            end
            if usertime >= 0
              cpu_utilization = @poller ? @poller.cpu_utilization : (usertime / (elapsed * @processor_count))
              OneApm::Manager.record_metric("CPU/User Time", usertime)
              OneApm::Manager.record_metric("CPU/User/Utilization", cpu_utilization)
            end
          end
          @last_utime = t.utime
          @last_stime = t.stime
          @last_time = now
        end

        def self.supported_on_this_platform?
          # Process.times on JRuby < 1.7.0 reports wall clock elapsed time, not actual cpu time used
          defined?(JRuby) ? (JRUBY_VERSION >= '1.7.0') : true
        end

      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
oneapm_rpm-1.4.2 lib/one_apm/collector/samplers/cpu_sampler.rb
oneapm_rpm-1.4.1 lib/one_apm/collector/samplers/cpu_sampler.rb
oneapm_rpm-1.4.0 lib/one_apm/collector/samplers/cpu_sampler.rb
oneapm_rpm-1.3.7 lib/one_apm/collector/samplers/cpu_sampler.rb
oneapm_rpm-1.3.7.rc1 lib/one_apm/collector/samplers/cpu_sampler.rb
oneapm_rpm-1.3.6 lib/one_apm/collector/samplers/cpu_sampler.rb
oneapm_rpm-1.3.6.rc1 lib/one_apm/collector/samplers/cpu_sampler.rb
oneapm_rpm-1.3.5 lib/one_apm/collector/samplers/cpu_sampler.rb
oneapm_rpm-1.3.5.rc1 lib/one_apm/collector/samplers/cpu_sampler.rb
oneapm_rpm-1.3.4 lib/one_apm/collector/samplers/cpu_sampler.rb
oneapm_rpm-1.3.4.rc1 lib/one_apm/collector/samplers/cpu_sampler.rb