#include "GcStats.h"
#include "Base/LogFile.h"
#include "Heap/Heap.h"
namespace MapleRuntime {
size_t g_gcCount = 0;
uint64_t g_gcTotalTimeUs = 0;
size_t g_gcCollectedTotalBytes = 0;
uint64_t GCStats::prevGcStartTime = TimeUtil::NanoSeconds() - LONG_MIN_HEU_GC_INTERVAL_NS;
uint64_t GCStats::prevGcFinishTime = TimeUtil::NanoSeconds() - LONG_MIN_HEU_GC_INTERVAL_NS;
void GCStats::Init()
{
isConcurrentMark = false;
async = false;
gcStartTime = TimeUtil::NanoSeconds();
gcEndTime = TimeUtil::NanoSeconds();
collectedObjects = 0;
collectedBytes = 0;
fromSpaceSize = 0;
smallGarbageSize = 0;
pinnedSpaceSize = 0;
pinnedGarbageSize = 0;
largeSpaceSize = 0;
largeGarbageSize = 0;
liveBytesBeforeGC = 0;
liveBytesAfterGC = 0;
garbageRatio = 0.0;
collectionRate = 0.0;
heapThreshold = std::min(CangjieRuntime::GetGCParam().gcThreshold, 20 * MB);
heapThreshold = std::min(static_cast<size_t>(Heap::GetHeap().GetMaxCapacity() * 0.2), heapThreshold);
}
void GCStats::Dump() const
{
size_t liveSize = Heap::GetHeap().GetAllocatedSize();
size_t heapSize = Heap::GetHeap().GetUsedPageSize();
double utilization = (heapSize == 0) ? 0 : (static_cast<double>(liveSize) / heapSize) * 100;
LOG(RTLOG_INFO,
"GC for %s: %s collected objects: %zu->%s, %.2f%% utilization (%zu->%s/%zu->%s), "
"total GC time: %llu->%s",
g_gcRequests[reason].name, (async ? "async:" : "sync:"),
collectedBytes, PrettyOrderInfo(collectedBytes, "B").Str(),
utilization, liveSize, PrettyOrderInfo(liveSize, "B").Str(),
heapSize, PrettyOrderInfo(heapSize, "B").Str(),
gcEndTime - gcStartTime, PrettyOrderMathNano(gcEndTime - gcStartTime, "s").Str());
VLOG(REPORT, "allocated size: %s, heap size: %s, heap utilization: %.2f%%", Pretty(liveSize).Str(),
Pretty(heapSize).Str(), utilization);
}
}