我想获取的是物理磁盘的性能指标: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 安霸cv22 + rtl8211f 千兆,udp传输丢包
- ¥15 关于区块链和边缘环境搭建的相关问题
- ¥15 windows远程桌面断卡重连软件卡顿问题
- ¥30 Unity 实现扫描效果
- ¥15 HbuilderX检测不到安卓模拟器
- ¥15 这个main已经在filename.obj中定义是什么错 C语言
- ¥15 关于#linux#的问题:exsi8.0系统 怎么更改web访问端口,不用80、443
- ¥15 使用elementor设计样式
- ¥15 谁能提供一个中文版的推销咨询网站连接?
- ¥15 springboot项目程序启动报错