Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/Terminator.java
40948 views
/*1* Copyright (c) 2001, 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 nsk.share;2425/**26* Terminator is used to terminate a stress test with PASS exit status27* before the test is terminated as timed out (and so failed).28*29* <p>Terminator class holds a thread which sleeps for the given amount30* of time, and then wakes up and executes <tt>System.exit()</tt>31* with the given exit status. That thread is daemon, so it doesn't32* prevent application from exiting once all its threads finish33* before it's time for termination. Appointing terminator in zero34* delay implies immediate <tt>exit()</tt>.35*36* <p>There is a limitation: you may appoint no more than one terminator37* per application.38*/39public class Terminator {40/**41* Use specific <tt>appoint()</tt> method to appoint terminator.42*43* @see #appoint(int)44* @see #appoint(int,int)45*/46protected Terminator() {}4748/**49* One terminator per application, or <tt>null</tt> (by default).50*/51private static Thread terminator = null;5253/**54* <p>Return timeout (or waittime) value munus the margin55* value (which is assumed 1 minute by default).56*57* <p>Treat <tt>args[0]</tt> as <tt>$TIMEOUT</tt> value, or seek for58* <tt>-waittime=$WAITTIME</tt> value. If both parameters59* (or either none of them) are assigned, throw an exception to60* report parameters inconsistency.61*62* <p>Also, seek for <tt>-margin=...</tt> assignment, or assume margin63* is 1 minute.64*65* @param args Is usually obtained via the application's command-line.66*67* @throws IllegalArgumentException If <tt>args[]</tt> is inconsistent.68*69* @see #appoint(int)70* @see #appoint(int,int)71*/72public static int parseAppointment(String args[]) {73int timeout=-1, margin=1;74int timeouts=0, waittimes=0, margins=0;75for (int i=0; i<args.length; i++) {76if (args[i].startsWith("-")) {77if (args[i].startsWith("-waittime=")) {78timeout = Integer.parseInt(args[i].substring(10));79waittimes++;80}81if (args[i].startsWith("-margin=")) {82margin = Integer.parseInt(args[i].substring(8));83margins++;84}85} else {86if (i == 0) {87timeout = Integer.parseInt(args[i]);88timeouts++;89}90}91};92if (timeouts==0 && waittimes==0)93throw new IllegalArgumentException(94"no $TIMEOUT, nor -waittime=$WAITTIME is set");95if (waittimes > 1)96throw new IllegalArgumentException(97"more than one -waittime=... is set");98if (margins > 1)99throw new IllegalArgumentException(100"more than one -margin=... is set");101102int result = timeout - margin;103if (result <= 0)104throw new IllegalArgumentException(105"delay appointment must be greater than "+margin+" minutes");106return result;107}108109/**110* Appoint terminator after the given amount of <tt>minutes</tt>,111* so that exit status would be 95 (to simulate JCK-like PASS112* status).113*114* @throws IllegalStateException If terminator is already appointed.115*116* @see #appoint(int,int)117* @see #parseAppointment(String[])118*/119public static void appoint(int minutes) {120appoint(minutes,95); // JCK-like PASS status121}122123/**124* Appoint Terminator for the given amount of <tt>minutes</tt>,125* so that the given <tt>status</tt> would be exited when time126* is over.127*128* @throws IllegalStateException If terminator is already appointed.129*130* @see #appoint(int)131* @see #parseAppointment(String[])132*/133public static void appoint(int minutes, int status) {134if (terminator != null)135throw new IllegalStateException("Terminator is already appointed.");136137final long timeToExit = System.currentTimeMillis() + 60*1000L*minutes;138final int exitStatus = status;139140terminator = new Thread(Terminator.class.getName()) {141public void run() {142long timeToSleep = timeToExit - System.currentTimeMillis();143if (timeToSleep > 0)144try {145//146// Use wait() instead of sleep(), because Java 2147// specification doesn't guarantee the method148// sleep() to yield to other threads.149//150Object someDummyObject = new Object();151synchronized (someDummyObject) {152someDummyObject.wait(timeToSleep);153}154} catch (InterruptedException exception) {155exception.printStackTrace(System.err);156return;157};158//159// OK, lets do it now:160//161System.err.println(162"#\n# Terminator: prescheduled program termination.\n#");163System.exit(exitStatus); // terminator to all threads164}165};166167terminator.setPriority(Thread.MAX_PRIORITY);168terminator.setDaemon(true);169terminator.start();170}171}172173174