Batchfile Request:How to kill a App once GPU Usage is below set Threshold using a given GPU? per nvidia-smi in Windows

Discuss other programming languages besides AutoHotkey
User avatar
tobsto occupied
Posts: 186
Joined: 20 Feb 2020, 09:34
Contact:

Batchfile Request:How to kill a App once GPU Usage is below set Threshold using a given GPU? per nvidia-smi in Windows

Post by tobsto occupied » 11 Apr 2021, 14:35

Would love to know how to integrate GPU Threshold once reached.

For example below 10% Usage for the Program/App to exit and if possible in a batch file, I've tried with a mix of CPU+memory threshold but that don't always work properly in all Apps, so I thought GPU threshold would solve this since it is pretty definite at times unlike CPU+memory thresholds which can vary greatly sometimes.

Here's a batch example for the threshold that needs a GPU threshold included

Code: Select all

@echo OFF
set VK_LAYER_PATH="" "DOOM;%VK_LAYER_PATH%"
set VK_INSTANCE_LAYERS=VK_LAYER_reshade
@start "" "DOOMx64vk.exe" +devMode_Enable 1 +com_skipKeyPressOnLoadScreens 1
TIMEOUT /t 20
:: set process_name:
 set process_name=DOOMx64vk.exe
 ::=================================================================
cd /D "C:\Program Files\NVIDIA Corporation\NVSMI\"
:GPU
for /f skip^=8^ tokens^=1-3^ delims^=^| %%a in ('"nvidia-smi.exe"') do (
    for /f "tokens=1-6 delims= " %%g in ("%%a") do set u=%%g & set v=%%h) & set w=%%j) & (
    for /f "tokens=1-3 delims= " %%g in ("%%b") do set x=%%g & set y=%%i) & (
    for /f "tokens=1-2 delims= " %%g in ("%%c") do set z=%%g) & goto :breakForLoop
:breakForLoop
set /a u+=0 & set /a v+=0 & set /a w+=0 & set /a x+=0 & set /a z+=0
set /a u=u & set /a v=v & set /a w=w & set /a x=x & set /a z=z
echo %u%, %v%, %w%, %x%, %y%, %z%
pause
timeout /t 5
goto GPU
if %z% Lss 20 TaskKill /F /im %process_name% & goto RUNNING
timeout /t 3
:RUNNING
tasklist|findstr %process_name% > nul &&
    taskkill /F /T /IM "cheatengine-x86_64.exe" & taskkill /F /T /IM "Werfault.exe" & exit /b
timeout /t 1
GOTO RUNNING

as youve guessed said gpu threshold function dont work been at this for months and nothing , hope someone pro enough can get this figured out would be very helpful

gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: Batchfile Request:How to kill a App once GPU Usage is below set Threshold using a given GPU? per nvidia-smi in Windo

Post by gregster » 11 Apr 2021, 14:41

Is this an AutoHotkey-related question ? If so, in what sense?

User avatar
tobsto occupied
Posts: 186
Joined: 20 Feb 2020, 09:34
Contact:

Re: Batchfile Request:How to kill a App once GPU Usage is below set Threshold using a given GPU? per nvidia-smi in Windo

Post by tobsto occupied » 11 Apr 2021, 19:43

no it aint but where else is there a gaming related help section for batch...do tell

gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: Batchfile Request:How to kill a App once GPU Usage is below set Threshold using a given GPU? per nvidia-smi in Windo

Post by gregster » 11 Apr 2021, 19:51

One thing is sure - it's not here.
Perhaps somewhere on these interwebs that I once heard of... well, if it's not AHK-related, I will move the topic to 'Other Programming Languages'.

User avatar
tobsto occupied
Posts: 186
Joined: 20 Feb 2020, 09:34
Contact:

Re: Batchfile Request:How to kill a App once GPU Usage is below set Threshold using a given GPU? per nvidia-smi in Windo

Post by tobsto occupied » 11 Apr 2021, 20:09

sure thing , thanks , hope someone can help out here would do me a real solid


User avatar
rommmcek
Posts: 1470
Joined: 15 Aug 2014, 15:18

