Skip to content

Instantly share code, notes, and snippets.

@ohaddahan
Last active March 14, 2019 10:43
Show Gist options
  • Save ohaddahan/343fe5aa73112d29a6fef7c4a1b5d219 to your computer and use it in GitHub Desktop.
Save ohaddahan/343fe5aa73112d29a6fef7c4a1b5d219 to your computer and use it in GitHub Desktop.
Checking if jemalloc is present
module CheckJEMalloc
require 'ffi'
extend FFI::Library
def self.je_malloc_exists?(jemalloc_so = nil)
if !jemalloc_so.nil? && !jemalloc_so.is_a?(String)
puts "ArgumentError : Only strings allowed as arguments"
return false
end
jemalloc_so = ENV.fetch('LD_PRELOAD','') if jemalloc_so.nil?
if jemalloc_so.empty?
puts "No jemalloc.so was given as an argument or LD_PRELOAD"
return false
end
begin
ffi_lib jemalloc_so
rescue LoadError => e
puts "LoadError : #{e}"
return false
end
begin
attach_function :je_malloc, [:int], :int
rescue FFI::NotFoundError => e
puts "FFI::NotFoundError : #{e}"
return false
end
return true
end
end
A. Using LD_PRELOAD:
A.1. Run IRB/Ruby/etc. with LD_PRELOAD .
env LD_PRELOAD=<PATH_TO_JEMALLOC> irb
env LD_PRELOAD=<PATH_TO_JEMALLOC> ruby
A.2. Require/Source CheckJEMalloc.rb .
A.3. Run CheckJEMalloc.je_malloc_exists? .
B. Without LD_PRELOAD:
B.1. Run IRB/Ruby/etc.
B.2. Require/Source CheckJEMalloc.rb .
B.3. Run CheckJEMalloc.je_malloc_exists?(<PATH_TO_JEMALLOC>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment