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.

151 lines
3.1 KiB

  1. /*++
  2. Copyright (C) 1998-2001 Microsoft Corporation
  3. Module Name:
  4. Abstract:
  5. History:
  6. --*/
  7. ///////////////////////////////////////////////////////////////////
  8. //
  9. // StressOps.cpp
  10. //
  11. ///////////////////////////////////////////////////////////////////
  12. #include "Refresher.h"
  13. #include "Agents.h"
  14. CBasicRefreshAgent::CBasicRefreshAgent()
  15. {
  16. m_pRef = 0;
  17. m_dwIterations = 0;
  18. m_dwPeriod = 0;
  19. m_hThread = 0;
  20. }
  21. CBasicRefreshAgent::~CBasicRefreshAgent()
  22. {
  23. }
  24. BOOL CBasicRefreshAgent::Create(CRefresher *pRef, DWORD dwIterations, DWORD dwPeriod, HANDLE hRefreshEvent)
  25. {
  26. m_hRefreshEvent = hRefreshEvent;
  27. m_pRef = pRef;
  28. m_dwPeriod = dwPeriod;
  29. m_dwIterations = dwIterations;
  30. m_bInfinite = (0 == dwIterations);
  31. if (m_dwIterations)
  32. {
  33. DWORD dwTID;
  34. m_hThread = CreateThread(NULL, 0, CBasicRefreshAgent::StressThreadEntry,
  35. (LPVOID)this, CREATE_SUSPENDED,
  36. &dwTID);
  37. InterlockedIncrement(&g_lRefThreadCount);
  38. }
  39. return (NULL != m_hThread);
  40. }
  41. void CBasicRefreshAgent::BeginStress()
  42. {
  43. if (m_hThread)
  44. ResumeThread(m_hThread);
  45. }
  46. void CBasicRefreshAgent::StressLoop()
  47. {
  48. DWORD dw = 0;
  49. while ((dw < m_dwIterations) || m_bInfinite)
  50. {
  51. if (!m_bInfinite)
  52. dw++;
  53. if (!m_pRef->Refresh())
  54. break;
  55. if (m_dwPeriod)
  56. Sleep(m_dwPeriod);
  57. }
  58. }
  59. DWORD WINAPI CBasicRefreshAgent::StressThreadEntry(LPVOID lpParameter)
  60. {
  61. CBasicRefreshAgent *pStress = (CBasicRefreshAgent*)lpParameter;
  62. pStress->StressLoop();
  63. if (InterlockedDecrement(&g_lRefThreadCount) == 0)
  64. if (pStress->m_hRefreshEvent)
  65. SetEvent(pStress->m_hRefreshEvent);
  66. return 0;
  67. }
  68. BOOL CRandomOpRefreshAgent::Create(CRefresher *pRef, DWORD dwPeriod)
  69. {
  70. CBasicRefreshAgent::Create(pRef, 0, dwPeriod, NULL);
  71. DWORD dwTID;
  72. m_hThreadAddRefs = CreateThread(NULL, 0, CRandomOpRefreshAgent::AddRefThreadEntry,
  73. (LPVOID)this, CREATE_SUSPENDED,
  74. &dwTID);
  75. m_hThreadRemoveRefs = CreateThread(NULL, 0, CRandomOpRefreshAgent::RemRefThreadEntry,
  76. (LPVOID)this, CREATE_SUSPENDED,
  77. &dwTID);
  78. return TRUE;
  79. }
  80. void CRandomOpRefreshAgent::BeginStress()
  81. {
  82. CBasicRefreshAgent::BeginStress();
  83. }
  84. void CRandomOpRefreshAgent::StressLoop()
  85. {
  86. DWORD dw = 0;
  87. while ((dw < m_dwIterations) || m_bInfinite)
  88. {
  89. if (!m_bInfinite)
  90. dw++;
  91. if (!m_pRef->Refresh())
  92. break;
  93. if (m_dwPeriod)
  94. Sleep(m_dwPeriod);
  95. }
  96. }
  97. DWORD WINAPI CRandomOpRefreshAgent::AddRefThreadEntry(LPVOID lpParameter)
  98. {
  99. CRandomOpRefreshAgent *pAgent = (CRandomOpRefreshAgent*)lpParameter;
  100. // If number of instances are lower than MAX_INST, then add instance then sleep for random period
  101. if (pAgent->m_pRef->GetNumObjects() < MAX_INST)
  102. pAgent->m_pRef->AddObject(L"root\\default", L"Win32_HiPerfCounter.Name=\"Inst_1\"");
  103. Sleep(0);
  104. return 0;
  105. }
  106. DWORD WINAPI CRandomOpRefreshAgent::RemRefThreadEntry(LPVOID lpParameter)
  107. {
  108. CRandomOpRefreshAgent *pAgent = (CRandomOpRefreshAgent*)lpParameter;
  109. // If number of instances are greater than MIN_INST, then remove instance then sleep for random period
  110. int nNumObj = pAgent->m_pRef->GetNumObjects();
  111. if (nNumObj > MIN_INST)
  112. pAgent->m_pRef->RemoveObject(GetTickCount() % nNumObj);
  113. Sleep(0);
  114. return 0;
  115. }