storaged: record userdata space utilization

Update disk space utilization together with diskstats since we will
use free space size to co-relate disk performance.

Bug: 63629306
Change-Id: I4e5694aaff3b71aa56db451f1bc92ccfb07e5086
diff --git a/storaged/storaged_info.cpp b/storaged/storaged_info.cpp
index 434bd74..b5fb13e 100644
--- a/storaged/storaged_info.cpp
+++ b/storaged/storaged_info.cpp
@@ -18,6 +18,7 @@
 
 #include <stdio.h>
 #include <string.h>
+#include <sys/statvfs.h>
 
 #include <android-base/file.h>
 #include <android-base/parseint.h>
@@ -30,13 +31,42 @@
 using namespace std;
 using namespace android::base;
 
-void report_storage_health()
-{
-    emmc_info_t mmc;
-    ufs_info_t ufs;
+const string emmc_info_t::emmc_sysfs = "/sys/bus/mmc/devices/mmc0:0001/";
+const string emmc_info_t::emmc_debugfs = "/d/mmc0/mmc0:0001/ext_csd";
+const char* emmc_info_t::emmc_ver_str[9] = {
+    "4.0", "4.1", "4.2", "4.3", "Obsolete", "4.41", "4.5", "5.0", "5.1"
+};
 
-    mmc.report();
-    ufs.report();
+const string ufs_info_t::health_file = "/sys/devices/soc/624000.ufshc/health";
+
+static bool FileExists(const std::string& filename)
+{
+  struct stat buffer;
+  return stat(filename.c_str(), &buffer) == 0;
+}
+
+storage_info_t* storage_info_t::get_storage_info()
+{
+    if (FileExists(emmc_info_t::emmc_sysfs) ||
+        FileExists(emmc_info_t::emmc_debugfs)) {
+        return new emmc_info_t;
+    }
+    if (FileExists(ufs_info_t::health_file)) {
+        return new ufs_info_t;
+    }
+    return new storage_info_t;
+}
+
+void storage_info_t::refresh()
+{
+    struct statvfs buf;
+    if (statvfs(userdata_path.c_str(), &buf) != 0) {
+        PLOG_TO(SYSTEM, WARNING) << "Failed to get userdata info";
+        return;
+    }
+
+    userdata_total_kb = buf.f_bsize * buf.f_blocks >> 10;
+    userdata_free_kb = buf.f_bfree * buf.f_blocks >> 10;
 }
 
 void storage_info_t::publish()
@@ -46,13 +76,12 @@
         << LOG_ID_EVENTS;
 }
 
-bool emmc_info_t::report()
+void emmc_info_t::report()
 {
     if (!report_sysfs() && !report_debugfs())
-        return false;
+        return;
 
     publish();
-    return true;
 }
 
 bool emmc_info_t::report_sysfs()
@@ -136,21 +165,21 @@
     return true;
 }
 
-bool ufs_info_t::report()
+void ufs_info_t::report()
 {
     string buffer;
     if (!ReadFileToString(health_file, &buffer)) {
-        return false;
+        return;
     }
 
     vector<string> lines = Split(buffer, "\n");
     if (lines.empty()) {
-        return false;
+        return;
     }
 
     char rev[8];
     if (sscanf(lines[0].c_str(), "ufs version: 0x%7s\n", rev) < 1) {
-        return false;
+        return;
     }
 
     version = "ufs " + string(rev);
@@ -175,10 +204,9 @@
     }
 
     if (eol == 0 || (lifetime_a == 0 && lifetime_b == 0)) {
-        return false;
+        return;
     }
 
     publish();
-    return true;
 }