pdh_error ThreadedSafePDH::PdhOpenQuery(LPCWSTR szDataSource, DWORD_PTR dwUserData, PDH::PDH_HQUERY * phQuery) { boost::unique_lock<boost::shared_mutex> lock(mutex_); if (!lock.owns_lock()) throw pdh_exception("Failed to get mutex for PdhOpenQuery"); if (pPdhOpenQuery == NULL) throw pdh_exception("Failed to initialize PdhOpenQuery :("); return pdh_error(pPdhOpenQuery(szDataSource, dwUserData, phQuery)); }
pdh_error ThreadedSafePDH::PdhAddCounter(PDH::PDH_HQUERY hQuery, LPCWSTR szFullCounterPath, DWORD_PTR dwUserData, PDH::PDH_HCOUNTER * phCounter) { boost::unique_lock<boost::shared_mutex> lock(mutex_); if (!lock.owns_lock()) throw pdh_exception("Failed to get mutex for PdhAddCounter"); if (pPdhAddCounter == NULL) throw pdh_exception("Failed to initialize PdhAddCounter :("); return pdh_error(pPdhAddCounter(hQuery,szFullCounterPath,dwUserData,phCounter)); }
pdh_error ThreadedSafePDH::PdhEnumObjectItems(LPCWSTR szDataSource, LPCWSTR szMachineName, LPCWSTR szObjectName, LPWSTR mszCounterList, LPDWORD pcchCounterListLength, LPWSTR mszInstanceList, LPDWORD pcchInstanceListLength, DWORD dwDetailLevel, DWORD dwFlags) { boost::unique_lock<boost::shared_mutex> lock(mutex_); if (!lock.owns_lock()) throw pdh_exception("Failed to get mutex for PdhEnumObjectItems"); if (pPdhEnumObjectItems == NULL) throw pdh_exception("Failed to initialize PdhEnumObjectItems :("); return pdh_error(pPdhEnumObjectItems(szDataSource, szMachineName, szObjectName, mszCounterList, pcchCounterListLength, mszInstanceList, pcchInstanceListLength, dwDetailLevel, dwFlags)); }
pdh_error ThreadedSafePDH::PdhExpandCounterPath(LPCTSTR szWildCardPath, LPTSTR mszExpandedPathList, LPDWORD pcchPathListLength) { boost::unique_lock<boost::shared_mutex> lock(mutex_); if (!lock.owns_lock()) throw pdh_exception("Failed to get mutex for PdhExpandCounterPath"); if (pPdhExpandCounterPath == NULL) throw pdh_exception("Failed to initialize PdhLookupPerfNameByIndex :("); return pdh_error(pPdhExpandCounterPath(szWildCardPath,mszExpandedPathList,pcchPathListLength)); }
pdh_error ThreadedSafePDH::PdhEnumObjects(LPCWSTR szDataSource, LPCWSTR szMachineName, LPWSTR mszObjectList, LPDWORD pcchBufferSize, DWORD dwDetailLevel, BOOL bRefresh) { boost::unique_lock<boost::shared_mutex> lock(mutex_); if (!lock.owns_lock()) throw pdh_exception("Failed to get mutex for PdhEnumObjects"); if (pPdhEnumObjects == NULL) throw pdh_exception("Failed to initialize PdhEnumObjects :("); return pdh_error(pPdhEnumObjects(szDataSource, szMachineName, mszObjectList, pcchBufferSize, dwDetailLevel, bRefresh)); }
pdh_error ThreadedSafePDH::PdhLookupPerfNameByIndex(LPCTSTR szMachineName,DWORD dwNameIndex,LPTSTR szNameBuffer,LPDWORD pcchNameBufferSize) { boost::unique_lock<boost::shared_mutex> lock(mutex_); if (!lock.owns_lock()) throw pdh_exception("Failed to get mutex for PdhLookupPerfNameByIndex"); if (pPdhLookupPerfNameByIndex == NULL) throw pdh_exception("Failed to initialize PdhLookupPerfNameByIndex :("); return pdh_error(pPdhLookupPerfNameByIndex(szMachineName,dwNameIndex,szNameBuffer,pcchNameBufferSize)); }
pdh_error ThreadedSafePDH::PdhRemoveCounter(PDH::PDH_HCOUNTER hCounter) { boost::unique_lock<boost::shared_mutex> lock(mutex_); if (!lock.owns_lock()) throw pdh_exception("Failed to get mutex for PdhRemoveCounter"); if (pPdhRemoveCounter == NULL) throw pdh_exception("Failed to initialize PdhRemoveCounter :("); return pdh_error(pPdhRemoveCounter(hCounter)); }
pdh_error ThreadedSafePDH::PdhGetFormattedCounterValue(PDH_HCOUNTER hCounter, DWORD dwFormat, LPDWORD lpdwType, PPDH_FMT_COUNTERVALUE pValue) { boost::unique_lock<boost::shared_mutex> lock(mutex_); if (!lock.owns_lock()) throw pdh_exception("Failed to get mutex for PdhGetFormattedCounterValue"); if (pPdhGetFormattedCounterValue == NULL) throw pdh_exception("Failed to initialize PdhGetFormattedCounterValue :("); return pdh_error(pPdhGetFormattedCounterValue(hCounter, dwFormat, lpdwType, pValue)); }
pdh_error ThreadedSafePDH::PdhGetCounterInfo(PDH::PDH_HCOUNTER hCounter, BOOLEAN bRetrieveExplainText, LPDWORD pdwBufferSize, PDH_COUNTER_INFO *lpBuffer) { boost::unique_lock<boost::shared_mutex> lock(mutex_); if (!lock.owns_lock()) throw pdh_exception("Failed to get mutex for PdhGetCounterInfo"); if (pPdhGetCounterInfo == NULL) throw pdh_exception("Failed to initialize PdhGetCounterInfo :("); return pdh_error(pPdhGetCounterInfo(hCounter,bRetrieveExplainText,pdwBufferSize,lpBuffer)); }
virtual PDHError PdhCloseQuery(PDH_HQUERY hQuery) { boost::unique_lock<boost::shared_mutex> lock(mutex_); if (!lock.owns_lock()) throw pdh_exception("Failed to get mutex for PdhCloseQuery"); if (pPdhCloseQuery == NULL) throw pdh_exception("Failed to initialize PdhCloseQuery :("); return PDH::PDHError(pPdhCloseQuery(hQuery)); }
pdh_error ThreadedSafePDH::PdhLookupPerfIndexByName(LPCTSTR szMachineName,LPCTSTR szName,DWORD *dwIndex) { boost::unique_lock<boost::shared_mutex> lock(mutex_); if (!lock.owns_lock()) throw pdh_exception("Failed to get mutex for PdhLookupPerfIndexByName"); if (pPdhLookupPerfIndexByName == NULL) throw pdh_exception("Failed to initialize PdhLookupPerfIndexByName"); return pdh_error(pPdhLookupPerfIndexByName(szMachineName,szName,dwIndex)); }
pdh_error ThreadedSafePDH::PdhCollectQueryData(PDH_HQUERY hQuery) { boost::unique_lock<boost::shared_mutex> lock(mutex_); if (!lock.owns_lock()) throw pdh_exception("Failed to get mutex for PdhCollectQueryData"); if (pPdhCollectQueryData == NULL) throw pdh_exception("Failed to initialize PdhCollectQueryData :("); return pdh_error(pPdhCollectQueryData(hQuery)); }
counter_info PDHCounter::getCounterInfo(BOOL bExplainText) { if (hCounter_ == NULL) throw pdh_exception("Counter is null!"); BYTE *lpBuffer = new BYTE[1025]; DWORD bufSize = 1024; pdh_error status = factory::get_impl()->PdhGetCounterInfo(hCounter_, bExplainText, &bufSize, (PDH_COUNTER_INFO*)lpBuffer); if (status.is_error()) throw pdh_exception(getName() + " getCounterInfo failed (no query)", status); return counter_info(lpBuffer, bufSize, TRUE); }
pdh_error ThreadedSafePDH::PdhValidatePath(LPCWSTR szFullPathBuffer, bool force_reload) { boost::unique_lock<boost::shared_mutex> lock(mutex_); if (!lock.owns_lock()) throw pdh_exception("Failed to get mutex for PdhValidatePath"); if (pPdhValidatePath == NULL) throw pdh_exception("Failed to initialize PdhValidatePath :("); pdh_error status = pdh_error(pPdhValidatePath(szFullPathBuffer)); if (status.is_error() && force_reload) { reload_unsafe(); status = pdh_error(pPdhValidatePath(szFullPathBuffer)); } return status; }
void close() { if (hQuery_ == NULL) throw pdh_exception("query is null!"); PDH::PDHFactory::get_impl()->remove_listener(this); on_unload(); counters_.clear(); }
void PDHCounter::remove() { if (hCounter_ == NULL) return; pdh_error status = factory::get_impl()->PdhRemoveCounter(hCounter_); if (status.is_error()) throw pdh_exception(getName() + " PdhRemoveCounter failed", status); hCounter_ = NULL; }
void PDHCounter::addToQuery(PDH::PDH_HQUERY hQuery) { if (hQuery == NULL) throw pdh_exception(getName(), "addToQuery failed (no query)."); if (hCounter_ != NULL) throw pdh_exception(getName(), "addToQuery failed (already opened)."); pdh_error status = factory::get_impl()->PdhAddCounter(hQuery, utf8::cvt<std::wstring>(counter_->get_counter()).c_str(), 0, &hCounter_); if (status.is_not_found()) { hCounter_ = NULL; status = factory::get_impl()->PdhAddEnglishCounter(hQuery, utf8::cvt<std::wstring>(counter_->get_counter()).c_str(), 0, &hCounter_); } if (status.is_error()) { hCounter_ = NULL; throw pdh_exception(getName() + " PdhAddCounter failed", status); } if (hCounter_ == NULL) throw pdh_exception("Counter is null!"); }
void gatherData() { collect(); for (CounterList::iterator it = counters_.begin(); it != counters_.end(); it++) { PDH::PDHError status = (*it)->collect(); if (status.is_negative_denominator()) { Sleep(500); collect(); status = (*it)->collect(); } if (status.is_negative_denominator()) { if (!hasDisplayedInvalidCOunter_) { hasDisplayedInvalidCOunter_ = true; throw pdh_exception((*it)->getName(), "Negative denominator issue (check FAQ for ways to solve this): ", status); } } else if (status.is_error()) { throw pdh_exception((*it)->getName(), "Failed to poll counter", status); } } }
virtual void on_reload() { if (hQuery_ != NULL) return; PDH::PDHError status = PDH::PDHFactory::get_impl()->PdhOpenQuery( NULL, 0, &hQuery_ ); if (status.is_error()) throw pdh_exception("PdhOpenQuery failed", status); for (CounterList::iterator it = counters_.begin(); it != counters_.end(); it++) { (*it)->addToQuery(getQueryHandle()); } }
virtual void on_unload() { if (hQuery_ == NULL) return; for (CounterList::iterator it = counters_.begin(); it != counters_.end(); it++) { (*it)->remove(); } PDH::PDHError status = PDH::PDHFactory::get_impl()->PdhCloseQuery(hQuery_); if (status.is_error()) throw pdh_exception("PdhCloseQuery failed", status); hQuery_ = NULL; }
void ThreadedSafePDH::remove_listener(subscriber* sub) { boost::unique_lock<boost::shared_mutex> lock(mutex_); if (!lock.owns_lock()) throw pdh_exception("Failed to get mutex for reload"); for(subscriber_list::iterator it = subscribers_.begin(); it != subscribers_.end(); ++it) { if ( (*it) == sub) it = subscribers_.erase(it); if (it == subscribers_.end()) break; } }
inline void collect() { PDH::PDHError status = PDH::PDHFactory::get_impl()->PdhCollectQueryData(hQuery_); if (status.is_error()) throw pdh_exception("PdhCollectQueryData failed: ", status); }
virtual void add_listener(PDHImplSubscriber* sub) { boost::unique_lock<boost::shared_mutex> lock(mutex_); if (!lock.owns_lock()) throw pdh_exception("Failed to get mutex for reload"); subscribers_.push_back(sub); }
void open() { if (hQuery_ != NULL) throw pdh_exception("query is not null!"); PDH::PDHFactory::get_impl()->add_listener(this); on_reload(); }
bool ThreadedSafePDH::reload() { boost::unique_lock<boost::shared_mutex> lock(mutex_); if (!lock.owns_lock()) throw pdh_exception("Failed to get mutex for reload"); return reload_unsafe(); }
void ThreadedSafePDH::add_listener(subscriber* sub) { boost::unique_lock<boost::shared_mutex> lock(mutex_); if (!lock.owns_lock()) throw pdh_exception("Failed to get mutex for reload"); subscribers_.push_back(sub); }