Sha256: 3001085daf2ff849ab070d87bad369c496e33cecb90c1cf0ec761f74d464e6ac

Contents?: true

Size: 1.37 KB

Versions: 29

Compression:

Stored size: 1.37 KB

Contents

# encoding: utf-8
require 'metriks'
require 'singleton'

class GCStats
  include Singleton

  # RUBY 1.9.X-Implementation
  module Yarv
    def enable
      GC::Profiler.enable
    end

    def clear
      GC::Profiler.clear
    end

    def gather
      count_collections
      count_allocations
      count_objects
      Metriks.timer('gc_stats.total_time').update(GC::Profiler.total_time)
    end

    def count_collections
      Metriks.histogram('gc_stats.collections').update(GC.count)
    end

    def count_allocations
      allocated_size = GC.respond_to?(:malloc_allocated_size) ? GC.malloc_allocated_size / 1000.0 : 0
      Metriks.histogram('gc_stats.allocated').update(allocated_size)
    end

    def count_objects
      objects = ObjectSpace.count_objects
      objects = objects[:TOTAL] - objects[:FREE]
      Metriks.histogram('gc_stats.objects').update(objects)
    end
  end

  if RUBY_ENGINE == 'ruby' and RUBY_VERSION =~ /^1\.9/
    extend Yarv
  end

  def self.available?
    respond_to?(:gather)
  end

  def self.start!
    if available?
      enable
      $log.info("gc:stats", enabled: true)

      EventMachine.next_tick do
        EventMachine::PeriodicTimer.new(60*60) do
          GC.start
        end

        EventMachine::PeriodicTimer.new(60) do
          gather
          clear
        end
      end
    else
      $log.info("gc:stats", enabled: false)
    end
  end
end

Version data entries

29 entries across 29 versions & 2 rubygems

Version Path
liquid-ext-1.2.6 lib/liquid/gc_stats.rb
liquid-ext-1.2.5 lib/liquid/gc_stats.rb
liquid-ext-1.2.4 lib/liquid/gc_stats.rb
liquid-ext-1.2.2 lib/liquid/gc_stats.rb
liquid-ext-1.2.1 lib/liquid/gc_stats.rb
liquid-ext-1.2.0 lib/liquid/gc_stats.rb
liquid-ext-1.1.1 lib/liquid/gc_stats.rb
liquid-ext-1.1.0 lib/liquid/gc_stats.rb
liquid-ext-1.0.2 lib/liquid/gc_stats.rb
liquid-ext-1.0.1 lib/liquid/gc_stats.rb
liquid-ext-1.0.0 lib/liquid/gc_stats.rb
madvertise-ext-0.9.7 lib/madvertise/gc_stats.rb
madvertise-ext-0.9.6 lib/madvertise/gc_stats.rb
madvertise-ext-0.9.5 lib/madvertise/gc_stats.rb
madvertise-ext-0.9.4 lib/madvertise/gc_stats.rb
madvertise-ext-0.9.3 lib/madvertise/gc_stats.rb
madvertise-ext-0.9.2 lib/madvertise/gc_stats.rb
madvertise-ext-0.9.1 lib/madvertise/gc_stats.rb
madvertise-ext-0.9.0 lib/madvertise/gc_stats.rb
madvertise-ext-0.8.0 lib/madvertise/gc_stats.rb