OSDN Git Service

Use random data for benchmark instead of zeros.
authorJeff Sharkey <jsharkey@android.com>
Wed, 29 Jul 2015 16:14:21 +0000 (09:14 -0700)
committerJeff Sharkey <jsharkey@android.com>
Wed, 29 Jul 2015 17:03:11 +0000 (10:03 -0700)
If we always write zeros, we're leaving a giant pile of known
plaintext at an almost deterministic location on newly formatted
volumes.  To avoid this, repeat a 64K chunk of random data.

Bug: 22816936
Change-Id: Iedc067a519bd676a93b9d74ea4f9f77c84c8461c

BenchmarkGen.h
bench/benchgen.py

index 3e46c21..6813b08 100644 (file)
@@ -31,6 +31,8 @@
 #include <algorithm>
 #include <string>
 
+#include <Utils.h>
+
 namespace android {
 namespace vold {
 
@@ -4024,35 +4026,32 @@ free(buf);
 return 0;
 }
 
-static status_t CreateFile(const char* name, size_t len) {
-    status_t res = -1;
-    int in = -1;
+static status_t CreateFile(const char* name, int len) {
+    int chunk = std::min(len, 65536);
     int out = -1;
+    std::string buf;
 
-    if ((in = TEMP_FAILURE_RETRY(open("/dev/zero", O_RDONLY))) < 0) {
-        PLOG(ERROR) << "Failed to open";
-        goto done;
+    if (android::vold::ReadRandomBytes(chunk, buf) != OK) {
+        LOG(ERROR) << "Failed to read random data";
+        return -EIO;
     }
     if ((out = TEMP_FAILURE_RETRY(open(name, O_WRONLY|O_CREAT|O_TRUNC))) < 0) {
         PLOG(ERROR) << "Failed to open " << name;
-        goto done;
+        return -errno;
     }
 
-    char buf[65536];
     while (len > 0) {
-        int n = read(in, buf, std::min(len, sizeof(buf)));
-        if (write(out, buf, n) != n) {
+        int n = write(out, buf.c_str(), std::min(len, chunk));
+        if (n < 0) {
             PLOG(ERROR) << "Failed to write";
-            goto done;
+            close(out);
+            return -errno;
         }
         len -= n;
     }
 
-    res = 0;
-done:
-    close(in);
     close(out);
-    return res;
+    return OK;
 }
 
 static status_t BenchmarkCreate() {
index 077336c..77262c4 100644 (file)
@@ -159,6 +159,8 @@ with open("BenchmarkGen.h", 'w') as bench:
 #include <algorithm>
 #include <string>
 
+#include <Utils.h>
+
 namespace android {
 namespace vold {
 
@@ -262,35 +264,32 @@ free(buf);
 return 0;
 }
 
-static status_t CreateFile(const char* name, size_t len) {
-    status_t res = -1;
-    int in = -1;
+static status_t CreateFile(const char* name, int len) {
+    int chunk = std::min(len, 65536);
     int out = -1;
+    std::string buf;
 
-    if ((in = TEMP_FAILURE_RETRY(open("/dev/zero", O_RDONLY))) < 0) {
-        PLOG(ERROR) << "Failed to open";
-        goto done;
+    if (android::vold::ReadRandomBytes(chunk, buf) != OK) {
+        LOG(ERROR) << "Failed to read random data";
+        return -EIO;
     }
     if ((out = TEMP_FAILURE_RETRY(open(name, O_WRONLY|O_CREAT|O_TRUNC))) < 0) {
         PLOG(ERROR) << "Failed to open " << name;
-        goto done;
+        return -errno;
     }
 
-    char buf[65536];
     while (len > 0) {
-        int n = read(in, buf, std::min(len, sizeof(buf)));
-        if (write(out, buf, n) != n) {
+        int n = write(out, buf.c_str(), std::min(len, chunk));
+        if (n < 0) {
             PLOG(ERROR) << "Failed to write";
-            goto done;
+            close(out);
+            return -errno;
         }
         len -= n;
     }
 
-    res = 0;
-done:
-    close(in);
     close(out);
-    return res;
+    return OK;
 }
 
 static status_t BenchmarkCreate() {