#ifndef PROCESS_STAT_H
#define PROCESS_STAT_H
 
#ifdef __cplusplus
extern “C” {
#endif
 
typedef long long int64_t;
typedef unsigned long long uint64_t;
 
/// 获取当前进程的cpu使用率,返回-1失败
int get_cpu_usage();
 
/// 获取当前进程内存和虚拟内存使用量,返回-1失败,0成功
int get_memory_usage(uint64_t* mem, uint64_t* vmem);
 
/// 获取当前进程总共读和写的IO字节数,返回-1失败,0成功
int get_io_bytes(uint64_t* read_bytes, uint64_t* write_bytes);
 
#ifdef __cplusplus
}
#endif
 
#endif/*PROCESS_STAT_H*/
 
/**
* 需要连接到psapi.lib
*/
 
#include
#include #include
#include “process_stat.h”
 
/// 时间转换
static uint64_t file_time_2_utc(const FILETIME* ftime)
{
LARGE_INTEGER li;
 
assert(ftime);
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;
}
 
int get_cpu_usage()
{
//cpu数量
static int processor_count_ = -1;
//上一次的时间
static int64_t last_time_ = 0;
static int64_t last_system_time_ = 0;
 
FILETIME now;
FILETIME creation_time;
FILETIME exit_time;
FILETIME kernel_time;
FILETIME user_time;
int64_t system_time;
int64_t time;
int64_t system_time_delta;
int64_t time_delta;
 
int cpu = -1;
 
if(processor_count_ == -1)
{
processor_count_ = get_processor_number();
}
 
GetSystemTimeAsFileTime(&now);
 
if (!GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time,
&kernel_time, &user_time))
{
// We don’t assert here because in some cases (such as in the Task
 
Manager)
// we may call this function on a process that has just exited but
 
we have
// not yet received the notification.
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))
{
// First call, just set the last values.
last_system_time_ = system_time;
last_time_ = time;
return -1;
}
 
system_time_delta = system_time – last_system_time_;
time_delta = time – last_time_;
 
assert(time_delta != 0);
 
if (time_delta == 0)
return -1;
 
// We add time_delta / 2 so the result is rounded.
cpu = (int)((system_time_delta * 100 + time_delta / 2) / time_delta);
last_system_time_ = system_time;
last_time_ = time;
return cpu;
}
 
int get_memory_usage(uint64_t* mem, uint64_t* vmem)
{
PROCESS_MEMORY_COUNTERS pmc;
if(GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)))
{
if(mem) *mem = pmc.WorkingSetSize;
if(vmem) *vmem = pmc.PagefileUsage;
return 0;
}
return -1;
}
 
int get_io_bytes(uint64_t* read_bytes, uint64_t* write_bytes)
{
IO_COUNTERS io_counter;
if(GetProcessIoCounters(GetCurrentProcess(), &io_counter))
{
if(read_bytes) *read_bytes = io_counter.ReadTransferCount;
if(write_bytes) *write_bytes = io_counter.WriteTransferCount;
return 0;
}
return -1;
}
 
#include “process_stat.h”
#include
#include
 
int main()
{
while(1)
{
int cpu;
uint64_t mem, vmem, r, w;
 
cpu = get_cpu_usage();
get_memory_usage(&mem, &vmem);
get_io_bytes(&r, &w);
 
printf(“CPU使用率: %u\n”,cpu);
printf(“内存使用: %u 字节\n”, mem);
printf(“虚拟内存使用: %u 字节\n”, vmem);
printf(“总共读: %u 字节\n”, r);
printf(“总共写: %u 字节\n”, w);
 
Sleep(1000);
}
return 0;
}

 

