00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "PerformanceDataSet.h"
00011 #include "TestToolBox/CommonSources/CommonDefinitions.h"
00012 #include "PDHMsg.h"
00013 #include <assert.h>
00014
00015
00016
00017 namespace TestToolBox
00018 {
00019
00020
00021
00022
00023
00024
00025 #ifdef NO_PDH_DLL
00026
00027
00028
00029
00030 PDH_FUNCTION
00031 PdhCollectQueryData (
00032 IN HQUERY hQuery)
00033 {return NO_ERROR;}
00034
00035 PDH_FUNCTION
00036 PdhCloseQuery (
00037 IN HQUERY hQuery
00038 )
00039 {return NO_ERROR;}
00040
00041
00042 PDH_FUNCTION
00043 PdhAddCounter (
00044 IN HQUERY hMyQuery,
00045 IN LPCSTR szFullCounterPath,
00046 IN DWORD dwUserData,
00047 IN HCOUNTER *phCounter)
00048 {return NO_ERROR;}
00049
00050 PDH_FUNCTION
00051 PdhAddCounterA (
00052 IN HQUERY hMyQuery,
00053 IN LPCSTR szFullCounterPath,
00054 IN DWORD dwUserData,
00055 IN HCOUNTER *phCounter)
00056 {return NO_ERROR;}
00057
00058 PDH_FUNCTION
00059 PdhOpenQuery (
00060 IN LPCSTR szDataSource,
00061 IN DWORD dwUserData,
00062 IN HQUERY *phQuery
00063 )
00064 {return NO_ERROR;}
00065
00066 PDH_FUNCTION
00067 PdhGetFormattedCounterValue (
00068 IN HCOUNTER hCounter,
00069 IN DWORD dwFormat,
00070 IN LPDWORD lpdwType,
00071 IN PPDH_FMT_COUNTERVALUE pValue
00072 )
00073 {return NO_ERROR;}
00074
00075
00076 #endif
00077
00078
00079
00080
00081
00082 inline PerformanceDataSet::PerformanceDataSet(void)
00083 :
00084 m_initFailed (false)
00085 {
00086 }
00087
00088
00089
00090
00091
00092
00093 inline PerformanceDataSet::~PerformanceDataSet()
00094 {
00095 TRCF("PerformanceDataSet::~PerformanceDataSet");
00096 TRC(TL_DEBUG, "Destructor ");
00097
00098 #ifndef NO_PDH_DLL
00099 PdhCloseQuery (m_hQuery); GlobalFree(m_phCounter);
00100 #endif
00101
00102 }
00103
00104
00105
00106
00107
00108
00109 inline bool PerformanceDataSet::InitPerfCounters (PerfCounterNames in_counterNames)
00110 {
00111 TRCF("PerfCounterBase::InitPerfCounters");
00112 TRC(TL_DEBUG, "Begin");
00113
00114 m_initFailed = false;
00115 m_perfCounterFullNames = in_counterNames;
00116
00117
00118
00119 PDH_STATUS pdhStatus = PdhOpenQuery (0, 0, &m_hQuery);
00120 assert(pdhStatus == ERROR_SUCCESS);
00121
00122
00123 size_t numCounters = m_perfCounterFullNames.size();
00124 m_phCounter = (HCOUNTER *)GlobalAlloc(GPTR, (sizeof(HCOUNTER) * numCounters));
00125 assert (m_phCounter );
00126 for (unsigned int n = 0; n < numCounters; n++)
00127 {
00128 TRC(TL_DEBUG, "Counter %d: >>%s<<", n, m_perfCounterFullNames[n].c_str());
00129
00130 pdhStatus = PdhAddCounterA (m_hQuery, m_perfCounterFullNames[n].c_str(), 0, &m_phCounter[n]);
00131 if (ERROR_SUCCESS != pdhStatus)
00132 {
00133 m_initFailed = true;
00134 TRC(TL_ERROR, "Add performance counter failed for %s", m_perfCounterFullNames[n].c_str());
00135 }
00136 }
00137 if (m_initFailed)
00138 {
00139 PdhCloseQuery (m_hQuery); GlobalFree(m_phCounter);
00140 }
00141 else
00142 {
00143 pdhStatus = PdhCollectQueryData (m_hQuery);
00144 if (ERROR_SUCCESS != pdhStatus)
00145 {
00146 m_initFailed = true;
00147 TRC(TL_ERROR, "Initialization of performance counters failed! Check your specific PC with utility ScanPerfCounters");
00148 PdhCloseQuery (m_hQuery); GlobalFree(m_phCounter);
00149 }
00150 }
00151 return !m_initFailed;
00152
00153 }
00154
00155
00156
00157
00158
00159
00160 inline PerfCounterValues PerformanceDataSet::GetCurrentCounterValues (void)
00161 {
00162 TRCF("PerformanceDataSet::GetCurrentCounterValues");
00163
00164 PerfCounterValues values;
00165
00166 PDH_STATUS pdhStatus = PdhCollectQueryData (m_hQuery);
00167 assert (pdhStatus == ERROR_SUCCESS);
00168
00169 unsigned int n = 0;
00170 HCOUNTER* pCurHand;
00171 DWORD ctrType;
00172 PDH_FMT_COUNTERVALUE fmtValue;
00173
00174 pCurHand = &m_phCounter[0];
00175
00176 while (n < m_perfCounterFullNames.size())
00177 {
00178 assert(*pCurHand) ;
00179 pdhStatus = PdhGetFormattedCounterValue (
00180 *pCurHand,
00181 PDH_FMT_DOUBLE,
00182 &ctrType,
00183 &fmtValue);
00184 if (pdhStatus == ERROR_SUCCESS)
00185 {
00186 values.push_back(fmtValue.doubleValue);
00187 }
00188 else
00189 {
00190 values.push_back(-1);
00191 TRC(TL_ERROR, "Error retrieving value for %s", m_perfCounterFullNames[n].c_str());
00192 }
00193 pCurHand++;
00194 n++;
00195 }
00196
00197 return values;
00198
00199 }
00200
00201
00202
00203
00204
00205
00206 #ifndef NO_PDH_DLL
00207
00208
00209 #undef PdhOpenQuery // PdhOpenQueryA
00210 extern "C" long __stdcall
00211 PdhOpenQuery (
00212 IN LPCSTR szDataSource,
00213 IN DWORD dwUserData,
00214 IN HQUERY *phQuery
00215 );
00216
00217 #endif
00218
00219
00220
00221
00222
00223
00224 };
00225
00226
00227
00228