Sha256: e38ce105bc38944599688e7597207a39d6763499201aa778b2d66cfe3fd4f931
Contents?: true
Size: 2 KB
Versions: 18
Compression:
Stored size: 2 KB
Contents
# Copyright (c) 2023 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true require 'objspace' require 'singleton' require 'contrast/components/heap_dump' require 'contrast/components/logger' require 'contrast/utils/head_dump_utils_extend' module Contrast module Utils # Implementation of a heap dump util to automate generation class HeapDumpUtil < Contrast::Agent::WorkerThread extend Contrast::Components::Logger::InstanceMethods include Contrast::Components::Logger::InstanceMethods extend Contrast::Components::HeapDump::InstanceMethods include Contrast::Utils::HeadDumpExtend LOG_ERROR_DUMPS = 'Unable to generate heap dumps' FILE_WRITE_FLAGS = 'w' class << self def enabled? heap_dump_enabled? end def control heap_dump_control end end 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 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 end end end
Version data entries
18 entries across 18 versions & 1 rubygems