Module: Gitlab::Metrics::System

Extended by:
System
Included in:
System
Defined in:
lib/gitlab/metrics/system.rb

Overview

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.

Constant Summary collapse

PROC_STAT_PATH =
'/proc/self/stat'
PROC_STATUS_PATH =
'/proc/self/status'
PROC_SMAPS_ROLLUP_PATH =
'/proc/self/smaps_rollup'
PROC_LIMITS_PATH =
'/proc/self/limits'
PROC_FD_GLOB =
'/proc/self/fd/*'
PRIVATE_PAGES_PATTERN =
/^(Private_Clean|Private_Dirty|Private_Hugetlb):\s+(?<value>\d+)/.freeze
PSS_PATTERN =
/^Pss:\s+(?<value>\d+)/.freeze
RSS_PATTERN =
/VmRSS:\s+(?<value>\d+)/.freeze
MAX_OPEN_FILES_PATTERN =
/Max open files\s*(?<value>\d+)/.freeze

Instance Method Summary collapse

Instance Method Details

#cpu_timeObject


56
57
58
# File 'lib/gitlab/metrics/system.rb', line 56

def cpu_time
  Process.clock_gettime(Process::CLOCK_PROCESS_CPUTIME_ID, :float_second)
end

#file_descriptor_countObject


48
49
50
# File 'lib/gitlab/metrics/system.rb', line 48

def file_descriptor_count
  Dir.glob(PROC_FD_GLOB).length
end

#max_open_file_descriptorsObject


52
53
54
# File 'lib/gitlab/metrics/system.rb', line 52

def max_open_file_descriptors
  sum_matches(PROC_LIMITS_PATH, max_fds: MAX_OPEN_FILES_PATTERN)[:max_fds]
end

#memory_usage_rssObject

Returns the current process' RSS (resident set size) in bytes.


38
39
40
# File 'lib/gitlab/metrics/system.rb', line 38

def memory_usage_rss
  sum_matches(PROC_STATUS_PATH, rss: RSS_PATTERN)[:rss].kilobytes
end

#memory_usage_uss_pssObject

Returns the current process' USS/PSS (unique/proportional set size) in bytes.


43
44
45
46
# File 'lib/gitlab/metrics/system.rb', line 43

def memory_usage_uss_pss
  sum_matches(PROC_SMAPS_ROLLUP_PATH, uss: PRIVATE_PAGES_PATTERN, pss: PSS_PATTERN)
    .transform_values(&:kilobytes)
end

#monotonic_timeObject

Returns the current monotonic clock time as seconds with microseconds precision.

Returns the time as a Float.


70
71
72
# File 'lib/gitlab/metrics/system.rb', line 70

def monotonic_time
  Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_second)
end

#process_runtime_elapsed_secondsObject

Returns the total time the current process has been running in seconds.


90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gitlab/metrics/system.rb', line 90

def process_runtime_elapsed_seconds
  # Entry 22 (1-indexed) contains the process `starttime`, see:
  # https://man7.org/linux/man-pages/man5/proc.5.html
  #
  # This value is a fixed timestamp in clock ticks.
  # To obtain an elapsed time in seconds, we divide by the number
  # of ticks per second and subtract from the system uptime.
  start_time_ticks = proc_stat_entries[21].to_f
  clock_ticks_per_second = Etc.sysconf(Etc::SC_CLK_TCK)
  uptime - (start_time_ticks / clock_ticks_per_second)
end

#real_time(precision = :float_second) ⇒ Object

Returns the current real time in a given precision.

Returns the time as a Float for precision = :float_second.


63
64
65
# File 'lib/gitlab/metrics/system.rb', line 63

def real_time(precision = :float_second)
  Process.clock_gettime(Process::CLOCK_REALTIME, precision)
end

#summaryObject


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gitlab/metrics/system.rb', line 23

def summary
  proportional_mem = memory_usage_uss_pss
  {
    version: RUBY_DESCRIPTION,
    gc_stat: GC.stat,
    memory_rss: memory_usage_rss,
    memory_uss: proportional_mem[:uss],
    memory_pss: proportional_mem[:pss],
    time_cputime: cpu_time,
    time_realtime: real_time,
    time_monotonic: monotonic_time
  }
end

#thread_cpu_duration(start_time) ⇒ Object


82
83
84
85
86
87
# File 'lib/gitlab/metrics/system.rb', line 82

def thread_cpu_duration(start_time)
  end_time = thread_cpu_time
  return unless start_time && end_time

  end_time - start_time
end

#thread_cpu_timeObject


74
75
76
77
78
79
80
# File 'lib/gitlab/metrics/system.rb', line 74

def thread_cpu_time
  # Not all OS kernels are supporting `Process::CLOCK_THREAD_CPUTIME_ID`
  # Refer: https://gitlab.com/gitlab-org/gitlab/issues/30567#note_221765627
  return unless defined?(Process::CLOCK_THREAD_CPUTIME_ID)

  Process.clock_gettime(Process::CLOCK_THREAD_CPUTIME_ID, :float_second)
end