Windows下用C语言获取进程cpu使用率,内存使用,IO情况
#ifndef PROCESS_STAT_H#define PROCESS_STAT_H#ifdef __cplusplusextern “C” {#endiftypedef 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 TaskManager)// we may call this function on a process that has just exited butwe 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#includeint 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情况的更多相关文章
- linux ps命令,查看某进程cpu和内存占用率情况, linux ps命令,查看进程cpu和内存占用率排序。 不指定
背景:有时需要单看某个进程的CPU及占用情况,有时需要看整体进程的一个占用情况.一. linux ps命令,查看某进程cpu和内存占用率情况[root@test vhost]# ps auxUSER ...
- Java进程CPU使用率高排查
Java进程CPU使用率高排查 生产java应用,CPU使用率一直很高,经常达到100%,通过以下步骤完美解决,分享一下.1.jps 获取Java进程的PID.2.jstack pid >> ...
- linux下的CPU、内存、IO、网络的压力测试
linux下的CPU.内存.IO.网络的压力测试 要远程测试其实很简单了,把结果放到一个微服务里直接在web里查看就可以了,或者同步到其他服务器上 一.对CPU进行简单测试: 1.通过bc命令计算特 ...
- [转帖]linux下的CPU、内存、IO、网络的压力测试
linux下的CPU.内存.IO.网络的压力测试 https://www.cnblogs.com/zhuochong/p/10185881.html 一.对CPU进行简单测试: 1.通过bc命令计算特 ...
- [转帖]linux下CPU、内存、IO、网络的压力测试,硬盘读写速度测试,Linux三个系统资源监控工具
linux下CPU.内存.IO.网络的压力测试,硬盘读写速度测试,Linux三个系统资源监控工具 https://blog.51cto.com/hao360/1587165 linux_python关 ...
- 为Zabbix配置Nova服务、Keystone和Placement进程CPU和内存usage监控
目前已经完成了RabbitMQ和MySQL的监控项配置,还差对nova-api.nova-conductor.nova-scheduler和keystone进程CPU和内存 usage的监控,类似的轮 ...
- 查看进程CPU、内存使用情况
本文介绍通过ps和top查看进程的cpu.内存等使用情况. 1.ps命令 1.1 概览 ps命令相关参数定义: -e 或者-A,选择所有的进程: -L 显示线程: -o 自定义输出格式: 输出格式: ...
- Cgroups控制cpu,内存,io示例
Cgroups是control groups的缩写,最初由Google工程师提出,后来编进linux内核. Cgroups是实现IaaS虚拟化(kvm.lxc等),PaaS容器沙箱(Docker等)的 ...
- Cgroups控制cpu,内存,io示例【转】
本文转载自:https://www.cnblogs.com/yanghuahui/p/3751826.html 百度私有PaaS云就是使用轻量的cgoups做的应用之间的隔离,以下是关于百度架构师许立 ...
随机推荐
- MySQL拷贝表的几种方式
假如我们有以下这样一个表: id username password ----------------------------------- 1 admin * ...
- SpringMVC简单搭建与入门
SpringMVC框架是spring框架的一个模块.springmvc和spring无需要通过中间整合层进行整合. 学习的时候,先了解一下流程至关重要,下面,简单介绍一下流程. 源码下载:http:/ ...
- [转]Java远程方法调用
Java远程方法调用,即Java RMI(Java Remote Method Invocation)是Java编程语言里,一种用于实现远程过程调用的应用程序编程接口.它使客户机上运行的程序可以调用远 ...
- 0.0C语言重点问题回顾
左值和右值得区别:左值是用来表明变量的身份的,右值更加侧重于值本身: void*是个例外,它只有基地址没有类型信息,所以无法解引用. int *p = malloc(100); char *s = m ...
- 【USACO 1.4.4】母亲的牛奶
[题目描述] 农民约翰有三个容量分别是A,B,C升的桶,A,B,C分别是三个从1到20的整数, 最初,A和B桶都是空的,而C桶是装满牛奶的.有时,约翰把牛奶从一个桶倒到另一个桶中,直到被灌桶装满或原 ...
- Hadoop 中 IPC 的源码分析
最近开始看 Hadoop 的一些源码,展开hadoop的源码包,各个组件分得比较清楚,于是开始看一下 IPC 的一些源码. IPC模块,也就是进程间通信模块,如果是在不同的机器上,那就可以理解为 RP ...
- App Store自动下载WiFi与蜂窝数据切换机制
写下这个给自己备忘,上次也有一次载了个跟头. 在iOS 7和8里面,除了设置--App Store里面自动更新,自动下载,以及使用蜂窝数据要关之外,别以为用了WiFi挂着程序,就万无一失了. 这种情况 ...
- sql批量插入数据之存储过程
-- ============================================= -- Author: jf_ou -- Create date: 2016/03/22 -- Desc ...
- js学习笔记之:时间(一)
日期和时间是javaScript中常用的对象,可以通过此对象判断星期.生日.纪念日等,提高网站的人性化.下面将通过实例来介绍一下学习javaScript中有关时间和日期的知识点: (1)日期和时间函数 ...
- 移除IOS下按钮的原生样式
写WAP页面的时候 一定要加上这组样式,以避免在IOS下面按钮被系统原生样式影响 input,textarea {outline-style:none;-webkit-appearance:none ...