High In Fiber - ShRUG November 2010

D79fc498d7a5b2ce12180890247476f0?s=47 Jon Rowe
November 08, 2010

High In Fiber - ShRUG November 2010

Presentation on Fibers given to ShRUG in November 2010

D79fc498d7a5b2ce12180890247476f0?s=128

Jon Rowe

November 08, 2010
Tweet

Transcript

  1. 2.

    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. 12.

    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. 13.

    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. 15.
  5. 16.
  6. 20.

    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
  7. 21.

    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
  8. 22.

    Example The next call resuming from the yield will hit

    the bottom of the loop and carry on giving us
  9. 26.

    Execution flow $ puts f.resume "Cats" Then you resumed me

    with Cats second yield value => nil
  10. 29.
  11. 31.

    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
  12. 43.

    Fiber Pool Once the fiber is setup We add it

    to the pool The fiber has not yet been run
  13. 44.

    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
  14. 46.

    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
  15. 48.

    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
  16. 49.

    Fiber Pool Now we can add fibers to our pool

    Now we need to tidy up some loose ends
  17. 53.

    Fiber Pool fibers_left_to_process? we haven't used that anywhere? In order

    to actually use our pool we need one more method
  18. 55.

    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
  19. 57.

    Fiber Pool All we are doing here is checking if

    there are fibers still ticking And waiting until they are finished
  20. 58.

    Fiber Pool Now we can use our fiber pool But

    it will need some setup So lets make a convenience wrapper
  21. 60.

    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
  22. 61.

    Fiber Pool We drain the pool We cheat and call

    EM.stop, I'll cover that in a bit We immediately execute our fiber
  23. 64.

    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
  24. 66.

    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
  25. 72.

    EventMachine? Fibers are cooperatively scheduled That doesn't make them instantly

    intelligent We need something to wake them up appropriately
  26. 73.

    EventMachine? I'm using EventMachine out of convenience It could be

    any control structure that can call f.resume
  27. 74.

    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
  28. 76.

    Fin

  29. 78.