找回密码
 加入
搜索
查看: 7216|回复: 17

[系统综合] 每10分记一次CPU使用率?

 火.. [复制链接]
发表于 2011-7-28 10:19:12 | 显示全部楼层 |阅读模式
#include <array.au3>
#include <file.au3>
Opt('MustDeclareVars', 1)
;Opt("TrayIconHide", 1)
HotKeySet("{ESC}", "_Exit")


Dim $dattime =@YEAR &@MON&@MDAY
Dim $log ="cpu.log"
Global Const $Process_All_Access = 0x1F0FFF
Global $CreateTime = DllStructCreate("dword;dword") ;创建数据结构
Global $ExitTime = DllStructCreate("dword;dword")
Global $KernelTime = DllStructCreate("dword;dword")
Global $UserTime = DllStructCreate("dword;dword")
Global $sUserTime, $sKernelTime, $eUserTime, $eKernelTime
Global $ret
Global $ProcHandle, $Process_CPU_Usage
Global $PID
Global $logical_cpus = CPU()
Global $info
_FileWriteLog(@ScriptDir & $log,"$logical_cpus"& $logical_cpus )

Local $list = ProcessList("MyPopo.exe")
If $list[0][0]=1 Then $PID=$list[1][1]
$ProcHandle = DllCall("kernel32.dll", "hwnd", "OpenProcess", "dword", $Process_All_Access, "int", False, "dword", $PID)
$ProcHandle = $ProcHandle[0]

_FileWriteLog(@ScriptDir & $log,"$ProcHandle"& $ProcHandle )


Dim $time=$dattime
While True
;While    $time<
        _GetProcTime($ProcHandle)
    $info="CPU usage: " & $Process_CPU_Usage & " % " & @CRLF
        _FileWriteLog(@ScriptDir & $log,$info )
    Sleep(1000)
    If Not ProcessExists($PID) Then _Exit()
WEnd
Func _GetProcTime($ProcessHandle)
    $ret = DllCall("kernel32.dll", "int", "GetProcessTimes", "int", $ProcessHandle, "ptr", DllStructGetPtr($CreateTime), "ptr", DllStructGetPtr($ExitTime), "ptr", DllStructGetPtr($KernelTime), "ptr", DllStructGetPtr($UserTime))
    If $ret[0] = 0 Then
        ConsoleWrite("(" & @ScriptLineNumber & ") : = Error in GetProcessTimes call" & @CRLF)
        SetError(1, 0, $ret[0])
    EndIf
    $sKernelTime = DllStructGetData($KernelTime, 1)
    $sUserTime = DllStructGetData($UserTime, 1)
    $Process_CPU_Usage = Floor(($sKernelTime - $eKernelTime + $sUserTime - $eUserTime) / 100000 / $logical_cpus)
    If $Process_CPU_Usage > 100 Then $Process_CPU_Usage = "100"
    $eKernelTime = $sKernelTime
    $eUserTime = $sUserTime
    Return $sUserTime + $sKernelTime
EndFunc   
Func CPU()
    Local $sLPSystemInfo = DllStructCreate( "ushort dwOemId;" & "short wProcessorArchitecture;" & "dword dwPageSize;" & _
                                                                            "ptr lpMinimumApplicationAddress;" & _
                                                                            "ptr lpMaximumApplicationAddress;" & _
                                                                            "long_ptr dwActiveProcessorMask;" & _
                                                                            "dword dwNumberOfProcessors;" & _
                                                                            "dword dwProcessorType;" & _
                                                                            "dword dwAllocationGranularity;" & _
                                                                            "short wProcessorLevel;" & _
                                                                            "short wProcessorRevision")
    Local $aResult = DllCall("Kernel32.dll", "none", "GetSystemInfo", "ptr",DllStructGetPtr($sLPSystemInfo))
    If @error Or Not IsArray($aResult) Then Return SetError(1, 0, 0)
    Return SetError(0, 0, DllStructGetData($sLPSystemInfo, "dwNumberOfProcessors"))
