BigW Consortium Gitlab

system.rb 1.69 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
module Gitlab
  module Metrics
    # Module for gathering system/process statistics such as the memory usage.
    #
    # This module relies on the /proc filesystem being available. If /proc is
    # not available the methods of this module will be stubbed.
    module System
      if File.exist?('/proc')
        # Returns the current process' memory usage in bytes.
        def self.memory_usage
          mem   = 0
          match = File.read('/proc/self/status').match(/VmRSS:\s+(\d+)/)

Douwe Maan committed
14
          if match && match[1]
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
            mem = match[1].to_f * 1024
          end

          mem
        end

        def self.file_descriptor_count
          Dir.glob('/proc/self/fd/*').length
        end
      else
        def self.memory_usage
          0.0
        end

        def self.file_descriptor_count
          0
        end
      end
33 34 35 36

      # THREAD_CPUTIME is not supported on OS X
      if Process.const_defined?(:CLOCK_THREAD_CPUTIME_ID)
        def self.cpu_time
37 38
          Process
            .clock_gettime(Process::CLOCK_THREAD_CPUTIME_ID, :millisecond)
39 40 41
        end
      else
        def self.cpu_time
42 43
          Process
            .clock_gettime(Process::CLOCK_PROCESS_CPUTIME_ID, :millisecond)
44 45
        end
      end
46 47 48

      # Returns the current real time in a given precision.
      #
49
      # Returns the time as a Fixnum.
50
      def self.real_time(precision = :millisecond)
51
        Process.clock_gettime(Process::CLOCK_REALTIME, precision)
52 53 54 55
      end

      # Returns the current monotonic clock time in a given precision.
      #
56
      # Returns the time as a Fixnum.
57
      def self.monotonic_time(precision = :millisecond)
58
        Process.clock_gettime(Process::CLOCK_MONOTONIC, precision)
59
      end
60 61 62
    end
  end
end