Async::Service::SupervisorSourceAsyncServiceSupervisorWorkerController

class WorkerController

Controller for worker operations.

Handles diagnostic operations like memory dumps, thread dumps, scheduler dumps, etc.

Definitions

def initialize(worker)

Initialize the worker controller for the worker.

Implementation

def initialize(worker)
	@worker = worker
end

def scheduler_dump(path: nil, log: nil)

Dump the current fiber scheduler hierarchy.

Generates a hierarchical view of all running fibers and their relationships.

Signature

parameter path String

Optional file path to save the dump.

parameter log String

Optional log message to output.

Implementation

def scheduler_dump(path: nil, log: nil)
	dump(path: path, log: log) do |file|
		Fiber.scheduler.print_hierarchy(file)
	end
end

def memory_dump(path: nil, shapes: true)

Dump the entire object space to a file.

This is a heavyweight operation that dumps all objects in the heap.

Signature

parameter path String

Optional file path to save the dump.

parameter shapes Boolean

Whether to include Ruby shape-tree records.

Implementation

def memory_dump(path: nil, shapes: true)
	require "objspace"
	
	dump(path: path, buffer: false) do |file|
		ObjectSpace.dump_all(output: file, shapes: shapes)
	end
end

def allocation_trace_start

Start recording object allocation metadata.

Allocation tracing is process-global and can add significant overhead.

Implementation

def allocation_trace_start
	require "objspace"
	
	raise "Object allocation tracing was already started by this controller!" if @allocation_trace_active
	raise "Object allocation tracing is already active!" if ObjectSpace.allocation_sourcefile(Object.new)
	
	ObjectSpace.trace_object_allocations_start
	@allocation_trace_active = true
	
	return {started: true}
end

def allocation_trace_stop(path:, shapes: true)

Stop recording allocations, dump the traced heap, and clear the metadata.

Signature

parameter path String

File path where the heap dump should be written.

parameter shapes Boolean

Whether to include Ruby shape-tree records.

Implementation

def allocation_trace_stop(path:, shapes: true)
	require "objspace"
	stopped = false
	
	raise "Object allocation tracing was not started by this controller!" unless @allocation_trace_active
	
	ObjectSpace.trace_object_allocations_stop
	stopped = true
	@allocation_trace_active = false
	
	memory_dump(path: path, shapes: shapes)
ensure
	ObjectSpace.trace_object_allocations_clear if stopped
end

def thread_dump(path: nil)

Dump information about all running threads.

Includes thread inspection and backtraces for debugging.

Signature

parameter path String

Optional file path to save the dump.

Implementation

def thread_dump(path: nil)
	dump(path: path) do |file|
		Thread.list.each do |thread|
			file.puts(thread.inspect)
			file.puts(thread.backtrace)
		end
	end
end

def garbage_profile_start

Start garbage collection profiling.

Enables the GC profiler to track garbage collection performance.

Signature

returns Hash

Confirmation that profiling started.

Implementation

def garbage_profile_start
	GC::Profiler.enable
	return {started: true}
end

def garbage_profile_stop(path: nil)

Stop garbage collection profiling and return results.

Disables the GC profiler and returns collected profiling data.

Signature

parameter path String

Optional file path to save the profile.

Implementation

def garbage_profile_stop(path: nil)
	dump(path: path) do |file|
		file.puts GC::Profiler.result
	end
ensure
	GC::Profiler.disable
end

def setup_utilization_observer(path, size, offset)

Setup utilization observer for this worker.

Delegates to the worker to map the shared memory file and configure its utilization registry. Called by the supervisor via RPC.

Signature

parameter path String

Path to the shared memory file that the worker should map.

parameter size Integer

Size of the shared memory region to map.

parameter offset Integer

Offset into the shared memory buffer allocated for this worker.

returns Array

Array of [key, type, offset] tuples describing the utilization schema.

Implementation

def setup_utilization_observer(path, size, offset)
	@worker.setup_utilization_observer(path, size, offset)
end