EndFunc   
Func _Exit()
    Exit
EndFunc   



上代码是3mile哥哥的
这个能一直记录着CPU的使用率,我想要每10分钟记录一次,要怎么改
发表于 2011-7-28 11:53:55 | 显示全部楼层
While True
sleep (600000)  ;加个延时
        _GetProcTime($ProcHandle)
    $info="CPU usage: " & $Process_CPU_Usage & " % " & @CRLF
        _FileWriteLog(@ScriptDir & $log,$info )
    Sleep(1000)
    If Not ProcessExists($PID) Then _Exit()
WEnd
发表于 2011-7-28 13:44:08 | 显示全部楼层
3M哥是银才,老是写一些偶看不懂又想懂的东东,真是羡慕死了
 楼主| 发表于 2011-7-29 10:57:01 | 显示全部楼层
回复 2# 骗子


    不对
 楼主| 发表于 2011-7-29 10:57:17 | 显示全部楼层
#include <array.au3>
#include <file.au3>
Opt('MustDeclareVars', 1)
;Opt("TrayIconHide", 1)
HotKeySet("{ESC}", "_Exit")

;Dim $dattime=@YEAR &"-"&@MON&"-"&@MDAY&" "&@HOUR&":"&@MIN&":"&@SEC &":"&@MSEC
Dim $log  
Dim  $ini="resource.ini"
Dim $resource ;进程名称
Dim  $ramvalue
Global $ramname ,$cpuname
Global $ramwrite ,$cpuwrite   
Global $millisecond  ;定义时间

Global Const $Process_All_Access = 0x1F0FFF
Global $CreateTime = DllStructCreate("dword;dword") ;创建数据结构
Global $ExitTime = DllStructCreate("dword;dword")
Global $KernelTime = DllStructCreate("dword;dword")
Global $UserTime = DllStructCreate("dword;dword")
Global $sUserTime, $sKernelTime, $eUserTime, $eKernelTime
Global $ret,$PID,$info,$logical_cpus = CPU()
Global $ProcHandle, $Process_CPU_Usage


If FileExists($ini)==0 Then  ;羊断有没有这个文件
        _FileWriteLog(@ScriptDir & $log,"源文件不存在" )
        Exit
EndIf
$log = IniRead(@ScriptDir & $ini, "log", "log","")  ;从INI读日志名称
  _FileWriteLog(@ScriptDir & $log, "**************程序执行开始***************")
;----------------------------------------- 读取INI------------------------------------------------------
$resource = IniRead(@ScriptDir & $ini, "resource", "name","") ;从INI读取数据
$ramname = IniRead(@ScriptDir & $ini, "csv", "ramname","") ;从INI读取数据
$cpuname= IniRead(@ScriptDir & $ini, "csv", "cpuname","") ;从INI读取数据
$millisecond  = IniRead(@ScriptDir & $ini, "time", "millisecond","") ;从INI读取数据

_FileWriteLog(@ScriptDir & $log,"$log=" & $log )
_FileWriteLog(@ScriptDir & $log,"$resource=" & $resource)  ;写入日志  
_FileWriteLog(@ScriptDir & $log,"$ramname=" & $ramname )  
_FileWriteLog(@ScriptDir & $log,"$cpuname=" & $cpuname)  ;写入日志  
_FileWriteLog(@ScriptDir & $log,"$millisecond=" & $millisecond)

If $resource<>""And  $log<>"" And $ramname <>""  Then  ;判断INI有没有数据
        ;写入日志
Else
             _FileWriteLog(@ScriptDir & $log,"读取ini文件失败")  
EndIf
Local $list = ProcessList($resource)
If $list[0][0]=1 Then $PID=$list[1][1]
$ProcHandle = DllCall("kernel32.dll", "hwnd", "OpenProcess", "dword", $Process_All_Access, "int", False, "dword", $PID)
$ProcHandle = $ProcHandle[0]

