Class: Contrast::Utils::HeapDumpUtil

Inherits:
Agent::WorkerThread show all
Extended by:
ClassMethods, Components::Logger::InstanceMethods
Includes:
Components::Logger::InstanceMethods, HeadDumpExtend
Defined in:
lib/contrast/utils/heap_dump_util.rb

Overview

Implementation of a heap dump util to automate generation

Constant Summary collapse

LOG_ERROR_DUMPS =
'Unable to generate heap dumps'
FILE_WRITE_FLAGS =
'w'

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Components::Logger::InstanceMethods

cef_logger, logger

Methods included from HeadDumpExtend

#capture_heap_dump, #log_enabled_warning

Methods inherited from Agent::WorkerThread

#attempt_to_start?, #clean_properties, #initialize, #running?, #stop!

Constructor Details

This class inherits a constructor from Contrast::Agent::WorkerThread

Class Method Details

.controlObject



27
28
29
# File 'lib/contrast/utils/heap_dump_util.rb', line 27

def control
  heap_dump_control
end

.enabled?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/contrast/utils/heap_dump_util.rb', line 23

def enabled?
  heap_dump_enabled?
end

Instance Method Details

#snapshot_heap(dir, clean) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/contrast/utils/heap_dump_util.rb', line 52

def snapshot_heap dir, clean
  output = "#{ Time.now.to_f }-heap.dump"
  output = File.join(dir, output)
  begin
    logger.info('OPENING HEADUMP FILE', dir: dir, file: output)
    file = File.new(output, FILE_WRITE_FLAGS)
    if clean
      logger.info('PERFORMING GARBAGE COLLECTION BEFORE HEAP DUMP')
      GC.start
    end
    ObjectSpace.dump_all(output: file)
  ensure
    file.close
  end
end

#start_thread!Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/contrast/utils/heap_dump_util.rb', line 32

def start_thread!
  return unless Contrast::Utils::HeapDumpUtil.enabled?

  control = Contrast::Utils::HeapDumpUtil.control
  log_enabled_warning
  dir = control[:path]
  FileUtils.mkdir_p(dir)
  return unless File.writable?(dir)

  delay = control[:delay]
  @_thread = Contrast::Agent::Thread.new do
    logger.info("HEAP DUMP THREAD INITIALIZED. WAITING #{ delay } SECONDS TO BEGIN.")
    sleep(delay)
    capture_heap_dump
  end
rescue StandardError => e
  logger.info(LOG_ERROR_DUMPS, e)
  nil
end