Installd: don't generate app image with empty reference profile

Use app images only if we are compiling profile-guided and
reference profile not empty.

Fixed test logic to use a non-empty dm_file so that the profile is actually opened by dexopt.cpp.

Test: 1.adb install old_version.apk
      2.open and use app for a while and make sure the reference profile generated
      3.adb install new_version.apk
      4.check if app image file exists?

Bug: 270427379
Change-Id: If143605e0f5541fcffd63aa52a257834d2448383
diff --git a/cmds/installd/dexopt.cpp b/cmds/installd/dexopt.cpp
index ce3d669..794750f 100644
--- a/cmds/installd/dexopt.cpp
+++ b/cmds/installd/dexopt.cpp
@@ -1917,10 +1917,11 @@
     // Open the reference profile if needed.
     UniqueFile reference_profile = maybe_open_reference_profile(
             pkgname, dex_path, profile_name, profile_guided, is_public, uid, is_secondary_dex);
-
-    if (reference_profile.fd() == -1) {
-        // We don't create an app image without reference profile since there is no speedup from
-        // loading it in that case and instead will be a small overhead.
+    struct stat sbuf;
+    if (reference_profile.fd() == -1 ||
+        (fstat(reference_profile.fd(), &sbuf) != -1 && sbuf.st_size == 0)) {
+        // We don't create an app image with empty or non existing reference profile since there
+        // is no speedup from loading it in that case and instead will be a small overhead.
         generate_app_image = false;
     }