diff options
author | Jeff Sharkey <jsharkey@android.com> | 2018-01-31 21:47:09 -0700 |
---|---|---|
committer | Jeff Sharkey <jsharkey@android.com> | 2018-01-31 21:47:22 -0700 |
commit | b18f899241231991e314c40990eb8e9f703732d4 (patch) | |
tree | 13009607cdf8412320156b67fde5b72aaa015afb /cmds/content/src | |
parent | 8028c8cd5d7d29ec04793dc9e040844611d6fdfb (diff) |
Use sendfile() and splice() to speed up copying.
There are several places across the OS where Java code is simply
copying data between two points, which requires bringing that data
out into userspace before going back into the kernel. (That's pretty
lame.) The patches for the recent Meltdown/Spectre security issues
have made this overhead even worse, so it's finally time to move this
copying directly into the kernel.
This change adds a couple new FileUtils.copy() methods which inspect
the given streams/FDs, and attempt to do as much optimization as
possible before falling back to a slower userspace-based copy.
Benchmarks are showing typical improvements of 44% for 32KB files,
50% for 32MB files, and 35% for 32MB pipes.
Plenty of tests are included, and there's a simple kill-switch that
can be used to enable/disable the feature if it starts causing any
trouble. (A future CL will enable the optimizations.)
Test: bit FrameworksCoreTests:android.os.FileUtilsTest
Test: vogar --mode app_process --benchmark frameworks/base/core/tests/benchmarks/src/android/os/FileUtilsBenchmark.java
Bug: 71932978
Change-Id: I52518d529da5d961610998b9f61399064d8025cd
Diffstat (limited to 'cmds/content/src')
-rw-r--r-- | cmds/content/src/com/android/commands/content/Content.java | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/cmds/content/src/com/android/commands/content/Content.java b/cmds/content/src/com/android/commands/content/Content.java index f75678b7fa1e..6e0bd3a81d84 100644 --- a/cmds/content/src/com/android/commands/content/Content.java +++ b/cmds/content/src/com/android/commands/content/Content.java @@ -26,6 +26,7 @@ import android.database.Cursor; import android.net.Uri; import android.os.Binder; import android.os.Bundle; +import android.os.FileUtils; import android.os.IBinder; import android.os.ParcelFileDescriptor; import android.os.Process; @@ -34,6 +35,7 @@ import android.text.TextUtils; import libcore.io.Streams; +import java.io.FileDescriptor; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -583,7 +585,7 @@ public class Content { @Override public void onExecute(IContentProvider provider) throws Exception { try (ParcelFileDescriptor fd = provider.openFile(null, mUri, "r", null, null)) { - Streams.copy(new FileInputStream(fd.getFileDescriptor()), System.out); + FileUtils.copy(fd.getFileDescriptor(), FileDescriptor.out); } } } @@ -596,7 +598,7 @@ public class Content { @Override public void onExecute(IContentProvider provider) throws Exception { try (ParcelFileDescriptor fd = provider.openFile(null, mUri, "w", null, null)) { - Streams.copy(System.in, new FileOutputStream(fd.getFileDescriptor())); + FileUtils.copy(FileDescriptor.in, fd.getFileDescriptor()); } } } |