$ramwrite =FileOpen(@ScriptDir & $ramname ,1)
$cpuwrite =FileOpen(@ScriptDir & $cpuname ,1)
While True
        Sleep(60000)
        _GetProcTime($ProcHandle)
        Dim $dattime=@YEAR &"-"&@MON&"-"&@MDAY&" "&@HOUR&":"&@MIN&":"&@SEC &":"&@MSEC
    $info=$Process_CPU_Usage & " % "
         FileWriteLine($cpuwrite , $dattime&","& $info & @CRLF)
                                                                                                ;内存
        $ramvalue=ProcessGetStats ($resource, 0 )
    $ramwrite =FileOpen(@ScriptDir & $ramname ,1)
    FileWriteLine($ramwrite ,$dattime&","& $ramvalue[0]/1024 & @CRLF)
   
   Sleep(1000)
    ;Sleep($millisecond)
    If Not ProcessExists($PID) Then _Exit()
WEnd
Func _GetProcTime($ProcessHandle)
    $ret = DllCall("kernel32.dll", "int", "GetProcessTimes", "int", $ProcessHandle, "ptr", DllStructGetPtr($CreateTime), "ptr", DllStructGetPtr($ExitTime), "ptr", DllStructGetPtr($KernelTime), "ptr", DllStructGetPtr($UserTime))
    If $ret[0] = 0 Then
        ConsoleWrite("(" & @ScriptLineNumber & ") : = Error in GetProcessTimes call" & @CRLF)
        SetError(1, 0, $ret[0])
    EndIf
    $sKernelTime = DllStructGetData($KernelTime, 1)
    $sUserTime = DllStructGetData($UserTime, 1)
    $Process_CPU_Usage = Floor(($sKernelTime - $eKernelTime + $sUserTime - $eUserTime) / 100000 / $logical_cpus)
    If $Process_CPU_Usage > 100 Then $Process_CPU_Usage = "100"
    $eKernelTime = $sKernelTime
    $eUserTime = $sUserTime
    Return $sUserTime + $sKernelTime
EndFunc   
Func CPU()
    Local $sLPSystemInfo = DllStructCreate( "ushort dwOemId;" & "short wProcessorArchitecture;" & "dword dwPageSize;" & _
                                                                            "ptr lpMinimumApplicationAddress;" & _
                                                                            "ptr lpMaximumApplicationAddress;" & _
                                                                            "long_ptr dwActiveProcessorMask;" & _
                                                                            "dword dwNumberOfProcessors;" & _
                                                                            "dword dwProcessorType;" & _
                                                                            "dword dwAllocationGranularity;" & _
                                                                            "short wProcessorLevel;" & _
                                                                            "short wProcessorRevision")
    Local $aResult = DllCall("Kernel32.dll", "none", "GetSystemInfo", "ptr",DllStructGetPtr($sLPSystemInfo))
    If @error Or Not IsArray($aResult) Then Return SetError(1, 0, 0)
    Return SetError(0, 0, DllStructGetData($sLPSystemInfo, "dwNumberOfProcessors"))
EndFunc   
Func _Exit()
    Exit
EndFunc   


就是CPU的不对
发表于 2011-7-29 12:09:02 | 显示全部楼层
回复 5# lusheng0028
#include <array.au3>
#include <file.au3>
#include <GDIPlus.au3>

