diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-06-16 21:25:58 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-06-16 21:25:58 +0300 |
commit | a5f4bba440d7f9ea47046a0a561d49adf0a1e6d4 (patch) | |
tree | fb69158581673816a8cd895f9d352dcb3c678b1e /lib/peek | |
parent | d16b2e8639e99961de6ddc93909f3bb5c1445ba1 (diff) |
Add latest changes from gitlab-org/gitlab@14-0-stable-eev14.0.0-rc42
Diffstat (limited to 'lib/peek')
-rw-r--r-- | lib/peek/views/active_record.rb | 19 | ||||
-rw-r--r-- | lib/peek/views/memory.rb | 76 |
2 files changed, 92 insertions, 3 deletions
diff --git a/lib/peek/views/active_record.rb b/lib/peek/views/active_record.rb index 774e4768597..8e1200338c2 100644 --- a/lib/peek/views/active_record.rb +++ b/lib/peek/views/active_record.rb @@ -43,6 +43,11 @@ module Peek count[item[:transaction]] ||= 0 count[item[:transaction]] += 1 end + + if ::Gitlab::Database::LoadBalancing.enable? + count[item[:db_role]] ||= 0 + count[item[:db_role]] += 1 + end end def setup_subscribers @@ -60,11 +65,19 @@ module Peek sql: data[:sql].strip, backtrace: Gitlab::BacktraceCleaner.clean_backtrace(caller), cached: data[:cached] ? 'Cached' : '', - transaction: data[:connection].transaction_open? ? 'In a transaction' : '' + transaction: data[:connection].transaction_open? ? 'In a transaction' : '', + db_role: db_role(data) } end + + def db_role(data) + return unless ::Gitlab::Database::LoadBalancing.enable? + + role = ::Gitlab::Database::LoadBalancing.db_role_for_connection(data[:connection]) || + ::Gitlab::Database::LoadBalancing::ROLE_UNKNOWN + + role.to_s.capitalize + end end end end - -Peek::Views::ActiveRecord.prepend_mod_with('Peek::Views::ActiveRecord') diff --git a/lib/peek/views/memory.rb b/lib/peek/views/memory.rb new file mode 100644 index 00000000000..399474dedf1 --- /dev/null +++ b/lib/peek/views/memory.rb @@ -0,0 +1,76 @@ +# frozen_string_literal: true + +module Peek + module Views + class Memory < View + MEM_TOTAL_LABEL = 'Total' + MEM_OBJECTS_LABEL = 'Objects allocated' + MEM_MALLOCS_LABEL = 'Allocator calls' + MEM_BYTES_LABEL = 'Large allocations' + + def initialize(options = {}) + super + + @thread_memory = {} + end + + def results + return thread_memory if thread_memory.empty? + + { + calls: byte_string(thread_memory[:mem_total_bytes]), + summary: { + MEM_OBJECTS_LABEL => number_string(thread_memory[:mem_objects]), + MEM_MALLOCS_LABEL => number_string(thread_memory[:mem_mallocs]), + MEM_BYTES_LABEL => byte_string(thread_memory[:mem_bytes]) + }, + details: [ + { + item_header: MEM_TOTAL_LABEL, + item_content: "Total memory use of this request. This includes both occupancy of existing heap slots " \ + "as well as newly allocated memory due to large objects. Not adjusted for freed memory. " \ + "Lower is better." + }, + { + item_header: MEM_OBJECTS_LABEL, + item_content: "Total number of objects allocated by the Ruby VM during this request. " \ + "Not adjusted for objects that were freed again. Lower is better." + }, + { + item_header: MEM_MALLOCS_LABEL, + item_content: "Total number of times Ruby had to call `malloc`, the C memory allocator. " \ + "This is necessary for objects that are too large to fit into a 40 Byte slot in Ruby's managed heap. " \ + "Lower is better." + }, + { + item_header: MEM_BYTES_LABEL, + item_content: "Memory allocated for objects that did not fit into a heap slot. " \ + "Not adjusted for memory that was freed again. Lower is better." + } + ] + } + end + + private + + attr_reader :thread_memory + + def setup_subscribers + subscribe 'process_action.action_controller' do + # Ensure that Peek will see memory instrumentation in `results` by triggering it when + # a request is done processing. Peek itself hooks into the same notification: + # https://github.com/peek/peek/blob/master/lib/peek/railtie.rb + Gitlab::InstrumentationHelper.instrument_thread_memory_allocations(thread_memory) + end + end + + def byte_string(bytes) + ActiveSupport::NumberHelper.number_to_human_size(bytes) + end + + def number_string(num) + ActiveSupport::NumberHelper.number_to_human(num, units: { thousand: 'k', million: 'M', billion: 'B' }) + end + end + end +end |