Stress profile saving for gc-stress tests

Force profiling saving for gc-stress configuration. It will verify the
interactions between profile saving and GC.

Test: run-test
Bug: 62004634
Change-Id: Ia6f76debedd39353b210975fe95b155f63b1fa57
diff --git a/test/etc/run-test-jar b/test/etc/run-test-jar
index bb6ace1..5200db5 100755
--- a/test/etc/run-test-jar
+++ b/test/etc/run-test-jar
@@ -75,6 +75,7 @@
 VDEX_FILTER=""
 PROFILE="n"
 RANDOM_PROFILE="n"
+SAVE_PROFILE="n"
 # The normal dex2oat timeout.
 DEX2OAT_TIMEOUT="300" # 5 mins
 # The *hard* timeout where we really start trying to kill the dex2oat.
@@ -364,6 +365,9 @@
     elif [ "x$1" = "x--random-profile" ]; then
         RANDOM_PROFILE="y"
         shift
+    elif [ "x$1" = "x--save-profile" ]; then
+        SAVE_PROFILE="y"
+        shift
     elif [ "x$1" = "x--set-stack-trace-dump-dir" ]; then
         SET_STACK_TRACE_DUMP_DIR="y"
         shift
@@ -680,6 +684,7 @@
     exit 1
 fi
 
+create_profile_cmdline="true"
 profman_cmdline="true"
 dex2oat_cmdline="true"
 vdex_cmdline="true"
@@ -688,6 +693,16 @@
 strip_cmdline="true"
 sync_cmdline="true"
 
+if [ "$SAVE_PROFILE" = "y" ]; then
+  profile_file="$DEX_LOCATION/$TEST_NAME.prof"
+  create_profile_cmdline="touch $DEX_LOCATION/$TEST_NAME.prof"
+  FLAGS="${FLAGS} -Xjitsaveprofilinginfo -Xps-profile-boot-class-path"
+  FLAGS="${FLAGS} -Xps-profile-path:${profile_file}"
+  # Use -Xjitinitialsize:32M to prevent profiling info creation failure.
+  # On debug build the code cache is too small.
+  FLAGS="${FLAGS} -Xjitinitialsize:32M"
+fi
+
 # PROFILE takes precedence over RANDOM_PROFILE, since PROFILE tests require a
 # specific profile to run properly.
 if [ "$PROFILE" = "y" ] || [ "$RANDOM_PROFILE" = "y" ]; then
@@ -801,6 +816,7 @@
 dm_cmdline=$(echo $dm_cmdline)
 vdex_cmdline=$(echo $vdex_cmdline)
 profman_cmdline=$(echo $profman_cmdline)
+create_profile_cmdline=$(echo $create_profile_cmdline)
 
 # Use an empty ASAN_OPTIONS to enable defaults.
 # Note: this is required as envsetup right now exports detect_leaks=0.
@@ -866,6 +882,7 @@
              mkdir -p ${mkdir_locations} && \
              export LD_LIBRARY_PATH=$LD_LIBRARY_PATH && \
              export PATH=$ANDROID_ROOT/bin:$PATH && \
+             $create_profile_cmdline && \
              $profman_cmdline && \
              $dex2oat_cmdline && \
              $dm_cmdline && \
@@ -962,6 +979,7 @@
     export ASAN_OPTIONS=$RUN_TEST_ASAN_OPTIONS
 
     mkdir -p ${mkdir_locations} || exit 1
+    $create_profile_cmdline || { echo "Failed to create profile." >&2 ; exit 2; }
     $profman_cmdline || { echo "Profman failed." >&2 ; exit 2; }
     $dex2oat_cmdline || { echo "Dex2oat failed." >&2 ; exit 2; }
     $dm_cmdline || { echo "Dex2oat failed." >&2 ; exit 2; }
diff --git a/test/run-test b/test/run-test
index 6bcb9cd..456349e 100755
--- a/test/run-test
+++ b/test/run-test
@@ -254,6 +254,9 @@
     elif [ "x$1" = "x--gcstress" ]; then
         basic_verify="true"
         gc_stress="true"
+        # Save profiles when stressing the GC.
+        # This will verify the interactions between profile saving and GC.
+        run_args="${run_args} --save-profile"
         shift
     elif [ "x$1" = "x--jvmti-step-stress" ]; then
         jvmti_step_stress="true"