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.

720 lines
18 KiB

  1. // StresDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CpuStres.h"
  5. #include "StresDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CAboutDlg dialog used for App About
  13. class CAboutDlg : public CDialog
  14. {
  15. public:
  16. CAboutDlg();
  17. // Dialog Data
  18. //{{AFX_DATA(CAboutDlg)
  19. enum { IDD = IDD_ABOUTBOX };
  20. //}}AFX_DATA
  21. // ClassWizard generated virtual function overrides
  22. //{{AFX_VIRTUAL(CAboutDlg)
  23. protected:
  24. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  25. //}}AFX_VIRTUAL
  26. // Implementation
  27. protected:
  28. //{{AFX_MSG(CAboutDlg)
  29. //}}AFX_MSG
  30. DECLARE_MESSAGE_MAP()
  31. };
  32. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  33. {
  34. //{{AFX_DATA_INIT(CAboutDlg)
  35. //}}AFX_DATA_INIT
  36. }
  37. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  38. {
  39. CDialog::DoDataExchange(pDX);
  40. //{{AFX_DATA_MAP(CAboutDlg)
  41. //}}AFX_DATA_MAP
  42. }
  43. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  44. //{{AFX_MSG_MAP(CAboutDlg)
  45. // No message handlers
  46. //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CStressDlg dialog
  50. CStressDlg::CStressDlg(CWnd* pParent /*=NULL*/)
  51. : CDialog(CStressDlg::IDD, pParent)
  52. {
  53. //{{AFX_DATA_INIT(CStressDlg)
  54. // NOTE: the ClassWizard will add member initialization here
  55. //}}AFX_DATA_INIT
  56. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  57. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  58. m_dwProcessPriority = NORMAL_PRIORITY_CLASS;
  59. m_ActivityValue[0] = SLOW_ACTIVITY;
  60. m_ActivityValue[1] = SLOW_ACTIVITY;
  61. m_ActivityValue[2] = SLOW_ACTIVITY;
  62. m_ActivityValue[3] = SLOW_ACTIVITY;
  63. m_PriorityValue[0] = THREAD_PRIORITY_NORMAL;
  64. m_PriorityValue[1] = THREAD_PRIORITY_NORMAL;
  65. m_PriorityValue[2] = THREAD_PRIORITY_NORMAL;
  66. m_PriorityValue[3] = THREAD_PRIORITY_NORMAL;
  67. m_Active[0] = TRUE;
  68. m_Active[1] = FALSE;
  69. m_Active[2] = FALSE;
  70. m_Active[3] = FALSE;
  71. m_ThreadHandle[0] = NULL;
  72. m_ThreadHandle[1] = NULL;
  73. m_ThreadHandle[2] = NULL;
  74. m_ThreadHandle[3] = NULL;
  75. m_dwLoopValue = 0x00010000;
  76. m_pMemory = NULL;
  77. m_dwVASize = 0;
  78. m_dwRandomScale = 1;
  79. }
  80. void CStressDlg::DoDataExchange(CDataExchange* pDX)
  81. {
  82. CDialog::DoDataExchange(pDX);
  83. //{{AFX_DATA_MAP(CStressDlg)
  84. // NOTE: the ClassWizard will add DDX and DDV calls here
  85. //}}AFX_DATA_MAP
  86. }
  87. BEGIN_MESSAGE_MAP(CStressDlg, CDialog)
  88. //{{AFX_MSG_MAP(CStressDlg)
  89. ON_WM_SYSCOMMAND()
  90. ON_WM_PAINT()
  91. ON_WM_QUERYDRAGICON()
  92. ON_BN_CLICKED(IDC_1_ACTIVE, On1Active)
  93. ON_CBN_SELCHANGE(IDC_1_ACTIVITY, OnSelchange1Activity)
  94. ON_CBN_SELCHANGE(IDC_1_PRIORITY, OnSelchange1Priority)
  95. ON_BN_CLICKED(IDC_2_ACTIVE, On2Active)
  96. ON_CBN_SELCHANGE(IDC_2_ACTIVITY, OnSelchange2Activity)
  97. ON_CBN_SELCHANGE(IDC_2_PRIORITY, OnSelchange2Priority)
  98. ON_BN_CLICKED(IDC_3_ACTIVE, On3Active)
  99. ON_CBN_SELCHANGE(IDC_3_ACTIVITY, OnSelchange3Activity)
  100. ON_CBN_SELCHANGE(IDC_3_PRIORITY, OnSelchange3Priority)
  101. ON_BN_CLICKED(IDC_4_ACTIVE, On4Active)
  102. ON_CBN_SELCHANGE(IDC_4_ACTIVITY, OnSelchange4Activity)
  103. ON_CBN_SELCHANGE(IDC_4_PRIORITY, OnSelchange4Priority)
  104. ON_CBN_SELCHANGE(IDC_PROCESS_PRIORITY, OnSelchangeProcessPriority)
  105. ON_EN_KILLFOCUS(IDC_SHARED_MEM_SIZE, OnKillfocusSharedMemSize)
  106. ON_EN_CHANGE(IDC_SHARED_MEM_SIZE, OnChangeSharedMemSize)
  107. ON_BN_CLICKED(IDC_USE_MEMORY, OnUseMemory)
  108. ON_WM_CLOSE()
  109. ON_WM_DESTROY()
  110. //}}AFX_MSG_MAP
  111. END_MESSAGE_MAP()
  112. // disable optimization since it will remove the "do-nothing" loops
  113. #pragma optimize ("", off)
  114. DWORD CStressDlg::OnePercentCalibration(DWORD dwLoopValue)
  115. {
  116. // find how many loops consume 10% of this processor
  117. LARGE_INTEGER liPerfFreq;
  118. LONGLONG llOnePercent;
  119. LARGE_INTEGER liStartTime;
  120. LARGE_INTEGER liEndTime;
  121. LONGLONG llDiff;
  122. LONGLONG llMinDiff;
  123. DWORD dwPriorityClass;
  124. DWORD dwThreadPriority;
  125. HANDLE hProcess;
  126. HANDLE hThread;
  127. DWORD dwLoopCounter;
  128. DWORD dwReturn;
  129. LONGLONG llResult;
  130. BOOL bGoodSample = FALSE;
  131. DWORD dwAttemptCount = 5;
  132. dwReturn = dwLoopValue;
  133. QueryPerformanceFrequency (&liPerfFreq);
  134. llOnePercent = liPerfFreq.QuadPart / 100;
  135. // the calibration run must take at least 50 ms
  136. llMinDiff = liPerfFreq.QuadPart / 20;
  137. hProcess = GetCurrentProcess();
  138. hThread = GetCurrentThread();
  139. dwPriorityClass = GetPriorityClass (hProcess);
  140. dwThreadPriority = GetThreadPriority (hThread);
  141. SetPriorityClass (hProcess, HIGH_PRIORITY_CLASS);
  142. SetThreadPriority (hThread, THREAD_PRIORITY_HIGHEST);
  143. while (!bGoodSample && dwAttemptCount) {
  144. // start timing
  145. QueryPerformanceCounter (&liStartTime);
  146. // do a trial loop
  147. for (dwLoopCounter = dwLoopValue; dwLoopCounter; dwLoopCounter--);
  148. // end timing
  149. QueryPerformanceCounter (&liEndTime);
  150. llDiff = liEndTime.QuadPart - liStartTime.QuadPart;
  151. if (llDiff > llMinDiff) {
  152. bGoodSample = TRUE;
  153. } else {
  154. // increase the loop counter value by a factor of 10
  155. dwLoopValue *= 10;
  156. dwAttemptCount--;
  157. }
  158. }
  159. // restore the priority since we're done with the critical part
  160. SetPriorityClass (hProcess, dwPriorityClass);
  161. SetThreadPriority (hThread, dwThreadPriority);
  162. if (!bGoodSample) {
  163. MessageBox("Unable to calibrate delay loop");
  164. } else {
  165. // findout what the 1% count is
  166. if (llDiff != llOnePercent) {
  167. // then adjust the initial loop value was too big so reduce
  168. llResult = llOnePercent * dwLoopValue / llDiff;
  169. if (llResult & 0xFFFFFFFF00000000) {
  170. // this processor is TOO FAST! so don't change.
  171. } else {
  172. dwReturn = (DWORD)(llResult & 0x00000000FFFFFFFF);
  173. }
  174. }
  175. }
  176. return dwReturn;
  177. }
  178. DWORD WorkerProc (LPDWORD dwArg)
  179. {
  180. DWORD dwLoopCounter;
  181. LPTHREAD_INFO_BLOCK pInfo;
  182. DWORD dwStartingValue;
  183. DWORD dwLocalValue;
  184. DWORD dwLocalIndex;
  185. pInfo = (LPTHREAD_INFO_BLOCK)dwArg;
  186. dwStartingValue = pInfo->Dlg->m_dwLoopValue;
  187. srand( (unsigned)time( NULL ) );
  188. while (pInfo->Dlg->m_Active[pInfo->dwId]) {
  189. for (dwLoopCounter = dwStartingValue; dwLoopCounter; dwLoopCounter--) {
  190. if (pInfo->Dlg->m_pMemory != NULL) {
  191. do {
  192. dwLocalIndex = rand();
  193. dwLocalIndex *= pInfo->Dlg->m_dwRandomScale;
  194. } while (dwLocalIndex >= pInfo->Dlg->m_dwVASize);
  195. pInfo->Dlg->m_pMemory[dwLocalIndex] = dwLocalIndex;
  196. dwLocalValue = pInfo->Dlg->m_pMemory[dwLocalIndex];
  197. // reduce loop iterations to account for
  198. // accessing memory
  199. if (pInfo->Dlg->m_Active[pInfo->dwId]) {
  200. if (dwLoopCounter > 20) {
  201. dwLoopCounter -= 20;
  202. } else {
  203. dwLoopCounter = 1;
  204. }
  205. } else {
  206. break; // out of loop
  207. }
  208. }
  209. }
  210. if (pInfo->Dlg->m_ActivityValue[pInfo->dwId] > 0) {
  211. Sleep (pInfo->Dlg->m_ActivityValue[pInfo->dwId]);
  212. }
  213. }
  214. pInfo->Dlg->m_ThreadHandle[pInfo->dwId] = NULL;
  215. delete pInfo;
  216. return 0;
  217. }
  218. #pragma optimize ("", on)
  219. void CStressDlg::CreateWorkerThread (DWORD dwId)
  220. {
  221. DWORD dwThreadId;
  222. LPTHREAD_INFO_BLOCK pInfo;
  223. // then start a new thread
  224. pInfo = new THREAD_INFO_BLOCK;
  225. if (!pInfo) {
  226. return;
  227. }
  228. pInfo->Dlg = this;
  229. pInfo->dwId = dwId;
  230. m_Active[dwId] = TRUE;
  231. m_ThreadHandle[dwId] = CreateThread (
  232. NULL, 0,
  233. (LPTHREAD_START_ROUTINE)WorkerProc,
  234. (LPVOID)pInfo, 0, &dwThreadId);
  235. if (m_ThreadHandle[dwId] == NULL) {
  236. m_Active[dwId] = FALSE;
  237. delete pInfo;
  238. } else {
  239. // establish thread priority
  240. SetThreadPriority (
  241. m_ThreadHandle[dwId],
  242. m_PriorityValue[dwId]);
  243. }
  244. }
  245. void CStressDlg::SetThreadActivity (CComboBox * cActivityCombo, DWORD dwId)
  246. {
  247. switch (cActivityCombo->GetCurSel()) {
  248. case 0:
  249. m_ActivityValue[dwId] = SLOW_ACTIVITY;
  250. break;
  251. case 1:
  252. m_ActivityValue[dwId] = MEDIUM_ACTIVITY;
  253. break;
  254. case 2:
  255. m_ActivityValue[dwId] = HIGH_ACTIVITY;
  256. break;
  257. case 3:
  258. m_ActivityValue[dwId] = HOG_ACTIVITY;
  259. break;
  260. }
  261. }
  262. void CStressDlg::SetThreadPriorityLevel (CComboBox * cPriorityCombo, DWORD dwId)
  263. {
  264. LONG lLastError = ERROR_SUCCESS;
  265. switch (cPriorityCombo->GetCurSel()) {
  266. case 0:
  267. m_PriorityValue[dwId] = (DWORD)THREAD_PRIORITY_IDLE;
  268. break;
  269. case 1:
  270. m_PriorityValue[dwId] = (DWORD)THREAD_PRIORITY_LOWEST;
  271. break;
  272. case 2:
  273. m_PriorityValue[dwId] = (DWORD)THREAD_PRIORITY_BELOW_NORMAL;
  274. break;
  275. case 3:
  276. m_PriorityValue[dwId] = (DWORD)THREAD_PRIORITY_NORMAL;
  277. break;
  278. case 4:
  279. m_PriorityValue[dwId] = (DWORD)THREAD_PRIORITY_ABOVE_NORMAL;
  280. break;
  281. case 5:
  282. m_PriorityValue[dwId] = (DWORD)THREAD_PRIORITY_HIGHEST;
  283. break;
  284. case 6:
  285. m_PriorityValue[dwId] = (DWORD)THREAD_PRIORITY_TIME_CRITICAL;
  286. break;
  287. }
  288. if (m_ThreadHandle[dwId] != NULL) {
  289. if (!SetThreadPriority (m_ThreadHandle[dwId], m_PriorityValue[dwId])) {
  290. lLastError = GetLastError ();
  291. }
  292. } // else no thread open
  293. }
  294. /////////////////////////////////////////////////////////////////////////////
  295. // CStressDlg message handlers
  296. BOOL CStressDlg::OnInitDialog()
  297. {
  298. CDialog::OnInitDialog();
  299. // Add "About..." menu item to system menu.
  300. // IDM_ABOUTBOX must be in the system command range.
  301. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  302. ASSERT(IDM_ABOUTBOX < 0xF000);
  303. CMenu* pSysMenu = GetSystemMenu(FALSE);
  304. CString strAboutMenu;
  305. strAboutMenu.LoadString(IDS_ABOUTBOX);
  306. if (!strAboutMenu.IsEmpty())
  307. {
  308. pSysMenu->AppendMenu(MF_SEPARATOR);
  309. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  310. }
  311. // calibrate loop counter
  312. m_dwLoopValue = OnePercentCalibration (m_dwLoopValue) * 10;
  313. // Set the icon for this dialog. The framework does this automatically
  314. // when the application's main window is not a dialog
  315. SetIcon(m_hIcon, TRUE); // Set big icon
  316. SetIcon(m_hIcon, FALSE); // Set small icon
  317. // set the priority of this thread to "highest" so the UI will be
  318. // responsive
  319. SetThreadPriority (GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
  320. // enable thread 1 & disable all others
  321. ((CComboBox *)GetDlgItem(IDC_PROCESS_PRIORITY))->SetCurSel(1);
  322. CheckDlgButton (IDC_1_ACTIVE, 1);
  323. ((CComboBox *)GetDlgItem(IDC_1_PRIORITY))->SetCurSel(3);
  324. ((CComboBox *)GetDlgItem(IDC_1_ACTIVITY))->SetCurSel(0);
  325. CheckDlgButton (IDC_2_ACTIVE, 0);
  326. ((CComboBox *)GetDlgItem(IDC_2_PRIORITY))->SetCurSel(3);
  327. ((CComboBox *)GetDlgItem(IDC_2_ACTIVITY))->SetCurSel(0);
  328. CheckDlgButton (IDC_3_ACTIVE, 0);
  329. ((CComboBox *)GetDlgItem(IDC_3_PRIORITY))->SetCurSel(3);
  330. ((CComboBox *)GetDlgItem(IDC_3_ACTIVITY))->SetCurSel(0);
  331. CheckDlgButton (IDC_4_ACTIVE, 0);
  332. ((CComboBox *)GetDlgItem(IDC_4_PRIORITY))->SetCurSel(3);
  333. ((CComboBox *)GetDlgItem(IDC_4_ACTIVITY))->SetCurSel(0);
  334. // set the process priority
  335. OnSelchangeProcessPriority();
  336. // start the first thread
  337. On1Active();
  338. // don't access memory by default
  339. CheckDlgButton (IDC_USE_MEMORY, 0);
  340. (GetDlgItem (IDC_SHARED_MEM_SIZE))->EnableWindow (FALSE);
  341. return TRUE; // return TRUE unless you set the focus to a control
  342. }
  343. void CStressDlg::OnSysCommand(UINT nID, LPARAM lParam)
  344. {
  345. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  346. {
  347. CAboutDlg dlgAbout;
  348. dlgAbout.DoModal();
  349. }
  350. else
  351. {
  352. CDialog::OnSysCommand(nID, lParam);
  353. }
  354. }
  355. // If you add a minimize button to your dialog, you will need the code below
  356. // to draw the icon. For MFC applications using the document/view model,
  357. // this is automatically done for you by the framework.
  358. void CStressDlg::OnPaint()
  359. {
  360. if (IsIconic())
  361. {
  362. CPaintDC dc(this); // device context for painting
  363. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  364. // Center icon in client rectangle
  365. int cxIcon = GetSystemMetrics(SM_CXICON);
  366. int cyIcon = GetSystemMetrics(SM_CYICON);
  367. CRect rect;
  368. GetClientRect(&rect);
  369. int x = (rect.Width() - cxIcon + 1) / 2;
  370. int y = (rect.Height() - cyIcon + 1) / 2;
  371. // Draw the icon
  372. dc.DrawIcon(x, y, m_hIcon);
  373. }
  374. else
  375. {
  376. CDialog::OnPaint();
  377. }
  378. }
  379. // The system calls this to obtain the cursor to display while the user drags
  380. // the minimized window.
  381. HCURSOR CStressDlg::OnQueryDragIcon()
  382. {
  383. return (HCURSOR) m_hIcon;
  384. }
  385. void CStressDlg::On1Active()
  386. {
  387. DWORD dwId = 0;
  388. if (IsDlgButtonChecked(IDC_1_ACTIVE)) {
  389. if (m_ThreadHandle[dwId] == NULL) {
  390. CreateWorkerThread (dwId);
  391. } else {
  392. // thread is already running
  393. }
  394. } else {
  395. m_Active[dwId] = FALSE;
  396. m_ThreadHandle[dwId] = NULL;
  397. }
  398. }
  399. void CStressDlg::OnSelchange1Activity()
  400. {
  401. CComboBox * cActivityCombo;
  402. DWORD dwId = 0;
  403. cActivityCombo = (CComboBox *)GetDlgItem (IDC_1_ACTIVITY);
  404. SetThreadActivity (cActivityCombo, dwId);
  405. }
  406. void CStressDlg::OnSelchange1Priority()
  407. {
  408. CComboBox * cPriorityCombo;
  409. DWORD dwId = 0;
  410. cPriorityCombo = (CComboBox *)GetDlgItem (IDC_1_PRIORITY);
  411. SetThreadPriorityLevel (cPriorityCombo, dwId);
  412. }
  413. void CStressDlg::On2Active()
  414. {
  415. DWORD dwId = 1;
  416. if (IsDlgButtonChecked(IDC_2_ACTIVE)) {
  417. if (m_ThreadHandle[dwId] == NULL) {
  418. CreateWorkerThread (dwId);
  419. } else {
  420. // thread is already running
  421. }
  422. } else {
  423. m_Active[dwId] = FALSE;
  424. m_ThreadHandle[dwId] = NULL;
  425. }
  426. }
  427. void CStressDlg::OnSelchange2Activity()
  428. {
  429. CComboBox * cActivityCombo;
  430. DWORD dwId = 1;
  431. cActivityCombo = (CComboBox *)GetDlgItem (IDC_2_ACTIVITY);
  432. SetThreadActivity (cActivityCombo, dwId);
  433. }
  434. void CStressDlg::OnSelchange2Priority()
  435. {
  436. CComboBox * cPriorityCombo;
  437. DWORD dwId = 1;
  438. cPriorityCombo = (CComboBox *)GetDlgItem (IDC_2_PRIORITY);
  439. SetThreadPriorityLevel (cPriorityCombo, dwId);
  440. }
  441. void CStressDlg::On3Active()
  442. {
  443. DWORD dwId = 2;
  444. if (IsDlgButtonChecked(IDC_3_ACTIVE)) {
  445. if (m_ThreadHandle[dwId] == NULL) {
  446. CreateWorkerThread (dwId);
  447. } else {
  448. // thread is already running
  449. }
  450. } else {
  451. m_Active[dwId] = FALSE;
  452. m_ThreadHandle[dwId] = NULL;
  453. }
  454. }
  455. void CStressDlg::OnSelchange3Activity()
  456. {
  457. CComboBox * cActivityCombo;
  458. DWORD dwId = 2;
  459. cActivityCombo = (CComboBox *)GetDlgItem (IDC_3_ACTIVITY);
  460. SetThreadActivity (cActivityCombo, dwId);
  461. }
  462. void CStressDlg::OnSelchange3Priority()
  463. {
  464. CComboBox * cPriorityCombo;
  465. DWORD dwId = 2;
  466. cPriorityCombo = (CComboBox *)GetDlgItem (IDC_3_PRIORITY);
  467. SetThreadPriorityLevel (cPriorityCombo, dwId);
  468. }
  469. void CStressDlg::On4Active()
  470. {
  471. DWORD dwId = 3;
  472. if (IsDlgButtonChecked(IDC_4_ACTIVE)) {
  473. if (m_ThreadHandle[dwId] == NULL) {
  474. CreateWorkerThread (dwId);
  475. } else {
  476. // thread is already running
  477. }
  478. } else {
  479. m_Active[dwId] = FALSE;
  480. CloseHandle (m_ThreadHandle[dwId]);
  481. m_ThreadHandle[dwId] = NULL;
  482. }
  483. }
  484. void CStressDlg::OnSelchange4Activity()
  485. {
  486. CComboBox * cActivityCombo;
  487. DWORD dwId = 3;
  488. cActivityCombo = (CComboBox *)GetDlgItem (IDC_4_ACTIVITY);
  489. SetThreadActivity (cActivityCombo, dwId);
  490. }
  491. void CStressDlg::OnSelchange4Priority()
  492. {
  493. CComboBox * cPriorityCombo;
  494. DWORD dwId = 3;
  495. cPriorityCombo = (CComboBox *)GetDlgItem (IDC_4_PRIORITY);
  496. SetThreadPriorityLevel (cPriorityCombo, dwId);
  497. }
  498. void CStressDlg::OnOK()
  499. {
  500. CDialog::OnOK();
  501. }
  502. void CStressDlg::OnSelchangeProcessPriority()
  503. {
  504. CComboBox * cPriorityCombo;
  505. DWORD dwPriorityClass;
  506. cPriorityCombo = (CComboBox *)GetDlgItem (IDC_PROCESS_PRIORITY);
  507. switch (cPriorityCombo->GetCurSel()) {
  508. case 0:
  509. dwPriorityClass = IDLE_PRIORITY_CLASS;
  510. break;
  511. case 1:
  512. dwPriorityClass = NORMAL_PRIORITY_CLASS;
  513. break;
  514. case 2:
  515. dwPriorityClass = HIGH_PRIORITY_CLASS;
  516. break;
  517. }
  518. SetPriorityClass (GetCurrentProcess(), dwPriorityClass);
  519. }
  520. void CStressDlg::OnKillfocusSharedMemSize()
  521. {
  522. return;
  523. }
  524. void CStressDlg::OnChangeSharedMemSize()
  525. {
  526. CString csMemSize;
  527. TCHAR szOldValue[MAX_PATH];
  528. DWORD dwMemSize;
  529. csMemSize.Empty();
  530. GetDlgItemText (IDC_SHARED_MEM_SIZE, csMemSize);
  531. dwMemSize = _tcstoul ((LPCTSTR)csMemSize, NULL, 10);
  532. dwMemSize *= 1024 / sizeof(DWORD);
  533. if (dwMemSize != m_dwVASize) {
  534. // threads must be stopped to change memory size
  535. if (m_Active[0] || m_Active[1] || m_Active[2] || m_Active[3]) {
  536. MessageBox (
  537. TEXT("All threads must be stopped before this value can be changed"));
  538. _stprintf (szOldValue, TEXT("%d"), ((m_dwVASize * sizeof(DWORD))/1024));
  539. (GetDlgItem(IDC_SHARED_MEM_SIZE))->SetWindowText(szOldValue);
  540. return;
  541. } else {
  542. if (m_pMemory != NULL) {
  543. delete (m_pMemory);
  544. m_pMemory = NULL;
  545. m_dwVASize = 0;
  546. }
  547. m_dwVASize = dwMemSize;
  548. m_pMemory = new DWORD[m_dwVASize];
  549. if (m_pMemory == NULL) {
  550. m_dwVASize = 0;
  551. }
  552. }
  553. }
  554. }
  555. void CStressDlg::OnUseMemory()
  556. {
  557. CString csMemSize;
  558. DWORD dwMemSize;
  559. BOOL bState;
  560. if (m_Active[0] || m_Active[1] || m_Active[2] || m_Active[3]) {
  561. MessageBox (TEXT("All threads must be stopped before this value can be changed"));
  562. bState = !IsDlgButtonChecked(IDC_USE_MEMORY); //revert state
  563. } else {
  564. if (IsDlgButtonChecked(IDC_USE_MEMORY)) {
  565. // if no memory is allocated, then allocate it
  566. if (m_pMemory != NULL) {
  567. delete (m_pMemory);
  568. m_pMemory = NULL;
  569. m_dwVASize = 0;
  570. }
  571. //get va size
  572. csMemSize.Empty();
  573. GetDlgItemText (IDC_SHARED_MEM_SIZE, csMemSize);
  574. dwMemSize = _tcstoul ((LPCTSTR)csMemSize, NULL, 10);
  575. m_dwVASize = dwMemSize * 1024 / sizeof(DWORD);
  576. m_pMemory = new DWORD[m_dwVASize];
  577. if (m_pMemory == NULL) {
  578. m_dwVASize = 0;
  579. } else {
  580. m_dwRandomScale = (m_dwVASize / RAND_MAX) + 1;
  581. }
  582. bState = TRUE;
  583. } else {
  584. // button is unchecked so free memory
  585. if (m_pMemory != NULL) {
  586. delete (m_pMemory);
  587. m_pMemory = NULL;
  588. m_dwVASize = 0;
  589. }
  590. bState = FALSE;
  591. }
  592. }
  593. CheckDlgButton (IDC_USE_MEMORY, bState);
  594. (GetDlgItem (IDC_SHARED_MEM_SIZE))->EnableWindow (bState);
  595. }
  596. void CStressDlg::OnClose()
  597. {
  598. // stop threads first
  599. m_Active[0] = FALSE;
  600. m_Active[1] = FALSE;
  601. m_Active[2] = FALSE;
  602. m_Active[3] = FALSE;
  603. // wait for the threads to finish
  604. while (m_ThreadHandle[0] || m_ThreadHandle[1] ||
  605. m_ThreadHandle[2] || m_ThreadHandle[3]) {
  606. Sleep(100);
  607. }
  608. // free memory block
  609. if (m_pMemory != NULL) {
  610. delete (m_pMemory);
  611. m_pMemory = NULL;
  612. m_dwVASize = 0;
  613. }
  614. CDialog::OnClose();
  615. }
  616. void CStressDlg::OnDestroy()
  617. {
  618. CDialog::OnDestroy();
  619. }