Opt('MustDeclareVars', 1)
Opt("TrayIconHide", 1)
HotKeySet("{ESC}", "_Exit");ESC退出
Dim $log = "cpu.log"
Global Const $Process_All_Access = 0x1F0FFF
Global $CreateTime = DllStructCreate("dword;dword")
Global $ExitTime = DllStructCreate("dword;dword")
Global $KernelTime = DllStructCreate("dword;dword")
Global $UserTime = DllStructCreate("dword;dword")
Global $sUserTime, $sKernelTime, $eUserTime, $eKernelTime
Global $ret
Global $ProcHandle, $Process_CPU_Usage
Global $PID, $CPUTime
Global $logical_cpus = CPU()
Global $info
Global $hwnd, $pic, $graphics, $bitmap, $backbuffer, $ffamily, $arial, $sformat, $blackbrush, $rectf, $arr
_FileWriteLog(@ScriptDir & $log, "$logical_cpus" & $logical_cpus)

Global $list = ProcessList("QQ.exe")
If $list[0][0] = 1 Then $PID = $list[1][1]
$ProcHandle = DllCall("kernel32.dll", "hwnd", "OpenProcess", "dword", $Process_All_Access, "int", False, "dword", $PID)
$ProcHandle = $ProcHandle[0]

$hwnd = GUICreate("GDI+ Example", 200, 40, @DesktopWidth - 200 - 10, @DesktopHeight - 40 - 45, 0x80000000,-1,guicreate(""))
$pic = GUICtrlCreatePic("", 0, 0, 200, 40)
GUISetState()

_GDIPlus_Startup()
$graphics = _GDIPlus_GraphicsCreateFromHWND(GUICtrlGetHandle($pic))
$bitmap = _GDIPlus_BitmapCreateFromGraphics(200, 40, $graphics)
$backbuffer = _GDIPlus_ImageGetGraphicsContext($bitmap)
$ffamily = _GDIPlus_FontFamilyCreate("微软雅黑")
$arial = _GDIPlus_FontCreate($ffamily, 15)
$sformat = _GDIPlus_StringFormatCreate()
$blackbrush = _GDIPlus_BrushCreateSolid(0xff0000FF)
$rectf = _GDIPlus_RectFCreate(0)
$arr = _GDIPlus_GraphicsMeasureString($graphics, "CPU usage: 100 % ", $arial, $rectf, $sformat)
AdlibRegister("_Test", 250);这里设置回显时间

While True
        If Not ProcessExists($PID) Then _Exit()
        Sleep(100)
WEnd

Func _Test()
        $CPUTime = _GetProcTime($ProcHandle)
        $info = " CPU usage: " & $Process_CPU_Usage & " % "
        
        _GDIPlus_GraphicsClear($backbuffer, 0xFFabcdef)
        _GDIPlus_GraphicsDrawStringEx($backbuffer, $info, $arial, $arr[0], $sformat, $blackbrush)
        _GDIPlus_GraphicsDrawImageRect($graphics, $bitmap, 0, 0, 200, 40)
EndFunc   ;==>_Test

Func _GetProcTime($ProcessHandle)
        $ret = DllCall("kernel32.dll", "int", "GetProcessTimes", "int", $ProcessHandle, "ptr", DllStructGetPtr($CreateTime), "ptr", DllStructGetPtr($ExitTime), "ptr", DllStructGetPtr($KernelTime), "ptr", DllStructGetPtr($UserTime))
        If $ret[0] = 0 Then
                ConsoleWrite("(" & @ScriptLineNumber & ") : = Error in GetProcessTimes call" & @CRLF)
                SetError(1, 0, $ret[0])
        EndIf
        $sKernelTime = DllStructGetData($KernelTime, 1)
        $sUserTime = DllStructGetData($UserTime, 1)
        $Process_CPU_Usage = Floor(($sKernelTime - $eKernelTime + $sUserTime - $eUserTime) / 100000 / $logical_cpus)
        If $Process_CPU_Usage > 100 Then $Process_CPU_Usage = "100"
        $eKernelTime = $sKernelTime
        $eUserTime = $sUserTime
        Return $sUserTime + $sKernelTime
