jerome Posted December 9, 2006 Share Posted December 9, 2006 Hello, Here is a little software I uses to get a little more CPU when trying to work with heavy background calculation processes. It lowers priority of time consuming process and can also return to normality. If it is usefull for someone... expandcollapse popup#include-once #region Header #comments-start Title: Process Management UDF Library for AutoIt3 Filename: Priority.au3 Description: A collection of UDF's that assist in defining several timers with function callbacks. Author: J. DERN (JD) Version: 01.00 Last Update: 27.11.06 Requirements: AutoIt v3.2 +, Developed/Tested on WindowsXP Pro Service Pack 2 Notes: This code is usefull If you are using background processes that can use a lot of CPU (scientific aclculations, transcoding, ...) #comments-end #endregion Header #region Global Variables and Constants #include <GUIConstants.au3> #include <Array.au3> #Include <GuiTab.au3> #include <GuiList.au3> Const $TRAY_ENABLE = 64 Const $TRAY_DISABLE = 128 Dim $PriorityCancel[10] PriorityRun() Func PriorityRun() PriorityInit() TraySetState() While 1 Sleep(100) WEnd EndFunc Func PriorityInit() Global $G_getcpu, $G_allcpu, $G_restore Opt("RunErrorsFatal", 0) Opt("GUIOnEventMode", 1) Opt("TrayOnEventMode",1) Opt("TrayMenuMode", 1) ; Default tray menu items (Script Paused/Exit) will not be shown. Opt("GUIResizeMode", $GUI_DOCKSIZE+$GUI_DOCKLEFT) TraySetToolTip ("PriorityTray 1.0") ;TraySetIcon("SHELL32.dll", 24) $G_getcpu = TrayCreateItem("Get CPU") TrayItemSetOnEvent (-1, "CB_GetCPU") $G_allcpu = TrayCreateItem("Get all CPU") TrayItemSetOnEvent (-1, "CB_AllCPU") $G_restore = TrayCreateItem("Normal mode") TrayItemSetOnEvent (-1, "CB_Restore") TrayItemSetState($G_restore, $TRAY_DISABLE) TrayCreateItem("Show CPU") TrayItemSetOnEvent (-1, "CB_DisplayProcess") TrayCreateItem("Exit") TrayItemSetOnEvent (-1, "CB_PriorityExit") EndFunc Func CB_PriorityExit() Exit EndFunc Func CB_GetCPU() GetBackCPU(1) TraySetIcon("C:\WINDOWS\system32\SHELL32.dll", 1) TrayItemSetState($G_restore, $TRAY_ENABLE) TraySetIcon("SHELL32.dll", 43) EndFunc Func CB_AllCPU() GetBackCPU(0) TrayItemSetState($G_restore, $TRAY_ENABLE) TraySetIcon("SHELL32.dll", 43) EndFunc Func CB_Restore() TrayItemSetState($G_restore, $TRAY_DISABLE) RestoreCPU() TraySetIcon() EndFunc Func CB_DisplayProcess() $Process = SortProcessCPU() $liste="" For $i=0 To UBound($Process, 1)-1 $liste = $liste & $Process[$i][0] & "% " & $Process[$i][1] & @CRLF Next TrayTip("Process", $liste, 10) EndFunc ;=================================================================================================== ========================== ;= = ;= U T I L I T I E S = ;= = ;=================================================================================================== ========================== Func GetBackCPU($Value) $PriorityCancel[0] = 0 $Process = SortProcessCPU() $Size = UBound($Process, 1) $limit = 0 If $Value = 1 Then $limit = $Process[0][0] / 4 For $i = 0 To $Size-1 If $Process[$i][0] >= $limit Then ProcessSetPriority($Process[$i][2], $Value) $PriorityCancel[0] += 1 If $PriorityCancel[0] > 9 Then ReDim $PriorityCancel[$PriorityCancel[0]+1] $PriorityCancel[$PriorityCancel[0]] = $Process[$i][2] EndIf Next EndFunc Func RestoreCPU() For $i = 1 To $PriorityCancel[0] ProcessSetPriority($PriorityCancel[$i], 2) Next EndFunc ;=============================================================================== ; Description: Get process with CPU load and PID, sort them by CPU % (highest first) ; Parameter(s): none ; Requirement(s): Win2000/XP ; Return Value(s): A two dimension array (use UBound($Process, 1) to get the size) ; [$i][0] is cpu % ; [$i][1] is name ; [$i][2] is PID ; Author(s): Jerome DERN (jdern "at" free "dot" fr) ; Note(s): none ;=============================================================================== Func SortProcessCPU() $array = _ProcessListCPU("", 1000) $size = $array[0][0] Dim $Process[$size+1][3] $i = 0 For $j=1 To $size If $array[$j][0] <> "Idle" And $array[$j][0] <> "System" And $array[$j][0] <> "Services"Then $Process[$i][0] = $array[$j][2] $Process[$i][1] = $array[$j][0] $Process[$i][2] = $array[$j][1] $i+=1 EndIf Next $size = $i _ArraySort($Process, 1, 0, $size, 3) Return $Process EndFunc ;=================================================================================================== ========================== ;= = ;= L I S T C P U C O N S U M I N G P R O C E S S = ;= = ;=================================================================================================== ========================== Func _ProcessListCPU($strProcess = "", $iSampleTime = 500, $sComputerName = @ComputerName) ;~ All Parameters are optional: ;~ - All process items will be returned if first parameter is not set ;~ - 500 ms is default sample time ;~ - This computer will be measured by default ;~ Process could be string ("Name") or PID number (1234) ;~ For NORMAL MODE(one time measuring): set Sample value to more than 0 ms ;~ ( average CPU usage will be measured during sleep time within function) ;~ For LOOP MODE (continuous measuring): set Sample value to 0 ms ;~ ( average CPU usage will be measured between two function calls ) ;~ ;~ Success: Returns 2-D array $array[0][0] = Number of processes (also upper bound of array) ;~ $array[0][1] = Total CPU usage by All Processes ;~ $array[0][2] = Total CPU usage by All Processes under given process "Name" ;~ ;~ $array[1][0] = 1st Process name ;~ $array[1][1] = 1st Process ID (PID) ;~ $array[1][2] = 1st Process CPU Usage ;~ ;~ $array[2][0] = 2nd Process name ;~ $array[2][1] = 2nd Process ID (PID) ;~ $array[2][2] = 2nd Process CPU Usage ;~ ... ;~ $array[n][0] = nth Process name ;~ $array[n][1] = nth Process ID (PID) ;~ $array[n][2] = nth Process CPU Usage ;~ ;~ Failure: Returns 2-D array $array[0][0] = "" ( wrong process name or PID ) ;~ $array[0][0] = -1 ( process collection not found) ;~ $array[0][0] = -2 ( WMI service not found or Computer not found) dim $aResultSet[1][3] if $strProcess = 0 AND IsNumber($strProcess) then $strProcess = "Idle" if $iSampleTime = "" AND IsString($iSampleTime) then $iSampleTime = 500 if $sComputerName = "" then $sComputerName = @ComputerName if not IsDeclared("aProcess") AND $iSampleTime = 0 then ;first time in loop mode $bFirstTimeInLoopMode = 1 else $bFirstTimeInLoopMode = 0 endif if not IsDeclared("aProcess") then global $aProcess[10000][10];[ nul, PID, name, CPU, iP1, iT1, iP2, iT2, iPP, iTT] ; 0 1 2 3 4 5 6 7 8 9 endif $objWMIService = ObjGet("winmgmts:\\" & $sComputerName & "\root\CIMV2") if @error then $aResultSet[0][0] = -2 return $aResultSet endif if $iSampleTime OR $bFirstTimeInLoopMode = 1 then ;skip if Loop Mode, but not First time $colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_PerfRawData_PerfProc_Process") For $objItem In $colItems $iPID = $objItem.IDProcess If $iPID = 0 AND $objItem.Name = "_Total" Then $iPID = 9999 $aProcess[$iPID][4] = $objItem.PercentProcessorTime $aProcess[$iPID][5] = $objItem.TimeStamp_Sys100NS next if $objItem = "" then $aResultSet[0][0] = -1 ;collection or process not found return $aResultSet endif sleep($iSampleTime) endif $colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_PerfRawData_PerfProc_Process") $iCountItems = 0 $iTotalUsedByName = 0 $iTotalUsed = 0 For $objItem In $colItems if $objItem.Name = "_Total" AND not($strProcess = "_Total") then exitloop $iPID = $objItem.IDProcess If $iPID = 0 AND $objItem.Name = "_Total" Then $iPID = 9999 ;~ $aProcess[$iPID][1] = $objItem.IDProcess ;~ $aProcess[$iPID][2] = $objItem.Name $aProcess[$iPID][6] = $objItem.PercentProcessorTime $aProcess[$iPID][7] = $objItem.TimeStamp_Sys100NS $aProcess[$iPID][8] = $aProcess[$iPID][6] - $aProcess[$iPID][4];$iPP = ($iP2 - $iP1) $aProcess[$iPID][9] = $aProcess[$iPID][7] - $aProcess[$iPID][5];$iTT = ($iT2 - $iT1) $aProcess[$iPID][3] = round( ($aProcess[$iPID][8] / $aProcess[$iPID][9]) * 100, 0) ;$iCPU = round( ($iPP/$iTT) * 100, 0) $aProcess[$iPID][4] = $aProcess[$iPID][6] ;$iP1 = $iP2 $aProcess[$iPID][5] = $aProcess[$iPID][7] ;$iT1 = $iT2 ;SumTotalUsed if not($objItem.Name = "Idle") AND not($objItem.Name = "_Total") Then $iTotalUsed = $iTotalUsed + $aProcess[$iPID][3] $aResultSet[0][1] = $iTotalUsed endif ;Result Set if (($strProcess = "") AND IsString($strProcess)) OR _ ;strNAME = "", but not 0 (StringUpper($strProcess) = StringUpper($objItem.Name)) OR _ ;strNAME = objNAME ((IsNumber($strProcess) AND $strProcess = $objItem.IDProcess)) then ;i1234 = obj1234 $iCountItems += 1 redim $aResultSet[$iCountItems +1][3] $aResultSet[$iCountItems][0] = $objItem.Name $aResultSet[$iCountItems][1] = $iPID $aResultSet[$iCountItems][2] = $aProcess[$iPID][3] $aResultSet[0][0] = $iCountItems ;SumTotalByName if not($objItem.Name = "Idle") OR ($strProcess = "Idle") Then $iTotalUsedByName = $iTotalUsedByName + $aProcess[$iPID][3] $aResultSet[0][2] = $iTotalUsedByName endif endif next if $objItem = "" then $aResultSet[0][0] = -1 ;collection or process not found return $aResultSet endif Return $aResultSet EndFunc;==>_ProcessListCPU() by novaTek ...ver 0.01 ;=================================================================================================== ========================== ;= = ;= E N D = ;= = ;=================================================================================================== ========================== Link to comment Share on other sites More sharing options...
littleclown Posted December 9, 2006 Share Posted December 9, 2006 Script.au3(71,32) : WARNING: $G_restore: possibly used before declaration. Link to comment Share on other sites More sharing options...
jerome Posted December 9, 2006 Author Share Posted December 9, 2006 Script.au3(71,32) : WARNING: $G_restore: possibly used before declaration. It was not a bug but I correct it... expandcollapse popup#include-once #region Header #comments-start Title: Process Management UDF Library for AutoIt3 Filename: Priority.au3 Description: A collection of UDF's that assist in defining several timers with function callbacks. Author: J. DERN (JD) Version: 01.00 Last Update: 27.11.06 Requirements: AutoIt v3.2 +, Developed/Tested on WindowsXP Pro Service Pack 2 Notes: This code is usefull If you are using background processes that can use a lot of CPU (scientific aclculations, transcoding, ...) #comments-end #endregion Header #region Global Variables and Constants #include <GUIConstants.au3> #include <Array.au3> #Include <GuiTab.au3> #include <GuiList.au3> Const $TRAY_ENABLE = 64 Const $TRAY_DISABLE = 128 Dim $PriorityCancel[10] Global $G_getcpu, $G_allcpu, $G_restore PriorityRun() Func PriorityRun() PriorityInit() TraySetState() While 1 Sleep(100) WEnd EndFunc Func PriorityInit() Opt("RunErrorsFatal", 0) Opt("GUIOnEventMode", 1) Opt("TrayOnEventMode",1) Opt("TrayMenuMode", 1) ; Default tray menu items (Script Paused/Exit) will not be shown. Opt("GUIResizeMode", $GUI_DOCKSIZE+$GUI_DOCKLEFT) TraySetToolTip ("PriorityTray 1.0") ;TraySetIcon("SHELL32.dll", 24) $G_getcpu = TrayCreateItem("Get CPU") TrayItemSetOnEvent (-1, "CB_GetCPU") $G_allcpu = TrayCreateItem("Get all CPU") TrayItemSetOnEvent (-1, "CB_AllCPU") $G_restore = TrayCreateItem("Normal mode") TrayItemSetOnEvent (-1, "CB_Restore") TrayItemSetState($G_restore, $TRAY_DISABLE) TrayCreateItem("Show CPU") TrayItemSetOnEvent (-1, "CB_DisplayProcess") TrayCreateItem("Exit") TrayItemSetOnEvent (-1, "CB_PriorityExit") EndFunc Func CB_PriorityExit() Exit EndFunc Func CB_GetCPU() GetBackCPU(1) TraySetIcon("C:\WINDOWS\system32\SHELL32.dll", 1) TrayItemSetState($G_restore, $TRAY_ENABLE) TraySetIcon("SHELL32.dll", 43) EndFunc Func CB_AllCPU() GetBackCPU(0) TrayItemSetState($G_restore, $TRAY_ENABLE) TraySetIcon("SHELL32.dll", 43) EndFunc Func CB_Restore() TrayItemSetState($G_restore, $TRAY_DISABLE) RestoreCPU() TraySetIcon() EndFunc Func CB_DisplayProcess() $Process = SortProcessCPU() $liste="" For $i=0 To UBound($Process, 1)-1 $liste = $liste & $Process[$i][0] & "% " & $Process[$i][1] & @CRLF Next TrayTip("Process", $liste, 10) EndFunc ;=================================================================================================== ========================== ;= = ;= U T I L I T I E S = ;= = ;=================================================================================================== ========================== Func GetBackCPU($Value) $PriorityCancel[0] = 0 $Process = SortProcessCPU() $Size = UBound($Process, 1) $limit = 0 If $Value = 1 Then $limit = $Process[0][0] / 4 For $i = 0 To $Size-1 If $Process[$i][0] >= $limit Then ProcessSetPriority($Process[$i][2], $Value) $PriorityCancel[0] += 1 If $PriorityCancel[0] > 9 Then ReDim $PriorityCancel[$PriorityCancel[0]+1] $PriorityCancel[$PriorityCancel[0]] = $Process[$i][2] EndIf Next EndFunc Func RestoreCPU() For $i = 1 To $PriorityCancel[0] ProcessSetPriority($PriorityCancel[$i], 2) Next EndFunc ;=============================================================================== ; Description: Get process with CPU load and PID, sort them by CPU % (highest first) ; Parameter(s): none ; Requirement(s): Win2000/XP ; Return Value(s): A two dimension array (use UBound($Process, 1) to get the size) ; [$i][0] is cpu % ; [$i][1] is name ; [$i][2] is PID ; Author(s): Jerome DERN (jdern "at" free "dot" fr) ; Note(s): none ;=============================================================================== Func SortProcessCPU() $array = _ProcessListCPU("", 1000) $size = $array[0][0] Dim $Process[$size+1][3] $i = 0 For $j=1 To $size If $array[$j][0] <> "Idle" And $array[$j][0] <> "System" And $array[$j][0] <> "Services"Then $Process[$i][0] = $array[$j][2] $Process[$i][1] = $array[$j][0] $Process[$i][2] = $array[$j][1] $i+=1 EndIf Next $size = $i _ArraySort($Process, 1, 0, $size, 3) Return $Process EndFunc ;=================================================================================================== ========================== ;= = ;= L I S T C P U C O N S U M I N G P R O C E S S = ;= = ;=================================================================================================== ========================== Func _ProcessListCPU($strProcess = "", $iSampleTime = 500, $sComputerName = @ComputerName) ;~ All Parameters are optional: ;~ - All process items will be returned if first parameter is not set ;~ - 500 ms is default sample time ;~ - This computer will be measured by default ;~ Process could be string ("Name") or PID number (1234) ;~ For NORMAL MODE(one time measuring): set Sample value to more than 0 ms ;~ ( average CPU usage will be measured during sleep time within function) ;~ For LOOP MODE (continuous measuring): set Sample value to 0 ms ;~ ( average CPU usage will be measured between two function calls ) ;~ ;~ Success: Returns 2-D array $array[0][0] = Number of processes (also upper bound of array) ;~ $array[0][1] = Total CPU usage by All Processes ;~ $array[0][2] = Total CPU usage by All Processes under given process "Name" ;~ ;~ $array[1][0] = 1st Process name ;~ $array[1][1] = 1st Process ID (PID) ;~ $array[1][2] = 1st Process CPU Usage ;~ ;~ $array[2][0] = 2nd Process name ;~ $array[2][1] = 2nd Process ID (PID) ;~ $array[2][2] = 2nd Process CPU Usage ;~ ... ;~ $array[n][0] = nth Process name ;~ $array[n][1] = nth Process ID (PID) ;~ $array[n][2] = nth Process CPU Usage ;~ ;~ Failure: Returns 2-D array $array[0][0] = "" ( wrong process name or PID ) ;~ $array[0][0] = -1 ( process collection not found) ;~ $array[0][0] = -2 ( WMI service not found or Computer not found) dim $aResultSet[1][3] if $strProcess = 0 AND IsNumber($strProcess) then $strProcess = "Idle" if $iSampleTime = "" AND IsString($iSampleTime) then $iSampleTime = 500 if $sComputerName = "" then $sComputerName = @ComputerName if not IsDeclared("aProcess") AND $iSampleTime = 0 then ;first time in loop mode $bFirstTimeInLoopMode = 1 else $bFirstTimeInLoopMode = 0 endif if not IsDeclared("aProcess") then global $aProcess[10000][10];[ nul, PID, name, CPU, iP1, iT1, iP2, iT2, iPP, iTT] ; 0 1 2 3 4 5 6 7 8 9 endif $objWMIService = ObjGet("winmgmts:\\" & $sComputerName & "\root\CIMV2") if @error then $aResultSet[0][0] = -2 return $aResultSet endif if $iSampleTime OR $bFirstTimeInLoopMode = 1 then ;skip if Loop Mode, but not First time $colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_PerfRawData_PerfProc_Process") For $objItem In $colItems $iPID = $objItem.IDProcess If $iPID = 0 AND $objItem.Name = "_Total" Then $iPID = 9999 $aProcess[$iPID][4] = $objItem.PercentProcessorTime $aProcess[$iPID][5] = $objItem.TimeStamp_Sys100NS next if $objItem = "" then $aResultSet[0][0] = -1 ;collection or process not found return $aResultSet endif sleep($iSampleTime) endif $colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_PerfRawData_PerfProc_Process") $iCountItems = 0 $iTotalUsedByName = 0 $iTotalUsed = 0 For $objItem In $colItems if $objItem.Name = "_Total" AND not($strProcess = "_Total") then exitloop $iPID = $objItem.IDProcess If $iPID = 0 AND $objItem.Name = "_Total" Then $iPID = 9999 ;~ $aProcess[$iPID][1] = $objItem.IDProcess ;~ $aProcess[$iPID][2] = $objItem.Name $aProcess[$iPID][6] = $objItem.PercentProcessorTime $aProcess[$iPID][7] = $objItem.TimeStamp_Sys100NS $aProcess[$iPID][8] = $aProcess[$iPID][6] - $aProcess[$iPID][4];$iPP = ($iP2 - $iP1) $aProcess[$iPID][9] = $aProcess[$iPID][7] - $aProcess[$iPID][5];$iTT = ($iT2 - $iT1) $aProcess[$iPID][3] = round( ($aProcess[$iPID][8] / $aProcess[$iPID][9]) * 100, 0) ;$iCPU = round( ($iPP/$iTT) * 100, 0) $aProcess[$iPID][4] = $aProcess[$iPID][6] ;$iP1 = $iP2 $aProcess[$iPID][5] = $aProcess[$iPID][7] ;$iT1 = $iT2 ;SumTotalUsed if not($objItem.Name = "Idle") AND not($objItem.Name = "_Total") Then $iTotalUsed = $iTotalUsed + $aProcess[$iPID][3] $aResultSet[0][1] = $iTotalUsed endif ;Result Set if (($strProcess = "") AND IsString($strProcess)) OR _ ;strNAME = "", but not 0 (StringUpper($strProcess) = StringUpper($objItem.Name)) OR _ ;strNAME = objNAME ((IsNumber($strProcess) AND $strProcess = $objItem.IDProcess)) then ;i1234 = obj1234 $iCountItems += 1 redim $aResultSet[$iCountItems +1][3] $aResultSet[$iCountItems][0] = $objItem.Name $aResultSet[$iCountItems][1] = $iPID $aResultSet[$iCountItems][2] = $aProcess[$iPID][3] $aResultSet[0][0] = $iCountItems ;SumTotalByName if not($objItem.Name = "Idle") OR ($strProcess = "Idle") Then $iTotalUsedByName = $iTotalUsedByName + $aProcess[$iPID][3] $aResultSet[0][2] = $iTotalUsedByName endif endif next if $objItem = "" then $aResultSet[0][0] = -1 ;collection or process not found return $aResultSet endif Return $aResultSet EndFunc;==>_ProcessListCPU() by novaTek ...ver 0.01 ;=================================================================================================== ========================== ;= = ;= E N D = ;= = ;=================================================================================================== ========================== Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now