Get amount of open fibers in ruby

199 views Asked by At

How can I get the current amount of open Fibers in a ruby application? My app uses EventMachine Synchrony lib for concurrency handling. While googling, I've not found any API which would return it.

For example if I have this piece of code:

EM::Synchrony::FiberIterator.new(@outputs, @outputs.size).each do |output|
# some code goes here
end

then how can I know how many fibers are there running? Without manually using counter++ and of course without @some_arr.size.

1

There are 1 answers

0
Roman Sosnin On

Ok, so 1 solution was found for this issue:

You can count number of existing objects (of all types) in ruby.

In our case we want to count the objects of type/class 'Fiber', so we use ObjectSpace.each_object ruby API:

stats = {}
stats["Fiber"] = 0
ObjectSpace.each_object(Fiber) {|o| stats["Fiber"] += 1 if o.alive?}

(we are interested only in alive Fibers, that's why using alive? API)

ObjectSpace for reference: https://ruby-doc.org/core-1.9.3/ObjectSpace.html

Fiber for reference: https://ruby-doc.org/core-1.9.3/Fiber.html