Trim both internal and adopted private storage.

Refactor fstrim code to be encapsulated in unique task object, and
give it option of benchmarking when finished.  Trimming now includes
both storage from fstab and adopted private volumes.  Cleaner timing
stats are logged for each unique volume.

Add wakelock during ongoing async move tasks.  Push disk sysfs path
to framework so it can parse any SD card registers as desired.

Bug: 21831325
Change-Id: I76577685f5cae4929c251ad314ffdaeb5eb1c8bf
diff --git a/Benchmark.cpp b/Benchmark.cpp
index e329588..7a3af65 100644
--- a/Benchmark.cpp
+++ b/Benchmark.cpp
@@ -22,6 +22,7 @@
 #include <base/file.h>
 #include <base/logging.h>
 #include <cutils/iosched_policy.h>
+#include <private/android_filesystem_config.h>
 
 #include <sys/time.h>
 #include <sys/resource.h>
@@ -33,14 +34,19 @@
 namespace android {
 namespace vold {
 
-static std::string simpleRead(const std::string& path) {
-    std::string tmp;
-    ReadFileToString(path, &tmp);
-    tmp.erase(tmp.find_last_not_of(" \n\r") + 1);
-    return tmp;
+static void notifyResult(const std::string& path, int64_t create_d,
+        int64_t drop_d, int64_t run_d, int64_t destroy_d) {
+    std::string res(path +
+            + " " + BenchmarkIdent()
+            + " " + std::to_string(create_d)
+            + " " + std::to_string(drop_d)
+            + " " + std::to_string(run_d)
+            + " " + std::to_string(destroy_d));
+    VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
+            ResponseCode::BenchmarkResult, res.c_str(), false);
 }
 
-nsecs_t Benchmark(const std::string& path, const std::string& sysPath) {
+static nsecs_t benchmark(const std::string& path) {
     errno = 0;
     int orig_prio = getpriority(PRIO_PROCESS, 0);
     if (errno != 0) {
@@ -82,9 +88,11 @@
     sync();
     nsecs_t create = systemTime(SYSTEM_TIME_BOOTTIME);
 
+    LOG(VERBOSE) << "Before drop_caches";
     if (!WriteStringToFile("3", "/proc/sys/vm/drop_caches")) {
         PLOG(ERROR) << "Failed to drop_caches";
     }
+    LOG(VERBOSE) << "After drop_caches";
     nsecs_t drop = systemTime(SYSTEM_TIME_BOOTTIME);
 
     BenchmarkRun();
@@ -95,6 +103,16 @@
     sync();
     nsecs_t destroy = systemTime(SYSTEM_TIME_BOOTTIME);
 
+    if (chdir(orig_cwd) != 0) {
+        PLOG(ERROR) << "Failed to chdir";
+    }
+    if (android_set_ioprio(0, orig_clazz, orig_ioprio)) {
+        PLOG(ERROR) << "Failed to android_set_ioprio";
+    }
+    if (setpriority(PRIO_PROCESS, 0, orig_prio) != 0) {
+        PLOG(ERROR) << "Failed to setpriority";
+    }
+
     nsecs_t create_d = create - start;
     nsecs_t drop_d = drop - create;
     nsecs_t run_d = run - drop;
@@ -105,39 +123,27 @@
     LOG(INFO) << "run took " << nanoseconds_to_milliseconds(run_d) << "ms";
     LOG(INFO) << "destroy took " << nanoseconds_to_milliseconds(destroy_d) << "ms";
 
-    std::string detail;
-    detail += "id=" + BenchmarkIdent()
-            + ",cr=" + std::to_string(create_d)
-            + ",dr=" + std::to_string(drop_d)
-            + ",ru=" + std::to_string(run_d)
-            + ",de=" + std::to_string(destroy_d)
-            + ",si=" + simpleRead(sysPath + "/size")
-            + ",ve=" + simpleRead(sysPath + "/device/vendor")
-            + ",mo=" + simpleRead(sysPath + "/device/model")
-            + ",csd=" + simpleRead(sysPath + "/device/csd");
+    notifyResult(path, create_d, drop_d, run_d, destroy_d);
 
-    // Scrub CRC and serial number out of CID
-    std::string cid = simpleRead(sysPath + "/device/cid");
-    if (cid.length() == 32) {
-        cid.erase(32, 1);
-        cid.erase(18, 8);
-        detail += ",cid=" + cid;
-    }
-
-    VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
-            ResponseCode::BenchmarkResult, detail.c_str(), false);
-
-    if (chdir(orig_cwd) != 0) {
-        PLOG(ERROR) << "Failed to chdir";
-    }
-    if (android_set_ioprio(0, orig_clazz, orig_ioprio)) {
-        PLOG(ERROR) << "Failed to android_set_ioprio";
-    }
-    if (setpriority(PRIO_PROCESS, 0, orig_prio) != 0) {
-        PLOG(ERROR) << "Failed to setpriority";
-    }
     return run_d;
 }
 
+nsecs_t BenchmarkPrivate(const std::string& path) {
+    std::string benchPath(path);
+    benchPath += "/misc";
+    if (android::vold::PrepareDir(benchPath, 01771, AID_SYSTEM, AID_MISC)) {
+        return -1;
+    }
+    benchPath += "/vold";
+    if (android::vold::PrepareDir(benchPath, 0700, AID_ROOT, AID_ROOT)) {
+        return -1;
+    }
+    benchPath += "/bench";
+    if (android::vold::PrepareDir(benchPath, 0700, AID_ROOT, AID_ROOT)) {
+        return -1;
+    }
+    return benchmark(benchPath);
+}
+
 }  // namespace vold
 }  // namespace android