BigW Consortium Gitlab

system.rb 1.69 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
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+)/)

          if match and match[1]
            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
          Process.
38
            clock_gettime(Process::CLOCK_THREAD_CPUTIME_ID, :millisecond)
39 40 41
        end
      else
        def self.cpu_time
42
          Process.
43
            clock_gettime(Process::CLOCK_PROCESS_CPUTIME_ID, :millisecond)
44 45
        end
      end
46 47 48 49 50

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

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