Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/mlvm/share/Env.java
41155 views
/*1* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package vm.mlvm.share;2425import java.util.Random;26import jdk.test.lib.Utils;2728import nsk.share.ArgumentParser;29import nsk.share.Log;30import nsk.share.Log.TraceLevel;31import nsk.share.test.LazyFormatString;3233public class Env {3435private static class StaticHolder {36public static ArgumentParser argParser;37public static Log log;38static {39init(new String[0]);40}4142public static void init(String... args) {43init(new IgnoreUnknownArgumentParser(args));44}4546public static void init(ArgumentParser ap) {47StaticHolder.argParser = ap;48StaticHolder.log = new Log(System.out, StaticHolder.argParser);49}50}5152public static ArgumentParser getArgParser() {53return StaticHolder.argParser;54}5556public static Log getLog() {57return StaticHolder.log;58}5960public static void init(String... args) {61StaticHolder.init(args);62}6364public static void init(ArgumentParser ap) {65StaticHolder.init(ap);66}6768//69// RNG70//7172private static long _seed = Utils.SEED;7374private static volatile boolean _wasSeedPrinted = false;7576// Thread local variable containing each thread's ID77private static final ThreadLocal<Random> _threadRNG =78new ThreadLocal<Random>() {79@Override protected Random initialValue() {80if ( ! _wasSeedPrinted ) {81_wasSeedPrinted = true;82traceImportant("RNG seed = " + _seed + " (0x" + Long.toHexString(_seed) + ")");83// ensure we also print out how to change seed84Utils.getRandomInstance();85}8687long seed = _seed;88String name = Thread.currentThread().getName();89for ( int n = 0; n < name.length(); n++ )90seed ^= name.charAt(n) << ((n % 7) * 8);9192traceVerbose(Thread.currentThread() + " RNG seed = " + seed + " (0x" + Long.toHexString(seed) + ")");9394return new Random(seed);95}96};9798public static Random getRNG() {99return _threadRNG.get();100}101102//103// Syntactic sugar104//105106public static void traceImportant(String msg) {107getLog().trace(TraceLevel.TRACE_IMPORTANT, msg);108}109110public static void traceImportant(String msg, Object... args) {111getLog().trace(TraceLevel.TRACE_IMPORTANT, new LazyFormatString(msg, args));112}113114public static void traceImportant(Throwable t, String msg, Object... args) {115getLog().trace(TraceLevel.TRACE_IMPORTANT, new LazyFormatString(msg, args), t);116}117118public static void traceNormal(String msg) {119getLog().trace(TraceLevel.TRACE_NORMAL, msg);120}121122public static void traceNormal(String msg, Object... args) {123getLog().trace(TraceLevel.TRACE_NORMAL, new LazyFormatString(msg, args));124}125126public static void traceNormal(Throwable t, String msg, Object... args) {127getLog().trace(TraceLevel.TRACE_NORMAL, new LazyFormatString(msg, args), t);128}129130public static void traceVerbose(String msg) {131getLog().trace(TraceLevel.TRACE_VERBOSE, msg);132}133134public static void traceVerbose(String msg, Object... args) {135getLog().trace(TraceLevel.TRACE_VERBOSE, new LazyFormatString(msg, args));136}137138public static void traceVerbose(Throwable t, String msg, Object... args) {139getLog().trace(TraceLevel.TRACE_VERBOSE, new LazyFormatString(msg, args), t);140}141142public static void traceDebug(String msg) {143getLog().trace(TraceLevel.TRACE_DEBUG, msg);144}145146public static void traceDebug(String msg, Object... args) {147getLog().trace(TraceLevel.TRACE_DEBUG, new LazyFormatString(msg, args));148}149150public static void traceDebug(Throwable t, String msg, Object... args) {151getLog().trace(TraceLevel.TRACE_DEBUG, new LazyFormatString(msg, args), t);152}153154public static void display(String msg) {155getLog().trace(TraceLevel.TRACE_IMPORTANT, msg);156}157158public static void display(String msg, Object... args) {159getLog().trace(TraceLevel.TRACE_IMPORTANT, new LazyFormatString(msg, args));160}161162public static void complain(String msg) {163getLog().complain(msg);164}165166public static void complain(String msg, Object... args) {167getLog().complain(new LazyFormatString(msg, args));168}169170public static void complain(Throwable t, String msg, Object... args) {171getLog().complain(new LazyFormatString(msg, args), t);172}173174/**175* Throws an arbitrary exception as unchecked one.176* The method does not return normally.177*178* If the exception is not a subclass of java.lang.RuntimeException`179* or java.lang.Error, it is wrapped into java.lang.RuntimeException180*181* @param exception Exception to throw (wrapping it when it is checked on)182*/183public static void throwAsUncheckedException(Throwable exception) {184if (exception instanceof RuntimeException) {185throw (RuntimeException) exception;186}187188if (exception instanceof Error) {189throw (Error) exception;190}191192throw new RuntimeException(exception.getMessage(), exception);193}194}195196197