获取系统CPU、内存和IO信息

process_stat.h的内容如下: 

/** @file 
* @brief 进程统计信息函数的声明 
* @author 张亚霏 
* @date 2009/05/03 
* @version 0.1 
* 
*/  
#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*/  

process_stat_win.c的内容如下: 
/** @file 
* @brief 进程统计信息函数的实现 
* @author 张亚霏 
* @date 2009/05/03 
* @version 0.1 
* 
* 部分代码来自MSDN的例子 
* 部分代码来自google chromium项目 
* 
* 需要连接到psapi.lib 
*/  

#include <windows.h>  
#include <psapi.h>  
#include <assert.h>  
#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;  
}  

可以这样使用: 
/** @file 
* @brief 进程统计信息函数的测试 
* @author 张亚霏 
* @date 2009/05/03 
* @version 0.1 
* 
*/  
#include "process_stat.h"  
#include <stdio.h>  
#include <Windows.h>  

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;   
}  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1 TORNADO的调试工具 4 1.1 WINDSHELL 4 1.1.1 简介 4 1.1.2 功能键 5 1.1.3 特殊控制符 6 1.1.4 计算功能 6 1.1.5 环境变量 6 1.1.6 内置指令 6 1.2 BROWSE 17 1.2.1 内存查看 17 1.2.2 模块信息 17 1.2.3 堆栈使用率 18 1.2.4 CPU占有率 18 1.2.5 任务信息 19 1.2.6 中断向量表 19 1.2.7 实体查看 20 1.3 DEBUGGER 21 1.3.1 条件断点 21 1.3.2 代码显示 21 1.4 TARGET SERVER 21 1.4.1 使用串口调试 21 1.4.2 重定向 21 1.5 WINDVIEW 22 1.5.1 记录层次 22 1.5.2 记录数据存储方式 22 1.5.3 传送方式 22 1.5.4 数据分析 23 1.6 TRIGGER 23 1.7 TELNET 24 1.8 调试模式 24 1.8.1 任务调试模式下的多任务调试 26 1.8.2 系统调试模式下多任务的调试: 27 1.8.3 中断服务程序的调试 28 2 V2支撑的调试手段 28 2.1 各模块提供的函数 28 2.2 如何查看ERROR.LOG文件 29 2.2.1 定位到具体的出错行 31 3 V3支撑的调试手段 31 1.1. 进程调试 31 1.2. 查看所有进程信息:OSS_DBGGETALLUSEPCBINFO 31 1.3. 查看当前运行的进程信息 31 1.3.1. 进程断点设置:b 31 1.3.2. 当前进程运行信息:OSS_DbgGetCurPCBInfo 32 1.3.3. 当前进程的消息信息:OSS_DbgGetCurMsgInfo 32 1.3.4. 进程断点取消:bd 32 1.3.5. 恢复进程运行:tr 32 1.4. 内存观察 32 1.4.1. 消息队列堆积、阻塞观察:tw 32 1.4.2. 任务消息队列观察:OSS_DbgShowQueueCtl 33 1.4.3. 任务UB使用观察:OSS_DbgShowTaskUB 33 1.4.4. 进程使用UB情况:OSS_DbgShowProcUBInfo 34 1.4.5. UB的配置和当前状态:OSS_DbgMemUbUsePrn/ OSS_DbgShowUbPool 34 1.5. 通信和定时器状态观察 35 1.5.1. 通信状态显示OSS_DbgShowComm 35 1.5.2. 测试板间通信是否正常:OSS_DbgRudpPing 35 1.5.3. 单板上定时器的使用信息:OSS_DbgGetTimerInfo 36 1.5.4. 单板进程使用定时器的信息:OSS_DbgGetTimerInfoOfProc 36 1.6. 杂项观察 36 1.6.1. 异常发生后信息观察:OSS_DbgShowExcInfo 36 1.6.2. 堆栈使用率/运行时间统计:zte 36 1.6.3. 进程最近打印内容观察:ztecall/ ztemsg 36 1.6.4. 进程最近打印的1K内容和打印时间:zteprint 37

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值