Skip to content

Instantly share code, notes, and snippets.

@NickLaMuro
Created December 6, 2017 16:27
Show Gist options
  • Save NickLaMuro/2d494d9fc0050f4e705ebab58437d108 to your computer and use it in GitHub Desktop.
Save NickLaMuro/2d494d9fc0050f4e705ebab58437d108 to your computer and use it in GitHub Desktop.
ManageIQ MiqServer mem log initializer patch
# Make sure the normal MiqServer is autoloaded before monkey patching it
MiqServer
require 'objspace'
class MiqServer < ApplicationRecord
alias _old_monitor_poll monitor_poll
def monitor_poll
begin
@memory_log_diagnostic_count ||= 0
@memory_dump_diagnostic_count ||= 0
@memory_log_diagnostic_count += 1
@memory_dump_diagnostic_count += 1
# Log every minute if monitor poll, assuming is set to 5.seconds
if @memory_log_diagnostic_count % 12 == 0
@memory_log_diagnostic_count = 0
mem_info = Sys::ProcTable::Smaps.new(Process.pid, MiqSystem.readfile_async("/proc/#{Process.pid}/smaps"))
# types = Hash.new { |h, k| h[k] = 0 }
# ObjectSpace.each_object do |obj|
# types[obj.class.name] += 1
# end.sort_by { |_k, v| -v }.take(50)
_log.info "Memory Info XXXX => {" \
"RSS: #{mem_info.rss}, " \
"PSS: #{mem_info.pss}, " \
"thread_count: #{Thread.list.length}, " \
"gc_stats: #{GC.stat.inspect}, " \
"objspace_count: #{ObjectSpace.count_objects.inspect}}"
# _log.info("Ruby Object Usage: #{types.inspect}")
end
# log every 3 hours, assuming server is set to monitor poll every 5.seconds
if @memory_dump_diagnostic_count % 2160 == 0
@memory_dump_diagnostic_count = 0
io = File.open("#{Rails.root}/tmp/miq_server_#{Time.now.to_i}_#{Process.pid}.dump", "w")
ObjectSpace.dump_all(output: io)
io.close
end
rescue => e
# no-op if we have an error
_log.info "XXXX ERR: #{e.message}"
end
_old_monitor_poll
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment