CPU占用率呈正弦实现,及实时输出进程和线程的CPU占用率
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占用率呈正弦实现,及实时输出进程和线程的CPU占用率的更多相关文章
- Linux下如何查看高CPU占用率线程 LINUX CPU利用率计算
目录(?)[-] proc文件系统 proccpuinfo文件 procstat文件 procpidstat文件 procpidtasktidstat文件 系统中有关进程cpu使用率的常用命令 ps ...
- Linux下如何查看高CPU占用率线程 LINUX CPU利用率计算(转)
Java 系统性能分析 命令 1. cpu分析 top , pidstat(sysstat) pid -p PID -t 1 10 vmstat 1 CPU上下文切换.运行队列.利用率 ps Hh - ...
- 查看线程linux cpu使用率
Linux下如何查看高CPU占用率线程 LINUX CPU利用率计算 转 http://www.cnblogs.com/lidabo/p/4738113.html目录(?)[-] proc文件系统 p ...
- 多线程程序 怎样查看每个线程的cpu占用
可以用下面的命令将 cpu 占用率高的线程找出来: ps H -eo user,pid,ppid,tid,time,%cpu,cmd --sort=%cpu 这个命令首先指定参数'H',显示线程相关的 ...
- 4 系统的 CPU 使用率很高,但为啥却找不到高 CPU的应用?
上一节讲了 CPU 使用率是什么,并通过一个案例教你使用 top.vmstat.pidstat 等工具,排查高 CPU 使用率的进程,然后再使用 perf top 工具,定位应用内部函数的问题.不过就 ...
- 06 案例篇:系统的 CPU 使用率很高,但为啥却找不到高 CPU 的应用?
上一节我讲了 CPU 使用率是什么,并通过一个案例教你使用 top.vmstat.pidstat 等工具,排查高 CPU 使用率的进程,然后再使用 perf top 工具,定位应用内部函数的问题.不过 ...
- Linux编程之《进程/线程绑定CPU》
Intro----- 通常我们在编写服务器代码时,可以通过将当前进程绑定到固定的CPU核心或者线程绑定到固定的CPU核心来提高系统调度程序的效率来提高程序执行的效率,下面将完整代码贴上. /***** ...
- linux进程、线程与cpu的亲和性(affinity)
参考:http://www.cnblogs.com/wenqiang/p/6049978.html 最近的工作中对性能的要求比较高,下面简单做一下总结: 一.什么是cpu亲和性(affinity) C ...
- 修复VirtualBox "This kernel requires the following features not present on the CPU: pae Unable to boot – please use a kernel appropriate for your CPU"
异常处理汇总-开发工具 http://www.cnblogs.com/dunitian/p/4522988.html 修复VirtualBox "This kernel requires ...
随机推荐
- python学习 3笔记
merge dict def merge(defaults, override): r = {} for k, v in defaults.items(): if k in override: if ...
- 动态生成一个设定好特殊样式的Tlabel,快速生成代码
动态生成一个设定好特殊样式的Tlabel,快速生成代码: 1.自己先在可视化界面设定一个Label,像这样: 2.选择label,快捷键ctrl+C 复制,粘贴带代码编辑器去,会生成一段这样的窗体代码 ...
- CSS中各种各样居中方法的总结
在开发前端页面的时候,元素的居中是一个永远都绕不开的问题.看似简单的居中二字,其实蕴含着许许多多的情况,对应着很多的处理方法,本文就试图对页面布局中的居中问题进行总结~~ 居中问题分为水平居中和竖直居 ...
- 使用display:table来解决一些问题
一直有,多栏的需求,当然用table布局,很快就做完了.不怎么喜欢用table,刚开始使用display:table,但是有一条老是不能达到我的效果,那就是有一行不固定宽度的时候,就不知道怎么处理,今 ...
- Python excel 库:Openpyxl xlrd 对比 介绍
打算用python做一个写mtk camera driver的自动化工具. 模板选用标准库里面string -> Template 即可 但要重定义替换字符,稍后说明 配置文件纠结几天:cfg, ...
- CF2.BC
B. Arpa's obvious problem and Mehrdad's terrible solution time limit per test 1 second memory limit ...
- pagination 分页
<!DOCTYPE html> <html> <head> <title>pagination</title> <style type ...
- MVC辅助方法
我相信很多人虽然经常用mvc去做一些东西,但是可能很少使用mvcHtml辅助方法 首先我们要创建一个mvc项目 二创建一个HtmlHelper文件夹 三编写文件 四调用辅助方法 调用辅助方法 和使用其 ...
- StreamingAssets文件夹在不同平台上的引用
On a desktop computer (Mac OS or Windows) the location of the files can be obtained with the followi ...
- Ajax全面基础学习(二)
两种配置ajax的方式 $.ajax('url',{ 配置ajax}); $.ajax({ url : 'url' 其他ajax配置}) ajax的回调函数 $.ajax('url',{ //请求成功 ...