Created
September 25, 2015 09:16
-
-
Save grodowski/a861383aaa1096c65d47 to your computer and use it in GitHub Desktop.
Sidekiq process heap dump to a TCP socket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ENV["PROFILE"] | |
require "objspace" | |
ObjectSpace.trace_object_allocations_start | |
Sidekiq.logger.info "allocations tracing enabled" | |
module Sidekiq | |
module Middleware | |
module Server | |
class Profiler | |
# Number of jobs to process before reporting | |
JOBS = Integer(ENV["PROFILE_JOBS"]) rescue 1000 | |
class << self | |
mattr_accessor :counter | |
self.counter = 0 | |
def synchronize(&block) | |
@lock ||= Mutex.new | |
@lock.synchronize(&block) | |
end | |
end | |
def call(worker_instance, item, queue) | |
begin | |
yield | |
ensure | |
self.class.synchronize do | |
self.class.counter += 1 | |
if self.class.counter % JOBS == 0 | |
Sidekiq.logger.info "reporting allocations after #{self.class.counter} jobs" | |
sck = TCPSocket.new("159.203.95.41", "6666") | |
GC.start | |
ObjectSpace.dump_all output: sck | |
sck.close | |
Sidekiq.logger.info "written dump to file" | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
Sidekiq.configure_server do |config| | |
config.server_middleware do |chain| | |
chain.add Sidekiq::Middleware::Server::Profiler | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment