zoukankan      html  css  js  c++  java
  • 获取内存大小、CPU大小、硬盘大小及使用率


      1 #include <cassert>
      2 #include <iostream>
      3 #include <windows.h>
      4 #include <process.h>
      5 #include <assert.h>
      6 #include <conio.h>
      7 #include <TlHelp32.h>
      8 #include <process.h>
      9 #include <Psapi.h>
     10 #include <time.h>
     11 #include <tchar.h>
     12 
     13 using namespace std;
     14 #pragma comment(lib, "Psapi.Lib")
     15 
     16 double FormatDouble2(double value)
     17 {
     18     return floor(value*100.0 + 0.5) / 100.0;
     19 }
     20 
     21 double FileTimeSub(FILETIME ftEndTime, FILETIME ftStartTime)
     22 {
     23     double nDeltaTime;
     24 
     25     double nEndTime = (unsigned long long)ftEndTime.dwHighDateTime << 32 | ftEndTime.dwLowDateTime;
     26     double nStartTime = (unsigned long long)ftStartTime.dwHighDateTime << 32 | ftStartTime.dwLowDateTime;
     27 
     28     nDeltaTime = nEndTime - nStartTime;
     29 
     30     return nDeltaTime;
     31 }
     32 
     33 /// 获得CPU的核数  
     34 int get_cpu_total()
     35 {
     36     SYSTEM_INFO info;
     37     GetSystemInfo(&info);
     38     return (int)info.dwNumberOfProcessors;
     39 }
     40 
     41 
     42 double get_cpu_usage()
     43 {
     44     FILETIME ftStartIdleTime, ftStartKernelTime, ftStartUserTime;
     45     FILETIME ftEndIdleTime, ftEndKernelTime, ftEndUserTime;
     46 
     47     GetSystemTimes(&ftStartIdleTime, &ftStartKernelTime, &ftStartUserTime);
     48     Sleep(1000);
     49     GetSystemTimes(&ftEndIdleTime, &ftEndKernelTime, &ftEndUserTime);
     50 
     51     double nDeltaIdleTime = FileTimeSub(ftEndIdleTime, ftStartIdleTime);
     52     double nDeltaKernelTime = FileTimeSub(ftEndKernelTime, ftStartKernelTime);
     53     double nDeltaUserTime = FileTimeSub(ftEndUserTime, ftStartUserTime);
     54     if (nDeltaKernelTime + nDeltaUserTime == 0)
     55     {
     56         return 0;
     57     }
     58 
     59     double nCPUUsageRate = ((nDeltaKernelTime + nDeltaUserTime - nDeltaIdleTime) * 100) / (nDeltaKernelTime + nDeltaUserTime);
     60 
     61     return FormatDouble2(nCPUUsageRate);
     62 }
     63 
     64 double get_memory_usage()
     65 {
     66     MEMORYSTATUS ms;
     67     ::GlobalMemoryStatus(&ms);
     68     return ms.dwMemoryLoad;
     69 }
     70 
     71 
     72 double get_memory_total()
     73 {
     74     double nGB = 1024 * 1024 * 1024;
     75     PERFORMANCE_INFORMATION stPerformance = { 0 };
     76     double cb = sizeof(stPerformance);
     77     GetPerformanceInfo(&stPerformance, cb);
     78     double nPageSize = stPerformance.PageSize;
     79     double memtotal = (INT64)stPerformance.PhysicalTotal * nPageSize;
     80 
     81     memtotal = memtotal / nGB;
     82     return FormatDouble2(memtotal);
     83 }
     84 
     85 double get_disk_usage()
     86 {
     87     double all_Total = 0;                 //函数外自定义
     88     double all_Free = 0;
     89     DWORD dwSize = MAX_PATH;
     90     TCHAR szLogicalDrives[MAX_PATH] = { 0 };
     91 
     92 
     93     DWORD dwResult = GetLogicalDriveStrings(dwSize, szLogicalDrives);
     94 
     95 
     96     if (dwResult > 0 && dwResult <= MAX_PATH)
     97     {
     98         TCHAR* szSingleDrive = szLogicalDrives;
     99 
    100         while (*szSingleDrive)
    101         {
    102             uint64_t available, total, free;
    103             if (GetDiskFreeSpaceEx(szSingleDrive, (ULARGE_INTEGER*)&available, (ULARGE_INTEGER*)&total, (ULARGE_INTEGER*)&free))
    104             {
    105                 uint64_t Total, Available, Free;
    106                 Total = total >> 20;
    107                 Available = available >> 20;
    108                 Free = free >> 20;
    109 
    110                 all_Total += Total;   //总
    111                 all_Free += Free;   //剩余
    112             }
    113 
    114             // 获取下一个驱动器号起始地址
    115             szSingleDrive += _tcslen(szSingleDrive) + 1;
    116         }
    117     }
    118     double useage = (((all_Total - all_Free) * 100) / all_Total);
    119     return FormatDouble2(useage);
    120 }
    121 
    122 
    123 double get_disk_total()   //单位:G
    124 {
    125     double all_Total = 0;                 //函数外自定义
    126     double all_Free = 0;
    127     DWORD dwSize = MAX_PATH;
    128     TCHAR szLogicalDrives[MAX_PATH] = { 0 };
    129 
    130 
    131     DWORD dwResult = GetLogicalDriveStrings(dwSize, szLogicalDrives);
    132 
    133 
    134     if (dwResult > 0 && dwResult <= MAX_PATH)
    135     {
    136         TCHAR* szSingleDrive = szLogicalDrives;
    137 
    138         while (*szSingleDrive)
    139         {
    140             uint64_t available, total, free;
    141             if (GetDiskFreeSpaceEx(szSingleDrive, (ULARGE_INTEGER*)&available, (ULARGE_INTEGER*)&total, (ULARGE_INTEGER*)&free))
    142             {
    143                 uint64_t Total, Available, Free;
    144                 Total = total >> 20;
    145                 Available = available >> 20;
    146                 Free = free >> 20;
    147 
    148                 all_Total += Total;   //总
    149                 all_Free += Free;   //剩余
    150             }
    151 
    152             // 获取下一个驱动器号起始地址
    153             szSingleDrive += _tcslen(szSingleDrive) + 1;
    154         }
    155     }
    156     all_Total = all_Total / 1024;
    157     return FormatDouble2(all_Total);
    158 }
    159 
    160 
    161 int main()
    162 {
    163 
    164     cout <<  "cpu_total: " << get_cpu_total() << endl;
    165     cout <<  "cpu_use: " << get_cpu_usage() << endl;
    166 
    167 
    168     cout <<  "memory_total: " << get_memory_total() << endl;
    169     cout <<  "memory_used: " << get_memory_usage() << endl;
    170     cout <<  "disk_total: " << get_disk_total() << endl;
    171     cout <<  "disk_used: " << get_disk_usage() << endl;
    172 }
    173 
    174 
    175 
    PS:会笑的人,运气通常都会比别人好。
  • 相关阅读:
    06.SpringMVC之参数绑定
    05.SpringMVC之请求映射
    04.SpringMVC之用
    03.SpringMVC之器
    02.SpringMVC之初体验
    01.SpringMVC之概述
    Spring 中的Null-Safety
    Spring中Resource(资源)的获取
    Spring的事件机制详解
    Spring详解(二)------注解配置IOC
  • 原文地址:https://www.cnblogs.com/thinkinc999/p/13541188.html
Copyright © 2011-2022 走看看