OSDN Git Service

atrace: don't use sendfile.
authorJosh Gao <jmgao@google.com>
Tue, 11 Apr 2017 22:21:13 +0000 (15:21 -0700)
committerJosh Gao <jmgao@google.com>
Tue, 11 Apr 2017 22:32:07 +0000 (15:32 -0700)
sendfile appears to have horrendous performance when used with the
ftrace output file, being up to 100x slower than the naive read/write
loop.

Switch to doing that instead, speeding up atrace from:

    $ atrace --async_start sched freq; sleep 1; time atrace --async_stop > dummy
    c apturing trace...    0m08.93s real     0m00.08s user     0m07.98s system

to:

    $ atrace --async_start sched freq; sleep 1; time atrace --async_stop > dummy
    capturing trace...    0m00.78s real     0m00.07s user     0m00.21s system

Bug: http://b/37164190
Test: atrace --async_start sched freq; sleep 1; time atrace --async_stop > dummy
Change-Id: I22fe1871e263867f9ac54c8f5b474df824b4bc69

cmds/atrace/atrace.cpp

index acf63c3..add5285 100644 (file)
@@ -26,7 +26,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/sendfile.h>
 #include <time.h>
 #include <unistd.h>
 #include <zlib.h>
@@ -974,11 +973,16 @@ static void dumpTrace(int outFd)
             fprintf(stderr, "error cleaning up zlib: %d\n", result);
         }
     } else {
-        ssize_t sent = 0;
-        while ((sent = sendfile(outFd, traceFD, NULL, 64*1024*1024)) > 0);
-        if (sent == -1) {
-            fprintf(stderr, "error dumping trace: %s (%d)\n", strerror(errno),
-                    errno);
+        char buf[4096];
+        ssize_t rc;
+        while ((rc = TEMP_FAILURE_RETRY(read(traceFD, buf, sizeof(buf)))) > 0) {
+            if (!android::base::WriteFully(outFd, buf, rc)) {
+                fprintf(stderr, "error writing trace: %s\n", strerror(errno));
+                break;
+            }
+        }
+        if (rc == -1) {
+            fprintf(stderr, "error dumping trace: %s\n", strerror(errno));
         }
     }