我想获取的是物理磁盘的性能指标:IO延迟、读IO/s、写IO/s、读KB/s、写KB/s、读error/s、写error/s(物理磁盘是disk0、disk1这种,不是c盘d盘这玩意)
5条回答 默认 最新
- churuxu 2023-07-11 12:51关注
你列的这些,有些是获取不了的,哪些能获取可以参考电脑自带的“性能监视器”
代码获取示例:#include <windows.h> #include <stdio.h> #include <pdh.h> #include <locale.h> #include <wchar.h> #include <stdlib.h> #pragma comment(lib, "pdh.lib") typedef struct query_context{ int count; PDH_HQUERY query; PDH_HCOUNTER counter[20]; }query_context; static query_context context_[16]; static WCHAR instance_names[4096]; //性能计数器名称,需要采集哪几个就在这里添加, 全部名称可通过list_counter_name()获取, 或通过系统自带“性能监视器”获取 static LPCWSTR counter_names_[] = { L"Disk Reads/sec", L"Disk Writes/sec", L"Disk Read Bytes/sec", L"Disk Write Bytes/sec", L"% Disk Time", }; //性能计数器对应显示名称 static LPCWSTR names_[] = { L"读次数/秒", L"写次数/秒", L"读字节数/秒", L"写字节数/秒", L"磁盘活动时间", }; #define check(exp,msg) if(!(exp)){printf("Error:%s\n",msg);exit(1);} static void list_counter_name(){ WCHAR counter_names[4096]; DWORD instance_length = 4096; DWORD counter_length = 4096; LPCWSTR counter_name; PdhEnumObjectItemsW(NULL,NULL,L"PhysicalDisk",counter_names,&counter_length,instance_names,&instance_length,PERF_DETAIL_WIZARD,0); for(counter_name = counter_names; *counter_name; counter_name += (wcslen(counter_name) + 1)){ wprintf(L"%ls\n", counter_name); } } static PDH_HCOUNTER add_counter(PDH_HQUERY query, LPCWSTR instance, LPCWSTR name){ WCHAR path[1024]; PDH_HCOUNTER counter; PDH_STATUS stat; _snwprintf(path, 1024, L"\\PhysicalDisk(%ls)\\%ls", instance, name); stat = PdhAddCounterW(query, path, (DWORD_PTR)NULL, &counter); if(stat)return NULL; return counter; } static void get_disk_performance(LPCWSTR instance, int index){ PDH_HQUERY query; PDH_HCOUNTER* counter; PDH_FMT_COUNTERVALUE value; PDH_STATUS stat; int i, count = 0; counter = context_[index].counter; query = context_[index].query; count = context_[index].count; if(!query){ stat = PdhOpenQuery(NULL, (DWORD_PTR)NULL, &query); check(stat == 0, "PdhOpenQuery"); context_[index].query = query; for (i=0; i<sizeof(counter_names_)/sizeof(LPCWSTR); i++){ counter[count ++] = add_counter(query, instance, counter_names_[i]); } context_[index].count = count; } wprintf(L"磁盘%d: (%ls)\n", index, instance); PdhCollectQueryData(query); for (i = 0; i < count; i++){ stat = PdhGetFormattedCounterValue(counter[i], PDH_FMT_DOUBLE, NULL, &value); if(stat){ wprintf(L"%ls 首次未采集\n", names_[i]); }else{ wprintf(L"%ls %f\n", names_[i], value.doubleValue); } } printf("\n"); } static void get_status(){ LPCWSTR instance ; int i = 0; for(instance = instance_names; *instance; instance += (wcslen(instance) + 1)){ if(wcscmp(instance, L"_Total") == 0)continue; get_disk_performance(instance, i); i ++; } } int wmain(){ setlocale(LC_ALL,""); list_counter_name(); while(1){ get_status(); Sleep(1000); system("cls"); } return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 如何让企业微信机器人实现消息汇总整合
- ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
- ¥15 如何用Python爬取各高校教师公开的教育和工作经历
- ¥15 TLE9879QXA40 电机驱动
- ¥20 对于工程问题的非线性数学模型进行线性化
- ¥15 Mirare PLUS 进行密钥认证?(详解)
- ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
- ¥20 想用ollama做一个自己的AI数据库
- ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
- ¥15 请问怎么才能复现这样的图呀