@  rev 55754 : 8228434: jdk/net/Sockets/Test.java fails after JDK-8227642
|  Summary: Move container constant to separate test lib class
~  Reviewed-by: duke
o  rev 55753 : 8228434: jdk/net/Sockets/Test.java fails after JDK-8227642
|  Reviewed-by: duke
~

   1 /*
   2  * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package requires;
  24 
  25 import java.io.BufferedInputStream;
  26 import java.io.FileInputStream;
  27 import java.io.IOException;
  28 import java.io.InputStream;
  29 import java.nio.file.Files;
  30 import java.nio.file.Path;
  31 import java.nio.file.Paths;
  32 import java.nio.file.StandardOpenOption;
  33 import java.util.ArrayList;
  34 import java.util.HashMap;
  35 import java.util.List;
  36 import java.util.Map;
  37 import java.util.Properties;
  38 import java.util.concurrent.Callable;
  39 import java.util.concurrent.TimeUnit;
  40 import java.util.function.Supplier;
  41 import java.util.regex.Matcher;
  42 import java.util.regex.Pattern;
  43 
  44 import sun.hotspot.code.Compiler;
  45 import sun.hotspot.cpuinfo.CPUInfo;
  46 import sun.hotspot.gc.GC;
  47 import sun.hotspot.WhiteBox;
  48 import jdk.test.lib.Platform;
  49 import jdk.test.lib.Container;
  50 
  51 /**
  52  * The Class to be invoked by jtreg prior Test Suite execution to
  53  * collect information about VM.
  54  * Do not use any APIs that may not be available in all target VMs.
  55  * Properties set by this Class will be available in the @requires expressions.
  56  */
  57 public class VMProps implements Callable<Map<String, String>> {
  58     // value known to jtreg as an indicator of error state
  59     private static final String ERROR_STATE = "__ERROR__";
  60 
  61     private static final WhiteBox WB = WhiteBox.getWhiteBox();
  62 
  63     private static class SafeMap {
  64         private final Map<String, String> map = new HashMap<>();
  65 
  66         public void put(String key, Supplier<String> s) {
  67             String value;
  68             try {
  69                 value = s.get();
  70             } catch (Throwable t) {
  71                 System.err.println("failed to get value for " + key);
  72                 t.printStackTrace(System.err);
  73                 value = ERROR_STATE + t;
  74             }
  75             map.put(key, value);
  76         }
  77     }
  78 
  79     /**
  80      * Collects information about VM properties.
  81      * This method will be invoked by jtreg.
  82      *
  83      * @return Map of property-value pairs.
  84      */
  85     @Override
  86     public Map<String, String> call() {
  87         SafeMap map = new SafeMap();
  88         map.put("vm.flavor", this::vmFlavor);
  89         map.put("vm.compMode", this::vmCompMode);
  90         map.put("vm.bits", this::vmBits);
  91         map.put("vm.flightRecorder", this::vmFlightRecorder);
  92         map.put("vm.simpleArch", this::vmArch);
  93         map.put("vm.debug", this::vmDebug);
  94         map.put("vm.jvmci", this::vmJvmci);
  95         map.put("vm.emulatedClient", this::vmEmulatedClient);
  96         // vm.hasSA is "true" if the VM contains the serviceability agent
  97         // and jhsdb.
  98         map.put("vm.hasSA", this::vmHasSA);
  99         // vm.hasSAandCanAttach is "true" if the VM contains the serviceability agent
 100         // and jhsdb and it can attach to the VM.
 101         map.put("vm.hasSAandCanAttach", this::vmHasSAandCanAttach);
 102         // vm.hasJFR is "true" if JFR is included in the build of the VM and
 103         // so tests can be executed.
 104         map.put("vm.hasJFR", this::vmHasJFR);
 105         map.put("vm.cpu.features", this::cpuFeatures);
 106         map.put("vm.rtm.cpu", this::vmRTMCPU);
 107         map.put("vm.rtm.compiler", this::vmRTMCompiler);
 108         map.put("vm.aot", this::vmAOT);
 109         map.put("vm.aot.enabled", this::vmAotEnabled);
 110         // vm.cds is true if the VM is compiled with cds support.
 111         map.put("vm.cds", this::vmCDS);
 112         map.put("vm.cds.custom.loaders", this::vmCDSForCustomLoaders);
 113         map.put("vm.cds.archived.java.heap", this::vmCDSForArchivedJavaHeap);
 114         // vm.graal.enabled is true if Graal is used as JIT
 115         map.put("vm.graal.enabled", this::isGraalEnabled);
 116         map.put("vm.compiler1.enabled", this::isCompiler1Enabled);
 117         map.put("vm.compiler2.enabled", this::isCompiler2Enabled);
 118         map.put("docker.support", this::dockerSupport);
 119         map.put("release.implementor", this::implementor);
 120         map.put("test.vm.gc.nvdimm", this::isNvdimmTestEnabled);
 121         vmGC(map); // vm.gc.X = true/false
 122         vmOptFinalFlags(map);
 123 
 124         dump(map.map);
 125         return map.map;
 126     }
 127 
 128     /**
 129      * Print a stack trace before returning error state;
 130      * Used by the various helper functions which parse information from
 131      * VM properties in the case where they don't find an expected property
 132      * or a property doesn't conform to an expected format.
 133      *
 134      * @return {@link #ERROR_STATE}
 135      */
 136     private String errorWithMessage(String message) {
 137         new Exception(message).printStackTrace();
 138         return ERROR_STATE + message;
 139     }
 140 
 141     /**
 142      * @return vm.simpleArch value of "os.simpleArch" property of tested JDK.
 143      */
 144     protected String vmArch() {
 145         String arch = System.getProperty("os.arch");
 146         if (arch.equals("x86_64") || arch.equals("amd64")) {
 147             return "x64";
 148         } else if (arch.contains("86")) {
 149             return "x86";
 150         } else {
 151             return arch;
 152         }
 153     }
 154 
 155     /**
 156      * @return VM type value extracted from the "java.vm.name" property.
 157      */
 158     protected String vmFlavor() {
 159         // E.g. "Java HotSpot(TM) 64-Bit Server VM"
 160         String vmName = System.getProperty("java.vm.name");
 161         if (vmName == null) {
 162             return errorWithMessage("Can't get 'java.vm.name' property");
 163         }
 164 
 165         Pattern startP = Pattern.compile(".* (\\S+) VM");
 166         Matcher m = startP.matcher(vmName);
 167         if (m.matches()) {
 168             return m.group(1).toLowerCase();
 169         }
 170         return errorWithMessage("Can't get VM flavor from 'java.vm.name'");
 171     }
 172 
 173     /**
 174      * @return VM compilation mode extracted from the "java.vm.info" property.
 175      */
 176     protected String vmCompMode() {
 177         // E.g. "mixed mode"
 178         String vmInfo = System.getProperty("java.vm.info");
 179         if (vmInfo == null) {
 180             return errorWithMessage("Can't get 'java.vm.info' property");
 181         }
 182         vmInfo = vmInfo.toLowerCase();
 183         if (vmInfo.contains("mixed mode")) {
 184             return "Xmixed";
 185         } else if (vmInfo.contains("compiled mode")) {
 186             return "Xcomp";
 187         } else if (vmInfo.contains("interpreted mode")) {
 188             return "Xint";
 189         } else {
 190             return errorWithMessage("Can't get compilation mode from 'java.vm.info'");
 191         }
 192     }
 193 
 194     /**
 195      * @return VM bitness, the value of the "sun.arch.data.model" property.
 196      */
 197     protected String vmBits() {
 198         String dataModel = System.getProperty("sun.arch.data.model");
 199         if (dataModel != null) {
 200             return dataModel;
 201         } else {
 202             return errorWithMessage("Can't get 'sun.arch.data.model' property");
 203         }
 204     }
 205 
 206     /**
 207      * @return "true" if Flight Recorder is enabled, "false" if is disabled.
 208      */
 209     protected String vmFlightRecorder() {
 210         Boolean isFlightRecorder = WB.getBooleanVMFlag("FlightRecorder");
 211         String startFROptions = WB.getStringVMFlag("StartFlightRecording");
 212         if (isFlightRecorder != null && isFlightRecorder) {
 213             return "true";
 214         }
 215         if (startFROptions != null && !startFROptions.isEmpty()) {
 216             return "true";
 217         }
 218         return "false";
 219     }
 220 
 221     /**
 222      * @return debug level value extracted from the "jdk.debug" property.
 223      */
 224     protected String vmDebug() {
 225         String debug = System.getProperty("jdk.debug");
 226         if (debug != null) {
 227             return "" + debug.contains("debug");
 228         } else {
 229             return errorWithMessage("Can't get 'jdk.debug' property");
 230         }
 231     }
 232 
 233     /**
 234      * @return true if VM supports JVMCI and false otherwise
 235      */
 236     protected String vmJvmci() {
 237         // builds with jvmci have this flag
 238         return "" + (WB.getBooleanVMFlag("EnableJVMCI") != null);
 239     }
 240 
 241     /**
 242      * @return true if VM runs in emulated-client mode and false otherwise.
 243      */
 244     protected String vmEmulatedClient() {
 245         String vmInfo = System.getProperty("java.vm.info");
 246         if (vmInfo == null) {
 247             return errorWithMessage("Can't get 'java.vm.info' property");
 248         }
 249         return "" + vmInfo.contains(" emulated-client");
 250     }
 251 
 252     /**
 253      * @return supported CPU features
 254      */
 255     protected String cpuFeatures() {
 256         return CPUInfo.getFeatures().toString();
 257     }
 258 
 259     /**
 260      * For all existing GC sets vm.gc.X property.
 261      * Example vm.gc.G1=true means:
 262      *    VM supports G1
 263      *    User either set G1 explicitely (-XX:+UseG1GC) or did not set any GC
 264      *
 265      * @param map - property-value pairs
 266      */
 267     protected void vmGC(SafeMap map) {
 268         for (GC gc: GC.values()) {
 269             map.put("vm.gc." + gc.name(),
 270                     () -> "" + (gc.isSupported()
 271                             && (gc.isSelected() || GC.isSelectedErgonomically())));
 272         }
 273     }
 274 
 275     /**
 276      * Selected final flag.
 277      *
 278      * @param map - property-value pairs
 279      * @param flagName - flag name
 280      */
 281     private void vmOptFinalFlag(SafeMap map, String flagName) {
 282         map.put("vm.opt.final." + flagName,
 283                 () -> String.valueOf(WB.getBooleanVMFlag(flagName)));
 284     }
 285 
 286     /**
 287      * Selected sets of final flags.
 288      *
 289      * @param map - property-value pairs
 290      */
 291     protected void vmOptFinalFlags(SafeMap map) {
 292         vmOptFinalFlag(map, "ClassUnloading");
 293         vmOptFinalFlag(map, "UseCompressedOops");
 294         vmOptFinalFlag(map, "EnableJVMCI");
 295         vmOptFinalFlag(map, "EliminateAllocations");
 296     }
 297 
 298     /**
 299      * @return "true" if VM has a serviceability agent.
 300      */
 301     protected String vmHasSA() {
 302         return "" + Platform.hasSA();
 303     }
 304 
 305     /**
 306      * @return "true" if VM has a serviceability agent and it can
 307      * attach to the VM.
 308      */
 309     protected String vmHasSAandCanAttach() {
 310         try {
 311             return "" + Platform.shouldSAAttach();
 312         } catch (IOException e) {
 313             e.printStackTrace();
 314             return errorWithMessage("Checking whether SA can attach to the VM failed.:" + e);
 315         }
 316     }
 317 
 318     /**
 319      * @return "true" if the VM is compiled with Java Flight Recorder (JFR)
 320      * support.
 321      */
 322     protected String vmHasJFR() {
 323         return "" + WB.isJFRIncludedInVmBuild();
 324     }
 325 
 326     /**
 327      * @return true if compiler in use supports RTM and false otherwise.
 328      */
 329     protected String vmRTMCompiler() {
 330         boolean isRTMCompiler = false;
 331 
 332         if (Compiler.isC2Enabled() &&
 333             (Platform.isX86() || Platform.isX64() || Platform.isPPC())) {
 334             isRTMCompiler = true;
 335         }
 336         return "" + isRTMCompiler;
 337     }
 338 
 339     /**
 340      * @return true if VM runs RTM supported CPU and false otherwise.
 341      */
 342     protected String vmRTMCPU() {
 343         return "" + CPUInfo.hasFeature("rtm");
 344     }
 345 
 346     /**
 347      * @return true if VM supports AOT and false otherwise
 348      */
 349     protected String vmAOT() {
 350         // builds with aot have jaotc in <JDK>/bin
 351         Path bin = Paths.get(System.getProperty("java.home"))
 352                         .resolve("bin");
 353         Path jaotc;
 354         if (Platform.isWindows()) {
 355             jaotc = bin.resolve("jaotc.exe");
 356         } else {
 357             jaotc = bin.resolve("jaotc");
 358         }
 359         return "" + Files.exists(jaotc);
 360     }
 361 
 362     /*
 363      * @return true if there is at least one loaded AOT'ed library.
 364      */
 365     protected String vmAotEnabled() {
 366         return "" + (WB.aotLibrariesCount() > 0);
 367     }
 368 
 369     /**
 370      * Check for CDS support.
 371      *
 372      * @return true if CDS is supported by the VM to be tested.
 373      */
 374     protected String vmCDS() {
 375         return "" + WB.isCDSIncludedInVmBuild();
 376     }
 377 
 378     /**
 379      * Check for CDS support for custom loaders.
 380      *
 381      * @return true if CDS provides support for customer loader in the VM to be tested.
 382      */
 383     protected String vmCDSForCustomLoaders() {
 384         return "" + ("true".equals(vmCDS()) && Platform.areCustomLoadersSupportedForCDS());
 385     }
 386 
 387     /**
 388      * Check for CDS support for archived Java heap regions.
 389      *
 390      * @return true if CDS provides support for archive Java heap regions in the VM to be tested.
 391      */
 392     protected String vmCDSForArchivedJavaHeap() {
 393         return "" + ("true".equals(vmCDS()) && WB.isJavaHeapArchiveSupported());
 394     }
 395 
 396     /**
 397      * Check if Graal is used as JIT compiler.
 398      *
 399      * @return true if Graal is used as JIT compiler.
 400      */
 401     protected String isGraalEnabled() {
 402         return "" + Compiler.isGraalEnabled();
 403     }
 404 
 405     /**
 406      * Check if Compiler1 is present.
 407      *
 408      * @return true if Compiler1 is used as JIT compiler, either alone or as part of the tiered system.
 409      */
 410     protected String isCompiler1Enabled() {
 411         return "" + Compiler.isC1Enabled();
 412     }
 413 
 414     /**
 415      * Check if Compiler2 is present.
 416      *
 417      * @return true if Compiler2 is used as JIT compiler, either alone or as part of the tiered system.
 418      */
 419     protected String isCompiler2Enabled() {
 420         return "" + Compiler.isC2Enabled();
 421     }
 422 
 423    /**
 424      * A simple check for docker support
 425      *
 426      * @return true if docker is supported in a given environment
 427      */
 428     protected String dockerSupport() {
 429         boolean isSupported = false;
 430         if (Platform.isLinux()) {
 431            // currently docker testing is only supported for Linux,
 432            // on certain platforms
 433 
 434            String arch = System.getProperty("os.arch");
 435 
 436            if (Platform.isX64()) {
 437               isSupported = true;
 438            } else if (Platform.isAArch64()) {
 439               isSupported = true;
 440            } else if (Platform.isS390x()) {
 441               isSupported = true;
 442            } else if (arch.equals("ppc64le")) {
 443               isSupported = true;
 444            }
 445         }
 446 
 447         if (isSupported) {
 448            try {
 449               isSupported = checkDockerSupport();
 450            } catch (Exception e) {
 451               isSupported = false;
 452            }
 453          }
 454 
 455         return "" + isSupported;
 456     }
 457 
 458     private boolean checkDockerSupport() throws IOException, InterruptedException {
 459         ProcessBuilder pb = new ProcessBuilder(Container.ENGINE_COMMAND, "ps");
 460         Process p = pb.start();
 461         p.waitFor(10, TimeUnit.SECONDS);
 462 
 463         return (p.exitValue() == 0);
 464     }
 465 
 466     private String implementor() {
 467         try (InputStream in = new BufferedInputStream(new FileInputStream(
 468                 System.getProperty("java.home") + "/release"))) {
 469             Properties properties = new Properties();
 470             properties.load(in);
 471             String implementorProperty = properties.getProperty("IMPLEMENTOR");
 472             if (implementorProperty != null) {
 473                 return implementorProperty.replace("\"", "");
 474             }
 475             return errorWithMessage("Can't get 'IMPLEMENTOR' property from 'release' file");
 476         } catch (IOException e) {
 477             e.printStackTrace();
 478             return errorWithMessage("Failed to read 'release' file " + e);
 479         }
 480     }
 481 
 482     private String isNvdimmTestEnabled() {
 483         String isEnabled = System.getenv("TEST_VM_GC_NVDIMM");
 484         return "" + "true".equalsIgnoreCase(isEnabled);
 485     }
 486 
 487     /**
 488      * Dumps the map to the file if the file name is given as the property.
 489      * This functionality could be helpful to know context in the real
 490      * execution.
 491      *
 492      * @param map
 493      */
 494     protected static void dump(Map<String, String> map) {
 495         String dumpFileName = System.getProperty("vmprops.dump");
 496         if (dumpFileName == null) {
 497             return;
 498         }
 499         List<String> lines = new ArrayList<>();
 500         map.forEach((k, v) -> lines.add(k + ":" + v));
 501         try {
 502             Files.write(Paths.get(dumpFileName), lines,
 503                     StandardOpenOption.APPEND, StandardOpenOption.CREATE);
 504         } catch (IOException e) {
 505             throw new RuntimeException("Failed to dump properties into '"
 506                     + dumpFileName + "'", e);
 507         }
 508     }
 509 
 510     /**
 511      * This method is for the testing purpose only.
 512      *
 513      * @param args
 514      */
 515     public static void main(String args[]) {
 516         Map<String, String> map = new VMProps().call();
 517         map.forEach((k, v) -> System.out.println(k + ": '" + v + "'"));
 518     }
 519 }
--- EOF ---