Make buffer fill policy configurable

Also add filter for package list for just the requesting package

Test: atest ProfilingFrameworkTests
Bug: 335688597
Change-Id: I83027eed15374c7ce74a2d5816ae4155c5841df0
diff --git a/framework/java/android/os/ProfilingManager.java b/framework/java/android/os/ProfilingManager.java
index fc507a2..ae0f3c8 100644
--- a/framework/java/android/os/ProfilingManager.java
+++ b/framework/java/android/os/ProfilingManager.java
@@ -57,33 +57,53 @@
     /* Begin public API defined keys. */
     /* End public API defined keys. */
 
-    /* Begin not-public API defined keys. */
+    /* Begin not-public API defined keys/values. */
     /**
      * Can only be used with profiling type heap profile, stack sampling, or system trace.
      * Value of type int.
-     * @hide */
+     * @hide
+     */
     public static final String KEY_DURATION_MS = "KEY_DURATION_MS";
 
     /**
      * Can only be used with profiling type heap profile. Value of type long.
-     * @hide */
+     * @hide
+     */
     public static final String KEY_SAMPLING_INTERVAL_BYTES = "KEY_SAMPLING_INTERVAL_BYTES";
 
     /**
      * Can only be used with profiling type heap profile. Value of type boolean.
-     * @hide */
+     * @hide
+     */
     public static final String KEY_TRACK_JAVA_ALLOCATIONS = "KEY_TRACK_JAVA_ALLOCATIONS";
 
     /**
      * Can only be used with profiling type stack sampling. Value of type int.
-     * @hide */
+     * @hide
+     */
     public static final String KEY_FREQUENCY_HZ = "KEY_FREQUENCY_HZ";
 
     /**
      * Can be used with all profiling types. Value of type int.
-     * @hide */
+     * @hide
+     */
     public static final String KEY_SIZE_KB = "KEY_SIZE_KB";
