Windows uses C language to get process CPU usage, memory usage, IO condition

Source: Internet
Author: User
Tags filetime

#ifndef PROCESS_STAT_H#define PROCESS_STAT_H#ifdef __cplusplusextern“C” {#endiftypedeflonglongint64_t;typedefunsigned longlong uint64_t;/// 获取当前进程的cpu使用率,返回-1失败intget_cpu_usage();/// 获取当前进程内存和虚拟内存使用量,返回-1失败,0成功intget_memory_usage(uint64_t* mem, uint64_t* vmem);/// 获取当前进程总共读和写的IO字节数,返回-1失败,0成功intget_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”/// 时间转换staticuint64_t file_time_2_utc(constFILETIME* ftime){LARGE_INTEGER li;assert(ftime);li.LowPart = ftime->dwLowDateTime;li.HighPart = ftime->dwHighDateTime;returnli.QuadPart;}/// 获得CPU的核数staticintget_processor_number(){SYSTEM_INFO info;GetSystemInfo(&info);return(int)info.dwNumberOfProcessors;}intget_cpu_usage(){//cpu数量staticintprocessor_count_ = -1;//上一次的时间static int64_t last_time_ = 0;staticint64_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;returncpu;}intget_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;}intget_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;return0;}return-1;}#include “process_stat.h”#include#includeintmain(){while(1){intcpu;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);}return0;}

 

Windows uses C language to get process CPU usage, memory usage, IO condition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.