Upgrade to Pro — share decks privately, control downloads, hide ads and more …

High In Fiber - ShRUG November 2010

Jon Rowe
November 08, 2010

High In Fiber - ShRUG November 2010

Presentation on Fibers given to ShRUG in November 2010

Jon Rowe

November 08, 2010

More Decks by Jon Rowe

Other Decks in Technology


  1. Word To The Wise This is probably going to be

    short... But hopefully sweet... But hopefully sweet... Please ask questions... But I don't promise to answer them...
  2. Scheduling Consider the example of 2 sections of code competing

    for CPU time. One takes 50ms of cpu time The other takes 40ms of blocking something and 10ms of cpu time
  3. Scheduling Run side by side these 2 sections of code

    would take 100ms in total If we can cooperatively schedule them, we can compress that into just 60ms
  4. What does that do? Creating the fiber does not execute

    the fiber To kick start the fiber we must `resume` it The first call to resume starts the execution of the fiber's block
  5. What does that do? Once executed the fiber follows normal

    behaviour Until it reaches `Fiber.yield` Upon which it "returns" the passed parameter Which drops out the resume
  6. Example The next call resuming from the yield will hit

    the bottom of the loop and carry on giving us
  7. Execution flow $ puts f.resume "Cats" Then you resumed me

    with Cats second yield value => nil
  8. Fiber Pool Say we have a blocking operation we want

    to run concurrently But we only want to do a certain number at the same time Let us invent a pool of worker fibers to do this cooperatively
  9. Fiber Pool Once the fiber is setup We add it

    to the pool The fiber has not yet been run
  10. Fiber Pool add_to_pool first checks to see if the pool

    is over_capacity? If it is we wait_for_free_pool_space Otherwise we add our fiber into our pool And then we execute it
  11. Fiber Pool wait_for_free_pool_space does two things calls wait_for_next_complete_fiber removes the

    completed fiber from the pool How does this work? If we look at our new fiber
  12. Fiber Pool Remember that our completion_callback returns our fiber The

    idea is that when our work has finished it calls our completion_callback Which returns control to the control_fibe Which then pops out the completed fiber to the Fiber.yield in wait_for_next_complete_fiber
  13. Fiber Pool Now we can add fibers to our pool

    Now we need to tidy up some loose ends
  14. Fiber Pool fibers_left_to_process? we haven't used that anywhere? In order

    to actually use our pool we need one more method
  15. Fiber Pool Once we have finished adding work to our

    pool we need someway to handle the remaining finishing fibers We need to tidy up after ourselves We need to drain the pool
  16. Fiber Pool All we are doing here is checking if

    there are fibers still ticking And waiting until they are finished
  17. Fiber Pool Now we can use our fiber pool But

    it will need some setup So lets make a convenience wrapper
  18. Fiber Pool We create the fiber which is to be

    our control_fiber We create a new pool We yield that pool to whatever code we are setting up
  19. Fiber Pool We drain the pool We cheat and call

    EM.stop, I'll cover that in a bit We immediately execute our fiber
  20. Caveat The EventMachine.run block is why we need the EM.stop

    command We eventually want to drop out of EventMachine I'm using EventMachine for notifying my fiber pool when my blocking command finishes We could use something else
  21. Fiber Pool We use our convenience method to start our

    fiber pool We take our created pool and we're going to add 30 tasks Each task is going to be simulating blocking operation by sleeping for 5 seconds When the blocking operation is complete we are going to call the completion_callback
  22. EventMachine? Fibers are cooperatively scheduled That doesn't make them instantly

    intelligent We need something to wake them up appropriately
  23. EventMachine? I'm using EventMachine out of convenience It could be

    any control structure that can call f.resume
  24. Evented IO Ideally I'd like to wake my fibers up

    directly upon receiving a message Then I don't have to have extra overhead
  25. Fin