Add API to get remaining lifetime as a percentage.
This differs slightly from the previous API, which exists for idle
maintenance, whereas this value is intended to be displayed to users.
First, it returns remaining lifetime, rather than used lifetime. Second,
it rounds up the returned value for usabilty purposes. This isn't an
issue on Pixel (which reports at 1% granularity), but devices which
report at 10% granularity should show 100% out-of-box, which is not
possible to distinguish in the old API.
Bug: 309886423
Test: StorageManager.getRemainingStorageLifetime
Change-Id: Ic5f6ec9969667302ba8bad95b2765e2cc740bed4
diff --git a/IdleMaint.cpp b/IdleMaint.cpp
index 7d3eaf4..fafa280 100644
--- a/IdleMaint.cpp
+++ b/IdleMaint.cpp
@@ -507,6 +507,30 @@
return -1;
}
+int32_t GetStorageRemainingLifetime() {
+ std::string path = getDevSysfsPath();
+ if (path.empty()) {
+ return -1;
+ }
+
+ std::string lifeTimeBasePath = path + "/health_descriptor/life_time_estimation_";
+
+ int32_t lifeTime = getLifeTime(lifeTimeBasePath + "c");
+ if (lifeTime == -1) {
+ int32_t lifeTimeA = getLifeTime(lifeTimeBasePath + "a");
+ int32_t lifeTimeB = getLifeTime(lifeTimeBasePath + "b");
+ lifeTime = std::max(lifeTimeA, lifeTimeB);
+ if (lifeTime <= 0) {
+ return -1;
+ }
+
+ // 1 = 0-10% used, 10 = 90-100% used. Subtract 1 so that a brand new
+ // device looks 0% used.
+ lifeTime = (lifeTime - 1) * 10;
+ }
+ return 100 - std::clamp(lifeTime, 0, 100);
+}
+
void SetGCUrgentPace(int32_t neededSegments, int32_t minSegmentThreshold, float dirtyReclaimRate,
float reclaimWeight, int32_t gcPeriod, int32_t minGCSleepTime,
int32_t targetDirtyRatio) {