Set the atrace clock to boot when possible and mono otherwise.
Bug: 32379831
Test: Inspected the trace_clock while running atrace, without including the .rc file change:
$ adb shell
marlin:/ # cat /d/tracing/trace_clock
[local] global counter uptime perf mono boot
marlin:/ # atrace --async_start freq
capturing trace...
marlin:/ # cat /d/tracing/trace_clock
local global counter uptime perf mono [boot]
marlin:/ # atrace --async_stop > /dev/null
marlin:/ # cat /d/tracing/trace_clock
local global counter uptime perf mono [boot]
marlin:/ # exit
Test: Inspected the trace_clock while running atrace, after the .rc file change:
$ adb shell
marlin:/ # cat /d/tracing/trace_clock
local global counter uptime perf mono [boot]
marlin:/ # atrace --async_start freq
capturing trace...
marlin:/ # cat /d/tracing/trace_clock
local global counter uptime perf mono [boot]
Change-Id: I9ec88df109b311b90c9d88fe3a70f9ce090b4d15
diff --git a/cmds/atrace/atrace.cpp b/cmds/atrace/atrace.cpp
index f8e0ad5..84820a6 100644
--- a/cmds/atrace/atrace.cpp
+++ b/cmds/atrace/atrace.cpp
@@ -450,8 +450,8 @@
return false;
}
- char buf[4097];
- ssize_t n = read(fd, buf, 4096);
+ char buf[100];
+ ssize_t n = read(fd, buf, 99);
close(fd);
if (n == -1) {
fprintf(stderr, "error reading %s: %s (%d)\n", k_traceClockPath,
@@ -475,13 +475,38 @@
return strcmp(mode, start) == 0;
}
-// Enable or disable the kernel's use of the global clock. Disabling the global
-// clock will result in the kernel using a per-CPU local clock.
+// Read the trace_clock sysfs file and return true if it contains the requested
+// value. The trace_clock file format is:
+// local [global] counter uptime perf
+static bool traceClockContains(const char *mode)
+{
+ int fd = open((g_traceFolder + k_traceClockPath).c_str(), O_RDONLY);
+ if (fd == -1) {
+ fprintf(stderr, "error opening %s: %s (%d)\n", k_traceClockPath,
+ strerror(errno), errno);
+ return false;
+ }
+
+ char buf[100];
+ ssize_t n = read(fd, buf, 99);
+ close(fd);
+ if (n == -1) {
+ fprintf(stderr, "error reading %s: %s (%d)\n", k_traceClockPath,
+ strerror(errno), errno);
+ return false;
+ }
+ buf[n] = '\0';
+
+ return strstr(buf, mode) != NULL;
+}
+
+// Set the clock to the best available option while tracing. Use 'boot' if it's
+// available; otherwise, use 'mono'.
// Any write to the trace_clock sysfs file will reset the buffer, so only
// update it if the requested value is not the current value.
-static bool setGlobalClockEnable(bool enable)
+static bool setClock()
{
- const char *clock = enable ? "global" : "local";
+ const char* clock = traceClockContains("boot") ? "boot" : "mono";
if (isTraceClock(clock)) {
return true;
@@ -783,7 +808,7 @@
ok &= setCategoriesEnableFromFile(g_categoriesFile);
ok &= setTraceOverwriteEnable(g_traceOverwrite);
ok &= setTraceBufferSizeKB(g_traceBufferSizeKB);
- ok &= setGlobalClockEnable(true);
+ ok &= setClock();
ok &= setPrintTgidEnableIfPresent(true);
ok &= setKernelTraceFuncs(g_kernelTraceFuncs);
@@ -859,7 +884,6 @@
// Set the options back to their defaults.
setTraceOverwriteEnable(true);
setTraceBufferSizeKB(1);
- setGlobalClockEnable(false);
setPrintTgidEnableIfPresent(false);
setKernelTraceFuncs(NULL);
}
@@ -1210,7 +1234,7 @@
if (ok && traceStart) {
if (!traceStream) {
- printf("capturing trace...");
+ printf("capturing trace...\n");
fflush(stdout);
}
diff --git a/cmds/atrace/atrace.rc b/cmds/atrace/atrace.rc
index cef41be..cfd2659 100644
--- a/cmds/atrace/atrace.rc
+++ b/cmds/atrace/atrace.rc
@@ -124,6 +124,10 @@
write /sys/kernel/debug/tracing/tracing_on 0
write /sys/kernel/tracing/tracing_on 0
+ # Set the trace clock to boot if it exists, falling back to mono if not.
+ write /d/tracing/trace_clock mono
+ write /d/tracing/trace_clock boot
+
# Allow only the shell group to read and truncate the kernel trace.
chown root shell /sys/kernel/debug/tracing/trace
chown root shell /sys/kernel/tracing/trace