Sha256: f24d118b751a0aed92c048857b96ef89688ecd737c918c27c42788b932a344c2

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

# encoding: utf-8

require 'one_apm/agent/sampler'

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

        named :cpu

        def initialize
          @processor_count = OneApm::Agent::SystemInfo.num_logical_processors
          poll
        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

        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

            if systemtime >= 0
              OneApm::Agent.record_metric("CPU/System Time", systemtime)
              OneApm::Agent.record_metric("CPU/System/Utilization", systemtime / (elapsed * @processor_count))
            end
            if usertime >= 0
              OneApm::Agent.record_metric("CPU/User Time", usertime)
              OneApm::Agent.record_metric("CPU/User/Utilization", usertime / (elapsed * @processor_count))
            end
          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
oneapm_rpm-1.1.2 lib/one_apm/agent/samplers/cpu_sampler.rb
oneapm_rpm-1.1.1 lib/one_apm/agent/samplers/cpu_sampler.rb
oneapm_rpm-1.1.0 lib/one_apm/agent/samplers/cpu_sampler.rb