Re: Batchfile Request:How to kill a App once GPU Usage is below set Threshold using a given GPU? per nvidia-smi in Windo

Post by rommmcek » 26 Apr 2021, 13:30

You don't need Pause here (may be useful only for debug purpose), so do comment it e.g. rem Pause or delete that line altogether.
Then move goto GPU command further down to before :RUNNING label, so the condition if %z% Lss 20 TaskKill /F /im %process_name% & goto RUNNING will be inside the loop.
Done!

User avatar
tobsto occupied
Posts: 186
Joined: 20 Feb 2020, 09:34
Contact:

Re: Batchfile Request:How to kill a App once GPU Usage is below set Threshold using a given GPU? per nvidia-smi in Windo

Post by tobsto occupied » 27 Apr 2021, 05:04

HOLY FREAKIN ...SHITTTT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! YESSSSSSSSSSSSSSSSSSSSSSSSSS!!!!!!!!!!


my man!!!! you did it again...freaking yehaaaaa! the long wait is finally over ...was absolutely worth it tho,

i tightened the timings and threshold abit as much as possible so it only takes a few moments to kill your frozen app, this is the updated batch script>

Code: Select all

@echo OFF
set VK_LAYER_PATH="" "DOOM;%VK_LAYER_PATH%"
set VK_INSTANCE_LAYERS=VK_LAYER_reshade
@start "" "DOOMLauncher.exe" +devMode_Enable 1 +com_restarted 1 +com_gameMode 1 +com_skipIntroVideo 1 +exec DOOMConfig.cfg 1 +image_usecompression 0 +com_skipKeyPressOnLoadScreens 1
TIMEOUT /t 20
:: set process_name:
 set process_name=DOOMx64vk.exe
 ::=================================================================
:Launch the App
tasklist | findstr /i %process_name% > nul && start /min "" "easy access to campaign.ahk"
timeout /t 20
cd /D "C:\Program Files\NVIDIA Corporation\NVSMI\"
:GPU
for /f skip^=8^ tokens^=1-3^ delims^=^| %%a in ('"nvidia-smi.exe"') do (
    for /f "tokens=1-6 delims= " %%g in ("%%a") do set u=%%g & set v=%%h) & set w=%%j) & (
    for /f "tokens=1-3 delims= " %%g in ("%%b") do set x=%%g & set y=%%i) & (
    for /f "tokens=1-2 delims= " %%g in ("%%c") do set z=%%g) & goto :breakForLoop
:breakForLoop
set /a u+=0 & set /a v+=0 & set /a w+=0 & set /a x+=0 & set /a z+=0
set /a u=u & set /a v=v & set /a w=w & set /a x=x & set /a z=z
echo %u%, %v%, %w%, %x%, %y%, %z%
timeout /t 10
if %z% Lss 10 TaskKill /F /im %process_name% & goto RUNNING
timeout /t 3
goto GPU
:RUNNING
tasklist|findstr %process_name% > nul &&
    taskkill /F /T /IM "cheatengine-x86_64.exe" & taskkill /F /T /IM "WerFault.exe" & exit /b
timeout /t 1
GOTO RUNNING

User avatar
rommmcek
Posts: 1470
Joined: 15 Aug 2014, 15:18

Re: Batchfile Request:How to kill a App once GPU Usage is below set Threshold using a given GPU? per nvidia-smi in Windo

Post by rommmcek » 27 Apr 2021, 08:36

Now you have to solve this homework so I'll see, if you learned something (should be otherwise around)!

User avatar
rommmcek
Posts: 1470
Joined: 15 Aug 2014, 15:18

Re: Batchfile Request:How to kill a App once GPU Usage is below set Threshold using a given GPU? per nvidia-smi in Windo

Post by rommmcek » 27 Apr 2021, 09:03

Now you don't need much of redundant stuff so batch is more succinct:

Code: Select all

@echo OFF
set VK_LAYER_PATH="" "DOOM;%VK_LAYER_PATH%"
set VK_INSTANCE_LAYERS=VK_LAYER_reshade
@start "" "DOOMLauncher.exe" +devMode_Enable 1 +com_restarted 1 +com_gameMode 1 +com_skipIntroVideo 1 +exec DOOMConfig.cfg 1 +image_usecompression 0 +com_skipKeyPressOnLoadScreens 1
TIMEOUT /t 20
:: set process_name:
 set process_name=DOOMx64vk.exe
 ::=================================================================
