Only madvise for jank-perceptible processes

The madvise optimization for ART artifacts was introduced to improve
app startup performance. This optimization is less critical for
background process startup, e.g., boot broadcast receivers. Suppress
the madvise calls in these background (non-jank perceptible) cases,
potentially reducing memory and IO pressure from background process
activity.

Bug: 235390330
Test: m + presubmit + boot tests (check traces for madvise activity)
Change-Id: Ib759e9f8ca84ecedd9a37d79970bc15c70116a3a
diff --git a/runtime/runtime.cc b/runtime/runtime.cc
index ebad371..280fd51 100644
--- a/runtime/runtime.cc
+++ b/runtime/runtime.cc
@@ -3372,6 +3372,14 @@
                                   const uint8_t* map_begin,
                                   const uint8_t* map_end,
                                   const std::string& file_name) {
+  // Short-circuit the madvise optimization for background processes. This
+  // avoids IO and memory contention with foreground processes, particularly
+  // those involving app startup.
+  const Runtime* runtime = Runtime::Current();
+  if (runtime != nullptr && !runtime->InJankPerceptibleProcessState()) {
+    return;
+  }
+
   // Ideal blockTransferSize for madvising files (128KiB)
   static constexpr size_t kIdealIoTransferSizeBytes = 128*1024;