ART: Fix swap usage determination in dex2oat

The decision needs to be postponed until we have access to the
dex files.

Bug: 27878043

(cherry picked from commit ce55f0d4da1db20676273032b3c79624f665b35b)

Change-Id: I55833c6ea827ec7a322711bd182fc723d1727f08
diff --git a/dex2oat/dex2oat.cc b/dex2oat/dex2oat.cc
index 3db85c1..2dce2f1 100644
--- a/dex2oat/dex2oat.cc
+++ b/dex2oat/dex2oat.cc
@@ -1226,18 +1226,6 @@
       unlink(swap_file_name_.c_str());
     }
 
-    // If we use a swap file, ensure we are above the threshold to make it necessary.
-    if (swap_fd_ != -1) {
-      if (!UseSwap(IsBootImage(), dex_files_)) {
-        close(swap_fd_);
-        swap_fd_ = -1;
-        VLOG(compiler) << "Decided to run without swap.";
-      } else {
-        LOG(INFO) << "Large app, accepted running with swap.";
-      }
-    }
-    // Note that dex2oat won't close the swap_fd_. The compiler driver's swap space will do that.
-
     return true;
   }
 
@@ -1408,6 +1396,24 @@
     }
 
     dex_files_ = MakeNonOwningPointerVector(opened_dex_files_);
+
+    // We had to postpone the swap decision till now, as this is the point when we actually
+    // know about the dex files we're going to use.
+
+    // Make sure that we didn't create the driver, yet.
+    CHECK(driver_ == nullptr);
+    // If we use a swap file, ensure we are above the threshold to make it necessary.
+    if (swap_fd_ != -1) {
+      if (!UseSwap(IsBootImage(), dex_files_)) {
+        close(swap_fd_);
+        swap_fd_ = -1;
+        VLOG(compiler) << "Decided to run without swap.";
+      } else {
+        LOG(INFO) << "Large app, accepted running with swap.";
+      }
+    }
+    // Note that dex2oat won't close the swap_fd_. The compiler driver's swap space will do that.
+
     if (IsBootImage()) {
       // For boot image, pass opened dex files to the Runtime::Create().
       // Note: Runtime acquires ownership of these dex files.