诺言 发表于 2011-7-16 18:55:24

有没有办法控制某一程序的CPU占用率

比如限制iexplore.exe的cpu使用率不能超过10%

谁有思路或者代码?

飘云 发表于 2011-7-16 19:18:19

这个。。。。俺没啥想法,可能要涉及到底层吧。。。。坐等高人

xrzmjz 发表于 2011-7-16 20:00:42

问题是这样有意义吗?短时间的高使用率对系统没有任何影响。而且高使用率往往是由于程序中存在的BUG导致的,难道你要去修复这些程序的BUG?

annybaby 发表于 2011-7-16 23:19:46

回复 1# 诺言


编程之美:微软技术面试心得

里面有一个专门讲了一下这个   让CPU占用率曲线听你指挥
你可以参考下面的资料
http://book.51cto.com/art/200803/66664.htm

飘云 发表于 2011-7-17 18:46:18

LS的,你推荐的那文章没用的,那文章是直接通过编写的程序来控制cpu占用率,而LZ需要的是通过编写的程序来控制其它程序的cpu占用率。

gzh888666 发表于 2011-7-19 16:11:13

坐等高人能写个UDF。这是个底层的活

lainline 发表于 2011-7-19 17:09:07

http://mion.faireal.net/BES/#messages

这个软件支持命令行

lainline 发表于 2011-7-19 17:32:21

下面是从英文站上拷贝来的 CPU监控适用于背景程序CPU占用特别大时让脚本正常运行#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

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] & "% " & $Process[$i] & @CRLF
    Next
    TrayTip("Process", $liste, 10)
EndFunc

;===================================================================================================

;=                                                                                                                         =
;=                                             U T I L I T I E S                                                         =
;=                                                                                                                         =
;===================================================================================================


Func GetBackCPU($Value)
    $PriorityCancel = 0
    $Process = SortProcessCPU()
    $Size = UBound($Process, 1)
    $limit = 0
    If $Value = 1 Then $limit = $Process / 4
    For $i = 0 To $Size-1
      If $Process[$i] >= $limit Then
            ProcessSetPriority($Process[$i], $Value)
            $PriorityCancel += 1
            If $PriorityCancel > 9 Then ReDim $PriorityCancel[$PriorityCancel+1]
            $PriorityCancel[$PriorityCancel] = $Process[$i]
      EndIf
    Next
EndFunc

Func RestoreCPU()
    For $i = 1 To $PriorityCancel
      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] is CPU %
;                   [$i] is name
;                   [$i] is PID
; Author(s):      Jerome DERN(jdern "at" free "dot" fr)
; Note(s):          none
;===============================================================================
Func SortProcessCPU()
    $array = _ProcessListCPU("", 1000)
    $size = $array
    Dim $Process[$size+1]
    $i = 0
    For $j=1 To $size
      If $array[$j] <> "Idle" And $array[$j] <> "System" And $array[$j] <> "Services"Then
            $Process[$i] = $array[$j]
            $Process[$i] = $array[$j]
            $Process[$i] = $array[$j]
            $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 = Number of processes (also upper bound of array)
;~      $array = Total CPU usage by All Processes
;~      $array = Total CPU usage by All Processes under given process "Name"
;~
;~      $array = 1st Process name
;~      $array = 1st Process ID (PID)
;~      $array = 1st Process CPU Usage
;~
;~      $array = 2nd Process name
;~      $array = 2nd Process ID (PID)
;~      $array = 2nd Process CPU Usage
;~      ...
;~      $array = nth Process name
;~      $array = nth Process ID (PID)
;~      $array = nth Process CPU Usage
;~
;~Failure: Returns 2-D array$array = ""( wrong process name or PID )
;~                $array = -1( process collection not found)
;~                $array = -2( WMI service not found or Computer not found)
   
    dim $aResultSet
   
    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;[ nul, PID, name, CPU, iP1, iT1, iP2, iT2, iPP, iTT]
                                 ;012    34   5   6   7   8   9
    endif
   
    $objWMIService = ObjGet("winmgmts:\\" & $sComputerName & "\root\CIMV2")
    if @error then
      $aResultSet = -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] = $objItem.PercentProcessorTime
            $aProcess[$iPID] = $objItem.TimeStamp_Sys100NS
      next
      
      if$objItem = "" then
            $aResultSet = -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] = $objItem.IDProcess
;~   $aProcess[$iPID] = $objItem.Name
      $aProcess[$iPID] = $objItem.PercentProcessorTime
      $aProcess[$iPID] = $objItem.TimeStamp_Sys100NS
      $aProcess[$iPID] = $aProcess[$iPID] - $aProcess[$iPID];$iPP = ($iP2 - $iP1)
      $aProcess[$iPID] = $aProcess[$iPID] - $aProcess[$iPID];$iTT = ($iT2 - $iT1)
      $aProcess[$iPID] = round( ($aProcess[$iPID] / $aProcess[$iPID]) * 100, 0)
                                                       ;$iCPU = round( ($iPP/$iTT) * 100, 0)
      $aProcess[$iPID] = $aProcess[$iPID]    ;$iP1 = $iP2
      $aProcess[$iPID] = $aProcess[$iPID]    ;$iT1 = $iT2
      
       ;SumTotalUsed
      if not($objItem.Name = "Idle") AND not($objItem.Name = "_Total") Then
            $iTotalUsed = $iTotalUsed + $aProcess[$iPID]
            $aResultSet = $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]
            $aResultSet[$iCountItems] = $objItem.Name
            $aResultSet[$iCountItems] = $iPID
            $aResultSet[$iCountItems] = $aProcess[$iPID]
            $aResultSet = $iCountItems
         
         ;SumTotalByName
            if not($objItem.Name = "Idle") OR ($strProcess = "Idle") Then
                $iTotalUsedByName = $iTotalUsedByName + $aProcess[$iPID]
                $aResultSet = $iTotalUsedByName
            endif
      endif
    next
   
    if$objItem = "" then
            $aResultSet = -1 ;collection or process not found
            return $aResultSet
      endif

    Return $aResultSet
EndFunc;==>_ProcessListCPU() by novaTek   ...ver 0.01

;===================================================================================================

;=                                                                                                                         =
;=                                                   E N D                                                               =
;=                                                                                                                         =
;===================================================================================================



页: [1]
查看完整版本: 有没有办法控制某一程序的CPU占用率