当前位置: 首页 > news >正文

VC++ PDH/性能计数器

例子:

PID=0,缺省为当前进程,但最好是获取当前进程ID传递进去,当然也可以选择其它进程的ID。

PerformanceCounter pc;

pc.Open(0, "//Processor(_Total)//% Processor Time");

源实现:

#include <windows.h>
#include <pdh.h>class PerformanceCounter{public:PerformanceCounter() noexcept;virtual ~PerformanceCounter() noexcept;public:virtual double              Next() noexcept;virtual void                Open(int pid, LPCSTR counter);virtual void                Dispose() noexcept;private:void                        Release() noexcept;private:std::atomic<void*>          m_phQuery   = NULL;std::atomic<void*>          m_phCounter = NULL;};PerformanceCounter::PerformanceCounter() noexcept: m_phQuery(NULL), m_phCounter(NULL){}PerformanceCounter::~PerformanceCounter() noexcept{Release();}void PerformanceCounter::Open(int pid, LPCSTR counter){void* phQuery = NULL;if (PdhOpenQueryA(NULL, pid, &phQuery) != ERROR_SUCCESS){throw std::exception("The handle to the PerformanceCounter could not be opened.");}void* phCounter = NULL;if (PdhAddCounterA(phQuery, counter, 0, &phCounter) != ERROR_SUCCESS){PdhCloseQuery(phCounter);throw std::exception("Unable to add a performance counter instance.");}else{Release();}m_phQuery.exchange(phQuery);m_phCounter.exchange(phCounter);}double PerformanceCounter::Next() noexcept{if (m_phQuery == NULL){return 0;}else{PdhCollectQueryData(m_phQuery);}PDH_FMT_COUNTERVALUE counterValue;if (PdhGetFormattedCounterValue(m_phCounter, PDH_FMT_DOUBLE, NULL, &counterValue) == ERROR_SUCCESS){return counterValue.doubleValue;}return 0;}void PerformanceCounter::Dispose() noexcept{Release();}void PerformanceCounter::Release() noexcept{void* phCounter = m_phCounter.exchange(NULL);if (NULL != phCounter){PdhRemoveCounter(phCounter);}void* phQuery = m_phQuery.exchange(NULL);if (phQuery != NULL){PdhCloseQuery(phQuery);}}

http://www.lryc.cn/news/345562.html

相关文章:

  • C++ 类和对象:面向对象编程基础
  • linux 基础命令使用
  • eve 导入linux
  • vivado新版本兼容老版本,vitis classic兼容sdk教程
  • 02.02.返回倒数第k个节点
  • MongoDB 从部署到掌握
  • electron-vite工具打包后通过内置配置文件动态修改接口地址实现方法
  • 每日一练2024.5.9
  • P2622 关灯问题
  • 从头开始的建材类电商小程序开发指南
  • 数据结构中的栈(C语言版)
  • (贪心05) 无重叠区间 划分字母区间 合并区间
  • 简单网络管理协议(SNMP)入门
  • leetcode解题思路分析(一百五十七)1366 - 1372 题
  • 药物代谢动力学学习笔记
  • IDEA中git的常用操作(保姆级教学)
  • 保研面试408复习 3——操作系统
  • 【代码随想录37期】Day02 有序数组的平方、长度最小的子数组、螺旋矩阵Ⅱ(施工中)
  • 通俗的理解网关的概念的用途(三):你的数据包是如何到达下一层的
  • 基于Springboot的校运会管理系统(有报告)。Javaee项目,springboot项目。
  • USP技术提升大语言模型的零样本学习能力
  • 前端安全防护实战:XSS、CSRF防御与同源策略详解(react 案例)
  • 2024C题生物质和煤共热解问题的研究 详细思路
  • 智慧旅游引领未来风尚,科技助力旅行更精彩:科技的力量推动旅游业创新发展,为旅行者带来更加便捷、高效和智能的旅行服务
  • 十.吊打面试官系列-Tomcat优化-通过压测Tomcat调优实战
  • JVM调优—减少FullGC
  • 力扣 256. 粉刷房子 LCR 091. 粉刷房子 python AC
  • C++STL细节,底层实现,面试题04
  • Linux查看Oracle数据库的环境变量
  • pg数据库学习知识要点分析-1