:Launch the App
tasklist | findstr /i %process_name% > nul && start /min "" "easy access to campaign.ahk"
timeout /t 20
cd /D "C:\Program Files\NVIDIA Corporation\NVSMI\"
:GPU
for /f skip^=8^ tokens^=1-3^ delims^=^| %%a in ('"nvidia-smi.exe"') do (
    for /f "tokens=1-2 delims= " %%g in ("%%c") do set z=%%g) & goto :breakForLoop
:breakForLoop
set /a z=z & timeout /t 13
if %z% Lss 10 TaskKill /F /im %process_name% & goto RUNNING
goto GPU
:RUNNING
tasklist|findstr %process_name% > nul &&
    taskkill /F /T /IM "cheatengine-x86_64.exe" & taskkill /F /T /IM "WerFault.exe" & exit /b
timeout /t 1
GOTO RUNNING
[Edit]: Fixing writing lapsus (z=0 -> z=z)!

User avatar
tobsto occupied
Posts: 186
Joined: 20 Feb 2020, 09:34
Contact:

Re: Batchfile Request:How to kill a App once GPU Usage is below set Threshold using a given GPU? per nvidia-smi in Windo

Post by tobsto occupied » 28 Apr 2021, 02:09

very nice.... :dance:

also , sure shall check out my homework and see what i can come up with :morebeard:

update> oh....that one lols, hell no ! lols, seriously tho i literally still dont get what mistake it has and ive been staring at it for a good time back then and now again :wtf: , i told you im pretty stupid about all things code, my brain is already overwhelmed with making stuff run as is
, sad facts are sad :crazy: :eh: :salute: :cry:

p.s btw if possible as bonus to the script , im not sure how to have it autokill WerFault pertaining said App when it pops up while using the App , havent managed to figure that one out yet tbh , ive tried this , but dont work >

Code: Select all

@echo OFF
set VK_LAYER_PATH="" "DOOM;%VK_LAYER_PATH%"
set VK_INSTANCE_LAYERS=VK_LAYER_reshade
@start "" "DOOMLauncher.exe" +devMode_Enable 1 +com_restarted 1 +com_gameMode 1 +com_skipIntroVideo 1 +exec DOOMConfig.cfg 1 +image_usecompression 0 +com_skipKeyPressOnLoadScreens 1
TIMEOUT /t 20
:: set process_name:
 set process_name=DOOMx64vk.exe & set process_Wer=WerFault.exe
 ::=================================================================
:Launch the App
tasklist | findstr /i %process_name% > nul && start /min "" "easy access to campaign.ahk"
timeout /t 20
cd /D "C:\Program Files\NVIDIA Corporation\NVSMI\"
:GPU
for /f skip^=8^ tokens^=1-3^ delims^=^| %%a in ('"nvidia-smi.exe"') do (
    for /f "tokens=1-2 delims= " %%g in ("%%c") do set z=%%g) & goto :breakForLoop
:breakForLoop
set /a z=z & timeout /t 13
if %z% Lss 10 TaskKill /F /im %process_name% & goto RUNNING
goto GPU
:RUNNING
tasklist|findstr %process_Wer% > nul && Goto Terminate
tasklist|findstr %process_name% > nul &&
    taskkill /F /T /IM "cheatengine-x86_64.exe" & taskkill /F /T /IM "WerFault.exe" & exit /b
:Terminate
taskkill /F /T /IM "WerFault.exe"
timeout /t 1
GOTO RUNNING

User avatar
tobsto occupied
Posts: 186
Joined: 20 Feb 2020, 09:34
Contact:

Re: Batchfile Request:How to kill a App once GPU Usage is below set Threshold using a given GPU? per nvidia-smi in Windo

Post by tobsto occupied » 30 Apr 2021, 03:26

