-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
executable file
·172 lines (138 loc) · 5.09 KB
/
run.sh
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
#!/bin/bash
# Copyright 2017, Oracle and/or its affliates. All rights reserved.
echo "$(date +%H:%M:%S): Hello from the Java Flight Recorder Wercker Step"
echo "For information on how to use this step, please review the documentation in the Wercker Marketplace,"
echo "or visit https://github.com/wercker/java-flight-recorder"
# check that all of the required parameters were provided
# note that wercker does not enforce this for us, so we have to check
if [[ -z "$WERCKER_JAVA_FLIGHT_RECORDER_APPLICATION" || -z "$WERCKER_JAVA_FLIGHT_RECORDER_FILENAME" || -z "$WERCKER_JAVA_FLIGHT_RECORDER_DURATION" ]]; then
echo "$(date +%H:%M:%S): All required parameters: application, filename, and duration MUST be specified"
exit 9
fi
# try to find Java
if [ -z "$JAVA_HOME" ] ; then
JAVACMD=`which java`
else
JAVACMD="$JAVA_HOME/bin/java"
fi
# find the real location of java - in case it is a symlink
if [ -z "$JAVA_HOME" ] ; then
# resolve links - $JAVACMD may be a link
PRG="$JAVACMD"
# need this for relative symlinks
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link" else
PRG="`dirname "$PRG"`/$link"
fi
done
saveddir=`pwd`
JAVA_HOME=`dirname "$PRG"`/..
# make it fully qualified
JAVA_HOME=`cd "$JAVA_HOME" && pwd`
cd "$saveddir"
fi
if [[ -z "$JAVA_HOME" ]]; then
echo "$(date +%H:%M:%S): Could not find Java in this box - please make sure you have Oracle JDK installed"
exit 8
fi
echo "$(date +%H:%M:%S): Found JAVA_HOME at $JAVA_HOME"
#
# Get ready to run the application under test
#
# check if any classpath entries were provided
if [[ -z "$WERCKER_JAVA_FLIGHT_RECORDER_CLASSPATH" ]]; then
CLASSPATH=""
else
CLASSPATH="-classpath $WERCKER_JAVA_FLIGHT_RECORDER_CLASSPATH"
fi
# turn on experimental options if requested
if [ "$WERCKER_JAVA_FLIGHT_RECORDER_EXPERIMENTAL" = "true" ]; then
EXPERIMENTAL="-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap"
else
EXPERIMENTAL=""
fi
if [[ -z "$WERCKER_JAVA_FLIGHT_RECORDER_TIMEOUT" ]]; then
TIMEOUT=""
else
TIMEOUT="timeout -k $WERCKER_JAVA_FLIGHT_RECORDER_TIMEOUT $WERCKER_JAVA_FLIGHT_RECORDER_TIMEOUT "
fi
# start the application under test
echo "$(date +%H:%M:%S): Starting the application under test..."
APPCMD="$JAVA_HOME/bin/java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder $CLASSPATH $EXPERIMENTAL $WERCKER_JAVA_FLIGHT_RECORDER_JAVA_OPTS $WERCKER_JAVA_FLIGHT_RECORDER_APPLICATION"
echo "The app command is: $APPCMD"
$TIMEOUT $APPCMD &
APPPID=$!
#
# Get ready to run the load driver
#
if [[ -z "$WERCKER_JAVA_FLIGHT_RECORDER_DRIVER" ]]; then
echo "$(date +%H:%M:%S): You did not provide a load driver, so I will not try to start one"
else
# check if any classpath entries were provided
if [[ -z "$WERCKER_JAVA_FLIGHT_RECORDER_DRIVER_CLASSPATH" ]]; then
DRIVER_CLASSPATH=""
else
DRIVER_CLASSPATH="-classpath $WERCKER_JAVA_FLIGHT_RECORDER_DRIVER_CLASSPATH"
fi
echo "$(date +%H:%M:%S): Starting the load driver..."
DRIVERCMD="$JAVA_HOME/bin/java $DRIVER_CLASSPATH $WERCKER_JAVA_FLIGHT_RECORDER_DRIVER_JAVA_OPTS $EXPERIMENTAL $WERCKER_JAVA_FLIGHT_RECORDER_DRIVER"
echo "$(date +%H:%M:%S): The load driver command is: $DRIVERCMD"
$TIMEOUT $DRIVERCMD &
DRIVERPID=$!
fi
#
# Get ready to start the flight recorder
#
if [[ -z "$WERCKER_JAVA_FLIGHT_RECORDER_DELAY" ]]; then
DELAY=""
else
DELAY="delay=$WERCKER_JAVA_FLIGHT_RECORDER_DELAY"
fi
if [[ -z "$WERCKER_JAVA_FLIGHT_RECORDER_MAXSIZE" ]]; then
MAXSIZE=""
else
MAXSIZE="maxsize=$WERCKER_JAVA_FLIGHT_RECORDER_MAXSIZE"
fi
if [[ -z "$WERCKER_JAVA_FLIGHT_RECORDER_MAXAGE" ]]; then
MAXAGE=""
else
MAXAGE="maxage=$WERCKER_JAVA_FLIGHT_RECORDER_MAXAGE"
fi
if [[ -z "$WERCKER_JAVA_FLIGHT_RECORDER_COMPRESS" ]]; then
COMPRESS=""
else
COMPRESS="compress=$WERCKER_JAVA_FLIGHT_RECORDER_COMPRESS"
fi
# start the recording
echo "$(date +%H:%M:%S): Starting Java Flight Recorder..."
JFRCMD="$JAVA_HOME/bin/jcmd $WERCKER_JAVA_FLIGHT_RECORDER_APPLICATION JFR.start duration=$WERCKER_JAVA_FLIGHT_RECORDER_DURATION filename=$WERCKER_JAVA_FLIGHT_RECORDER_FILENAME $DELAY $MAXSIZE $MAXAGE $COMPRESS "
echo "$(date +%H:%M:%S): The JFR command is: $JFRCMD"
$JFRCMD &
JFRPID=$!
#
# Wait for everything to finish
#
# wait for the application and the recording to finish
# note that timeout will kill them if they go over the specified timeout
echo "$(date +%H:%M:%S): Waiting for the application and load driver to finish..."
PIDS=()
PIDS+=$APPPID
PIDS+=$DRIVERPID
wait "${PIDS[@]}"
echo ""
#
# tell JFR to save and stop (if it has not already)
#
# check if the recording is still running
RUNNING=`$JAVA_HOME/bin/jcmd | grep $JFRPID | wc | awk ' { print $1; } '`
if [ "$RUNNING" == "1" ]; then
$JAVA_HOME/bin/jcmd $WERCKER_JAVA_FLIGHT_RECORDER_APPLICATION JFR.dump
$JAVA_HOME/bin/jcmd $WERCKER_JAVA_FLIGHT_RECORDER_APPLICATION JFR.stop
fi
# push the output to the next pipeline
echo "$(date +%H:%M:%S): Saving the recordings..."
cp $WERCKER_JAVA_FLIGHT_RECORDER_FILENAME /pipeline/output
echo "$(date +%H:%M:%S): Java Flight Recorder Wercker Step Finished"