Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Fix NaN during cpu metric calculation.

kishorenc9a4105111df2
diff --git a/include/system_metrics.h b/include/system_metrics.h
index 9c640e68..2adbb316 100644
--- a/include/system_metrics.h
+++ b/include/system_metrics.h
@@ -70,10 +70,12 @@ private:
auto prev_total = prev_idle + prev_active;
auto now_total = now_idle + now_active;
- auto total_diff = float(now_total - prev_total);
- auto idle_diff = float(now_idle - prev_idle);
+ float total_diff = float(now_total - prev_total);
+ float idle_diff = float(now_idle - prev_idle);
+ float active_diff = total_diff - idle_diff;
- float active_percentage = ((total_diff - idle_diff) / total_diff) * 100;
+ // take care to avoid division by zero!
+ float active_percentage = (now_total == prev_total) ? 0 : ((active_diff / total_diff) * 100);
float idle_percentage = 100 - active_percentage;
cpu_stat_t stat;