python获取进程cpu的占有率windows下
时间:2011-03-10 来源:崔启龙
python获取进程cpu的占有率:
一种方法是利用windows的wmi接口。由于任务比较紧,另外不知道怎么地在本机上测试总是没有成功。可能是python调用的借口有问题。
索性弃之不用,用第二种方法。
第二种方法是给python写扩展,此处用到的是c++,采用boost python。毕竟自己搞windows编程的时间比较长,用api什么的比较顺手。
至于怎么写扩展,我会在下一篇文章中提到。网上也有很多教程。
1、先写windows的dll代码如下:
1 #include "stdafx.h"
2 #include "windows.h"
3 #include <boost/python/module.hpp>
4 #include <boost/python/def.hpp>
5 #include <shellapi.h>
6 #include <psapi.h>
7
8 #pragma comment(lib, "psapi.lib")
9
10 //定义获取CPU负载的结构体
11 typedef struct _THREAD_INFO
12 {
13 LARGE_INTEGER CreateTime;
14 DWORD dwUnknown1;
15 DWORD dwStartAddress;
16 DWORD StartEIP;
17 DWORD dwOwnerPID;
18 DWORD dwThreadId;
19 DWORD dwCurrentPriority;
20 DWORD dwBasePriority;
21 DWORD dwContextSwitches;
22 DWORD Unknown;
23 DWORD WaitReason;
24
25 }THREADINFO, *PTHREADINFO;
26
27 typedef struct _UNICODE_STRING
28 {
29 USHORT Length;
30 USHORT MaxLength;
31 PWSTR Buffer;
32 } UNICODE_STRING;
33
34 typedef struct _PROCESS_INFO
35 {
36 DWORD dwOffset;
37 DWORD dwThreadsCount;
38 DWORD dwUnused1[6];
39 LARGE_INTEGER CreateTime;
40 LARGE_INTEGER UserTime;
41 LARGE_INTEGER KernelTime;
42 UNICODE_STRING ProcessName;
43
44 DWORD dwBasePriority;
45 DWORD dwProcessID;
46 DWORD dwParentProcessId;
47 DWORD dwHandleCount;
48 DWORD dwUnused3[2];
49
50 DWORD dwVirtualBytesPeak;
51 DWORD dwVirtualBytes;
52 ULONG dwPageFaults;
53 DWORD dwWorkingSetPeak;
54 DWORD dwWorkingSet;
55 DWORD dwQuotaPeakPagedPoolUsage;
56 DWORD dwQuotaPagedPoolUsage;
57 DWORD dwQuotaPeakNonPagedPoolUsage;
58 DWORD dwQuotaNonPagedPoolUsage;
59 DWORD dwPageFileUsage;
60 DWORD dwPageFileUsagePeak;
61
62 DWORD dCommitCharge;
63 THREADINFO ThreadSysInfo[1];
64
65 } PROCESSINFO, *PPROCESSINFO;
66
67
68 #ifdef _MANAGED
69 #pragma managed(push, off)
70 #endif
71
72 BOOL APIENTRY DllMain( HMODULE hModule,
73 DWORD ul_reason_for_call,
74 LPVOID lpReserved
75 )
76 {
77 return TRUE;
78 }
79 int GetProessCPU(DWORD dwProcessId)
80 {
81 int nCpuUsage = 0;
82 DWORD dwInfoSize = 0x20000;
83 PPROCESSINFO pProcessInfo;
84 DWORD dwWorkingSet;
85 long ( __stdcall *NtQuerySystemInformation )( DWORD, PVOID, DWORD, DWORD);
86
87 static __int64 nLastTotalProcessCPUUsage = 0;
88 static __int64 nLastCurrentProcessCPUUsage = 0;
89 __int64 nTotalProcessCPUUsage = 0;
90 __int64 nCurrentProcessCPUUsage = 0;
91
92 //
93 PVOID pProcInfo = (PVOID)(new char[dwInfoSize]);
94 NtQuerySystemInformation = (long(__stdcall*)(DWORD,PVOID,DWORD,DWORD))GetProcAddress(GetModuleHandle(L"ntdll.dll"), "NtQuerySystemInformation");
95 NtQuerySystemInformation(5, pProcInfo, dwInfoSize, 0);
96 pProcessInfo = (PPROCESSINFO)pProcInfo;
97 do
98 {
99 nTotalProcessCPUUsage += (__int64)pProcessInfo->KernelTime.QuadPart + (__int64)pProcessInfo->UserTime.QuadPart;
100 if(pProcessInfo->dwProcessID == dwProcessId)
101 {
102 dwWorkingSet = pProcessInfo->dwWorkingSet;
103 nCurrentProcessCPUUsage += (__int64)pProcessInfo->KernelTime.QuadPart + (__int64)pProcessInfo->UserTime.QuadPart;
104 }
105 if(pProcessInfo->dwOffset == 0)
106 {
107 break;
108 }
109 pProcessInfo = (PPROCESSINFO)((char*)pProcessInfo + pProcessInfo->dwOffset);
110 }while(true);
111
112 __int64 nTotalDelta = nTotalProcessCPUUsage - nLastTotalProcessCPUUsage;
113 __int64 nCurrentDelta = nCurrentProcessCPUUsage - nLastCurrentProcessCPUUsage;
114 if(nTotalDelta > 0 && nCurrentDelta >= 0)
115 {
116 nCpuUsage = int(100 * nCurrentDelta / nTotalDelta);
117 }
118
119 nLastTotalProcessCPUUsage = nTotalProcessCPUUsage;
120 nLastCurrentProcessCPUUsage = nCurrentProcessCPUUsage;
121 delete[] pProcInfo;
122
123 return nCpuUsage;
124 }
125
126 BOOST_PYTHON_MODULE(cpudll)
127 {
128 using namespace boost::python;
129 def("GetProessCPU",GetProessCPU);
130 }
131
132 #ifdef _MANAGED
133 #pragma managed(pop)
134 #endif
生成dll后。把后缀名改为pdy就可以import了。
相关阅读 更多 +