md_doc_combiner-explainer.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
  5. <meta http-equiv="X-UA-Compatible" content="IE=9"/>
  6. <meta name="generator" content="Doxygen 1.8.6"/>
  7. <title>GRPC C++: Combiner Explanation</title>
  8. <link href="tabs.css" rel="stylesheet" type="text/css"/>
  9. <script type="text/javascript" src="jquery.js"></script>
  10. <script type="text/javascript" src="dynsections.js"></script>
  11. <link href="search/search.css" rel="stylesheet" type="text/css"/>
  12. <script type="text/javascript" src="search/search.js"></script>
  13. <script type="text/javascript">
  14. $(document).ready(function() { searchBox.OnSelectItem(0); });
  15. </script>
  16. <link href="doxygen.css" rel="stylesheet" type="text/css" />
  17. </head>
  18. <body>
  19. <div id="top"><!-- do not remove this div, it is closed by doxygen! -->
  20. <div id="titlearea">
  21. <table cellspacing="0" cellpadding="0">
  22. <tbody>
  23. <tr style="height: 56px;">
  24. <td style="padding-left: 0.5em;">
  25. <div id="projectname">GRPC C++
  26. &#160;<span id="projectnumber">1.3.0</span>
  27. </div>
  28. </td>
  29. </tr>
  30. </tbody>
  31. </table>
  32. </div>
  33. <!-- end header part -->
  34. <!-- Generated by Doxygen 1.8.6 -->
  35. <script type="text/javascript">
  36. var searchBox = new SearchBox("searchBox", "search",false,'Search');
  37. </script>
  38. <div id="navrow1" class="tabs">
  39. <ul class="tablist">
  40. <li><a href="index.html"><span>Main&#160;Page</span></a></li>
  41. <li class="current"><a href="pages.html"><span>Related&#160;Pages</span></a></li>
  42. <li><a href="modules.html"><span>Modules</span></a></li>
  43. <li><a href="namespaces.html"><span>Namespaces</span></a></li>
  44. <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
  45. <li><a href="files.html"><span>Files</span></a></li>
  46. <li>
  47. <div id="MSearchBox" class="MSearchBoxInactive">
  48. <span class="left">
  49. <img id="MSearchSelect" src="search/mag_sel.png"
  50. onmouseover="return searchBox.OnSearchSelectShow()"
  51. onmouseout="return searchBox.OnSearchSelectHide()"
  52. alt=""/>
  53. <input type="text" id="MSearchField" value="Search" accesskey="S"
  54. onfocus="searchBox.OnSearchFieldFocus(true)"
  55. onblur="searchBox.OnSearchFieldFocus(false)"
  56. onkeyup="searchBox.OnSearchFieldChange(event)"/>
  57. </span><span class="right">
  58. <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
  59. </span>
  60. </div>
  61. </li>
  62. </ul>
  63. </div>
  64. <!-- window showing the filter options -->
  65. <div id="MSearchSelectWindow"
  66. onmouseover="return searchBox.OnSearchSelectShow()"
  67. onmouseout="return searchBox.OnSearchSelectHide()"
  68. onkeydown="return searchBox.OnSearchSelectKey(event)">
  69. <a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Data Structures</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Friends</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark">&#160;</span>Macros</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(11)"><span class="SelectionMark">&#160;</span>Groups</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(12)"><span class="SelectionMark">&#160;</span>Pages</a></div>
  70. <!-- iframe showing the search results (closed by default) -->
  71. <div id="MSearchResultsWindow">
  72. <iframe src="javascript:void(0)" frameborder="0"
  73. name="MSearchResults" id="MSearchResults">
  74. </iframe>
  75. </div>
  76. </div><!-- top -->
  77. <div class="header">
  78. <div class="headertitle">
  79. <div class="title">Combiner Explanation </div> </div>
  80. </div><!--header-->
  81. <div class="contents">
  82. <div class="textblock"><h2>Talk by ctiller, notes by vjpai</h2>
  83. <p>Typical way of doing critical section</p>
  84. <p>``` mu.lock() do_stuff() mu.unlock() ```</p>
  85. <p>An alternative way of doing it is</p>
  86. <p>``` class combiner { run(f) { mu.lock() f() mu.unlock() } mutex mu; }</p>
  87. <p>combiner.run(do_stuff) ```</p>
  88. <p>If you have two threads calling combiner, there will be some kind of queuing in place. It's called <code>combiner</code> because you can pass in more than one do_stuff at once and they will run under a common <code>mu</code>.</p>
  89. <p>The implementation described above has the issue that you're blocking a thread for a period of time, and this is considered harmful because it's an application thread that you're blocking.</p>
  90. <p>Instead, get a new property:</p>
  91. <ul>
  92. <li>Keep things running in serial execution</li>
  93. <li>Don't ever sleep the thread</li>
  94. <li>But maybe allow things to end up running on a different thread from where they were started</li>
  95. <li>This means that <code>do_stuff</code> doesn't necessarily run to completion when <code>combiner.run</code> is invoked</li>
  96. </ul>
  97. <p>``` class combiner { mpscq q; // multi-producer single-consumer queue can be made non-blocking state s; // is it empty or executing</p>
  98. <p>run(f) { if (q.push(f)) { // q.push returns true if it's the first thing while (q.pop(&amp;f)) { // modulo some extra work to avoid races f(); } } } } ```</p>
  99. <p>The basic idea is that the first one to push onto the combiner executes the work and then keeps executing functions from the queue until the combiner is drained.</p>
  100. <p>Our combiner does some additional work, with the motivation of write-batching.</p>
  101. <p>We have a second tier of <code>run</code> called <code>run_finally</code>. Anything queued onto <code>run_finally</code> runs after we have drained the queue. That means that there is essentially a finally-queue. This is not guaranteed to be final, but it's best-effort. In the process of running the finally item, we might put something onto the main combiner queue and so we'll need to re-enter.</p>
  102. <p><code>chttp2</code> runs all ops in the run state except if it sees a write it puts that into a finally. That way anything else that gets put into the combiner can add to that write.</p>
  103. <p>``` class combiner { mpscq q; // multi-producer single-consumer queue can be made non-blocking state s; // is it empty or executing queue finally; // you can only do run_finally when you are already running something from the combiner</p>
  104. <p>run(f) { if (q.push(f)) { // q.push returns true if it's the first thing loop: while (q.pop(&amp;f)) { // modulo some extra work to avoid races f(); } while (finally.pop(&amp;f)) { f(); } goto loop; } } } ```</p>
  105. <p>So that explains how combiners work in general. In gRPC, there is <code>start_batch(..., tag)</code> and then work only gets activated by somebody calling <code>cq::next</code> which returns a tag. This gives an API-level guarantee that there will be a thread doing polling to actually make work happen. However, some operations are not covered by a poller thread, such as cancellation that doesn't have a completion. Other callbacks that don't have a completion are the internal work that gets done before the batch gets completed. We need a condition called <code>covered_by_poller</code> that means that the item will definitely need some thread at some point to call <code>cq::next</code> . This includes those callbacks that directly cause a completion but also those that are indirectly required before getting a completion. If we can't tell for sure for a specific path, we have to assumed it is not covered by poller.</p>
  106. <p>The above combiner has the problem that it keeps draining for a potentially infinite amount of time and that can lead to a huge tail latency for some operations. So we can tweak it by returning to the application if we know that it is valid to do so:</p>
  107. <p>``` while (q.pop(&amp;f)) { f(); if (control_can_be_returned &amp;&amp; some_still_queued_thing_is_covered_by_poller) { offload_combiner_work_to_some_other_thread(); } } ```</p>
  108. <p><code>offload</code> is more than <code>break</code>; it does <code>break</code> but also causes some other thread that is currently waiting on a poll to break out of its poll. This is done by setting up a per-polling-island work-queue (distributor) wakeup FD. The work-queue is the converse of the combiner; it tries to spray events onto as many threads as possible to get as much concurrency as possible.</p>
  109. <p>So <code>offload</code> really does:</p>
  110. <p>``` workqueue.run(continue_from_while_loop); break; ```</p>
  111. <p>This needs us to add another class variable for a <code>workqueue</code> (which is really conceptually a distributor).</p>
  112. <p>``` workqueue::run(f) { q.push(f) eventfd.wakeup() }</p>
  113. <p>workqueue::readable() { eventfd.consume(); q.pop(&amp;f); f(); if (!q.empty()) { eventfd.wakeup(); // spray across as many threads as are waiting on this workqueue } } ```</p>
  114. <p>In principle, <code>run_finally</code> could get starved, but this hasn't happened in practice. If we were concerned about this, we could put a limit on how many things come off the regular <code>q</code> before the <code>finally</code> queue gets processed. </p>
  115. </div></div><!-- contents -->
  116. <!-- start footer part -->
  117. <hr class="footer"/><address class="footer"><small>
  118. Generated on Thu Apr 27 2017 17:26:13 for GRPC C++ by &#160;<a href="http://www.doxygen.org/index.html">
  119. <img class="footer" src="doxygen.png" alt="doxygen"/>
  120. </a> 1.8.6
  121. </small></address>
  122. </body>
  123. </html>