Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
7.0 KiB

  1. /*++
  2. Copyright (C) 1999-2001 Microsoft Corporation
  3. Module Name:
  4. PERFTHRD.CPP
  5. Abstract:
  6. History:
  7. --*/
  8. #include "precomp.h"
  9. #include <stdio.h>
  10. #include <process.h>
  11. //#include <wbemcli.h>
  12. //#include <cominit.h>
  13. #include <WinMgmtR.h>
  14. #include "ntreg.h"
  15. #include "perfthrd.h"
  16. #include "adaputil.h"
  17. // IMPORTANT!!!!
  18. // This code MUST be revisited to do the following:
  19. // A>>>>> Exception Handling around the outside calls
  20. // B>>>>> Use a named mutex around the calls
  21. // C>>>>> Make the calls on another thread
  22. // D>>>>> Place and handle registry entries that indicate a bad DLL!
  23. CPerfThread::CPerfThread( CAdapPerfLib* pPerfLib ) : CAdapThread( pPerfLib )
  24. ////////////////////////////////////////////////////////////////////////////////////////////
  25. //
  26. // Constructor
  27. //
  28. ////////////////////////////////////////////////////////////////////////////////////////////
  29. {
  30. CNTRegistry reg;
  31. if ( CNTRegistry::no_error == reg.Open( HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\WBEM\\CIMOM" ) )
  32. {
  33. long lError = reg.GetDWORD( L"ADAPPerflibTimeout", &m_dwPerflibTimeoutSec );
  34. if ( CNTRegistry::no_error == lError )
  35. {
  36. // This is what we want
  37. }
  38. else if ( CNTRegistry::not_found == lError )
  39. {
  40. // Not set, so add it
  41. reg.SetDWORD( L"ADAPPerflibTimeout", PERFTHREAD_DEFAULT_TIMEOUT );
  42. m_dwPerflibTimeoutSec = PERFTHREAD_DEFAULT_TIMEOUT;
  43. }
  44. else
  45. {
  46. // Unknown error, continue with default value
  47. m_dwPerflibTimeoutSec = PERFTHREAD_DEFAULT_TIMEOUT;
  48. }
  49. }
  50. else
  51. {
  52. m_dwPerflibTimeoutSec = PERFTHREAD_DEFAULT_TIMEOUT;
  53. }
  54. }
  55. HRESULT CPerfThread::Open( CAdapPerfLib* pLib )
  56. ////////////////////////////////////////////////////////////////////////////////////////////
  57. //
  58. // Open creates a new open request object using the CAdapPerfLib parameter. It then queues
  59. // it up and waits for PERFTHREAD_TIMEOUT milliseconds. If the operation has not returned
  60. // in time, then ...
  61. //
  62. ////////////////////////////////////////////////////////////////////////////////////////////
  63. {
  64. HRESULT hr = WBEM_E_FAILED;
  65. try
  66. {
  67. // Create new request object
  68. // =========================
  69. CPerfOpenRequest* pRequest = new CPerfOpenRequest;
  70. CAdapReleaseMe armRequest( pRequest );
  71. if ( NULL == pRequest )
  72. {
  73. hr = WBEM_E_OUT_OF_MEMORY;
  74. }
  75. else
  76. {
  77. // Queue the request and return
  78. // ============================
  79. Enqueue( pRequest );
  80. // Wait for the call to return
  81. // ===========================
  82. switch ( WaitForSingleObject( pRequest->GetWhenDoneHandle(), ( m_dwPerflibTimeoutSec * 1000 ) ) )
  83. {
  84. case WAIT_OBJECT_0:
  85. {
  86. // SUCCESS: Call returned before it timed-out
  87. // ==========================================
  88. hr = pRequest->GetHRESULT();
  89. }break;
  90. case WAIT_TIMEOUT:
  91. {
  92. pLib->SetStatus( ADAP_PERFLIB_IS_INACTIVE );
  93. hr = WBEM_E_FAILED; //Reset();
  94. if (!pLib->GetEventLogCalled())
  95. {
  96. pLib->SetEventLogCalled(TRUE);
  97. CAdapUtility::NTLogEvent( EVENTLOG_WARNING_TYPE, WBEM_MC_ADAP_PERFLIB_FUNCTION_TIMEOUT, (LPCWSTR)pLib->GetServiceName(), L"open" );
  98. }
  99. }
  100. }
  101. }
  102. }
  103. catch(...)
  104. {
  105. ERRORTRACE( ( LOG_WMIADAP, "CPerfThread::Open() failed due to out of memory exception.\n" ) );
  106. hr = WBEM_E_FAILED;
  107. }
  108. return hr;
  109. }
  110. HRESULT CPerfThread::GetPerfBlock( CAdapPerfLib* pLib, PERF_OBJECT_TYPE** ppData,
  111. DWORD* pdwBytes, DWORD* pdwNumObjTypes, BOOL fCostly )
  112. {
  113. HRESULT hr = WBEM_E_FAILED;
  114. try
  115. {
  116. CPerfCollectRequest* pRequest = new CPerfCollectRequest( fCostly );
  117. CAdapReleaseMe armRequest( pRequest );
  118. Enqueue( pRequest );
  119. switch ( WaitForSingleObject( pRequest->GetWhenDoneHandle(), ( m_dwPerflibTimeoutSec * 1000 ) ) )
  120. {
  121. case WAIT_OBJECT_0:
  122. {
  123. hr = pRequest->GetHRESULT();
  124. pRequest->GetData( ppData, pdwBytes, pdwNumObjTypes );
  125. if (FAILED(hr)){
  126. pLib->SetStatus( ADAP_PERFLIB_FAILED );
  127. }
  128. }break;
  129. case WAIT_TIMEOUT:
  130. {
  131. pLib->SetStatus( ADAP_PERFLIB_IS_INACTIVE );
  132. hr = WBEM_E_FAILED; //Reset();
  133. if (!pLib->GetEventLogCalled())
  134. {
  135. pLib->SetEventLogCalled(TRUE);
  136. CAdapUtility::NTLogEvent( EVENTLOG_WARNING_TYPE, WBEM_MC_ADAP_BAD_PERFLIB_TIMEOUT, (LPCWSTR)pLib->GetServiceName(), L"collect" );
  137. }
  138. }break;
  139. }
  140. }
  141. catch(...)
  142. {
  143. // DEVDEV
  144. // should se call pLib->SetStatus(SOMETHING); ?
  145. //
  146. ERRORTRACE( ( LOG_WMIADAP, "CPerfThread::GetPerfBlock() failed due to out of memory exception.\n" ) );
  147. return WBEM_E_OUT_OF_MEMORY;
  148. }
  149. return hr;
  150. }
  151. HRESULT CPerfThread::Close( CAdapPerfLib* pLib )
  152. {
  153. HRESULT hr = WBEM_E_FAILED;
  154. try
  155. {
  156. CPerfCloseRequest* pRequest = new CPerfCloseRequest;
  157. CAdapReleaseMe armRequest( pRequest );
  158. Enqueue( pRequest );
  159. switch ( WaitForSingleObject( pRequest->GetWhenDoneHandle(), ( m_dwPerflibTimeoutSec * 1000 ) ) )
  160. {
  161. case WAIT_OBJECT_0:
  162. {
  163. hr = pRequest->GetHRESULT();
  164. }break;
  165. case WAIT_TIMEOUT:
  166. {
  167. pLib->SetStatus( ADAP_PERFLIB_IS_INACTIVE );
  168. hr = WBEM_E_FAILED; //Reset();
  169. if (!pLib->GetEventLogCalled())
  170. {
  171. pLib->SetEventLogCalled(TRUE);
  172. CAdapUtility::NTLogEvent( EVENTLOG_WARNING_TYPE, WBEM_MC_ADAP_BAD_PERFLIB_TIMEOUT, (LPCWSTR)pLib->GetServiceName(), L"close" );
  173. }
  174. }break;
  175. }
  176. }
  177. catch(...)
  178. {
  179. ERRORTRACE( ( LOG_WMIADAP, "CPerfThread::Close() failed due to out of memory exception.\n" ) );
  180. hr = WBEM_E_OUT_OF_MEMORY;
  181. }
  182. return hr;
  183. }
  184. HRESULT CPerfOpenRequest::Execute( CAdapPerfLib* pPerfLib )
  185. {
  186. // Call the open function in the perflib
  187. // =====================================
  188. m_hrReturn = pPerfLib->_Open();
  189. return m_hrReturn;
  190. }
  191. HRESULT CPerfCollectRequest::Execute( CAdapPerfLib* pPerfLib )
  192. {
  193. // Call the collect function in the perflib
  194. // ========================================
  195. m_hrReturn = pPerfLib->_GetPerfBlock( &m_pData, &m_dwBytes, &m_dwNumObjTypes, m_fCostly );
  196. return m_hrReturn;
  197. }
  198. HRESULT CPerfCloseRequest::Execute( CAdapPerfLib* pPerfLib )
  199. {
  200. // Call the open function in the perflib
  201. // =====================================
  202. m_hrReturn = pPerfLib->_Close();
  203. return m_hrReturn;
  204. }