EndFunc   ;==>_GetProcTime
Func CPU()
        Local $sLPSystemInfo = DllStructCreate("ushort dwOemId;" & "short wProcessorArchitecture;" & "dword dwPageSize;" & _
                        "ptr lpMinimumApplicationAddress;" & _
                        "ptr lpMaximumApplicationAddress;" & _
                        "long_ptr dwActiveProcessorMask;" & _
                        "dword dwNumberOfProcessors;" & _
                        "dword dwProcessorType;" & _
                        "dword dwAllocationGranularity;" & _
                        "short wProcessorLevel;" & _
                        "short wProcessorRevision")
        Local $aResult = DllCall("Kernel32.dll", "none", "GetSystemInfo", "ptr", DllStructGetPtr($sLPSystemInfo))
        If @error Or Not IsArray($aResult) Then Return SetError(1, 0, 0)
        Return SetError(0, 0, DllStructGetData($sLPSystemInfo, "dwNumberOfProcessors"))
EndFunc   ;==>CPU
Func _Exit()
        _GDIPlus_BrushDispose($blackbrush)
        _GDIPlus_StringFormatDispose($sformat)
        _GDIPlus_FontDispose($arial)
        _GDIPlus_FontFamilyDispose($ffamily)
        _GDIPlus_GraphicsDispose($backbuffer)
        _GDIPlus_BitmapDispose($bitmap)
        _GDIPlus_GraphicsDispose($graphics)
        _GDIPlus_Shutdown()
        Exit
EndFunc   ;==>_Exit
发表于 2011-7-29 13:00:12 | 显示全部楼层
有用的东东
 楼主| 发表于 2011-7-29 13:09:41 | 显示全部楼层
回复 6# 3mile


    AdlibRegister("_Test", 60000)
这样的值就不对了  CPU值老是30%以上的多,基本上和看任务管理器对不上
发表于 2011-7-29 13:37:08 | 显示全部楼层
回复 8# lusheng0028
#include <array.au3>
#include <file.au3>
#include <GDIPlus.au3>
 
Opt('MustDeclareVars', 1)
Opt("TrayIconHide", 1)
HotKeySet("{ESC}", "_Exit");ESC退出
Dim $log = "cpu.log"
Global Const $Process_All_Access = 0x1F0FFF
Global $CreateTime = DllStructCreate("dword;dword")
Global $ExitTime = DllStructCreate("dword;dword")
Global $KernelTime = DllStructCreate("dword;dword")
Global $UserTime = DllStructCreate("dword;dword")
Global $sUserTime, $sKernelTime, $eUserTime, $eKernelTime
Global $ret
Global $ProcHandle, $Process_CPU_Usage
Global $PID, $CPUTime
Global $logical_cpus = CPU()
Global $info
Global $hwnd, $pic, $graphics, $bitmap, $backbuffer, $ffamily, $arial, $sformat, $blackbrush, $rectf, $arr,$time
_FileWriteLog(@ScriptDir & $log, "$logical_cpus" & $logical_cpus)
 
Global $list = ProcessList("QQ.exe")
If $list[0][0] = 1 Then $PID = $list[1][1]
$ProcHandle = DllCall("kernel32.dll", "hwnd", "OpenProcess", "dword", $Process_All_Access, "int", False, "dword", $PID)
$ProcHandle = $ProcHandle[0]
 
$hwnd = GUICreate("GDI+ Example", 200, 40, @DesktopWidth - 200 - 10, @DesktopHeight - 40 - 45, 0x80000000,-1,guicreate(""))
$pic = GUICtrlCreatePic("", 0, 0, 200, 40)
GUISetState()
 
