找回密码
 加入
搜索
查看: 3151|回复: 7

[系统综合] 有没有办法控制某一程序的CPU占用率

  [复制链接]
发表于 2011-7-16 18:55:24 | 显示全部楼层 |阅读模式
比如限制iexplore.exe的cpu使用率不能超过10%

谁有思路或者代码?
发表于 2011-7-16 19:18:19 | 显示全部楼层
这个。。。。俺没啥想法,可能要涉及到底层吧。。。。坐等高人
发表于 2011-7-16 20:00:42 | 显示全部楼层
问题是这样有意义吗?短时间的高使用率对系统没有任何影响。而且高使用率往往是由于程序中存在的BUG导致的,难道你要去修复这些程序的BUG?
发表于 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占用率。
发表于 2011-7-19 16:11:13 | 显示全部楼层
坐等高人能写个UDF。这是个底层的活
发表于 2011-7-19 17:09:07 | 显示全部楼层
http://mion.faireal.net/BES/#messages

这个软件支持命令行
发表于 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[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                                                               =
;=                                                                                                                         =
;===================================================================================================
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-20 12:52 , Processed in 0.084101 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表