Set VM dirty ratio to zero when UMS is active

Improves UI responsiveness when copying large amount of data to the device.

BUG: 3131847

Change-Id: I4aa5ade7e2cd7e5110c8f0f7ee43bdc57577e11d
Signed-off-by: Mike Lockwood <lockwood@google.com>
diff --git a/VolumeManager.cpp b/VolumeManager.cpp
index 3b229b7..32b5679 100644
--- a/VolumeManager.cpp
+++ b/VolumeManager.cpp
@@ -57,6 +57,10 @@
     mBroadcaster = NULL;
     mUsbMassStorageEnabled = false;
     mUsbConnected = false;
+    mUmsSharingCount = 0;
+    mSavedDirtyRatio = -1;
+    // set dirty ratio to 0 when UMS is active
+    mUmsDirtyRatio = 0;
 
     readInitialState();
 }
@@ -1064,6 +1068,21 @@
 
     close(fd);
     v->handleVolumeShared();
+    if (mUmsSharingCount++ == 0) {
+        FILE* fp;
+        mSavedDirtyRatio = -1; // in case we fail
+        if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
+            char line[16];
+            if (fgets(line, sizeof(line), fp) && sscanf(line, "%d", &mSavedDirtyRatio)) {
+                fprintf(fp, "%d\n", mUmsDirtyRatio);
+            } else {
+                SLOGE("Failed to read dirty_ratio (%s)", strerror(errno));
+            }
+            fclose(fp);
+        } else {
+            SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
+        }
+    }
     return 0;
 }
 
@@ -1100,6 +1119,16 @@
 
     close(fd);
     v->handleVolumeUnshared();
+    if (--mUmsSharingCount == 0 && mSavedDirtyRatio != -1) {
+        FILE* fp;
+        if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
+            fprintf(fp, "%d\n", mSavedDirtyRatio);
+            fclose(fp);
+        } else {
+            SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
+        }
+        mSavedDirtyRatio = -1;
+    }
     return 0;
 }