Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/metasploit/framework/profiler.rb
Views: 11779
# frozen_string_literal: true12require 'pathname'3require 'tmpdir'45module Metasploit6module Framework7module Profiler8class << self9def start10return unless record_global_cpu? || record_global_memory?11raise 'Cannot profile memory and cpu at the same time' if record_global_cpu? && record_global_memory?1213if record_global_cpu?14require 'ruby-prof'1516results_path = tmp_cpu_results_path17profile = RubyProf::Profile.new18profile.start1920at_exit do21result = profile.stop22save_cpu_result(result, path: results_path)23end24end2526if record_global_memory?27require 'memory_profiler'2829results_path = tmp_memory_results_path30profile = MemoryProfiler31profile.start3233at_exit do34puts "Generating memory dump #{results_path}"35result = profile.stop36save_memory_result(result, path: results_path)37end38end39end4041def record_cpu42require 'ruby-prof'4344results_path = tmp_cpu_results_path45profile = RubyProf::Profile.new46profile.start4748yield4950result = profile.stop51save_cpu_result(result, path: results_path)52end5354def record_memory55raise 'Cannot mix global memory recording and localised memory recording' if record_global_memory?5657require 'memory_profiler'5859results_path = tmp_memory_results_path60profile = MemoryProfiler61profile.start6263yield6465result = profile.stop66save_memory_result(result, path: results_path)67end6869private7071def record_global_cpu?72ENV['METASPLOIT_CPU_PROFILE']73end7475def record_global_memory?76ENV['METASPLOIT_MEMORY_PROFILE']77end7879def tmp_path_for(name:)80tmp_directory = Dir.mktmpdir("msf-profile-#{Time.now.strftime('%Y%m%d%H%M%S')}")81Pathname.new(tmp_directory).join(name)82end8384def tmp_cpu_results_path85path = tmp_path_for(name: 'cpu')86::FileUtils.mkdir_p(path)87path88end8990def tmp_memory_results_path91tmp_path_for(name: 'memory')92end9394def save_cpu_result(result, path:)95require 'rex/compat'9697puts "Generating CPU dump #{path}"9899printer = RubyProf::MultiPrinter.new(result, %i[flat graph_html tree stack])100printer.print(path: path)101102Rex::Compat.open_file(path)103end104105def save_memory_result(result, path:)106require 'rex/compat'107108result.pretty_print(to_file: path)109Rex::Compat.open_file(path)110end111end112end113end114end115116117