awips2/cave/build/tools/capture.bat
2022-05-05 12:34:50 -05:00

129 lines
4.6 KiB
Batchfile

@echo OFF
REM This is a MSFT Windows version of the CAVE capture script.
REM This script will identify any running CAVE processes (cave.exe)
REM and use Java to generate jstacks and (optionally) heap dumps
REM for the process(es). All of the files generated by this capture
REM script and any associated CAVE logs will be placed in a directory
REM in the user's 'My Documents' directory.
REM Usage: capture.bat [-d][-h]
REM Verify that the location of the Java JDK is known.
IF "%JAVA_JDK%" == "" (
echo ERROR: Unable to determine the location of the Java JDK. Use the JAVA_JDK Env Variable to set the location.
GOTO EOF
)
SET heap_dump=0
REM Process and validate any command line parameters, displaying usage
REM information if necessary.
IF "%1" == "" GOTO run
REM Verify that there is only one argument.
IF NOT "%2" == "" GOTO usage
REM Check for the -d argument.
IF "%1" == "-d" (
SET heap_dump=1
GOTO run
)
REM Either an invalid argument or the -h argument.
GOTO usage
:usage
echo Usage: %0 [-d][-h]
echo -d: Enables the heap dump capture
echo -h: Displays the usage message
GOTO EOF
:run
SET jstack_iterations=15
SET PROCESS=cave.exe
REM Prepare Environment
SET Path=%JAVA_JDK%\bin;%Path%
REM Build a date/time string to use in the name for the capture directory.
SET RND=%random%
SET RND_DATETIME_FILE=%TMP%\awips2dt_%RND%.tmp
REM Python is used to retrieve the current date and time because the order
REM of the Windows date/time fields is not necessarily guaranteed and the
REM Windows date/time fields can only be extracted using substring operations
REM instead of -formatter- strings like Linux allows.
python -c "from datetime import datetime; print datetime.now().strftime('%%Y%%m%%d_%%H%%M%%S');" > %RND_DATETIME_FILE%
SET /p CAPTURE_DATETIME= < %RND_DATETIME_FILE%
DEL %RND_DATETIME_FILE%
SET CAPTURE_DIRECTORY=%userprofile%\capture_%CAPTURE_DATETIME%
SET CAVEDATA_LOGS_DIRECTORY=%userprofile%\%logdir%
SET CAPTURE_LOG=%CAPTURE_DIRECTORY%\capture_info.log
REM Get the PID of any currently running CAVE process.
for /f "tokens=2 delims=," %%F in ('tasklist /nh /fi "imagename eq %PROCESS%" /fo csv') do call :process_pid %%~F
GOTO prepare_exit
:process_pid
IF NOT EXIST "%CAPTURE_DIRECTORY%" (MKDIR "%CAPTURE_DIRECTORY%")
SET process_pid=%1
SET jstack_counter=0
echo Found %PROCESS% with pid %process_pid% ...
echo Found %PROCESS% with pid %process_pid% ... >> %CAPTURE_LOG%
:while_jstack
if %jstack_counter% lss %jstack_iterations% (
set /a jstack_counter+=1
echo Completing jstack iteration %jstack_counter% ...
echo Completing jstack iteration %jstack_counter% ... >> %CAPTURE_LOG%
SET jstack_log=%CAPTURE_DIRECTORY%\pid_%process_pid%_jstack_%jstack_counter%.log
jstack -l %process_pid% >> "%jstack_log%"
echo Running command: jstack -l %process_pid% >> %CAPTURE_LOG%
IF ERRORLEVEL 1 (
echo jstack for pid %process_pid% failed to connect, rerunning with -F >> %CAPTURE_LOG%
echo jstack for pid %process_pid% failed to connect, rerunning with -F
echo Running command: jstack -l -F %process_pid% >> %CAPTURE_LOG%
jstack -l -F %process_pid% >> "%jstack_log%"
)
goto :while_jstack
)
REM Copy the CAVE logs for the pid to the capture directory.
echo Running command: xcopy "%CAVEDATA_LOGS_DIRECTORY%\*pid_%process_pid%*.log" "%CAPTURE_DIRECTORY%" >> %CAPTURE_LOG%
xcopy "%CAVEDATA_LOGS_DIRECTORY%\*pid_%process_pid%*.log" "%CAPTURE_DIRECTORY%" >> %CAPTURE_LOG%
IF ERRORLEVEL 0 GOTO check_heap
echo Failed to copy the CAVE logs files.
echo Failed to copy the CAVE logs files. >> %CAPTURE_LOG%
:check_heap
REM Create a heap dump if the option has been enabled.
IF %heap_dump% NEQ 1 (
echo Heap dumps have been disabled. >> %CAPTURE_LOG%
exit /b
)
SET heap_path=%CAPTURE_DIRECTORY%\pid_%process_pid%_heap.hprof
SET dump_path=%CAPTURE_DIRECTORY%\pid_%process_pid%_dump.txt
echo Running command: jmap -heap %process_pid% >> %CAPTURE_LOG%
jmap -heap %process_pid% > %heap_path%
echo Running command: jmap -dump:live,format=b,file=%dump_path% %process_pid% >> %CAPTURE_LOG%
jmap -dump:live,format=b,file=%dump_path% %process_pid%
IF ERRORLEVEL 1 (
echo jmap for pid %process_pid% failed to connect, rerunning with -F >> %CAPTURE_LOG%
echo jmap for pid %process_pid% failed to connect, rerunning with -F
echo Running command: jmap -dump:live,format=b,file=%dump_path% -F %process_pid% >> %CAPTURE_LOG%
jmap -dump:live,format=b,file=%dump_path% -F %process_pid%
)
REM end of process_pid sub-routine
exit /b
:prepare_exit
IF NOT EXIST %CAPTURE_DIRECTORY% (GOTO EOF)
echo Capture data is available in %CAPTURE_DIRECTORY% ...
start "" "%CAPTURE_DIRECTORY%"
PAUSE
:EOF