Leaked source code of windows server 2003
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.

446 lines
13 KiB

  1. // Copyright (c) 1997-1999 Microsoft Corporation
  2. #include "precomp.h"
  3. #ifdef EXT_DEBUG
  4. #undef THIS_FILE
  5. static char THIS_FILE[] = __FILE__;
  6. #endif
  7. #include "PerfPage.h"
  8. // avoid some warnings.
  9. #undef HDS_HORZ
  10. #undef HDS_BUTTONS
  11. #undef HDS_HIDDEN
  12. #include "resource.h"
  13. #include <stdlib.h>
  14. #include <TCHAR.h>
  15. #include "..\Common\util.h"
  16. #include <windowsx.h>
  17. #include "helpid.h"
  18. DWORD aPerformanceHelpIds[] = {
  19. IDOK, IDH_NO_HELP,
  20. IDCANCEL, IDH_NO_HELP,
  21. IDC_PERF_VM_ALLOCD, (IDH_PERF + 1),
  22. IDC_PERF_VM_ALLOCD_LABEL, (IDH_PERF + 1),
  23. IDC_PERF_GROUP, -1,
  24. IDC_PERF_TEXT, (IDH_PERF + 3),
  25. IDC_PERF_TEXT2, -1,
  26. IDC_PERF_VM_ALLOCD_TEXT, -1,
  27. IDC_PERF_WORKSTATION, (IDH_PERF + 4),
  28. IDC_PERF_SERVER, (IDH_PERF + 5),
  29. IDC_PERF_VM_GROUP, -1,
  30. IDC_PERF_VM_ALLOCD_TEXT, -1,
  31. IDC_PERF_CHANGE, (IDH_PERF + 7),
  32. IDC_PERF_CACHE_GROUP, -1,
  33. IDC_PERF_CACHE_TEXT, -1,
  34. IDC_PERF_CACHE_TEXT2, -1,
  35. IDC_PERF_APPS, (IDH_PERF + 14),
  36. IDC_PERF_SYSCACHE, (IDH_PERF + 15),
  37. 0, 0
  38. };
  39. #define PROCESS_PRIORITY_SEPARATION_MIN 0
  40. #define PROCESS_PRIORITY_SEPARATION_MAX 2
  41. #define PERF_TYPEVARIABLE 1
  42. #define PERF_TYPEFIXED 2
  43. #define PERF_LENLONG 1
  44. #define PERF_LENSHORT 2
  45. #define OPTIMIZE_APPS 0
  46. #define OPTIMIZE_CACHE 1
  47. INT_PTR CALLBACK StaticPerfDlgProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
  48. {
  49. // if this is the initDlg msg...
  50. if(message == WM_INITDIALOG)
  51. {
  52. // transfer the 'this' ptr to the extraBytes.
  53. SetWindowLongPtr(hwndDlg, DWLP_USER, lParam);
  54. }
  55. // DWL_USER is the 'this' ptr.
  56. PerfPage *me = (PerfPage *)GetWindowLongPtr(hwndDlg, DWLP_USER);
  57. if(me != NULL)
  58. {
  59. // call into the DlgProc() that has some context.
  60. return me->DlgProc(hwndDlg, message, wParam, lParam);
  61. }
  62. else
  63. {
  64. return FALSE;
  65. }
  66. }
  67. //--------------------------------------------------------------
  68. PerfPage::PerfPage(WbemServiceThread *serviceThread)
  69. : WBEMPageHelper(serviceThread)
  70. {
  71. m_dwPrevCacheOption = m_dwCurCacheOption = 0;
  72. m_nowWorkstation = m_wasWorkstation = false;
  73. IWbemClassObject *pInst = NULL;
  74. // NOTE: This one's a little different. I create it right away so
  75. // I can use it as a helper even before I put up its' dlg.
  76. m_VDlg = new VirtualMemDlg(m_serviceThread);
  77. // its all in one class.
  78. if((pInst = FirstInstanceOf("Win32_OperatingSystem")) != NULL)
  79. {
  80. m_os = pInst;
  81. }
  82. }
  83. //--------------------------------------------------------------
  84. INT_PTR PerfPage::DoModal(HWND hDlg)
  85. {
  86. return DialogBoxParam(HINST_THISDLL,
  87. (LPTSTR) MAKEINTRESOURCE(IDD_PERFORMANCE),
  88. hDlg, StaticPerfDlgProc, (LPARAM)this);
  89. }
  90. //--------------------------------------------------------------
  91. PerfPage::~PerfPage()
  92. {
  93. delete m_VDlg;
  94. }
  95. //--------------------------------------------------------------
  96. INT_PTR CALLBACK PerfPage::DlgProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
  97. {
  98. static BOOL fVMInited = FALSE;
  99. DWORD dw;
  100. HRESULT hr = 0;
  101. BOOL bDirty = FALSE;
  102. m_hDlg = hwndDlg;
  103. switch (message)
  104. {
  105. case WM_INITDIALOG:
  106. Init();
  107. return TRUE;
  108. break;
  109. case WM_DESTROY:
  110. // If the dialog box is going away, then close the
  111. // registry key.
  112. if (fVMInited)
  113. {
  114. // VirtualFreeStructures();
  115. }
  116. break;
  117. case WM_COMMAND:
  118. switch (LOWORD(wParam))
  119. {
  120. case IDC_PERF_CHANGE:
  121. dw = m_VDlg->DoModal(m_hDlg);
  122. if (fVMInited)
  123. {
  124. unsigned long val = 0;
  125. m_VDlg->ComputeAllocated(&val);
  126. SetDlgItemMB(m_hDlg, IDC_PERF_VM_ALLOCD, val);
  127. }
  128. /*****************
  129. if((dw != RET_NO_CHANGE) &&
  130. (dw != RET_CHANGE_NO_REBOOT))
  131. {
  132. MsgBoxParam(m_hDlg, SYSTEM + 39, IDS_TITLE,
  133. MB_OK | MB_ICONINFORMATION);
  134. g_fRebootRequired = TRUE;
  135. }
  136. ********************/
  137. break;
  138. case IDC_PERF_WORKSTATION:
  139. if(BN_CLICKED == HIWORD(wParam))
  140. {
  141. m_nowWorkstation = true;
  142. // Workstations have maximum foreground boost
  143. m_appBoost = PROCESS_PRIORITY_SEPARATION_MAX;
  144. // Workstations have variable, short quanta
  145. m_quantLength = PERF_LENSHORT;
  146. m_quantType = PERF_TYPEVARIABLE;
  147. }
  148. break;
  149. case IDC_PERF_SERVER:
  150. if(BN_CLICKED == HIWORD(wParam))
  151. {
  152. m_nowWorkstation = false;
  153. // Servers have minimum foreground boost
  154. m_appBoost = PROCESS_PRIORITY_SEPARATION_MIN;
  155. // Servers have fixed, long quanta
  156. m_quantLength = PERF_LENLONG;
  157. m_quantType = PERF_TYPEFIXED;
  158. }
  159. break;
  160. case IDC_PERF_APPS:
  161. if(BN_CLICKED == HIWORD(wParam))
  162. {
  163. m_dwCurCacheOption = OPTIMIZE_APPS;
  164. g_fRebootRequired = (m_dwCurCacheOption != m_dwPrevCacheOption);
  165. }
  166. break;
  167. case IDC_PERF_SYSCACHE:
  168. if(BN_CLICKED == HIWORD(wParam))
  169. {
  170. m_dwCurCacheOption = OPTIMIZE_CACHE;
  171. g_fRebootRequired = (m_dwCurCacheOption != m_dwPrevCacheOption);
  172. }
  173. break;
  174. case IDOK:
  175. if (m_wasWorkstation != m_nowWorkstation) // Change?
  176. {
  177. if((bool)m_os)
  178. {
  179. hr = m_os.Put(_T("ForegroundApplicationBoost"),
  180. variant_t((BYTE)m_appBoost));
  181. hr = m_os.Put(_T("QuantumType"),
  182. variant_t((BYTE)m_quantType));
  183. hr = m_os.Put(_T("QuantumLength"),
  184. variant_t((BYTE)m_quantLength));
  185. bDirty = TRUE;
  186. }
  187. else
  188. {
  189. MsgBoxParam(m_hDlg, IDS_LOST_CONNECTION, IDS_TITLE,
  190. MB_OK | MB_ICONINFORMATION);
  191. EndDialog(m_hDlg, 0);
  192. }
  193. }
  194. if (m_dwPrevCacheOption != m_dwCurCacheOption) // Change?
  195. {
  196. if((bool)m_os)
  197. {
  198. hr = m_os.Put(_T("LargeSystemCache"),
  199. (long)m_dwCurCacheOption);
  200. if (SUCCEEDED(hr))
  201. {
  202. bDirty = TRUE;
  203. MsgBoxParam(m_hDlg, IDS_MUST_REBOOT, IDS_SYS_CHANGE_CAPTION, MB_OK | MB_ICONINFORMATION, NULL, NULL);
  204. }
  205. }
  206. else
  207. {
  208. MsgBoxParam(m_hDlg, IDS_LOST_CONNECTION, IDS_TITLE,
  209. MB_OK | MB_ICONINFORMATION);
  210. EndDialog(m_hDlg, 0);
  211. }
  212. }
  213. if (bDirty)
  214. {
  215. hr = m_WbemServices.PutInstance(m_os,
  216. WBEM_FLAG_CREATE_OR_UPDATE);
  217. }
  218. EndDialog(m_hDlg, 0);
  219. break;
  220. case IDCANCEL:
  221. EndDialog(m_hDlg, 0);
  222. break;
  223. default:
  224. break;
  225. } //endswitch LOWORD
  226. break;
  227. case WM_HELP: // F1
  228. ::WinHelp((HWND)((LPHELPINFO)lParam)->hItemHandle,
  229. L"sysdm.hlp",
  230. HELP_WM_HELP,
  231. (ULONG_PTR)(LPSTR)aPerformanceHelpIds);
  232. break;
  233. case WM_CONTEXTMENU: // right mouse click
  234. WinHelp((HWND)wParam, HELP_FILE, HELP_CONTEXTMENU,
  235. (ULONG_PTR)(LPSTR)aPerformanceHelpIds);
  236. break;
  237. default:
  238. return FALSE;
  239. }
  240. return FALSE;
  241. }
  242. //--------------------------------------------------------------------
  243. // TCHAR szPriKey[] = TEXT("System\\CurrentControlSet\\Control\\PriorityControl");
  244. TCHAR szPriMemoryKey[] = TEXT("System\\CurrentControlSet\\Control\\Session Manager\\Memory Management");
  245. void PerfPage::Init(void)
  246. {
  247. HRESULT h1 = 0, h2 = 0, h3 = 0;
  248. HRESULT hr;
  249. // if anything goes wrong, act like a server.
  250. m_appBoost = PROCESS_PRIORITY_SEPARATION_MIN;
  251. m_quantType = PERF_TYPEVARIABLE;
  252. m_quantLength = PERF_LENLONG;
  253. // if the class was found...
  254. if((bool)m_os)
  255. {
  256. // NOTE: I want the return codes. Dont use GetLong() is the case.
  257. h1 = m_os.Get(_T("QuantumType"), m_quantType);
  258. h2 = m_os.Get(_T("QuantumLength"), m_quantLength);
  259. h3 = m_os.Get(_T("ForegroundApplicationBoost"), m_appBoost);
  260. // did it all work?
  261. if((h1 == 0) && (h2 == 0) && (h3 == 0))
  262. {
  263. bool bWorkstation;
  264. // if it's never been set, quantLength & quantType are zero.
  265. // - determine whether we're on wks or srv
  266. if ((m_quantLength == 0) && (m_quantType == 0))
  267. {
  268. bWorkstation = true;
  269. m_quantLength = PERF_LENSHORT;
  270. m_quantType = PERF_TYPEVARIABLE;
  271. IWbemClassObject *pInst = NULL;
  272. if (pInst = FirstInstanceOf("Win32_ComputerSystem"))
  273. {
  274. CWbemClassObject obj = pInst;
  275. long role;
  276. obj.Get((bstr_t)"DomainRole", role);
  277. if (role > 1)
  278. {
  279. bWorkstation = false;
  280. m_quantLength = PERF_LENLONG;
  281. m_quantType = PERF_TYPEFIXED;
  282. }
  283. pInst->Release();
  284. }
  285. // else - we just use defaults from above
  286. }
  287. else if ((m_quantLength == PERF_LENSHORT) &&
  288. (m_quantType == PERF_TYPEVARIABLE))
  289. bWorkstation = true;
  290. else
  291. bWorkstation = false;
  292. //-----------------------------------------
  293. // Short, Variable Quanta (or 2 zeros) == Workstation-like interactive response.
  294. // Long, Fixed Quanta == Server-like interactive response.
  295. if(bWorkstation)
  296. {
  297. m_appBoost = PROCESS_PRIORITY_SEPARATION_MAX;
  298. // to optimize Puts later.
  299. m_nowWorkstation = m_wasWorkstation = true;
  300. Button_SetCheck(GetDlgItem(m_hDlg, IDC_PERF_WORKSTATION),
  301. BST_CHECKED);
  302. Button_SetCheck(GetDlgItem(m_hDlg, IDC_PERF_SERVER),
  303. BST_UNCHECKED);
  304. }
  305. else // its a server.
  306. {
  307. m_appBoost = PROCESS_PRIORITY_SEPARATION_MIN;
  308. m_nowWorkstation = m_wasWorkstation = false;
  309. Button_SetCheck(GetDlgItem(m_hDlg, IDC_PERF_WORKSTATION),
  310. BST_UNCHECKED);
  311. Button_SetCheck(GetDlgItem(m_hDlg, IDC_PERF_SERVER),
  312. BST_CHECKED);
  313. }
  314. BOOL writable = TRUE;
  315. // NOTE: for backwards compability with wmi builds that didn't have this
  316. // method (in RemoteRegWriteable()), assume 'true' unless a newer build says you cant do this.
  317. RemoteRegWriteable(szPriMemoryKey, writable);
  318. ::EnableWindow(GetDlgItem(m_hDlg, IDC_PERF_APPS), writable);
  319. ::EnableWindow(GetDlgItem(m_hDlg, IDC_PERF_SYSCACHE), writable);
  320. ::EnableWindow(GetDlgItem(m_hDlg, IDC_PERF_WORKSTATION), writable);
  321. ::EnableWindow(GetDlgItem(m_hDlg, IDC_PERF_SERVER), writable);
  322. } // endif it worked
  323. }
  324. else
  325. {
  326. ::EnableWindow(GetDlgItem(m_hDlg, IDC_PERF_WORKSTATION), FALSE);
  327. ::EnableWindow(GetDlgItem(m_hDlg, IDC_PERF_SERVER), FALSE);
  328. ::EnableWindow(GetDlgItem(m_hDlg, IDC_PERF_APPS), FALSE);
  329. ::EnableWindow(GetDlgItem(m_hDlg, IDC_PERF_SYSCACHE), FALSE);
  330. } //endif class was found.
  331. //
  332. // Get LargeSystemCache property and set controls correspondingly.
  333. //
  334. hr = m_os.Get(_T("LargeSystemCache"), (long&)m_dwPrevCacheOption);
  335. m_dwCurCacheOption = m_dwPrevCacheOption;
  336. if (SUCCEEDED(hr))
  337. {
  338. if (m_dwPrevCacheOption == OPTIMIZE_APPS)
  339. {
  340. Button_SetCheck(GetDlgItem(m_hDlg, IDC_PERF_APPS),
  341. BST_CHECKED);
  342. Button_SetCheck(GetDlgItem(m_hDlg, IDC_PERF_SYSCACHE),
  343. BST_UNCHECKED);
  344. }
  345. else if (m_dwPrevCacheOption == OPTIMIZE_CACHE)
  346. {
  347. Button_SetCheck(GetDlgItem(m_hDlg, IDC_PERF_APPS),
  348. BST_UNCHECKED);
  349. Button_SetCheck(GetDlgItem(m_hDlg, IDC_PERF_SYSCACHE),
  350. BST_CHECKED);
  351. }
  352. else // Unsupported/unknown value - disable the controls.
  353. {
  354. ::EnableWindow(GetDlgItem(m_hDlg, IDC_PERF_APPS), FALSE);
  355. ::EnableWindow(GetDlgItem(m_hDlg, IDC_PERF_SYSCACHE), FALSE);
  356. }
  357. }
  358. else
  359. {
  360. ::EnableWindow(GetDlgItem(m_hDlg, IDC_PERF_APPS), FALSE);
  361. ::EnableWindow(GetDlgItem(m_hDlg, IDC_PERF_SYSCACHE), FALSE);
  362. }
  363. // Init the virtual memory part.
  364. unsigned long vAlloc = 0;
  365. bool enable = m_VDlg->ComputeAllocated(&vAlloc);
  366. if(enable)
  367. {
  368. SetDlgItemMB( m_hDlg, IDC_PERF_VM_ALLOCD, vAlloc );
  369. }
  370. else
  371. {
  372. ::EnableWindow(GetDlgItem(m_hDlg, IDC_PERF_VM_ALLOCD_LABEL), FALSE);
  373. ::EnableWindow(GetDlgItem(m_hDlg, IDC_PERF_VM_ALLOCD), FALSE);
  374. ::EnableWindow(GetDlgItem(m_hDlg, IDC_PERF_CHANGE), FALSE);
  375. MsgBoxParam(m_hDlg, IDS_NO_VM, IDS_TITLE, MB_OK|MB_ICONWARNING);
  376. }
  377. }