-    /* End not-public API defined keys. */
+
+    /**
+     * Can be used with profiling type system trace.
+     * Value of type int must be one of:
+     * {@link VALUE_BUFFER_FILL_POLICY_DISCARD}
+     * {@link VALUE_BUFFER_FILL_POLICY_RING_BUFFER}
+     * @hide
+     */
+    public static final String KEY_BUFFER_FILL_POLICY = "KEY_BUFFER_FILL_POLICY";
+
+    /** @hide */
+    public static final int VALUE_BUFFER_FILL_POLICY_DISCARD = 1;
+
+    /** @hide */
+    public static final int VALUE_BUFFER_FILL_POLICY_RING_BUFFER = 2;
+    /* End not-public API defined keys/values. */
 
     /**
      * @hide *
diff --git a/service/java/com/android/os/profiling/Configs.java b/service/java/com/android/os/profiling/Configs.java
index 478512b..2f6496a 100644
--- a/service/java/com/android/os/profiling/Configs.java
+++ b/service/java/com/android/os/profiling/Configs.java
@@ -24,6 +24,8 @@
 public final class Configs {
 
     static final String HEAP_PROFILE_TRACK_JAVA_ALLOCATIONS = "heaps: \"com.android.art\"";
+    private static final String BUFFER_FILL_POLICY_DISCARD = "DISCARD";
+    private static final String BUFFER_FILL_POLICY_RING_BUFFER = "RING_BUFFER";
 
     private static final String STUB_DURATION = "{{duration}}";
     private static final String STUB_PACKAGE_NAME = "{{package_name}}";
@@ -33,9 +35,11 @@
     private static final String STUB_SIZE = "{{size_kb}}";
     private static final String STUB_FLUSH_TIMEOUT = "{{flush_timeout}}";
     private static final String STUB_DATA_SOURCE_STOP_TIMEOUT = "{{data_source_stop_timeout}}";
+    private static final String STUB_BUFFER_FILL_POLICY = "{{buffer_fill_policy}}";
 
     static final String CONFIG_HEAP_PROFILE = "buffers {\n"
             + "  size_kb: " + STUB_SIZE + "\n"
+            + "  fill_policy: DISCARD\n"
             + "}\n"
             + "\n"
             + "data_sources {\n"
@@ -100,8 +104,12 @@
             + "flush_timeout_ms: " + STUB_FLUSH_TIMEOUT + "\n"
             + "duration_ms: " + STUB_DURATION;
     static final String CONFIG_SYSTEM_TRACE = "buffers {\n"
+            + "  size_kb: 4096\n"
+            + "  fill_policy: DISCARD\n"
+            + "}\n"
+            + "buffers {\n"
             + "  size_kb: " + STUB_SIZE + "\n"
-            + "  fill_policy: RING_BUFFER\n"
+            + "  fill_policy: " + STUB_BUFFER_FILL_POLICY + "\n"
             + "}\n"
             + "\n"
             + "data_sources {\n"
@@ -118,13 +126,16 @@
             + "  config {\n"
             + "    name: \"android.packages_list\"\n"
             + "    target_buffer: 0\n"
+            + "    packages_list_config {\n"
+            + "      package_name_filter: \"" + STUB_PACKAGE_NAME + "\"\n"
+            + "    }\n"
             + "  }\n"
             + "}\n"
             + "\n"
             + "data_sources {\n"
             + "  config {\n"
             + "    name: \"linux.ftrace\"\n"
-            + "    target_buffer: 0\n"
+            + "    target_buffer: 1\n"
             + "    ftrace_config {\n"
             + "      throttle_rss_stat: true\n"
             + "      disable_generic_events: true\n"
@@ -181,7 +192,7 @@
             + "data_sources {\n"
             + "  config {\n"
             + "    name: \"android.surfaceflinger.frametimeline\"\n"
-            + "    target_buffer: 0\n"
+            + "    target_buffer: 1\n"
             + "  }\n"
             + "}\n"
             + "incremental_state_config {\n"
@@ -602,13 +613,17 @@
                         sSystemTraceSizeKbMin,
                         sSystemTraceSizeKbMax,
                         paramsCopy);
+                String systemTraceBufferFillPolicy = getBufferFillPolicyString(
+                        getAndRemove(ProfilingManager.KEY_BUFFER_FILL_POLICY,
+                                ProfilingManager.VALUE_BUFFER_FILL_POLICY_RING_BUFFER, paramsCopy));
 
                 confirmEmptyOrThrow(paramsCopy);
 
                 return CONFIG_SYSTEM_TRACE
                         .replace(STUB_PACKAGE_NAME, packageName)
                         .replace(STUB_DURATION, String.valueOf(systemTraceDuration))
-                        .replace(STUB_SIZE, String.valueOf(systemTraceSizeKb));
+                        .replace(STUB_SIZE, String.valueOf(systemTraceSizeKb))
+                        .replace(STUB_BUFFER_FILL_POLICY, systemTraceBufferFillPolicy);
 
             // Invalid type
             default:
@@ -656,6 +671,18 @@
         return duration + FILE_PROCESSING_DELAY_MS;
     }
 
+    private static String getBufferFillPolicyString(int bufferFillPolicy)
+            throws IllegalArgumentException {
+        switch (bufferFillPolicy) {
+            case ProfilingManager.VALUE_BUFFER_FILL_POLICY_DISCARD:
+                return BUFFER_FILL_POLICY_DISCARD;
+            case ProfilingManager.VALUE_BUFFER_FILL_POLICY_RING_BUFFER:
+                return BUFFER_FILL_POLICY_RING_BUFFER;
+            default:
+                throw new IllegalArgumentException("Invalid buffer fill policy.");
+        }
+    }
+
     private static int getWithinBounds(String key, int defaultValue, int minValue,
             int maxValue, @Nullable Bundle params) {
         if (params == null) {
@@ -683,6 +710,18 @@
         return defaultValue;
     }
 
+    private static int getAndRemove(String key, int defaultValue, @Nullable Bundle bundle) {
+        if (bundle == null) {
+            return defaultValue;
+        }
+        if (bundle.containsKey(key)) {
+            int value = bundle.getInt(key);
+            bundle.remove(key);
+            return value;
+        }
+        return defaultValue;
+    }
+
     private static int getAndRemoveWithinBounds(String key, int defaultValue, int minValue,
             int maxValue, @Nullable Bundle bundle) {
         if (bundle == null) {