_GDIPlus_Startup()
$graphics = _GDIPlus_GraphicsCreateFromHWND(GUICtrlGetHandle($pic))
$bitmap = _GDIPlus_BitmapCreateFromGraphics(200, 40, $graphics)
$backbuffer = _GDIPlus_ImageGetGraphicsContext($bitmap)
$ffamily = _GDIPlus_FontFamilyCreate("微软雅黑")
$arial = _GDIPlus_FontCreate($ffamily, 15)
$sformat = _GDIPlus_StringFormatCreate()
$blackbrush = _GDIPlus_BrushCreateSolid(0xff0000FF)
$rectf = _GDIPlus_RectFCreate(0)
$arr = _GDIPlus_GraphicsMeasureString($graphics, "CPU usage: 100 % ", $arial, $rectf, $sformat)
$time=TimerInit()
AdlibRegister("_Test", 250);这里设置回显时间
 
While True
        If Not ProcessExists($PID) Then _Exit()
        Sleep(100)
WEnd
 
Func _Test()
        $CPUTime = _GetProcTime($ProcHandle)
        $info = " CPU usage: " & $Process_CPU_Usage & " % "
        
        _GDIPlus_GraphicsClear($backbuffer, 0xFFabcdef)
        _GDIPlus_GraphicsDrawStringEx($backbuffer, $info, $arial, $arr[0], $sformat, $blackbrush)
        _GDIPlus_GraphicsDrawImageRect($graphics, $bitmap, 0, 0, 200, 40)
                if TimerDiff($time)>6*1000 Then 
                        MsgBox(0,0,$info)
                        $time=TimerInit()
                EndIf
EndFunc   ;==>_Test
 
Func _GetProcTime($ProcessHandle)
        $ret = DllCall("kernel32.dll", "int", "GetProcessTimes", "int", $ProcessHandle, "ptr", DllStructGetPtr($CreateTime), "ptr", DllStructGetPtr($ExitTime), "ptr", DllStructGetPtr($KernelTime), "ptr", DllStructGetPtr($UserTime))
        If $ret[0] = 0 Then
                ConsoleWrite("(" & @ScriptLineNumber & ") : = Error in GetProcessTimes call" & @CRLF)
                SetError(1, 0, $ret[0])
        EndIf
        $sKernelTime = DllStructGetData($KernelTime, 1)
        $sUserTime = DllStructGetData($UserTime, 1)
        $Process_CPU_Usage = Floor(($sKernelTime - $eKernelTime + $sUserTime - $eUserTime) / 100000 / $logical_cpus)
        If $Process_CPU_Usage > 100 Then $Process_CPU_Usage = "100"
        $eKernelTime = $sKernelTime
        $eUserTime = $sUserTime
        Return $sUserTime + $sKernelTime
EndFunc   ;==>_GetProcTime
Func CPU()
        Local $sLPSystemInfo = DllStructCreate("ushort dwOemId;" & "short wProcessorArchitecture;" & "dword dwPageSize;" & _
                        "ptr lpMinimumApplicationAddress;" & _
                        "ptr lpMaximumApplicationAddress;" & _
                        "long_ptr dwActiveProcessorMask;" & _
                        "dword dwNumberOfProcessors;" & _
                        "dword dwProcessorType;" & _
                        "dword dwAllocationGranularity;" & _
                        "short wProcessorLevel;" & _
                        "short wProcessorRevision")
        Local $aResult = DllCall("Kernel32.dll", "none", "GetSystemInfo", "ptr", DllStructGetPtr($sLPSystemInfo))
        If @error Or Not IsArray($aResult) Then Return SetError(1, 0, 0)
        Return SetError(0, 0, DllStructGetData($sLPSystemInfo, "dwNumberOfProcessors"))
EndFunc   ;==>CPU
Func _Exit()
        _GDIPlus_BrushDispose($blackbrush)
        _GDIPlus_StringFormatDispose($sformat)
        _GDIPlus_FontDispose($arial)
        _GDIPlus_FontFamilyDispose($ffamily)
        _GDIPlus_GraphicsDispose($backbuffer)
        _GDIPlus_BitmapDispose($bitmap)
        _GDIPlus_GraphicsDispose($graphics)
        _GDIPlus_Shutdown()
        Exit
