metrics: add urgent ratio metric

Bug: 199288752
Test: <trace_processor_shell> --run-metrics android_simpleperf \
      <perfetto trace>
Change-Id: If4d60d51965d9ea273cc2e0318440c96a135ef98
diff --git a/Android.bp b/Android.bp
index e104f17..3b4bbdd 100644
--- a/Android.bp
+++ b/Android.bp
@@ -3673,6 +3673,7 @@
         "protos/perfetto/metrics/android/powrails_metric.proto",
         "protos/perfetto/metrics/android/process_metadata.proto",
         "protos/perfetto/metrics/android/profiler_smaps.proto",
+        "protos/perfetto/metrics/android/simpleperf.proto",
         "protos/perfetto/metrics/android/startup_metric.proto",
         "protos/perfetto/metrics/android/surfaceflinger.proto",
         "protos/perfetto/metrics/android/sysui_cuj_metrics.proto",
@@ -3726,6 +3727,7 @@
         "protos/perfetto/metrics/android/powrails_metric.proto",
         "protos/perfetto/metrics/android/process_metadata.proto",
         "protos/perfetto/metrics/android/profiler_smaps.proto",
+        "protos/perfetto/metrics/android/simpleperf.proto",
         "protos/perfetto/metrics/android/startup_metric.proto",
         "protos/perfetto/metrics/android/surfaceflinger.proto",
         "protos/perfetto/metrics/android/sysui_cuj_metrics.proto",
@@ -8006,6 +8008,7 @@
         "src/trace_processor/metrics/android/android_package_list.sql",
         "src/trace_processor/metrics/android/android_powrails.sql",
         "src/trace_processor/metrics/android/android_proxy_power.sql",
+        "src/trace_processor/metrics/android/android_simpleperf.sql",
         "src/trace_processor/metrics/android/android_startup.sql",
         "src/trace_processor/metrics/android/android_startup_launches.sql",
         "src/trace_processor/metrics/android/android_surfaceflinger.sql",
diff --git a/BUILD b/BUILD
index 264030b..3e34832 100644
--- a/BUILD
+++ b/BUILD
@@ -1030,6 +1030,7 @@
         "src/trace_processor/metrics/android/android_package_list.sql",
         "src/trace_processor/metrics/android/android_powrails.sql",
         "src/trace_processor/metrics/android/android_proxy_power.sql",
+        "src/trace_processor/metrics/android/android_simpleperf.sql",
         "src/trace_processor/metrics/android/android_startup.sql",
         "src/trace_processor/metrics/android/android_startup_launches.sql",
         "src/trace_processor/metrics/android/android_surfaceflinger.sql",
@@ -2565,6 +2566,7 @@
         "protos/perfetto/metrics/android/powrails_metric.proto",
         "protos/perfetto/metrics/android/process_metadata.proto",
         "protos/perfetto/metrics/android/profiler_smaps.proto",
+        "protos/perfetto/metrics/android/simpleperf.proto",
         "protos/perfetto/metrics/android/startup_metric.proto",
         "protos/perfetto/metrics/android/surfaceflinger.proto",
         "protos/perfetto/metrics/android/sysui_cuj_metrics.proto",
diff --git a/protos/perfetto/metrics/android/BUILD.gn b/protos/perfetto/metrics/android/BUILD.gn
index aa97665..39ddcf5 100644
--- a/protos/perfetto/metrics/android/BUILD.gn
+++ b/protos/perfetto/metrics/android/BUILD.gn
@@ -42,6 +42,7 @@
     "powrails_metric.proto",
     "process_metadata.proto",
     "profiler_smaps.proto",
+    "simpleperf.proto",
     "startup_metric.proto",
     "surfaceflinger.proto",
     "sysui_cuj_metrics.proto",
diff --git a/protos/perfetto/metrics/android/simpleperf.proto b/protos/perfetto/metrics/android/simpleperf.proto
new file mode 100644
index 0000000..7fcebe4
--- /dev/null
+++ b/protos/perfetto/metrics/android/simpleperf.proto
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+syntax = "proto2";
+
+package perfetto.protos;
+
+// Metric that stores information related to atrace events generated by
+// simpleperf tool
+message AndroidSimpleperfMetric {
+  optional double urgent_ratio = 1;
+}
diff --git a/protos/perfetto/metrics/metrics.proto b/protos/perfetto/metrics/metrics.proto
index 5f2c801..59ac8cb 100644
--- a/protos/perfetto/metrics/metrics.proto
+++ b/protos/perfetto/metrics/metrics.proto
@@ -39,6 +39,7 @@
 import "protos/perfetto/metrics/android/package_list.proto";
 import "protos/perfetto/metrics/android/powrails_metric.proto";
 import "protos/perfetto/metrics/android/profiler_smaps.proto";
+import "protos/perfetto/metrics/android/simpleperf.proto";
 import "protos/perfetto/metrics/android/startup_metric.proto";
 import "protos/perfetto/metrics/android/surfaceflinger.proto";
 import "protos/perfetto/metrics/android/sysui_cuj_metrics.proto";
