zoukankan      html  css  js  c++  java
  • Windows下用C语言获取进程cpu使用率,内存使用,IO情况

     

     
    #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 ”,cpu);
    printf(“内存使用: %u 字节 ”, mem);
    printf(“虚拟内存使用: %u 字节 ”, vmem);
    printf(“总共读: %u 字节 ”, r);
    printf(“总共写: %u 字节 ”, w);
     
    Sleep(1000);
    }
    return 0;
    }

     

  • 相关阅读:
    第一题 (Map)利用Map,完成下面的功能:
    练习5 练习目标:继承、多态、方法的重写。
    练习4:用数组表示多重性
    练习3:修改withdraw 方法
    练习2 练习目标-使用引用类型的成员变量:在本练习中,将扩展银行项目,添加一个(客户类)Customer类。Customer类将包含一个Account对象。
    练习1:创建一个简单的银行程序包
    2.建立exception包,建立Bank类,类中有变量double balance表示存款,Bank类的构造方法能增加存款,Bank类中有取款的发方法withDrawal(double dAmount),当取款的数额大于存款时,抛出InsufficientFundsException,取款数额为负数,抛出NagativeFundsException,如new Bank(100),表示存入银行1
    总复习测试(二)
    总复习测试(一)
    新闻发布系统<分页>
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/4572377.html
Copyright © 2011-2022 走看看