ok after abit of more tinkerin ive also found out the :Running wasnt killing the listed processes in it and the threshold being yet still too high for 100% reliability on all apps aka when they take abit longer to load and gpu usage being too low, so tweaked that and ive fixed said non functioning kill process by adding () around the commands also added a starter to the kill process or else it would not work proper aka following processes not being killed at all and even the batch infinite running instead of exiting , this for an app

its also odd the cpu+memory dont work on every app , like it simple exits the batch due to it thinking cpu+mem are below threshold , dont even kill the app if it it truly was so , and yet for an app it works proper , this is my app batch that includes cpu+memory+gpu threshold:

Code: Select all

@echo OFF
Title Autolaunch and Autoexit All Game Processes tied to app
::========= Set cpuThreshold [%] and memThreshold [according to displayed value in Cmd window] =========
set /a cpuThreshold=10        & set /a memThreshold=480000
::initialize vars                            and set process_name:
set /a low=0 & setlocal enableDelayedExpansion & set process_name=app.exe
set pn=%process_name:.exe=%
::==================================================================
::Launch the App
start "" "app.exe"
TIMEOUT /t 3
tasklist | findstr /i %process_name% > nul && start /min "" "appaddon.exe"
timeout /t 20
cd /D "C:\Program Files\NVIDIA Corporation\NVSMI\"
:GPU
for /f skip^=8^ tokens^=1-3^ delims^=^| %%a in ('"nvidia-smi.exe"') do (
    for /f "tokens=1-2 delims= " %%g in ("%%c") do set z=%%g) & goto :breakForLoop
:breakForLoop
set /a z=z & timeout /t 13
if %z% Lss 5 TaskKill /F /im %process_name% & goto RUNNING
::Get NumberOfCores
for /f "tokens=*" %%f in ('wmic cpu get NumberOfCores /value ^| find "="') do set %%f
:CheckCpuAndMem
set /a process_snapshot=0 & set /a memUsg=0
::CPU
for /f skip^=2^ tokens^=3^ delims^=^" %%p in (
    'typeperf "\Process(%pn%)\%% Processor Time" -sc 1') do for /f "tokens=1 delims=." %%a in (
    "%%p") do set /a process_snapshot=%%a/%NumberOfCores%
if %process_snapshot% LSS %cpuThreshold% (set /a low+=1) else (set /a low=0)
::Mem
for /f "tokens=5" %%p in (
  'tasklist ^|findstr %process_name%') do for /f "tokens=1,2,3 delims=." %%a in ("%%p") do set /a memUsg=%%a%%b%%c/1000
if %memUsg% lss %memThreshold% (set /a low+=1) else (set /a low=0)
::echo feedback
@echo process_snapshot: %process_snapshot%, memUsage: %memUsg%;    LowCpuAndMem: %low%
::12 checks (Cpu & Mem) times 3 sec timeout plus processing delay yields ca 30 sec
if %low% GEQ 12 taskkill /F /T /IM %process_name% & goto RUNNING
timeout /t 3
tasklist|findstr %process_name% > nul && GOTO GPU
:RUNNING
tasklist|findstr %process_name% > nul && RUNNING
 taskkill /F /T /IM WerFault.exe & exit /b
timeout /t 1
GOTO RUNNING

and this works on another app, only gpu threshold , since cpu+memory simple kill batch without touching the app >

Code: Select all

@echo OFF
Title appvk_with_reshade
set VK_LAYER_PATH="" "app;%VK_LAYER_PATH%"
set VK_INSTANCE_LAYERS=VK_LAYER_reshade
@start "" "app.exe" 
TIMEOUT /t 20
:: set process_name:
 set process_name=app.exe
 ::=================================================================
:Launch the App
timeout /t 3
tasklist | findstr /i %process_name% > nul && start /min "" "app.ahk"
timeout /t 20
cd /D "C:\Program Files\NVIDIA Corporation\NVSMI\"
:GPU
for /f skip^=8^ tokens^=1-3^ delims^=^| %%a in ('"nvidia-smi.exe"') do (
    for /f "tokens=1-2 delims= " %%g in ("%%c") do set z=%%g) & goto :breakForLoop
