fix: log warning if cache-hit rate is below specified threshold

This commit is contained in:
Akash Yadav
2023-10-09 19:50:05 +05:30
parent 1e0dc747e0
commit b2447c28ff
2 changed files with 20 additions and 4 deletions
@@ -26,8 +26,9 @@ import java.util.Objects;
public class LogLine implements Recyclable {
private static final RecyclableObjectPool<LogLine> logLinePool = new RecyclableObjectPool<>(
RecyclableObjectPool.CAPACITY_DEFAULT, LogLine.class, LogLine::new);
// do not cache too many LogLine items
// LogLines should be recycled as soon as they are appended to the log view
private static final RecyclableObjectPool<LogLine> logLinePool = new RecyclableObjectPool<>(16, LogLine.class, LogLine::new);
public String unformatted;
public String date;
@@ -36,6 +36,9 @@ class RecyclableObjectPool<RecyclableT : RecyclableObjectPool.Recyclable> @JvmOv
companion object {
var DEBUG = false
var LOG_METRICS_ON_N_ACCESS = 400
private const val CACHE_HIT_WARNING_THRESHOLD = 50f
private val log by lazy {
ILogger.newInstance("RecyclableObjectPool")
@@ -89,7 +92,7 @@ class RecyclableObjectPool<RecyclableT : RecyclableObjectPool.Recyclable> @JvmOv
accCount.set(IntPair.pack(access, ++cacheHit))
} ?: objFactory.create()
if (DEBUG && access % 100 == 0) {
if (DEBUG && access % LOG_METRICS_ON_N_ACCESS == 0) {
logMetrics()
}
@@ -127,16 +130,28 @@ class RecyclableObjectPool<RecyclableT : RecyclableObjectPool.Recyclable> @JvmOv
val rec = recCount.get()
val access = IntPair.getFirst(acc)
val cacheHit = IntPair.getSecond(acc)
val cacheHitRate = cacheHitRate()
val simpleName = objName.let { if (it.contains('.')) it.substringAfterLast('.') else it }
log.debug("${javaClass.simpleName}: $simpleName($objName)")
log.debug(" Recycle count : $rec")
log.debug(" Access count : $access")
log.debug(" Cache hit count : $cacheHit")
log.debug(" Cache hit rate : ${cacheHitRate()}%")
log.debug(" Cache hit rate : $cacheHitRate%")
log.debug(" Cache miss rate : ${cacheMissRate()}%")
log.debug(" Recycle rate : ${recycleRate()}%")
log.debug(" Cache utilization rate : ${cacheUtilization()}%")
log.debug(" Objects in cache : ${cache.size}")
if (cacheHitRate < CACHE_HIT_WARNING_THRESHOLD) {
// Cache hit rate is less than threshold
// something might be wrong
// log an error
log.error("!!!!!!!!!!!!!!!!!!!!! CRITICAL ERROR !!!!!!!!!!!!!!!!!!!!!")
log.error("Cache-hit rate for '${simpleName}' is less than ${CACHE_HIT_WARNING_THRESHOLD}%!!")
log.error("Make sure that instances of $objName are obtained using the object pool")
log.error("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
}
}
/**