changeset 21560:a9fba0dfb155

added lower runtime overhead mechanism for setting "graal.version" system property
author Doug Simon <doug.simon@oracle.com>
date Thu, 28 May 2015 16:39:41 +0200
parents be896a1983c0
children ce2113326bc8
files graal/com.oracle.graal.api.runtime/src/com/oracle/graal/api/runtime/Graal.java mx/mx_graal.py
diffstat 2 files changed, 25 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.api.runtime/src/com/oracle/graal/api/runtime/Graal.java	Thu May 28 15:36:48 2015 +0200
+++ b/graal/com.oracle.graal.api.runtime/src/com/oracle/graal/api/runtime/Graal.java	Thu May 28 16:39:41 2015 +0200
@@ -22,7 +22,6 @@
  */
 package com.oracle.graal.api.runtime;
 
-import java.io.*;
 import java.util.*;
 
 import sun.reflect.*;
@@ -43,24 +42,14 @@
             rt = factory.getRuntime();
         }
         if (rt != null) {
-            updateGraalVersionProperty();
+            // The constant is patched in-situ by the build system
+            System.setProperty("graal.version", "@@graal.version@@");
+            assert !System.getProperty("graal.version").startsWith("@@") && !System.getProperty("graal.version").endsWith("@@") : "Graal version string constant was not patched by build system";
             return rt;
         }
         return new InvalidGraalRuntime();
     }
 
-    private static void updateGraalVersionProperty() {
-        try {
-            InputStream s = Graal.class.getResourceAsStream("graal.version");
-            byte[] buf = new byte[s.available()];
-            new DataInputStream(s).readFully(buf);
-            String version = new String(buf);
-            System.setProperty("graal.version", version);
-        } catch (IOException e) {
-            throw new InternalError(e);
-        }
-    }
-
     /**
      * Gets the singleton {@link GraalRuntime} instance available to the application.
      */
--- a/mx/mx_graal.py	Thu May 28 15:36:48 2015 +0200
+++ b/mx/mx_graal.py	Thu May 28 16:39:41 2015 +0200
@@ -707,7 +707,26 @@
     jreJVMCIServicesDir = join(jreJVMCIDir, 'services')
     _extractJVMCIServiceFiles(jvmciJars, jreJVMCIServicesDir)
 
-
+def _patchGraalVersionConstant(dist):
+    """
+    Patches the constant "@@graal.version@@" in the constant pool of Graal.class
+    with the computed Graal version string.
+    """
+    zfOutFd, zfOutPath = tempfile.mkstemp(suffix='', prefix=basename(dist.path) + '.', dir=dirname(dist.path))
+    zfOut = zipfile.ZipFile(zfOutPath, 'w')
+    zf = zipfile.ZipFile(dist.path, 'r')
+    for zi in zf.infolist():
+        data = zf.read(zi)
+        if zi.filename == 'com/oracle/graal/api/runtime/Graal.class':
+            versionSpec = '{:' + str(len('@@graal.version@@')) + '}'
+            versionStr = versionSpec.format(graal_version())
+            assert '@@graal.version@@' in data, 'could not find "@@graal.version@@" constant in ' + dist.path + '!' + zi.filename
+            data = data.replace('@@graal.version@@', versionStr)
+        zfOut.writestr(zi, data)
+    zfOut.close()
+    os.close(zfOutFd)
+    zf.close()
+    shutil.move(zfOutPath, dist.path)
 
 def _installDistInJdks(deployableDist):
     """
@@ -717,9 +736,8 @@
     dist = mx.distribution(deployableDist.name)
 
     if dist.name == 'GRAAL':
-        zf = zipfile.ZipFile(dist.path, 'a')
-        zf.writestr('com/oracle/graal/api/runtime/graal.version', graal_version())
-        zf.close()
+        _patchGraalVersionConstant(dist)
+
     elif dist.name == 'GRAAL_TRUFFLE':
         # The content in graalRuntime.inline.hpp is generated from Graal
         # classes that contain com.oracle.jvmci.options.Option annotated fields.