:breakForLoop
set /a z=z & timeout /t 13
if %z% Lss 10 TaskKill /F /im %process_name% & goto RUNNING
goto GPU
:RUNNING
tasklist|findstr %process_name% > nul && (
   taskkill /F /T /IM "WerFault.exe" & exit /b)
timeout /t 1
GOTO RUNNING
also no idea how to integrate both of these functions>

Code: Select all


set process_Wer=WerFault.exe

:Loading
 timeout /t 5 & set /a LoadTime= %LoadTime%+5
 if %LoadTime% Geq 60 Goto Running
 tasklist|findstr %process_name% > nul && Goto Running
 Goto Loading
 
 tasklist|findstr %process_Wer% > nul && Goto Terminate
 
 :Terminate
taskkill /F /T /IM "WerFault.exe"
 

what is also interesting of a find is for one app a gpu threshold of 10 works proper , but for another a threshold of 5 works and with 10 quickly exits
Last edited by tobsto occupied on 24 Jul 2021, 12:00, edited 4 times in total.

User avatar
tobsto occupied
Posts: 186
Joined: 20 Feb 2020, 09:34
Contact:

Re: Batchfile Request:How to kill a App once GPU Usage is below set Threshold using a given GPU? per nvidia-smi in Windo

Post by tobsto occupied » 02 May 2021, 09:09

update 3 ...ok i just fucked up something , heres a new script that works for both and also exits proper on below threshold of cpu+memory or gpu and the other apps that are listed under running , my bad , here>

Code: Select all

@echo OFF
Title Autolaunch and Autoexit All Game Processes tied to App
set VK_LAYER_PATH="" "App;%VK_LAYER_PATH%"
set VK_INSTANCE_LAYERS=VK_LAYER_reshade
@start "" "app.exe"
::========= Set cpuThreshold [%] and memThreshold [according to displayed value in Cmd window] =========
set /a cpuThreshold=10        & set /a memThreshold=480000
::initialize vars                            and set process_name:
set /a low=0 & setlocal enableDelayedExpansion & set process_name=app.exe
set pn=%process_name:.exe=%
::==================================================================
::Launch the App
timeout /t 30
tasklist | findstr /i %process_name% > nul && start /min "" "app.ahk"
timeout /t 20
cd /D "C:\Program Files\NVIDIA Corporation\NVSMI\"
:GPU
for /f skip^=8^ tokens^=1-3^ delims^=^| %%a in ('"nvidia-smi.exe"') do (
    for /f "tokens=1-2 delims= " %%g in ("%%c") do set z=%%g) & goto :breakForLoop
:breakForLoop
set /a z=z & timeout /t 13
if %z% Lss 5 TaskKill /F /im %process_name% & goto RUNNING
::Get NumberOfCores
for /f "tokens=*" %%f in ('wmic cpu get NumberOfCores /value ^| find "="') do set %%f
:CheckCpuAndMem
set /a process_snapshot=0 & set /a memUsg=0
::CPU
for /f skip^=2^ tokens^=3^ delims^=^" %%p in (
    'typeperf "\Process(%pn%)\%% Processor Time" -sc 1') do for /f "tokens=1 delims=." %%a in (
    "%%p") do set /a process_snapshot=%%a/%NumberOfCores%
if %process_snapshot% LSS %cpuThreshold% (set /a low+=1) else (set /a low=0)
::Mem
for /f "tokens=5" %%p in (
  'tasklist ^|findstr %process_name%') do for /f "tokens=1,2,3 delims=." %%a in ("%%p") do set /a memUsg=%%a%%b%%c/1000
if %memUsg% lss %memThreshold% (set /a low+=1) else (set /a low=0)
::echo feedback
@echo process_snapshot: %process_snapshot%, memUsage: %memUsg%;    LowCpuAndMem: %low%
::12 checks (Cpu & Mem) times 3 sec timeout plus processing delay yields ca 30 sec
if %low% GEQ 12 taskkill /F /T /IM %process_name% & goto RUNNING
timeout /t 3
tasklist|findstr %process_name% > nul && GOTO GPU
:RUNNING
tasklist|findstr %process_name% > nul && goto RUNNING
taskkill /F /T /IM WerFault.exe & exit /b