Windows下用C语言获取进程cpu使用率,内存使用,IO情况的更多相关文章

  1. linux ps命令,查看某进程cpu和内存占用率情况, linux ps命令,查看进程cpu和内存占用率排序。 不指定

    背景:有时需要单看某个进程的CPU及占用情况,有时需要看整体进程的一个占用情况.一. linux ps命令,查看某进程cpu和内存占用率情况[root@test vhost]# ps auxUSER  ...

  2. Java进程CPU使用率高排查

    Java进程CPU使用率高排查 生产java应用,CPU使用率一直很高,经常达到100%,通过以下步骤完美解决,分享一下.1.jps 获取Java进程的PID.2.jstack pid >> ...

  3. linux下的CPU、内存、IO、网络的压力测试

    linux下的CPU.内存.IO.网络的压力测试  要远程测试其实很简单了,把结果放到一个微服务里直接在web里查看就可以了,或者同步到其他服务器上 一.对CPU进行简单测试: 1.通过bc命令计算特 ...

  4. [转帖]linux下的CPU、内存、IO、网络的压力测试

    linux下的CPU.内存.IO.网络的压力测试 https://www.cnblogs.com/zhuochong/p/10185881.html 一.对CPU进行简单测试: 1.通过bc命令计算特 ...

  5. [转帖]linux下CPU、内存、IO、网络的压力测试,硬盘读写速度测试,Linux三个系统资源监控工具

    linux下CPU.内存.IO.网络的压力测试,硬盘读写速度测试,Linux三个系统资源监控工具 https://blog.51cto.com/hao360/1587165 linux_python关 ...

  6. 为Zabbix配置Nova服务、Keystone和Placement进程CPU和内存usage监控

    目前已经完成了RabbitMQ和MySQL的监控项配置,还差对nova-api.nova-conductor.nova-scheduler和keystone进程CPU和内存 usage的监控,类似的轮 ...

  7. 查看进程CPU、内存使用情况

    本文介绍通过ps和top查看进程的cpu.内存等使用情况. 1.ps命令 1.1 概览 ps命令相关参数定义: -e 或者-A,选择所有的进程: -L 显示线程: -o 自定义输出格式: 输出格式: ...

  8. Cgroups控制cpu,内存,io示例

    Cgroups是control groups的缩写,最初由Google工程师提出,后来编进linux内核. Cgroups是实现IaaS虚拟化(kvm.lxc等),PaaS容器沙箱(Docker等)的 ...

  9. Cgroups控制cpu,内存,io示例【转】

    本文转载自:https://www.cnblogs.com/yanghuahui/p/3751826.html 百度私有PaaS云就是使用轻量的cgoups做的应用之间的隔离,以下是关于百度架构师许立 ...

随机推荐

  1. ANSI escape code

    最近在做iOS上的SSH终端项目,主要是在手机上远程连接Unix系统,并进行一些简单的指令操作,类似于SecureCRT:今天想总结一下这个项目中遇到的新东西----ANSI escape code. ...

  2. json+一般处理程序读取数据库数据

    一般处理程序的语法结构 string jsoncallback = context.Request["jsoncallback"]; 声明变量 前台传值使用        stri ...

  3. (转)C#Interface简介

    接口:描述可属于任何类或结构的一组相关功能,通过interface关键字来声明:接口只包含方法.委托或事件和属性的签名(接口包含的成员).不能包含字段(因为字段是包含数据的).方法的实现是“继承”接口 ...

  4. TIMESTAMP和DATETIME的区别

    TIMESTAMP和DATETIME的区别 1. 存储空间不同 a) TIMESTAMP占用4个字节 b) DATETIME占用8个字节 2. 受时区影响 c) TIMESTAMP实际记录的是1970 ...

  5. ASP.Net MVC4排序检索分页的实现

    前言 上一节我们做到了如下的一个基础查询页面.本节我们向这个页面中加入排序.搜索和分页功能. 排序 从上图中的地址栏中可以看到这个页面调用的是Company Controller下的Index Act ...

  6. windows批处理命令之ren

    1.批处理批量修改文件后缀名(假设我需要把一个文件夹中的很多txt文件改为sql文件): 1)在需要被处理的文件的文件夹里先新建一个txt文本,然后在文本中写入: ren *.txt *.sql 2) ...

  7. 关于几种常用的Adapter使用区别

    Adapter常用的实现类如下: 1.ArrayAdapter:简单.易用的Adapter,通常用于将数组或List集合的多个值包装成多个列表项. 2.SimpleAdapter:并不简单.功能强大的 ...

  8. 伯克利DB的一个BUG

    一旦没有手工close掉伯克利DB,则缓存里的数据不会主动写入到文件中,因此非常难于排查这个BUG,记录在这里提醒自己

  9. Android 判断当前网络连接类型

    实际应用开发时,如果存在需要用户获取大量数据的情况,最好是先判断下网络类型,提示用户当前的网络类型,是否需要连接Wifi,etc.(手机流量太贵啦,当然土豪是无视这玩意的, (/ □ \)). 定义网 ...

  10. 最近国外很拉风的,,基于.net 的一个手表

    site:http://agentwatches.com/ 这个项目是一个国外工作室,筹集资金 创立的. 直接用c# 代码编译显示在手机上.能和智能手机通信等. 并且是开源的. 很酷 其次.它提供了. ...