File: Harakiri.java

package info (click to toggle)
openjdk-11 11.0.16%2B8-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 784,576 kB
  • sloc: java: 5,128,021; xml: 1,192,224; cpp: 1,124,021; ansic: 422,433; javascript: 155,577; sh: 17,084; objc: 13,327; python: 4,522; asm: 3,570; makefile: 2,858; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (175 lines) | stat: -rw-r--r-- 6,547 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package nsk.share;

/**
 * Harakiri is used to terminate a stress test with PASS exit status
 * before the test is terminated as timed out (and so failed).
 *
 * <p>Harakiri class holds a thread which sleeps for the given amount
 * of time, and then wakes up and executes <tt>System.exit()</tt>
 * with the given exit status. That thread is daemon, so it doesn't
 * prevent application from exiting once all its threads finish
 * before it's time for harakiri. Appointing harakiri in zero
 * delay implies immediate <tt>exit()</tt>.
 *
 * <p>There is a limitation: you may appoint no more than one harakiri
 * per application.
 */
public class Harakiri {
    /**
     * Use specific <tt>appoint()</tt> method to appoint harakiri.
     *
     * @see #appoint(int)
     * @see #appoint(int,int)
     */
    protected Harakiri() {}

    /**
     * One harakiri per application, or <tt>null</tt> (by default).
     */
    private static Thread harakiri = null;

    /**
     * <p>Return timeout (or waittime) value munus the margin
     * value (which is assumed 1 minute by default).
     *
     * <p>Treat <tt>args[0]</tt> as <tt>$TIMEOUT</tt> value, or seek for
     * <tt>-waittime=$WAITTIME</tt> value. If both parameters
     * (or either none of them) are assigned, throw an exception to
     * report parameters inconsistency.
     *
     * <p>Also, seek for <tt>-margin=...</tt> assignment, or assume margin
     * is 1 minute.
     *
     * @param args Is usually obtained via the application's command-line.
     *
     * @throws IllegalArgumentException If <tt>args[]</tt> is inconsistent.
     *
     * @see #appoint(int)
     * @see #appoint(int,int)
     */
    public static int parseAppointment(String args[]) {
        int timeout=-1, margin=1;
        int timeouts=0, waittimes=0, margins=0;
        for (int i=0; i<args.length; i++) {
            if (args[i].startsWith("-")) {
                if (args[i].startsWith("-waittime=")) {
                    timeout = Integer.parseInt(args[i].substring(10));
                    waittimes++;
                }
                if (args[i].startsWith("-margin=")) {
                    margin = Integer.parseInt(args[i].substring(8));
                    margins++;
                }
            } else {
                if (i == 0) {
                    timeout = Integer.parseInt(args[i]);
                    timeouts++;
                }
            }
        };
        if (timeouts==0 && waittimes==0)
            throw new IllegalArgumentException(
                "no $TIMEOUT, nor -waittime=$WAITTIME is set");
        if (waittimes > 1)
            throw new IllegalArgumentException(
                "more than one -waittime=... is set");
        if (margins > 1)
            throw new IllegalArgumentException(
                "more than one -margin=... is set");

        int result = timeout - margin;
        if (result <= 0)
            throw new IllegalArgumentException(
                "delay appointment must be greater than "+margin+" minutes");
        return result;
    }

    /**
     * Appoint harakiri after the given amount of <tt>minutes</tt>,
     * so that exit status would be 95 (to simulate JCK-like PASS
     * status).
     *
     * @throws IllegalStateException If harakiri is already appointed.
     *
     * @see #appoint(int,int)
     * @see #parseAppointment(String[])
     */
    public static void appoint(int minutes) {
        appoint(minutes,95); // JCK-like PASS status
    }

    /**
     * Appoint Harakiri for the given amount of <tt>minutes</tt>,
     * so that the given <tt>status</tt> would be exited when time
     * is over.
     *
     * @throws IllegalStateException If harakiri is already appointed.
     *
     * @see #appoint(int)
     * @see #parseAppointment(String[])
     */
    public static void appoint(int minutes, int status) {
        if (harakiri != null)
            throw new IllegalStateException("Harakiri is already appointed.");

        final long timeToExit = System.currentTimeMillis() + 60*1000L*minutes;
        final int  exitStatus = status;

        harakiri = new Thread(Harakiri.class.getName()) {
            public void run() {
                long timeToSleep = timeToExit - System.currentTimeMillis();
                if (timeToSleep > 0)
                    try {
                        //
                        // Use wait() instead of sleep(), because Java 2
                        // specification doesn't guarantee the method
                        // sleep() to yield to other threads.
                        //
                        Object someDummyObject = new Object();
                        synchronized (someDummyObject) {
                            someDummyObject.wait(timeToSleep);
                        }
                    } catch (InterruptedException exception) {
                        exception.printStackTrace(System.err);
                       //
                       // OOPS, the dagger for harakiri looks broken:
                       //
                       return;
                    };
                //
                // OK, lets do it now:
                //
                System.err.println(
                    "#\n# Harakiri: prescheduled program termination.\n#");
                System.exit(exitStatus); // harakiri to all threads
            }
        };

        harakiri.setPriority(Thread.MAX_PRIORITY);
        harakiri.setDaemon(true);
        harakiri.start();
    }
}