MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / whileBlockedCron

Function whileBlockedCron

src/server.cpp:2777–2819  ·  view source on GitHub ↗

This function fill in the role of serverCron during RDB or AOF loading, and * also during blocked scripts. * It attempts to do its duties at a similar rate as the configured g_pserver->hz, * and updates cronloops variable so that similarly to serverCron, the * run_with_period can be used. */

Source from the content-addressed store, hash-verified

2775 * and updates cronloops variable so that similarly to serverCron, the
2776 * run_with_period can be used. */
2777void whileBlockedCron() {
2778 /* Here we may want to perform some cron jobs (normally done g_pserver->hz times
2779 * per second). */
2780
2781 /* Since this function depends on a call to blockingOperationStarts, let's
2782 * make sure it was done. */
2783 serverAssert(g_pserver->blocked_last_cron);
2784
2785 /* In case we where called too soon, leave right away. This way one time
2786 * jobs after the loop below don't need an if. and we don't bother to start
2787 * latency monitor if this function is called too often. */
2788 if (g_pserver->blocked_last_cron >= g_pserver->mstime)
2789 return;
2790
2791 mstime_t latency;
2792 latencyStartMonitor(latency);
2793
2794 /* In some cases we may be called with big intervals, so we may need to do
2795 * extra work here. This is because some of the functions in serverCron rely
2796 * on the fact that it is performed every 10 ms or so. For instance, if
2797 * activeDefragCycle needs to utilize 25% cpu, it will utilize 2.5ms, so we
2798 * need to call it multiple times. */
2799 long hz_ms = 1000/g_pserver->hz;
2800 while (g_pserver->blocked_last_cron < g_pserver->mstime) {
2801
2802 /* Defrag keys gradually. */
2803 activeDefragCycle();
2804
2805 g_pserver->blocked_last_cron += hz_ms;
2806
2807 /* Increment cronloop so that run_with_period works. */
2808 g_pserver->cronloops++;
2809 }
2810
2811 /* Other cron jobs do not need to be done in a loop. No need to check
2812 * g_pserver->blocked_last_cron since we have an early exit at the top. */
2813
2814 /* Update memory stats during loading (excluding blocked scripts) */
2815 if (g_pserver->loading) cronUpdateMemoryStats();
2816
2817 latencyEndMonitor(latency);
2818 latencyAddSampleIfNeeded("while-blocked-cron",latency);
2819}
2820
2821extern __thread int ProcessingEventsWhileBlocked;
2822

Callers 1

Calls 2

activeDefragCycleFunction · 0.85
cronUpdateMemoryStatsFunction · 0.85

Tested by

no test coverage detected