Replace generic GC type flag with a specific Generational CC flag.

The "gctype" device configuration flag (from the "runtime_native_boot"
namespace) was a string passed verbatim as an argument to the runtime
option "-Xgc". It was too generic, conveyed no typing information, and
was error-prone (there was no control over what was passed from the
server to the zygote, and then to the runtime).

This change replaces "gctype" with a specific, Boolean
"enable_generational_cc" flag. This new flag better reflects the
nature of the experiment to be conducted (either enable or disable
generational garbage collection in ART's concurrent copying
collector).

Test: core/jni/runtime_native_boot-flags-test.sh
Bug: 72446017
Bug: 120794191
Bug: 123754583
Change-Id: I30a73171c0dc3c7bc891c4f164eed0ba42b0f420
diff --git a/core/jni/AndroidRuntime.cpp b/core/jni/AndroidRuntime.cpp
index c45900c..ce2e353 100644
--- a/core/jni/AndroidRuntime.cpp
+++ b/core/jni/AndroidRuntime.cpp
@@ -232,8 +232,15 @@
 
 // Namespace for Android Runtime flags applied during boot time.
 static const char* RUNTIME_NATIVE_BOOT_NAMESPACE = "runtime_native_boot";
-// Feature flag name for Garbage Collector type.
-static const char* GCTYPE = "gctype";
+// Feature flag name to enable/disable generational garbage collection in ART's
+// Concurrent Copying (CC) garbage collector.
+static const char* ENABLE_GENERATIONAL_CC = "enable_generational_cc";
+// Runtime option enabling generational garbage collection in ART's Concurrent
+// Copying (CC) garbage collector.
+static const char* kGenerationalCCRuntimeOption = "-Xgc:generational_cc";
+// Runtime option disabling generational garbage collection in ART's Concurrent
+// Copying (CC) garbage collector.
+static const char* kNoGenerationalCCRuntimeOption = "-Xgc:nogenerational_cc";
 
 static AndroidRuntime* gCurRuntime = NULL;
 
@@ -785,17 +792,21 @@
       addOption("-XX:LowMemoryMode");
     }
 
-    std::string gc_type_override =
-            server_configurable_flags::GetServerConfigurableFlag(RUNTIME_NATIVE_BOOT_NAMESPACE,
-                                                                 GCTYPE,
-                                                                 /*default_value=*/ "");
-    std::string gc_type_override_temp;
-    if (gc_type_override.empty()) {
-        parseRuntimeOption("dalvik.vm.gctype", gctypeOptsBuf, "-Xgc:");
-    } else {
-        // Copy the string so it doesn't go out of scope since addOption does not make a copy.
-        gc_type_override_temp = "-Xgc:" + gc_type_override;
-        addOption(gc_type_override_temp.c_str());
+    /*
+     * Garbage-collection related options.
+     */
+    parseRuntimeOption("dalvik.vm.gctype", gctypeOptsBuf, "-Xgc:");
+
+    // If it set, honor the "enable_generational_cc" device configuration;
+    // otherwise, let the runtime use its default behavior.
+    std::string enable_generational_cc =
+        server_configurable_flags::GetServerConfigurableFlag(RUNTIME_NATIVE_BOOT_NAMESPACE,
+                                                             ENABLE_GENERATIONAL_CC,
+                                                             /*default_value=*/ "");
+    if (enable_generational_cc == "true") {
+        addOption(kGenerationalCCRuntimeOption);
+    } else if (enable_generational_cc == "false") {
+        addOption(kNoGenerationalCCRuntimeOption);
     }
 
     parseRuntimeOption("dalvik.vm.backgroundgctype", backgroundgcOptsBuf, "-XX:BackgroundGC=");
diff --git a/core/jni/runtime_native_boot-flags-test.sh b/core/jni/runtime_native_boot-flags-test.sh
index 66e18bb..01f37f0 100755
--- a/core/jni/runtime_native_boot-flags-test.sh
+++ b/core/jni/runtime_native_boot-flags-test.sh
@@ -172,12 +172,14 @@
   done
 }
 
-# test_android_runtime_flag FLAG VALUE
-# ------------------------------------
-# Test device configuration FLAG with VALUE.
+# test_android_runtime_flag FLAG VALUE GC_RUNTIME_OPTION
+# ------------------------------------------------------
+# Test device configuration FLAG with VALUE. Check that GC_RUNTIME_OPTION is
+# passed as GC Runtime option by the zygote.
 function test_android_runtime_flag {
   local flag=$1
   local value=$2
+  local gc_runtime_option=$3
 
   # Persistent system property (set after a reboot) associated with the device
   # configuration flag.
@@ -196,21 +198,21 @@
   local context="Flag set, before reboot"
   check_device_config_flag "$context" "$flag" "$value"
   check_system_property "$context" "$prop" "$value"
-  check_no_zygote_gc_runtime_option "$context" "$value"
+  check_no_zygote_gc_runtime_option "$context" "$gc_runtime_option"
 
   # Reboot device for the flag value to take effect.
   reboot_and_wait_for_device
   context="Flag set, after 1st reboot"
   check_device_config_flag "$context" "$flag" "$value"
   check_system_property "$context" "$prop" "$value"
-  check_zygote_gc_runtime_option "$context" "$value"
+  check_zygote_gc_runtime_option "$context" "$gc_runtime_option"
 
   # Reboot device a second time and check that the state has persisted.
   reboot_and_wait_for_device
   context="Flag set, after 2nd reboot"
   check_device_config_flag "$context" "$flag" "$value"
   check_system_property "$context" "$prop" "$value"
-  check_zygote_gc_runtime_option "$context" "$value"
+  check_zygote_gc_runtime_option "$context" "$gc_runtime_option"
 
   say "Unsetting device configuration flag..."
   adb shell device_config delete "$namespace" "$flag" >/dev/null
@@ -222,7 +224,7 @@
   context="Flag unset, after 3rd reboot"
   check_no_device_config_flag "$context" "$flag"
   check_no_system_property "$context" "$prop"
-  check_no_zygote_gc_runtime_option "$context" "$value"
+  check_no_zygote_gc_runtime_option "$context" "$gc_runtime_option"
 }
 
 # Enumerate Zygote processes.
@@ -232,9 +234,9 @@
   (zygote32_64|zygote64_32) zygotes="zygote zygote64";;
 esac
 
-# Test "gctype" flag values.
-test_android_runtime_flag gctype nogenerational_cc
-test_android_runtime_flag gctype generational_cc
+# Test "enable_generational_cc" flag values.
+test_android_runtime_flag enable_generational_cc false nogenerational_cc
+test_android_runtime_flag enable_generational_cc true generational_cc
 
 if [[ "$exit_status" -eq 0 ]]; then
   banner "All tests passed."