@@ -91,7 +92,7 @@
 
 // Root message for all Perfetto-based metrics.
 //
-// Next id: 37
+// Next id: 38
 message TraceMetrics {
   reserved 4, 10, 13, 14, 16, 19;
 
@@ -184,6 +185,9 @@
   // Multiuser - metrics for switching users.
   optional AndroidMultiuserMetric android_multiuser = 36;
 
+  // Metrics related to simpleperf tool
+  optional AndroidSimpleperfMetric android_simpleperf = 37;
+
   // Demo extensions.
   extensions 450 to 499;
 
diff --git a/protos/perfetto/metrics/perfetto_merged_metrics.proto b/protos/perfetto/metrics/perfetto_merged_metrics.proto
index 15a119f..93f9ff2 100644
--- a/protos/perfetto/metrics/perfetto_merged_metrics.proto
+++ b/protos/perfetto/metrics/perfetto_merged_metrics.proto
@@ -791,6 +791,16 @@
 
 // End of protos/perfetto/metrics/android/profiler_smaps.proto
 
+// Begin of protos/perfetto/metrics/android/simpleperf.proto
+
+// Metric that stores information related to atrace events generated by
+// simpleperf tool
+message AndroidSimpleperfMetric {
+  optional double urgent_ratio = 1;
+}
+
+// End of protos/perfetto/metrics/android/simpleperf.proto
+
 // Begin of protos/perfetto/metrics/android/startup_metric.proto
 
 // Android app startup metrics.
@@ -1197,7 +1207,7 @@
 
 // Root message for all Perfetto-based metrics.
 //
-// Next id: 37
+// Next id: 38
 message TraceMetrics {
   reserved 4, 10, 13, 14, 16, 19;
 
@@ -1290,6 +1300,9 @@
   // Multiuser - metrics for switching users.
   optional AndroidMultiuserMetric android_multiuser = 36;
 
+  // Metrics related to simpleperf tool
+  optional AndroidSimpleperfMetric android_simpleperf = 37;
+
   // Demo extensions.
   extensions 450 to 499;
 
diff --git a/src/trace_processor/metrics/BUILD.gn b/src/trace_processor/metrics/BUILD.gn
index 8311cde..0f28cc0 100644
--- a/src/trace_processor/metrics/BUILD.gn
+++ b/src/trace_processor/metrics/BUILD.gn
@@ -38,6 +38,7 @@
   "android/android_lmk.sql",
   "android/android_powrails.sql",
   "android/android_proxy_power.sql",
+  "android/android_simpleperf.sql",
   "android/android_startup_launches.sql",
   "android/android_startup.sql",
   "android/android_package_list.sql",
diff --git a/src/trace_processor/metrics/android/android_simpleperf.sql b/src/trace_processor/metrics/android/android_simpleperf.sql
new file mode 100644
index 0000000..fa58575
--- /dev/null
+++ b/src/trace_processor/metrics/android/android_simpleperf.sql
@@ -0,0 +1,40 @@
+--
+-- Copyright 2021 The Android Open Source Project
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+--     https://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+
+-- Find all counters from track that satisfies regex 'slc/qurg2_(wr|rd):lvl=0x(0|1|3|7)%'
+DROP VIEW IF EXISTS all_qurg2;
+CREATE VIEW all_qurg2 AS
+SELECT
+  ts,
+  track_id,
+  name,
+  value
+FROM counters
+WHERE name LIKE 'slc/qurg2\___:lvl=0x_%' ESCAPE '\';
+
+-- Find all counters from track that satisfies regex 'slc/qurg2_(wr|rd):lvl=0x(1|3|7)%'
+DROP VIEW IF EXISTS non_zero_qurg2;
+CREATE VIEW non_zero_qurg2 AS
+SELECT
+  *
+FROM all_qurg2
+WHERE name NOT LIKE 'slc/qurg2\___:lvl=0x0%' ESCAPE '\';
+
+DROP VIEW IF EXISTS android_simpleperf_output;
+CREATE VIEW android_simpleperf_output AS
+SELECT AndroidSimpleperfMetric(
+  'urgent_ratio', (SELECT sum(value) FROM non_zero_qurg2) / (SELECT sum(value) FROM all_qurg2)
+);
diff --git a/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor b/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor
index c909308..889a30d 100644
--- a/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor
+++ b/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor
Binary files differ
diff --git a/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor.sha1 b/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor.sha1
index 8f474c4..295f392 100644
--- a/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor.sha1
+++ b/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor.sha1
@@ -2,5 +2,5 @@
 // SHA1(tools/gen_binary_descriptors)
 // 9fc6d77de57ec76a80b76aa282f4c7cf5ce55eec
 // SHA1(protos/perfetto/metrics/metrics.proto)
-// d861d7b0dd079940872852cbb4a4dfc3d67ac672
+// c01f53e60acabf2ce991251f9e8c51f697a696f5
   
\ No newline at end of file