MCPcopy Create free account
hub / github.com/F-Stack/f-stack / cronUpdateMemoryStats

Function cronUpdateMemoryStats

app/redis-6.2.6/src/server.c:1993–2025  ·  view source on GitHub ↗

Called from serverCron and loadingCron to update cached memory metrics. */

Source from the content-addressed store, hash-verified

1991
1992/* Called from serverCron and loadingCron to update cached memory metrics. */
1993void cronUpdateMemoryStats() {
1994 /* Record the max memory used since the server was started. */
1995 if (zmalloc_used_memory() > server.stat_peak_memory)
1996 server.stat_peak_memory = zmalloc_used_memory();
1997
1998 run_with_period(100) {
1999 /* Sample the RSS and other metrics here since this is a relatively slow call.
2000 * We must sample the zmalloc_used at the same time we take the rss, otherwise
2001 * the frag ratio calculate may be off (ratio of two samples at different times) */
2002 server.cron_malloc_stats.process_rss = zmalloc_get_rss();
2003 server.cron_malloc_stats.zmalloc_used = zmalloc_used_memory();
2004 /* Sampling the allocator info can be slow too.
2005 * The fragmentation ratio it'll show is potentially more accurate
2006 * it excludes other RSS pages such as: shared libraries, LUA and other non-zmalloc
2007 * allocations, and allocator reserved pages that can be pursed (all not actual frag) */
2008 zmalloc_get_allocator_info(&server.cron_malloc_stats.allocator_allocated,
2009 &server.cron_malloc_stats.allocator_active,
2010 &server.cron_malloc_stats.allocator_resident);
2011 /* in case the allocator isn't providing these stats, fake them so that
2012 * fragmentation info still shows some (inaccurate metrics) */
2013 if (!server.cron_malloc_stats.allocator_resident) {
2014 /* LUA memory isn't part of zmalloc_used, but it is part of the process RSS,
2015 * so we must deduct it in order to be able to calculate correct
2016 * "allocator fragmentation" ratio */
2017 size_t lua_memory = lua_gc(server.lua,LUA_GCCOUNT,0)*1024LL;
2018 server.cron_malloc_stats.allocator_resident = server.cron_malloc_stats.process_rss - lua_memory;
2019 }
2020 if (!server.cron_malloc_stats.allocator_active)
2021 server.cron_malloc_stats.allocator_active = server.cron_malloc_stats.allocator_resident;
2022 if (!server.cron_malloc_stats.allocator_allocated)
2023 server.cron_malloc_stats.allocator_allocated = server.cron_malloc_stats.zmalloc_used;
2024 }
2025}
2026
2027/* This is our timer interrupt, called server.hz times per second.
2028 * Here is where we do a number of things that need to be done asynchronously.

Callers 2

serverCronFunction · 0.85
whileBlockedCronFunction · 0.85

Calls 4

zmalloc_used_memoryFunction · 0.85
zmalloc_get_rssFunction · 0.85
lua_gcFunction · 0.50

Tested by

no test coverage detected