<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hi! Thank you for your answer!<div class="">Fixed codestyle and answered your question inline.</div><div class=""><br class=""></div><div class="">--<br class="">Serge Petrenko<br class=""><a href="mailto:sergepetrenko@tarantool.org" class="">sergepetrenko@tarantool.org</a></div><div class=""><br class=""></div><div class=""><blockquote type="cite" class="">15 нояб. 2019 г., в 19:11, Alexander Turenko <<a href="mailto:alexander.turenko@tarantool.org" class="">alexander.turenko@tarantool.org</a>> написал(а):<br class=""><br class="">I have no any knowledge around <a href="http://fiber.top" class="">fiber.top</a>(), just asked a question<br class="">because of curiosity.<br class=""><br class="">WBR, Alexander Turenko.<br class=""><br class="">On Fri, Nov 15, 2019 at 05:58:58PM +0300, Serge Petrenko wrote:<br class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;" class="">We didn't refresh last remembered clock on fiber.top_enable()<br class="">This means that the fiber issuing fiber.top_enable() would get a huge<br class="">chunk of cpu time on its first yield. Fix this.<br class="">Also reset clock_delta and cpu_miss_count.<br class=""><br class="">If fiber.top() is issued on the same ev loop iteration as<br class="">fiber.top_enable(), clock_delta_last is 0 for both cord and all the<br class="">fibers, so report "instant" and "average" stats per last iteration as 0<br class="">instead of NaN.<br class=""><br class="">Follow-up #2694<br class="">---<br class="">src/lib/core/fiber.c | 21 +++++++++++++++++++++<br class="">src/lua/fiber.c      | 12 ++++++++++--<br class="">2 files changed, 31 insertions(+), 2 deletions(-)<br class=""><br class="">diff --git a/src/lib/core/fiber.c b/src/lib/core/fiber.c<br class="">index aebaba7f0..258c094f5 100644<br class="">--- a/src/lib/core/fiber.c<br class="">+++ b/src/lib/core/fiber.c<br class="">@@ -1203,9 +1203,30 @@ fiber_top_enable()<br class=""><span class="Apple-tab-span" style="white-space:pre">     </span><span class="Apple-tab-span" style="white-space:pre">    </span>ev_check_start(cord()->loop, &cord()->check_event);<br class=""><span class="Apple-tab-span" style="white-space:pre">  </span><span class="Apple-tab-span" style="white-space:pre">    </span>fiber_top_enabled = true;<br class=""><br class="">+<span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">    </span>/*<br class="">+<span class="Apple-tab-span" style="white-space:pre">    </span><span class="Apple-tab-span" style="white-space:pre">    </span> * Reset cord and fiber clock stats in order to<br class="">+<span class="Apple-tab-span" style="white-space:pre">  </span><span class="Apple-tab-span" style="white-space:pre">    </span> * count from zero even on reenable.<br class="">+<span class="Apple-tab-span" style="white-space:pre">     </span><span class="Apple-tab-span" style="white-space:pre">    </span> */<br class=""><span class="Apple-tab-span" style="white-space:pre">       </span><span class="Apple-tab-span" style="white-space:pre">    </span>cord()->clock_acc = 0;<br class=""><span class="Apple-tab-span" style="white-space:pre">      </span><span class="Apple-tab-span" style="white-space:pre">    </span>cord()->cpu_miss_count_last = 0;<br class=""><span class="Apple-tab-span" style="white-space:pre">    </span><span class="Apple-tab-span" style="white-space:pre">    </span>cord()->clock_delta_last = 0;<br class="">+<span class="Apple-tab-span" style="white-space:pre">      </span><span class="Apple-tab-span" style="white-space:pre">    </span>cord()->clock_delta = 0;<br class="">+<br class="">+<span class="Apple-tab-span" style="white-space:pre">     </span><span class="Apple-tab-span" style="white-space:pre">    </span>struct fiber * fiber;<br class=""></blockquote><br class="">Code style: struct fiber *fiber;<br class=""></blockquote><div class=""><br class=""></div><div class="">Fixed</div><br class=""><blockquote type="cite" class=""><br class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;" class="">+<span class="Apple-tab-span" style="white-space:pre">     </span><span class="Apple-tab-span" style="white-space:pre">    </span>rlist_foreach_entry(fiber, &cord()->alive, link) {<br class="">+<span class="Apple-tab-span" style="white-space:pre">     </span><span class="Apple-tab-span" style="white-space:pre">    </span><span class="Apple-tab-span" style="white-space:pre">    </span>fiber->clock_acc = 0;<br class="">+<span class="Apple-tab-span" style="white-space:pre">      </span><span class="Apple-tab-span" style="white-space:pre">    </span><span class="Apple-tab-span" style="white-space:pre">    </span>fiber->clock_delta_last = 0;<br class="">+<span class="Apple-tab-span" style="white-space:pre">       </span><span class="Apple-tab-span" style="white-space:pre">    </span><span class="Apple-tab-span" style="white-space:pre">    </span>fiber->clock_delta = 0;<br class="">+<span class="Apple-tab-span" style="white-space:pre">    </span><span class="Apple-tab-span" style="white-space:pre">    </span><span class="Apple-tab-span" style="white-space:pre">    </span>fiber->cputime = 0;<br class="">+<span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">    </span>}<br class="">+<br class="">+<span class="Apple-tab-span" style="white-space:pre">       </span><span class="Apple-tab-span" style="white-space:pre">    </span>cord()->sched.clock_acc = 0;<br class="">+<span class="Apple-tab-span" style="white-space:pre">       </span><span class="Apple-tab-span" style="white-space:pre">    </span>cord()->sched.clock_delta_last = 0;<br class="">+<span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">    </span>cord()->sched.clock_delta = 0;<br class="">+<span class="Apple-tab-span" style="white-space:pre">     </span><span class="Apple-tab-span" style="white-space:pre">    </span>cord()->sched.cputime = 0;<br class="">+<br class="">+<span class="Apple-tab-span" style="white-space:pre">   </span><span class="Apple-tab-span" style="white-space:pre">    </span>cord()->clock_last = __rdtscp(&cord()->cpu_id_last);<br class="">+<span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">    </span>cord()->cpu_miss_count = 0;<br class=""></blockquote><br class="">Should not we doing such clean up in fiber_top_disable() rather then<br class="">here?<br class=""></blockquote><div class=""><br class=""></div><div class="">It doesn’t matter IMO. Well, yes, you don’t need a cleanup when you call</div><div class="">fiber.top_enable() for the first time, but, other than that, I see no difference.</div><br class=""><blockquote type="cite" class=""><br class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span class="Apple-tab-span" style="white-space:pre">     </span><span class="Apple-tab-span" style="white-space:pre">    </span>struct timespec ts;<br class=""><span class="Apple-tab-span" style="white-space:pre">    </span><span class="Apple-tab-span" style="white-space:pre">    </span>if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) != 0) {<br class=""><span class="Apple-tab-span" style="white-space:pre">    </span><span class="Apple-tab-span" style="white-space:pre">    </span><span class="Apple-tab-span" style="white-space:pre">    </span>say_debug("clock_gettime(): failed to get this"<br class="">diff --git a/src/lua/fiber.c b/src/lua/fiber.c<br class="">index 8b3b22e55..647505643 100644<br class="">--- a/src/lua/fiber.c<br class="">+++ b/src/lua/fiber.c<br class="">@@ -335,11 +335,19 @@ lbox_fiber_top_entry(struct fiber *f, void *cb_ctx)<br class=""><span class="Apple-tab-span" style="white-space:pre">   </span>lua_newtable(L);<br class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">  </span>lua_pushliteral(L, "average");<br class="">-<span class="Apple-tab-span" style="white-space:pre">      </span>lua_pushnumber(L, f->clock_acc / (double)cord()->clock_acc * 100);<br class="">+<span class="Apple-tab-span" style="white-space:pre">      </span>if (cord()->clock_acc)<br class="">+<span class="Apple-tab-span" style="white-space:pre">     </span><span class="Apple-tab-span" style="white-space:pre">    </span>lua_pushnumber(L, f->clock_acc / (double)cord()->clock_acc * 100);<br class="">+<span class="Apple-tab-span" style="white-space:pre">      </span>else<br class="">+<span class="Apple-tab-span" style="white-space:pre">  </span><span class="Apple-tab-span" style="white-space:pre">    </span>lua_pushnumber(L, 0);<br class=""><span class="Apple-tab-span" style="white-space:pre">  </span>lua_settable(L, -3);<br class="">+<br class=""><span class="Apple-tab-span" style="white-space:pre">     </span>lua_pushliteral(L, "instant");<br class="">-<span class="Apple-tab-span" style="white-space:pre">      </span>lua_pushnumber(L, f->clock_delta_last / (double)cord()->clock_delta_last * 100);<br class="">+<span class="Apple-tab-span" style="white-space:pre">        </span>if (cord()->clock_delta_last)<br class="">+<span class="Apple-tab-span" style="white-space:pre">      </span><span class="Apple-tab-span" style="white-space:pre">    </span>lua_pushnumber(L, f->clock_delta_last / (double)cord()->clock_delta_last * 100);<br class="">+<span class="Apple-tab-span" style="white-space:pre">        </span>else<br class="">+<span class="Apple-tab-span" style="white-space:pre">  </span><span class="Apple-tab-span" style="white-space:pre">    </span>lua_pushnumber(L, 0);<br class=""><span class="Apple-tab-span" style="white-space:pre">  </span>lua_settable(L, -3);<br class=""></blockquote><br class="">Code style: lines over 80 chars.<br class=""></blockquote><div class=""><br class=""></div><div class="">Fixed</div><br class=""><blockquote type="cite" class=""><br class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;" class="">+<br class=""><span class="Apple-tab-span" style="white-space:pre"> </span>lua_pushliteral(L, "time");<br class=""><span class="Apple-tab-span" style="white-space:pre">  </span>lua_pushnumber(L, f->cputime / (double) FIBER_TIME_RES);<br class=""><span class="Apple-tab-span" style="white-space:pre">    </span>lua_settable(L, -3);<br class="">-- <br class="">2.21.0 (Apple Git-122)<br class=""></blockquote></blockquote><div class=""><br class=""></div></div><div class=""><br class="">diff --git a/src/lib/core/fiber.c b/src/lib/core/fiber.c<br class="">index 258c094f5..c6aab18b2 100644<br class="">--- a/src/lib/core/fiber.c<br class="">+++ b/src/lib/core/fiber.c<br class="">@@ -1212,7 +1212,7 @@ fiber_top_enable()<br class="">                cord()->clock_delta_last = 0;<br class="">                cord()->clock_delta = 0;<br class=""> <br class="">-               struct fiber * fiber;<br class="">+               struct fiber *fiber;<br class="">                rlist_foreach_entry(fiber, &cord()->alive, link) {<br class="">                        fiber->clock_acc = 0;<br class="">                        fiber->clock_delta_last = 0;<br class="">diff --git a/src/lua/fiber.c b/src/lua/fiber.c<br class="">index 647505643..9268b0a19 100644<br class="">--- a/src/lua/fiber.c<br class="">+++ b/src/lua/fiber.c<br class="">@@ -335,17 +335,21 @@ lbox_fiber_top_entry(struct fiber *f, void *cb_ctx)<br class="">        lua_newtable(L);<br class=""> <br class="">        lua_pushliteral(L, "average");<br class="">-       if (cord()->clock_acc)<br class="">-               lua_pushnumber(L, f->clock_acc / (double)cord()->clock_acc * 100);<br class="">-       else<br class="">+       if (cord()->clock_acc) {<br class="">+               lua_pushnumber(L, f->clock_acc /<br class="">+                                 (double)cord()->clock_acc * 100);<br class="">+       } else {<br class="">                lua_pushnumber(L, 0);<br class="">+       }<br class="">        lua_settable(L, -3);<br class=""> <br class="">        lua_pushliteral(L, "instant");<br class="">-       if (cord()->clock_delta_last)<br class="">-               lua_pushnumber(L, f->clock_delta_last / (double)cord()->clock_delta_last * 100);<br class="">-       else<br class="">+       if (cord()->clock_delta_last) {<br class="">+               lua_pushnumber(L, f->clock_delta_last /<br class="">+                                 (double)cord()->clock_delta_last * 100);<br class="">+       } else {<br class="">                lua_pushnumber(L, 0);<br class="">+       }<br class="">        lua_settable(L, -3);<br class=""> <br class="">        lua_pushliteral(L, "time");<br class=""><br class=""></div></body></html>