EndFunc   ;==>_Exit
 楼主| 发表于 2011-7-29 18:17:25 | 显示全部楼层
回复 9# 3mile


   3哥,这个是有问题的,对不上任务管理器的,这个现在都50%以上,怎么可能呢。看任务管理器才1%
 楼主| 发表于 2011-7-29 21:11:19 | 显示全部楼层
#include <array.au3>
#include <file.au3>

Opt('MustDeclareVars', 1)
;Opt("TrayIconHide", 1)
HotKeySet("{ESC}", "_Exit")

Dim $log  
Dim  $ini="resource.ini"
Dim $resource ;进程名称
Dim  $ramvalue
Global $ramname ,$cpuname
Global $ramwrite ,$cpuwrite   
Global $millisecond  ;定义时间

Global Const $Process_All_Access = 0x1F0FFF
Global $CreateTime = DllStructCreate("dword;dword") ;创建数据结构
Global $ExitTime = DllStructCreate("dword;dword")
Global $KernelTime = DllStructCreate("dword;dword")
Global $UserTime = DllStructCreate("dword;dword")
Global $sUserTime, $sKernelTime, $eUserTime, $eKernelTime
Global $ret,$PID,$info,$logical_cpus = CPU()
Global $ProcHandle, $Process_CPU_Usage


                 
If FileExists($ini)==0 Then  ;羊断有没有这个文件
        _FileWriteLog(@ScriptDir & $log,"源文件不存在" )
        Exit
EndIf
$log = IniRead(@ScriptDir & $ini, "log", "log","")  ;从INI读日志名称
  _FileWriteLog(@ScriptDir & $log, "**************程序执行开始***************")
;----------------------------------------- 读取INI------------------------------------------------------
$resource = IniRead(@ScriptDir & $ini, "resource", "name","") ;从INI读取数据
$ramname = IniRead(@ScriptDir & $ini, "csv", "ramname","") ;从INI读取数据
$cpuname= IniRead(@ScriptDir & $ini, "csv", "cpuname","") ;从INI读取数据
$millisecond  = IniRead(@ScriptDir & $ini, "time", "millisecond","") ;从INI读取数据



If $resource<>""And  $log<>"" And $ramname <>""  Then  ;判断INI有没有数据
         _FileWriteLog(@ScriptDir & $log,"$log=" & $log )
    _FileWriteLog(@ScriptDir & $log,"$resource=" & $resource)  ;写入日志  
    _FileWriteLog(@ScriptDir & $log,"$ramname=" & $ramname )  
     _FileWriteLog(@ScriptDir & $log,"$cpuname=" & $cpuname)  ;写入日志  
   _FileWriteLog(@ScriptDir & $log,"$millisecond=" & $millisecond)
Else
             _FileWriteLog(@ScriptDir & $log,"读取ini文件失败")  
EndIf
Local $list = ProcessList($resource)
If $list[0][0]=1 Then $PID=$list[1][1]
$ProcHandle = DllCall("kernel32.dll", "hwnd", "OpenProcess", "dword", $Process_All_Access, "int", False, "dword", $PID)
$ProcHandle = $ProcHandle[0]

$ramwrite =FileOpen(@ScriptDir & $ramname ,1)
$cpuwrite =FileOpen(@ScriptDir & $cpuname ,1)
While True
        ;Sleep(60000)
        Dim $i=0
        If $i>2 Then
          Exit
        Else
        _GetProcTime($ProcHandle)
        Dim $dattime=@YEAR &"-"&@MON&"-"&@MDAY&" "&@HOUR&":"&@MIN&":"&@SEC &":"&@MSEC
    $info=$Process_CPU_Usage & " % "
        If $info<>"100 % " Then
         FileWriteLine($cpuwrite , $dattime&","& $info & @CRLF)
               
                ;内存
                $ramvalue=ProcessGetStats ($resource, 0 )
        $ramwrite =FileOpen(@ScriptDir & $ramname ,1)
        FileWriteLine($ramwrite ,$dattime&","& $ramvalue[0]/1024 & @CRLF)
        EndIf
        EndIf
   
   Sleep(1000)
    ;Sleep($millisecond)
         $i=$i+1
    If Not ProcessExists($PID) Then _Exit()
   
