diff options
author | Martijn Coenen <maco@google.com> | 2015-11-30 18:51:11 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2015-11-30 18:51:11 +0000 |
commit | 83a98b101c2850a936c0e612e4eb16d815db025e (patch) | |
tree | 7a5ade0a4b26ec27a5e8d2acd210e1b27d699ad3 | |
parent | 8f237272e0630b8f279db7f90aebfff73a1a9c00 (diff) | |
parent | d95358713b98875733e91fe8fb0459a0b88eab14 (diff) | |
download | native-83a98b101c2850a936c0e612e4eb16d815db025e.tar.gz |
Merge "Atrace: support streaming data to stdout."
-rw-r--r-- | cmds/atrace/atrace.cpp | 50 |
1 files changed, 47 insertions, 3 deletions
diff --git a/cmds/atrace/atrace.cpp b/cmds/atrace/atrace.cpp index decaded9c5..5a66bc302a 100644 --- a/cmds/atrace/atrace.cpp +++ b/cmds/atrace/atrace.cpp @@ -26,6 +26,7 @@ #include <string.h> #include <sys/sendfile.h> #include <time.h> +#include <unistd.h> #include <zlib.h> #include <binder/IBinder.h> @@ -211,6 +212,9 @@ static const char* k_tracingOnPath = static const char* k_tracePath = "/sys/kernel/debug/tracing/trace"; +static const char* k_traceStreamPath = + "/sys/kernel/debug/tracing/trace_pipe"; + static const char* k_traceMarkerPath = "/sys/kernel/debug/tracing/trace_marker"; @@ -729,6 +733,31 @@ static void stopTrace() setTracingEnabled(false); } +// Read data from the tracing pipe and forward to stdout +static void streamTrace() +{ + char trace_data[4096]; + int traceFD = open(k_traceStreamPath, O_RDWR); + if (traceFD == -1) { + fprintf(stderr, "error opening %s: %s (%d)\n", k_traceStreamPath, + strerror(errno), errno); + return; + } + while (!g_traceAborted) { + ssize_t bytes_read = read(traceFD, trace_data, 4096); + if (bytes_read > 0) { + write(STDOUT_FILENO, trace_data, bytes_read); + fflush(stdout); + } else { + if (!g_traceAborted) { + fprintf(stderr, "read returned %zd bytes err %d (%s)\n", + bytes_read, errno, strerror(errno)); + } + break; + } + } +} + // Read the current kernel trace and write it to stdout. static void dumpTrace() { @@ -875,6 +904,10 @@ static void showHelp(const char *cmd) " --async_dump dump the current contents of circular trace buffer\n" " --async_stop stop tracing and dump the current contents of circular\n" " trace buffer\n" + " --stream stream trace to stdout as it enters the trace buffer\n" + " Note: this can take significant CPU time, and is best\n" + " used for measuring things that are not affected by\n" + " CPU performance, like pagecache usage.\n" " --list_categories\n" " list the available tracing categories\n" ); @@ -886,6 +919,7 @@ int main(int argc, char **argv) bool traceStart = true; bool traceStop = true; bool traceDump = true; + bool traceStream = false; if (argc == 2 && 0 == strcmp(argv[1], "--help")) { showHelp(argv[0]); @@ -900,6 +934,7 @@ int main(int argc, char **argv) {"async_stop", no_argument, 0, 0 }, {"async_dump", no_argument, 0, 0 }, {"list_categories", no_argument, 0, 0 }, + {"stream", no_argument, 0, 0 }, { 0, 0, 0, 0 } }; @@ -966,6 +1001,9 @@ int main(int argc, char **argv) async = true; traceStart = false; traceStop = false; + } else if (!strcmp(long_options[option_index].name, "stream")) { + traceStream = true; + traceDump = false; } else if (!strcmp(long_options[option_index].name, "list_categories")) { listSupportedCategories(); exit(0); @@ -991,8 +1029,10 @@ int main(int argc, char **argv) ok &= startTrace(); if (ok && traceStart) { - printf("capturing trace..."); - fflush(stdout); + if (!traceStream) { + printf("capturing trace..."); + fflush(stdout); + } // We clear the trace after starting it because tracing gets enabled for // each CPU individually in the kernel. Having the beginning of the trace @@ -1002,7 +1042,7 @@ int main(int argc, char **argv) ok = clearTrace(); writeClockSyncMarker(); - if (ok && !async) { + if (ok && !async && !traceStream) { // Sleep to allow the trace to be captured. struct timespec timeLeft; timeLeft.tv_sec = g_traceDurationSeconds; @@ -1013,6 +1053,10 @@ int main(int argc, char **argv) } } while (nanosleep(&timeLeft, &timeLeft) == -1 && errno == EINTR); } + + if (traceStream) { + streamTrace(); + } } // Stop the trace and restore the default settings. |