|
#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
我这个是每1分读了次CPU的值,就是不对,每30分读呢 |
|