md_doc_core_combiner-explainer.html 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://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.17"/>
  7. <meta name="viewport" content="width=device-width, initial-scale=1"/>
  8. <title>GRPC Core: Combiner Explanation</title>
  9. <link href="tabs.css" rel="stylesheet" type="text/css"/>
  10. <script type="text/javascript" src="jquery.js"></script>
  11. <script type="text/javascript" src="dynsections.js"></script>
  12. <link href="search/search.css" rel="stylesheet" type="text/css"/>
  13. <script type="text/javascript" src="search/searchdata.js"></script>
  14. <script type="text/javascript" src="search/search.js"></script>
  15. <link href="doxygen.css" rel="stylesheet" type="text/css" />
  16. </head>
  17. <body>
  18. <div id="top"><!-- do not remove this div, it is closed by doxygen! -->
  19. <div id="titlearea">
  20. <table cellspacing="0" cellpadding="0">
  21. <tbody>
  22. <tr style="height: 56px;">
  23. <td id="projectalign" style="padding-left: 0.5em;">
  24. <div id="projectname">GRPC Core
  25. &#160;<span id="projectnumber">15.0.0</span>
  26. </div>
  27. </td>
  28. </tr>
  29. </tbody>
  30. </table>
  31. </div>
  32. <!-- end header part -->
  33. <!-- Generated by Doxygen 1.8.17 -->
  34. <script type="text/javascript">
  35. /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  36. var searchBox = new SearchBox("searchBox", "search",false,'Search');
  37. /* @license-end */
  38. </script>
  39. <script type="text/javascript" src="menudata.js"></script>
  40. <script type="text/javascript" src="menu.js"></script>
  41. <script type="text/javascript">
  42. /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  43. $(function() {
  44. initMenu('',true,false,'search.php','Search');
  45. $(document).ready(function() { init_search(); });
  46. });
  47. /* @license-end */</script>
  48. <div id="main-nav"></div>
  49. <!-- window showing the filter options -->
  50. <div id="MSearchSelectWindow"
  51. onmouseover="return searchBox.OnSearchSelectShow()"
  52. onmouseout="return searchBox.OnSearchSelectHide()"
  53. onkeydown="return searchBox.OnSearchSelectKey(event)">
  54. </div>
  55. <!-- iframe showing the search results (closed by default) -->
  56. <div id="MSearchResultsWindow">
  57. <iframe src="javascript:void(0)" frameborder="0"
  58. name="MSearchResults" id="MSearchResults">
  59. </iframe>
  60. </div>
  61. </div><!-- top -->
  62. <div class="PageDoc"><div class="header">
  63. <div class="headertitle">
  64. <div class="title">Combiner Explanation </div> </div>
  65. </div><!--header-->
  66. <div class="contents">
  67. <div class="textblock"><h1><a class="anchor" id="autotoc_md82"></a>
  68. Talk by ctiller, notes by vjpai</h1>
  69. <p>Typical way of doing critical section</p>
  70. <div class="fragment"><div class="line">mu.lock()</div>
  71. <div class="line">do_stuff()</div>
  72. <div class="line">mu.unlock()</div>
  73. </div><!-- fragment --><p>An alternative way of doing it is</p>
  74. <div class="fragment"><div class="line">class combiner {</div>
  75. <div class="line"> run(f) {</div>
  76. <div class="line"> mu.lock()</div>
  77. <div class="line"> f()</div>
  78. <div class="line"> mu.unlock()</div>
  79. <div class="line"> }</div>
  80. <div class="line"> mutex mu;</div>
  81. <div class="line">}</div>
  82. <div class="line"> </div>
  83. <div class="line">combiner.run(do_stuff)</div>
  84. </div><!-- fragment --><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>
  85. <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>
  86. <p>Instead, get a new property:</p><ul>
  87. <li>Keep things running in serial execution</li>
  88. <li>Don't ever sleep the thread</li>
  89. <li>But maybe allow things to end up running on a different thread from where they were started</li>
  90. <li>This means that <code>do_stuff</code> doesn't necessarily run to completion when <code>combiner.run</code> is invoked</li>
  91. </ul>
  92. <div class="fragment"><div class="line">class combiner {</div>
  93. <div class="line"> mpscq q; // multi-producer single-consumer queue can be made non-blocking</div>
  94. <div class="line"> state s; // is it empty or executing</div>
  95. <div class="line"> </div>
  96. <div class="line"> run(f) {</div>
  97. <div class="line"> if (q.push(f)) {</div>
  98. <div class="line"> // q.push returns true if it&#39;s the first thing</div>
  99. <div class="line"> while (q.pop(&amp;f)) { // modulo some extra work to avoid races</div>
  100. <div class="line"> f();</div>
  101. <div class="line"> }</div>
  102. <div class="line"> }</div>
  103. <div class="line"> }</div>
  104. <div class="line">}</div>
  105. </div><!-- fragment --><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>
  106. <p>Our combiner does some additional work, with the motivation of write-batching.</p>
  107. <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>
  108. <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>
  109. <div class="fragment"><div class="line">class combiner {</div>
  110. <div class="line"> mpscq q; // multi-producer single-consumer queue can be made non-blocking</div>
  111. <div class="line"> state s; // is it empty or executing</div>
  112. <div class="line"> queue finally; // you can only do run_finally when you are already running something from the combiner</div>
  113. <div class="line"> </div>
  114. <div class="line"> run(f) {</div>
  115. <div class="line"> if (q.push(f)) {</div>
  116. <div class="line"> // q.push returns true if it&#39;s the first thing</div>
  117. <div class="line"> loop:</div>
  118. <div class="line"> while (q.pop(&amp;f)) { // modulo some extra work to avoid races</div>
  119. <div class="line"> f();</div>
  120. <div class="line"> }</div>
  121. <div class="line"> while (finally.pop(&amp;f)) {</div>
  122. <div class="line"> f();</div>
  123. <div class="line"> }</div>
  124. <div class="line"> goto loop;</div>
  125. <div class="line"> }</div>
  126. <div class="line"> }</div>
  127. <div class="line">}</div>
  128. </div><!-- fragment --><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>
  129. <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>
  130. <div class="fragment"><div class="line">while (q.pop(&amp;f)) {</div>
  131. <div class="line"> f();</div>
  132. <div class="line"> if (control_can_be_returned &amp;&amp; some_still_queued_thing_is_covered_by_poller) {</div>
  133. <div class="line"> offload_combiner_work_to_some_other_thread();</div>
  134. <div class="line"> }</div>
  135. <div class="line">}</div>
  136. </div><!-- fragment --><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>
  137. <p>So <code>offload</code> really does:</p>
  138. <div class="fragment"><div class="line">workqueue.run(continue_from_while_loop);</div>
  139. <div class="line">break;</div>
  140. </div><!-- fragment --><p>This needs us to add another class variable for a <code>workqueue</code> (which is really conceptually a distributor).</p>
  141. <div class="fragment"><div class="line">workqueue::run(f) {</div>
  142. <div class="line"> q.push(f)</div>
  143. <div class="line"> eventfd.wakeup()</div>
  144. <div class="line">}</div>
  145. <div class="line"> </div>
  146. <div class="line">workqueue::readable() {</div>
  147. <div class="line"> eventfd.consume();</div>
  148. <div class="line"> q.pop(&amp;f);</div>
  149. <div class="line"> f();</div>
  150. <div class="line"> if (!q.empty()) {</div>
  151. <div class="line"> eventfd.wakeup(); // spray across as many threads as are waiting on this workqueue</div>
  152. <div class="line"> }</div>
  153. <div class="line">}</div>
  154. </div><!-- fragment --><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>
  155. </div></div><!-- contents -->
  156. </div><!-- PageDoc -->
  157. <!-- start footer part -->
  158. <hr class="footer"/><address class="footer"><small>
  159. Generated on Wed Mar 3 2021 19:17:11 for GRPC Core by &#160;<a href="http://www.doxygen.org/index.html">
  160. <img class="footer" src="doxygen.png" alt="doxygen"/>
  161. </a> 1.8.17
  162. </small></address>
  163. </body>
  164. </html>