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 ...
随机推荐
- 老生长谈的$.extend()方法
jq的extend()是jq插件扩展很重要的部分,到这里证明是可以自己在jq的基础上,分为两种方法去扩展或开发,为jq本身添加一个方法,可以理解成扩展静态方法和自定义方法. 今天有看到一篇帖子,对这部 ...
- Jekyll 安装权限问题 ERROR: While executing gem ... (Errno::EPERM) Operation not permitted - /usr/bin/jekyll
OS X El Capitan 新特性(System Integrity Protection or SIP)中加强了权限,但是可以对这里进行操作 /usr/local/bin 可以尝试使用以下指令进 ...
- mac linux 删除一个文件下边所有文件和文件夹
sudo rm -r -f 目录
- DB Scan算法的分析与实现
摘自:http://www.cnblogs.com/weixliu/archive/2012/12/08/2808815.html 根据上面第二个数据集的簇的形状比较怪异,分簇结果应该是连起来的属于一 ...
- ECF R9(632E) & DP
Description: 给你$n$个数可以任取$k$个(可重复取),输出所有可能的和. $n \leq 1000,a_i \leq 1000$ Solution: 好神的DP,我们排序后把每个数都减 ...
- IE11 上的3个bug
1.IE 11在popstate上无法正常使用,所以,需要使用老方法hashchange.有一个叫History.js的library,是可以解决这个问题.但如果url在"#"后跟 ...
- Data对象
var myDate = new Date(); Date()返回当日的日期 例如今天是2016/8/19 getFullYear()返回当前日期的年 myDate.getFullYear() 201 ...
- 转载:Chrome调试折腾记_(1)调试控制中心快捷键详解!!!
转载:http://blog.csdn.net/crper/article/details/48098625 大多浏览器的调试功能的启用快捷键都一致…按下F12;还是熟悉的味道; 或者直接 Ctrl ...
- React-Native -课后练习
import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, View, Image, } fro ...
- redis主从配置及主从切换
环境描述: 主redis:192.168.10.1 6379从redis:192.168.10.2 6380 一.主从配置 1.将主从redis配置文件redis.conf中的aemonize no ...