atrace: make clock & bus bw tracing optional

This change makes enabling clock tracing and bus bandwidth tracing depend on
whether the device's kernel supports these features.  If it's not supported it
will be skipped.

Change-Id: I51d57299693635b0ecebb75de5dccd5b62d47be3
diff --git a/atrace/atrace.c b/atrace/atrace.c
index 381a4ed..5edc47b 100644
--- a/atrace/atrace.c
+++ b/atrace/atrace.c
@@ -93,6 +93,11 @@
 static const char* k_traceMarkerPath =
     "/sys/kernel/debug/tracing/trace_marker";
 
+// Check whether a file exists.
+static bool fileExists(const char* filename) {
+    return access(filename, F_OK) != -1;
+}
+
 // Write a string to a file, returning true if the write was successful.
 bool writeStr(const char* filename, const char* str)
 {
@@ -151,11 +156,14 @@
 // Enable or disable tracing of the Bus utilization.
 static bool setBusUtilizationTracingEnable(bool enable)
 {
-    bool ok = false;
+    bool ok = true, oneSet = false;
     // these can be platform specific so make sure that at least
     // one succeeds.
-    ok |= setKernelOptionEnable(k_memoryBusEnablePath, enable);
-    return ok;
+    if (fileExists(k_memoryBusEnablePath)) {
+        ok &= setKernelOptionEnable(k_memoryBusEnablePath, enable);
+        oneSet |= ok;
+    }
+    return ok && (oneSet || !enable);
 }
 
 // Enable or disable tracing of the CPU clock frequency.
@@ -163,7 +171,9 @@
 {
     bool ok = true;
     ok &= setKernelOptionEnable(k_cpuFreqEnablePath, enable);
-    ok &= setKernelOptionEnable(k_clockSetRateEnablePath, enable);
+    if (fileExists(k_clockSetRateEnablePath)) {
+        ok &= setKernelOptionEnable(k_clockSetRateEnablePath, enable);
+    }
     return ok;
 }
 
@@ -177,7 +187,11 @@
 // the CPU load.
 static bool setGovernorLoadTracingEnable(bool enable)
 {
-    return setKernelOptionEnable(k_governorLoadEnablePath, enable);
+    bool ok = true;
+    if (fileExists(k_governorLoadEnablePath) || enable) {
+        ok &= setKernelOptionEnable(k_governorLoadEnablePath, enable);
+    }
+    return ok;
 }
 
 // Enable or disable tracing of the kernel workqueues.
@@ -232,11 +246,6 @@
     return writeStr(k_traceClockPath, enable ? "global" : "local");
 }
 
-// Check whether a file exists.
-static bool fileExists(const char* filename) {
-    return access(filename, F_OK) != -1;
-}
-
 // Enable tracing in the kernel.
 static bool startTrace(bool isRoot)
 {
@@ -245,12 +254,9 @@
     // Set up the tracing options that don't require root.
     ok &= setTraceOverwriteEnable(g_traceOverwrite);
     ok &= setSchedSwitchTracingEnable(g_traceSchedSwitch);
-    ok &= setBusUtilizationTracingEnable(g_traceBusUtilization);
     ok &= setFrequencyTracingEnable(g_traceFrequency);
     ok &= setCpuIdleTracingEnable(g_traceCpuIdle);
-    if (fileExists(k_governorLoadEnablePath) || g_traceGovernorLoad) {
-        ok &= setGovernorLoadTracingEnable(g_traceGovernorLoad);
-    }
+    ok &= setGovernorLoadTracingEnable(g_traceGovernorLoad);
     ok &= setTraceBufferSizeKB(g_traceBufferSizeKB);
     ok &= setGlobalClockEnable(true);
 
@@ -258,6 +264,7 @@
     // require root should have errored out earlier if we're not running as
     // root.
     if (isRoot) {
+        ok &= setBusUtilizationTracingEnable(g_traceBusUtilization);
         ok &= setWorkqueueTracingEnabled(g_traceWorkqueue);
         ok &= setDiskTracingEnabled(g_traceDisk);
     }
@@ -281,14 +288,12 @@
     // Set the options back to their defaults.
     setTraceOverwriteEnable(true);
     setSchedSwitchTracingEnable(false);
-    setBusUtilizationTracingEnable(false);
     setFrequencyTracingEnable(false);
-    if (fileExists(k_governorLoadEnablePath)) {
-        setGovernorLoadTracingEnable(false);
-    }
+    setGovernorLoadTracingEnable(false);
     setGlobalClockEnable(false);
 
     if (isRoot) {
+        setBusUtilizationTracingEnable(false);
         setWorkqueueTracingEnabled(false);
         setDiskTracingEnabled(false);
     }