atrace: add clock rate and bus utilization

Change-Id: I4e1764b51225436fc2b3d61454ee822f511a1a7d
diff --git a/atrace/atrace.c b/atrace/atrace.c
index d81cb5c..381a4ed 100644
--- a/atrace/atrace.c
+++ b/atrace/atrace.c
@@ -30,7 +30,8 @@
 /* Command line options */
 static int g_traceDurationSeconds = 5;
 static bool g_traceSchedSwitch = false;
-static bool g_traceCpuFrequency = false;
+static bool g_traceFrequency = false;
+static bool g_traceBusUtilization = false;
 static bool g_traceCpuIdle = false;
 static bool g_traceDisk = false;
 static bool g_traceGovernorLoad = false;
@@ -58,9 +59,15 @@
 static const char* k_schedWakeupEnablePath =
     "/sys/kernel/debug/tracing/events/sched/sched_wakeup/enable";
 
+static const char* k_memoryBusEnablePath =
+    "/sys/kernel/debug/tracing/events/memory_bus/enable";
+
 static const char* k_cpuFreqEnablePath =
     "/sys/kernel/debug/tracing/events/power/cpu_frequency/enable";
 
+static const char *k_clockSetRateEnablePath =
+    "/sys/kernel/debug/tracing/events/power/clock_set_rate/enable";
+
 static const char* k_cpuIdleEnablePath =
     "/sys/kernel/debug/tracing/events/power/cpu_idle/enable";
 
@@ -141,10 +148,23 @@
     return ok;
 }
 
-// Enable or disable tracing of the CPU clock frequency.
-static bool setCpuFrequencyTracingEnable(bool enable)
+// Enable or disable tracing of the Bus utilization.
+static bool setBusUtilizationTracingEnable(bool enable)
 {
-    return setKernelOptionEnable(k_cpuFreqEnablePath, enable);
+    bool ok = false;
+    // these can be platform specific so make sure that at least
+    // one succeeds.
+    ok |= setKernelOptionEnable(k_memoryBusEnablePath, enable);
+    return ok;
+}
+
+// Enable or disable tracing of the CPU clock frequency.
+static bool setFrequencyTracingEnable(bool enable)
+{
+    bool ok = true;
+    ok &= setKernelOptionEnable(k_cpuFreqEnablePath, enable);
+    ok &= setKernelOptionEnable(k_clockSetRateEnablePath, enable);
+    return ok;
 }
 
 // Enable or disable tracing of CPU idle events.
@@ -225,7 +245,8 @@
     // Set up the tracing options that don't require root.
     ok &= setTraceOverwriteEnable(g_traceOverwrite);
     ok &= setSchedSwitchTracingEnable(g_traceSchedSwitch);
-    ok &= setCpuFrequencyTracingEnable(g_traceCpuFrequency);
+    ok &= setBusUtilizationTracingEnable(g_traceBusUtilization);
+    ok &= setFrequencyTracingEnable(g_traceFrequency);
     ok &= setCpuIdleTracingEnable(g_traceCpuIdle);
     if (fileExists(k_governorLoadEnablePath) || g_traceGovernorLoad) {
         ok &= setGovernorLoadTracingEnable(g_traceGovernorLoad);
@@ -260,7 +281,8 @@
     // Set the options back to their defaults.
     setTraceOverwriteEnable(true);
     setSchedSwitchTracingEnable(false);
-    setCpuFrequencyTracingEnable(false);
+    setBusUtilizationTracingEnable(false);
+    setFrequencyTracingEnable(false);
     if (fileExists(k_governorLoadEnablePath)) {
         setGovernorLoadTracingEnable(false);
     }
@@ -380,10 +402,11 @@
                     "  -b N            use a trace buffer size of N KB\n"
                     "  -c              trace into a circular buffer\n"
                     "  -d              trace disk I/O\n"
-                    "  -f              trace CPU frequency changes\n"
+                    "  -f              trace clock frequency changes\n"
                     "  -l              trace CPU frequency governor load\n"
                     "  -s              trace the kernel scheduler switches\n"
                     "  -t N            trace for N seconds [defualt 5]\n"
+                    "  -u              trace bus utilization\n"
                     "  -w              trace the kernel workqueue\n"
                     "  -z              compress the trace dump\n");
 }
@@ -415,7 +438,7 @@
     for (;;) {
         int ret;
 
-        ret = getopt(argc, argv, "b:cidflst:wz");
+        ret = getopt(argc, argv, "b:cidflst:uwz");
 
         if (ret < 0) {
             break;
@@ -447,7 +470,7 @@
             break;
 
             case 'f':
-                g_traceCpuFrequency = true;
+                g_traceFrequency = true;
             break;
 
             case 's':
@@ -458,6 +481,14 @@
                 g_traceDurationSeconds = atoi(optarg);
             break;
 
+            case 'u':
+                if (!isRoot) {
+                    fprintf(stderr, "error: tracing bus utilization requires root privileges\n");
+                    exit(1);
+                }
+                g_traceBusUtilization = true;
+            break;
+
             case 'w':
                 if (!isRoot) {
                     fprintf(stderr, "error: tracing kernel work queues requires root privileges\n");