WEnd


Func _GetProcTime($ProcessHandle)
    $ret = DllCall("kernel32.dll", "int", "GetProcessTimes", "int", $ProcessHandle, "ptr", DllStructGetPtr($CreateTime), "ptr", DllStructGetPtr($ExitTime), "ptr", DllStructGetPtr($KernelTime), "ptr", DllStructGetPtr($UserTime))
  If $ret[0] = 0 Then
        ConsoleWrite("(" & @ScriptLineNumber & ") : = Error in GetProcessTimes call" & @CRLF)
        SetError(1, 0, $ret[0])
    EndIf
    $sKernelTime = DllStructGetData($KernelTime, 1)
    $sUserTime = DllStructGetData($UserTime, 1)
    $Process_CPU_Usage = Floor(($sKernelTime - $eKernelTime + $sUserTime - $eUserTime) / 100000 / $logical_cpus)
    If $Process_CPU_Usage > 100 Then $Process_CPU_Usage = "100"
    $eKernelTime = $sKernelTime
    $eUserTime = $sUserTime
    Return $sUserTime + $sKernelTime
EndFunc   
Func CPU()
    Local $sLPSystemInfo = DllStructCreate( "ushort dwOemId;" & "short wProcessorArchitecture;" & "dword dwPageSize;" & _
                                                                            "ptr lpMinimumApplicationAddress;" & _
                                                                            "ptr lpMaximumApplicationAddress;" & _
                                                                            "long_ptr dwActiveProcessorMask;" & _
                                                                            "dword dwNumberOfProcessors;" & _
                                                                            "dword dwProcessorType;" & _
                                                                            "dword dwAllocationGranularity;" & _
                                                                            "short wProcessorLevel;" & _
                                                                            "short wProcessorRevision")
    Local $aResult = DllCall("Kernel32.dll", "none", "GetSystemInfo", "ptr",DllStructGetPtr($sLPSystemInfo))
    If @error Or Not IsArray($aResult) Then Return SetError(1, 0, 0)
    Return SetError(0, 0, DllStructGetData($sLPSystemInfo, "dwNumberOfProcessors"))
EndFunc   
Func _Exit()
    Exit
EndFunc   

原码在上面,
INI在下面

[resource]
name=kwmusic.exe
[log]
log=resource.log
[csv]
ramname=ram.csv
cpuname =cpu.csv
[time]
millisecond=60000

哪位大哥有好的方法
 楼主| 发表于 2011-7-29 21:12:05 | 显示全部楼层
真的不行,就做EXE,几分钟调一次
发表于 2011-7-29 21:16:58 | 显示全部楼层
sleep(1000 * 60 * 10)
发表于 2011-7-29 21:45:53 | 显示全部楼层
参考此贴:性能统计与检测UDF - Pdh.au3

将顶楼的代码做以下修改:
1、\PhysicalDisk(_Total)\Disk Read Bytes/sec 改成 \Process(QQ)\% Processor Time。
2、将 36 行的 _PdhSetCounterScaleFactor($hCounter, -4) 注释或删除。
3、将 54 行的 Sleep(1000) 改成你需要的时间间隔。
发表于 2011-7-30 06:50:21 | 显示全部楼层
参考此贴:

将顶楼的代码做以下修改:
1、\PhysicalDisk(_Total)\Disk Read Bytes/sec 改成 \Process(Q ...
pusofalse 发表于 2011-7-29 21:45


嗯,api调用性能计数器
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-11-14 14:24 , Processed in 0.081036 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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