CPU占用率呈正弦实现,及实时输出进程和线程的CPU占用率
2014-09-04 18:43
225 查看
CPU占用率呈正弦实现,及实时输出进程和线程的CPU占用率
#include "stdafx.h" #include <windows.h> #include <math.h> // 时间转换 static __int64 file_time_2_utc(const FILETIME* ftime) { LARGE_INTEGER li; li.LowPart = ftime->dwLowDateTime; li.HighPart = ftime->dwHighDateTime; return li.QuadPart; } // 获得CPU的核数 static int get_processor_number() { SYSTEM_INFO info; GetSystemInfo(&info); return (int)info.dwNumberOfProcessors; } // 获取进程CPU占用 int get_cpu_usage(HANDLE hand) { //cpu数量 static int processor_count_ = -1; //上一次的时间 static __int64 last_time_ = 0; static __int64 last_system_time_ = 0; FILETIME now; FILETIME creation_time; FILETIME exit_time; FILETIME kernel_time; FILETIME user_time; __int64 system_time; __int64 time; __int64 system_time_delta; __int64 time_delta; int cpu = -1; if(hand==NULL) return cpu; if(processor_count_ == -1) { processor_count_ = get_processor_number(); } GetSystemTimeAsFileTime(&now); //HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pid); if (!GetProcessTimes(hand, &creation_time, &exit_time, &kernel_time, &user_time)) { return -1; } system_time = (file_time_2_utc(&kernel_time) + file_time_2_utc(&user_time)) / processor_count_; time = file_time_2_utc(&now); if ((last_system_time_ == 0) || (last_time_ == 0)) { last_system_time_ = system_time; last_time_ = time; return -1; } system_time_delta = system_time - last_system_time_; time_delta = time - last_time_; if (time_delta == 0) return -1; cpu = (int)((system_time_delta * 100 + time_delta / 2) / time_delta); last_system_time_ = system_time; last_time_ = time; return cpu; } // 获取线程CPU占用 int get_thread_cpu_usage(HANDLE hand) { //cpu数量 static int processor_count_ = -1; //上一次的时间 static __int64 last_time_ = 0; static __int64 last_system_time_ = 0; FILETIME now; FILETIME creation_time; FILETIME exit_time; FILETIME kernel_time; FILETIME user_time; __int64 system_time; __int64 time; __int64 system_time_delta; __int64 time_delta; int cpu = -1; if(hand==NULL) return cpu; if(processor_count_ == -1) { processor_count_ = get_processor_number(); } GetSystemTimeAsFileTime(&now); //HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pid); if (!GetThreadTimes(hand, &creation_time, &exit_time, &kernel_time, &user_time)) { return -1; } system_time = (file_time_2_utc(&kernel_time) + file_time_2_utc(&user_time)) / processor_count_; time = file_time_2_utc(&now); if ((last_system_time_ == 0) || (last_time_ == 0)) { last_system_time_ = system_time; last_time_ = time; return -1; } system_time_delta = system_time - last_system_time_; time_delta = time - last_time_; if (time_delta == 0) return -1; cpu = (int)((system_time_delta * 100 + time_delta / 2) / time_delta); last_system_time_ = system_time; last_time_ = time; return cpu; } //CPU占用率呈正弦实现 const double SPLIT = 0.01; const int COUNT = 200; const double PI = 3.14159265; const int INTERVAL = 300; UINT ThreadFunT() { DWORD busySpan[COUNT]; //array of busy times DWORD idleSpan[COUNT]; //array of idle times int half = INTERVAL / 2; double radian = 0.0; for(int i = 0; i < COUNT; i++) { busySpan[i] = (DWORD)(half + (sin(PI * radian) * half)); idleSpan[i] = INTERVAL - busySpan[i]; radian += SPLIT; } DWORD startTime = 0; int j = 0; while (true) { j = j % COUNT; startTime = GetTickCount(); while ((GetTickCount() - startTime) <= busySpan[j]) ; Sleep(idleSpan[j]); j++; } return 0; } int main(int argc, char* argv[]) { HANDLE m,c; DWORD ThreadID; int i=0; //让进程在指定处理器上运行 SetProcessAffinityMask( GetCurrentProcess(), 0x00000001 //cpu mask ); m=GetCurrentProcess(); // c=CreateThread(NULL, // 0, // (LPTHREAD_START_ROUTINE)ThreadFunT, // NULL, // 0, // &ThreadID); Sleep(300); c=CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadFunT, NULL, 0, &ThreadID); while (i<200) { Sleep(100); printf("CPU:%d%% Thread:%d%%\n",get_cpu_usage(m),get_thread_cpu_usage(c)); i++; } printf("Hello World!\n"); return 0; }
相关文章推荐
- cpu-hogs 的 systemtap 工具,可以实时统计某段时间内实际消耗掉的 CPU 时间在所有用户进程及内核线程之间的比例分布。
- linux问题排查 - 高cpu占用率的进程和线程
- IIS 6进程CPU占用率50%,一段时间后,网站崩掉,如何处理? (downmoon)
- 取特定进程的CPU占用率
- 双线程输出素数(双核CPU的利用率可达100%)
- BES - 控制进程的CPU占用率
- BES - 控制进程的CPU占用率
- 在 Linux 上实现基于 Socket 的多进程实时通信
- solaris下查看进程的cpu占用率
- 实现实时线程
- 进程 线程 及其在win2000中的实现
- 从内核实现看Linux进程与线程
- 进程间通信实现子进程标准输出转移
- 取特定进程的CPU占用率
- IIS 6进程CPU占用率50%,一段时间后,网站崩掉,如何处理?
- Windows xp 自动更新导致svchost.exe进程CPU占用率100%解决方案
- Windows XP自动更新导致svchost.exe进程CPU占用率100%解决方案
- 关于cidaemon.exe进程CPU占用率高的解决
- Linux 线程实现机制分析--线程 进程经典文章
- linux下通过dup2标准输出重定向查看系统进程方法(也是popen函数实现的方法)