summaryrefslogtreecommitdiff
path: root/lib/gitlab/memory/watchdog/monitor/unique_memory_growth.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitlab/memory/watchdog/monitor/unique_memory_growth.rb')
-rw-r--r--lib/gitlab/memory/watchdog/monitor/unique_memory_growth.rb47
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/gitlab/memory/watchdog/monitor/unique_memory_growth.rb b/lib/gitlab/memory/watchdog/monitor/unique_memory_growth.rb
new file mode 100644
index 00000000000..2a1512c4cff
--- /dev/null
+++ b/lib/gitlab/memory/watchdog/monitor/unique_memory_growth.rb
@@ -0,0 +1,47 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module Memory
+ class Watchdog
+ module Monitor
+ class UniqueMemoryGrowth
+ attr_reader :max_mem_growth
+
+ def initialize(max_mem_growth:)
+ @max_mem_growth = max_mem_growth
+ end
+
+ def call
+ worker_uss = Gitlab::Metrics::System.memory_usage_uss_pss[:uss]
+ reference_uss = reference_mem[:uss]
+ memory_limit = max_mem_growth * reference_uss
+
+ return { threshold_violated: false, payload: {} } unless worker_uss > memory_limit
+
+ { threshold_violated: true, payload: payload(worker_uss, reference_uss, memory_limit) }
+ end
+
+ private
+
+ def payload(worker_uss, reference_uss, memory_limit)
+ {
+ message: 'memory limit exceeded',
+ memwd_uss_bytes: worker_uss,
+ memwd_ref_uss_bytes: reference_uss,
+ memwd_max_uss_bytes: memory_limit
+ }
+ end
+
+ # On pre-fork systems this would be the primary process memory from which workers fork.
+ # Otherwise it is the current process' memory.
+ #
+ # We initialize this lazily because in the initializer the application may not have
+ # finished booting yet, which would yield an incorrect baseline.
+ def reference_mem
+ @reference_mem ||= Gitlab::Metrics::System.memory_usage_uss_pss(pid: Gitlab::Cluster::PRIMARY_PID)
+ end
+ end
+ end
+ end
+ end
+end