about adding an additional watch for WerFault during run and Load wait , still would love a solution for that tho :salute:
Last edited by tobsto occupied on 24 Jul 2021, 11:56, edited 1 time in total.

User avatar
tobsto occupied
Posts: 186
Joined: 20 Feb 2020, 09:34
Contact:

Re: Batchfile Request:How to kill a App once GPU Usage is below set Threshold using a given GPU? per nvidia-smi in Windo

Post by tobsto occupied » 29 Jun 2021, 13:21

still would appreciate a solution to this, still cant figure out how to make the Loading feature actually be respected, also have no idea how to add an additional process watch for WerFault.exe popup tied to App exe, as said function should be so that as soon as WerFault pops up during app usage for that instance of WerFault.exe to get killed, much appreciated

Code: Select all

@echo OFF
Title Autolaunch and Autoexit All  Processes tied to App
::========= Set cpuThreshold [%] and memThreshold [according to displayed value in Cmd window] =========
set /a cpuThreshold=10        & set /a memThreshold=480000
::initialize vars                            and set process_name:
set /a low=0 & setlocal enableDelayedExpansion & set process_name=app.exe
set pn=%process_name:.exe=%
::==================================================================
::Launch the App
start "" "app.exe"

:Loading
timeout /t 5 & set /a LoadTime+= %LoadTime%+5
if LoadTime Geq 60 goto :RUNNING

timeout /t 20
cd /D "C:\Program Files\NVIDIA Corporation\NVSMI\"
:GPU
for /f skip^=8^ tokens^=1-3^ delims^=^| %%a in ('"nvidia-smi.exe"') do (
    for /f "tokens=1-2 delims= " %%g in ("%%c") do set z=%%g) & goto :breakForLoop
:breakForLoop
set /a z=z & timeout /t 30
if %z% Lss 10 TaskKill /F /im %process_name% & goto :RUNNING
::Get NumberOfCores
for /f "tokens=*" %%f in ('wmic cpu get NumberOfCores /value ^| find "="') do set %%f
:CheckCpuAndMem
set /a process_snapshot=0 & set /a memUsg=0
::CPU
for /f skip^=2^ tokens^=3^ delims^=^" %%p in (
    'typeperf "\Process(%pn%)\%% Processor Time" -sc 1') do for /f "tokens=1 delims=." %%a in (
    "%%p") do set /a process_snapshot=%%a/%NumberOfCores%
if %process_snapshot% LSS %cpuThreshold% (set /a low+=1) else (set /a low=0)
::Mem
for /f "tokens=5" %%p in (
  'tasklist ^|findstr %process_name%') do for /f "tokens=1,2,3 delims=." %%a in ("%%p") do set /a memUsg=%%a%%b%%c/1000
if %memUsg% lss %memThreshold% (set /a low+=1) else (set /a low=0)
::echo feedback
@echo process_snapshot: %process_snapshot%, memUsage: %memUsg%;    LowCpuAndMem: %low%
::12 checks (Cpu & Mem) times 3 sec timeout plus processing delay yields ca 30 sec
if %low% GEQ 12 taskkill /F /T /IM %process_name% & goto :RUNNING
timeout /t 3
goto :Loading
:RUNNING
tasklist|findstr %process_name% > nul && goto :RUNNING
taskkill /F /T /IM WerFault.exe & exit /b
Last edited by tobsto occupied on 24 Jul 2021, 11:51, edited 1 time in total.





User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Batchfile Request:How to kill a App once GPU Usage is below set Threshold using a given GPU? per nvidia-smi in Windo

Post by jNizM » 06 Oct 2021, 04:52

Why a batch script and not a powershell script?
Powershell is more powerfull.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

User avatar
tobsto occupied
Posts: 186
Joined: 20 Feb 2020, 09:34
Contact:

Re: Batchfile Request:How to kill a App once GPU Usage is below set Threshold using a given GPU? per nvidia-smi in Windo

Post by tobsto occupied » 11 Oct 2021, 06:54

well ....i got PS7 .....but i dont know jack shit how to use it vs batch ...im like literally total noob, and im already with batch , with PS7 id get even less anywhere

Post Reply

Return to “Other Programming Languages”