Merge "perfetto-ui: Add canned queries"
diff --git a/docs/trace-processor.md b/docs/trace-processor.md
index 5644457..c79d459 100644
--- a/docs/trace-processor.md
+++ b/docs/trace-processor.md
@@ -148,7 +148,10 @@
 
 ### CPU time for top 100 processes
 ``` sql
-select proc_name, cpu_sec from (select process.name as proc_name, upid, cpu_sec from (select utid, sum(dur)/1e9 as cpu_sec from sched group by utid) left join thread using(utid) left join process using(upid)) group by upid order by cpu_sec desc limit 100
+select process.name, tot_proc/1e9 as cpu_sec from (select upid, sum(tot_thd) as
+tot_proc from (select utid, sum(dur) as tot_thd from sched group by utid) join
+thread using(utid) group by upid) join process using(upid) order by cpu_sec desc
+limit 100;
 ```
 
 ### CPU time for top 100 processes broken down by cpu
diff --git a/ui/src/frontend/sidebar.ts b/ui/src/frontend/sidebar.ts
index 8776b9b..da204ab 100644
--- a/ui/src/frontend/sidebar.ts
+++ b/ui/src/frontend/sidebar.ts
@@ -16,6 +16,7 @@
 
 import {
   createPermalink,
+  executeQuery,
   navigate,
   openTraceFromFile,
   openTraceFromUrl
@@ -23,6 +24,46 @@
 
 import {globals} from './globals';
 
+const ALL_PROCESSES_QUERY = 'select name, pid from process order by name;';
+
+const CPU_TIME_FOR_PROCESSES = `
+select
+  process.name,
+  tot_proc/1e9 as cpu_sec
+from
+  (select
+    upid,
+    sum(tot_thd) as tot_proc
+  from
+    (select
+      utid,
+      sum(dur) as tot_thd
+    from sched group by utid)
+  join thread using(utid) group by upid)
+join process using(upid)
+order by cpu_sec desc limit 100;`;
+
+const CYCLES_PER_P_STATE_PER_CPU = `
+select ref as cpu, value as freq, sum(dur * value)/1e6 as mcycles
+from counters group by cpu, freq order by mcycles desc limit 20;`;
+
+const CPU_TIME_BY_CLUSTER_BY_PROCESS = `
+select
+thread.name as comm,
+case when cpug = 0 then 'big' else 'little' end as core,
+cpu_sec from
+  (select cpu/4 cpug, utid, sum(dur)/1e9 as cpu_sec
+  from sched group by utid, cpug order by cpu_sec desc)
+left join thread using(utid)
+limit 20;`;
+
+function createCannedQuery(query: string): (_: Event) => void {
+  return (e: Event) => {
+    e.preventDefault();
+    globals.dispatch(executeQuery('0', 'command', query));
+  };
+}
+
 const EXAMPLE_TRACE_URL =
     'https://storage.googleapis.com/perfetto-misc/example_trace_30s';
 
@@ -63,8 +104,26 @@
     title: 'Metrics and auditors',
     summary: 'Add new tracks to the workspace',
     items: [
-      {t: 'CPU Usage breakdown', a: navigateHome, i: 'table_chart'},
-      {t: 'Memory breakdown', a: navigateHome, i: 'memory'},
+      {
+        t: 'All Processes',
+        a: createCannedQuery(ALL_PROCESSES_QUERY),
+        i: 'search',
+      },
+      {
+        t: 'CPU Time by process',
+        a: createCannedQuery(CPU_TIME_FOR_PROCESSES),
+        i: 'search',
+      },
+      {
+        t: 'Cycles by p-state by CPU',
+        a: createCannedQuery(CYCLES_PER_P_STATE_PER_CPU),
+        i: 'search',
+      },
+      {
+        t: 'CPU Time by cluster by process',
+        a: createCannedQuery(CPU_TIME_BY_CLUSTER_BY_PROCESS),
+        i: 'search',
+      },
     ],
   },
 ];