atrace: add support for tracing CPU idle events

This change adds the -i command line flag to atrace to enable tracing CPU idle
kernel events.

Change-Id: I4a2c8619224c0f50c7fd7c61c7b24fa2211f656c
diff --git a/atrace/atrace.c b/atrace/atrace.c
index 7b4f55e..a674295 100644
--- a/atrace/atrace.c
+++ b/atrace/atrace.c
@@ -29,6 +29,7 @@
 static int g_traceDurationSeconds = 5;
 static bool g_traceSchedSwitch = false;
 static bool g_traceCpuFrequency = false;
+static bool g_traceCpuIdle = false;
 static bool g_traceGovernorLoad = false;
 static bool g_traceWorkqueue = false;
 static bool g_traceOverwrite = false;
@@ -54,6 +55,9 @@
 static const char* k_cpuFreqEnablePath =
     "/sys/kernel/debug/tracing/events/power/cpu_frequency/enable";
 
+static const char* k_cpuIdleEnablePath =
+    "/sys/kernel/debug/tracing/events/power/cpu_idle/enable";
+
 static const char* k_governorLoadEnablePath =
     "/sys/kernel/debug/tracing/events/cpufreq_interactive/enable";
 
@@ -117,6 +121,12 @@
     return setKernelOptionEnable(k_cpuFreqEnablePath, enable);
 }
 
+// Enable or disable tracing of CPU idle events.
+static bool setCpuIdleTracingEnable(bool enable)
+{
+    return setKernelOptionEnable(k_cpuIdleEnablePath, enable);
+}
+
 // Enable or disable tracing of the interactive CPU frequency governor's idea of
 // the CPU load.
 static bool setGovernorLoadTracingEnable(bool enable)
@@ -184,6 +194,7 @@
     ok &= setTraceOverwriteEnable(g_traceOverwrite);
     ok &= setSchedSwitchTracingEnable(g_traceSchedSwitch);
     ok &= setCpuFrequencyTracingEnable(g_traceCpuFrequency);
+    ok &= setCpuIdleTracingEnable(g_traceCpuIdle);
     if (fileExists(k_governorLoadEnablePath) || g_traceGovernorLoad) {
         ok &= setGovernorLoadTracingEnable(g_traceGovernorLoad);
     }
@@ -363,7 +374,7 @@
     for (;;) {
         int ret;
 
-        ret = getopt(argc, argv, "b:cflst:wz");
+        ret = getopt(argc, argv, "b:ciflst:wz");
 
         if (ret < 0) {
             break;
@@ -378,6 +389,10 @@
                 g_traceOverwrite = true;
             break;
 
+            case 'i':
+                g_traceCpuIdle = true;
+            break;
+
             case 'l':
                 g_traceGovernorLoad = true;
             break;