Fixed obvious typos.
[reactos.git] / rosapps / taskmgr / PerformancePage.cpp
1 /*
2 * ReactOS Task Manager
3 *
4 * performancepage.cpp
5 *
6 * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "stdafx.h"
24 #include "TASKMGR.h"
25 #include "PerformancePage.h"
26 #include "perfdata.h"
27 #include "graph.h"
28
29 HWND hPerformancePage; // Performance Property Page
30
31 HWND hPerformancePageCpuUsageGraph; // CPU Usage Graph
32 HWND hPerformancePageMemUsageGraph; // MEM Usage Graph
33 HWND hPerformancePageMemUsageHistoryGraph; // Memory Usage History Graph
34
35 HWND hPerformancePageTotalsFrame; // Totals Frame
36 HWND hPerformancePageCommitChargeFrame; // Commit Charge Frame
37 HWND hPerformancePageKernelMemoryFrame; // Kernel Memory Frame
38 HWND hPerformancePagePhysicalMemoryFrame; // Physical Memory Frame
39
40 HWND hPerformancePageCommitChargeTotalEdit; // Commit Charge Total Edit Control
41 HWND hPerformancePageCommitChargeLimitEdit; // Commit Charge Limit Edit Control
42 HWND hPerformancePageCommitChargePeakEdit; // Commit Charge Peak Edit Control
43
44 HWND hPerformancePageKernelMemoryTotalEdit; // Kernel Memory Total Edit Control
45 HWND hPerformancePageKernelMemoryPagedEdit; // Kernel Memory Paged Edit Control
46 HWND hPerformancePageKernelMemoryNonPagedEdit; // Kernel Memory NonPaged Edit Control
47
48 HWND hPerformancePagePhysicalMemoryTotalEdit; // Physical Memory Total Edit Control
49 HWND hPerformancePagePhysicalMemoryAvailableEdit; // Physical Memory Available Edit Control
50 HWND hPerformancePagePhysicalMemorySystemCacheEdit; // Physical Memory System Cache Edit Control
51
52 HWND hPerformancePageTotalsHandleCountEdit; // Total Handles Edit Control
53 HWND hPerformancePageTotalsProcessCountEdit; // Total Processes Edit Control
54 HWND hPerformancePageTotalsThreadCountEdit; // Total Threads Edit Control
55
56 static int nPerformancePageWidth;
57 static int nPerformancePageHeight;
58
59 static HANDLE hPerformancePageEvent = NULL; // When this event becomes signaled then we refresh the performance page
60
61 void PerformancePageRefreshThread(void *lpParameter);
62
63 LRESULT CALLBACK PerformancePageWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
64 {
65 RECT rc;
66 int nXDifference;
67 int nYDifference;
68
69 switch (message)
70 {
71 case WM_INITDIALOG:
72
73 // Save the width and height
74 GetClientRect(hDlg, &rc);
75 nPerformancePageWidth = rc.right;
76 nPerformancePageHeight = rc.bottom;
77
78 // Update window position
79 SetWindowPos(hDlg, NULL, 15, 30, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
80
81 //
82 // Get handles to all the controls
83 //
84 hPerformancePageTotalsFrame = GetDlgItem(hDlg, IDC_TOTALS_FRAME);
85 hPerformancePageCommitChargeFrame = GetDlgItem(hDlg, IDC_COMMIT_CHARGE_FRAME);
86 hPerformancePageKernelMemoryFrame = GetDlgItem(hDlg, IDC_KERNEL_MEMORY_FRAME);
87 hPerformancePagePhysicalMemoryFrame = GetDlgItem(hDlg, IDC_PHYSICAL_MEMORY_FRAME);
88 hPerformancePageCommitChargeTotalEdit = GetDlgItem(hDlg, IDC_COMMIT_CHARGE_TOTAL);
89 hPerformancePageCommitChargeLimitEdit = GetDlgItem(hDlg, IDC_COMMIT_CHARGE_LIMIT);
90 hPerformancePageCommitChargePeakEdit = GetDlgItem(hDlg, IDC_COMMIT_CHARGE_PEAK);
91 hPerformancePageKernelMemoryTotalEdit = GetDlgItem(hDlg, IDC_KERNEL_MEMORY_TOTAL);
92 hPerformancePageKernelMemoryPagedEdit = GetDlgItem(hDlg, IDC_KERNEL_MEMORY_PAGED);
93 hPerformancePageKernelMemoryNonPagedEdit = GetDlgItem(hDlg, IDC_KERNEL_MEMORY_NONPAGED);
94 hPerformancePagePhysicalMemoryTotalEdit = GetDlgItem(hDlg, IDC_PHYSICAL_MEMORY_TOTAL);
95 hPerformancePagePhysicalMemoryAvailableEdit = GetDlgItem(hDlg, IDC_PHYSICAL_MEMORY_AVAILABLE);
96 hPerformancePagePhysicalMemorySystemCacheEdit = GetDlgItem(hDlg, IDC_PHYSICAL_MEMORY_SYSTEM_CACHE);
97 hPerformancePageTotalsHandleCountEdit = GetDlgItem(hDlg, IDC_TOTALS_HANDLE_COUNT);
98 hPerformancePageTotalsProcessCountEdit = GetDlgItem(hDlg, IDC_TOTALS_PROCESS_COUNT);
99 hPerformancePageTotalsThreadCountEdit = GetDlgItem(hDlg, IDC_TOTALS_THREAD_COUNT);
100 hPerformancePageCpuUsageGraph = GetDlgItem(hDlg, IDC_CPU_USAGE_GRAPH);
101 hPerformancePageMemUsageGraph = GetDlgItem(hDlg, IDC_MEM_USAGE_GRAPH);
102 hPerformancePageMemUsageHistoryGraph = GetDlgItem(hDlg, IDC_MEM_USAGE_HISTORY_GRAPH);
103
104 // Start our refresh thread
105 _beginthread(PerformancePageRefreshThread, 0, NULL);
106
107 //
108 // Subclass graph buttons
109 //
110 OldGraphWndProc = SetWindowLong(hPerformancePageCpuUsageGraph, GWL_WNDPROC, (LONG)Graph_WndProc);
111 SetWindowLong(hPerformancePageMemUsageGraph, GWL_WNDPROC, (LONG)Graph_WndProc);
112 SetWindowLong(hPerformancePageMemUsageHistoryGraph, GWL_WNDPROC, (LONG)Graph_WndProc);
113
114 return TRUE;
115
116 case WM_COMMAND:
117 break;
118
119 case WM_SIZE:
120 int cx, cy;
121
122 if (wParam == SIZE_MINIMIZED)
123 return 0;
124
125 cx = LOWORD(lParam);
126 cy = HIWORD(lParam);
127 nXDifference = cx - nPerformancePageWidth;
128 nYDifference = cy - nPerformancePageHeight;
129 nPerformancePageWidth = cx;
130 nPerformancePageHeight = cy;
131
132 // Reposition the performance page's controls
133 /*GetWindowRect(hApplicationPageListCtrl, &rc);
134 cx = (rc.right - rc.left) + nXDifference;
135 cy = (rc.bottom - rc.top) + nYDifference;
136 SetWindowPos(hApplicationPageListCtrl, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
137 InvalidateRect(hApplicationPageListCtrl, NULL, TRUE);
138
139 GetClientRect(hApplicationPageEndTaskButton, &rc);
140 MapWindowPoints(hApplicationPageEndTaskButton, hDlg, (LPPOINT)(&rc), (sizeof(RECT)/sizeof(POINT)) );
141 cx = rc.left + nXDifference;
142 cy = rc.top + nYDifference;
143 SetWindowPos(hApplicationPageEndTaskButton, NULL, cx, cy, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
144 InvalidateRect(hApplicationPageEndTaskButton, NULL, TRUE);
145
146 GetClientRect(hApplicationPageSwitchToButton, &rc);
147 MapWindowPoints(hApplicationPageSwitchToButton, hDlg, (LPPOINT)(&rc), (sizeof(RECT)/sizeof(POINT)) );
148 cx = rc.left + nXDifference;
149 cy = rc.top + nYDifference;
150 SetWindowPos(hApplicationPageSwitchToButton, NULL, cx, cy, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
151 InvalidateRect(hApplicationPageSwitchToButton, NULL, TRUE);
152
153 GetClientRect(hApplicationPageNewTaskButton, &rc);
154 MapWindowPoints(hApplicationPageNewTaskButton, hDlg, (LPPOINT)(&rc), (sizeof(RECT)/sizeof(POINT)) );
155 cx = rc.left + nXDifference;
156 cy = rc.top + nYDifference;
157 SetWindowPos(hApplicationPageNewTaskButton, NULL, cx, cy, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
158 InvalidateRect(hApplicationPageNewTaskButton, NULL, TRUE);*/
159
160 break;
161 }
162
163 return 0;
164 }
165
166 void RefreshPerformancePage(void)
167 {
168 // Signal the event so that our refresh thread
169 // will wake up and refresh the performance page
170 SetEvent(hPerformancePageEvent);
171 }
172
173 void PerformancePageRefreshThread(void *lpParameter)
174 {
175 ULONG CommitChargeTotal;
176 ULONG CommitChargeLimit;
177 ULONG CommitChargePeak;
178
179 ULONG KernelMemoryTotal;
180 ULONG KernelMemoryPaged;
181 ULONG KernelMemoryNonPaged;
182
183 ULONG PhysicalMemoryTotal;
184 ULONG PhysicalMemoryAvailable;
185 ULONG PhysicalMemorySystemCache;
186
187 ULONG TotalHandles;
188 ULONG TotalThreads;
189 ULONG TotalProcesses;
190
191 TCHAR Text[260];
192
193 // Create the event
194 hPerformancePageEvent = CreateEvent(NULL, TRUE, TRUE, "Performance Page Event");
195
196 // If we couldn't create the event then exit the thread
197 if (!hPerformancePageEvent)
198 return;
199
200 while (1)
201 {
202 DWORD dwWaitVal;
203
204 // Wait on the event
205 dwWaitVal = WaitForSingleObject(hPerformancePageEvent, INFINITE);
206
207 // If the wait failed then the event object must have been
208 // closed and the task manager is exiting so exit this thread
209 if (dwWaitVal == WAIT_FAILED)
210 return;
211
212 if (dwWaitVal == WAIT_OBJECT_0)
213 {
214 // Reset our event
215 ResetEvent(hPerformancePageEvent);
216
217 //
218 // Update the commit charge info
219 //
220 CommitChargeTotal = PerfDataGetCommitChargeTotalK();
221 CommitChargeLimit = PerfDataGetCommitChargeLimitK();
222 CommitChargePeak = PerfDataGetCommitChargePeakK();
223 _ultot(CommitChargeTotal, Text, 10);
224 SetWindowText(hPerformancePageCommitChargeTotalEdit, Text);
225 _ultot(CommitChargeLimit, Text, 10);
226 SetWindowText(hPerformancePageCommitChargeLimitEdit, Text);
227 _ultot(CommitChargePeak, Text, 10);
228 SetWindowText(hPerformancePageCommitChargePeakEdit, Text);
229 wsprintf(Text, _T("Mem Usage: %dK / %dK"), CommitChargeTotal, CommitChargeLimit);
230 SendMessage(hStatusWnd, SB_SETTEXT, 2, (LPARAM)Text);
231
232 //
233 // Update the kernel memory info
234 //
235 KernelMemoryTotal = PerfDataGetKernelMemoryTotalK();
236 KernelMemoryPaged = PerfDataGetKernelMemoryPagedK();
237 KernelMemoryNonPaged = PerfDataGetKernelMemoryNonPagedK();
238 _ultot(KernelMemoryTotal, Text, 10);
239 SetWindowText(hPerformancePageKernelMemoryTotalEdit, Text);
240 _ultot(KernelMemoryPaged, Text, 10);
241 SetWindowText(hPerformancePageKernelMemoryPagedEdit, Text);
242 _ultot(KernelMemoryNonPaged, Text, 10);
243 SetWindowText(hPerformancePageKernelMemoryNonPagedEdit, Text);
244
245 //
246 // Update the physical memory info
247 //
248 PhysicalMemoryTotal = PerfDataGetPhysicalMemoryTotalK();
249 PhysicalMemoryAvailable = PerfDataGetPhysicalMemoryAvailableK();
250 PhysicalMemorySystemCache = PerfDataGetPhysicalMemorySystemCacheK();
251 _ultot(PhysicalMemoryTotal, Text, 10);
252 SetWindowText(hPerformancePagePhysicalMemoryTotalEdit, Text);
253 _ultot(PhysicalMemoryAvailable, Text, 10);
254 SetWindowText(hPerformancePagePhysicalMemoryAvailableEdit, Text);
255 _ultot(PhysicalMemorySystemCache, Text, 10);
256 SetWindowText(hPerformancePagePhysicalMemorySystemCacheEdit, Text);
257
258 //
259 // Update the totals info
260 //
261 TotalHandles = PerfDataGetSystemHandleCount();
262 TotalThreads = PerfDataGetTotalThreadCount();
263 TotalProcesses = PerfDataGetProcessCount();
264 _ultot(TotalHandles, Text, 10);
265 SetWindowText(hPerformancePageTotalsHandleCountEdit, Text);
266 _ultot(TotalThreads, Text, 10);
267 SetWindowText(hPerformancePageTotalsThreadCountEdit, Text);
268 _ultot(TotalProcesses, Text, 10);
269 SetWindowText(hPerformancePageTotalsProcessCountEdit, Text);
270
271 //
272 // Redraw the graphs
273 //
274 InvalidateRect(hPerformancePageCpuUsageGraph, NULL, FALSE);
275 InvalidateRect(hPerformancePageMemUsageGraph, NULL, FALSE);
276 InvalidateRect(hPerformancePageMemUsageHistoryGraph, NULL, FALSE);
277 }
278 }
279 }
280
281 void PerformancePage_OnViewShowKernelTimes(void)
282 {
283 HMENU hMenu;
284 HMENU hViewMenu;
285
286 hMenu = GetMenu(hMainWnd);
287 hViewMenu = GetSubMenu(hMenu, 2);
288
289 // Check or uncheck the show 16-bit tasks menu item
290 if (GetMenuState(hViewMenu, ID_VIEW_SHOWKERNELTIMES, MF_BYCOMMAND) & MF_CHECKED)
291 {
292 CheckMenuItem(hViewMenu, ID_VIEW_SHOWKERNELTIMES, MF_BYCOMMAND|MF_UNCHECKED);
293 TaskManagerSettings.ShowKernelTimes = FALSE;
294 }
295 else
296 {
297 CheckMenuItem(hViewMenu, ID_VIEW_SHOWKERNELTIMES, MF_BYCOMMAND|MF_CHECKED);
298 TaskManagerSettings.ShowKernelTimes = TRUE;
299 }
300
301 RefreshPerformancePage();
302 }
303
304 void PerformancePage_OnViewCPUHistoryOneGraphAll(void)
305 {
306 HMENU hMenu;
307 HMENU hViewMenu;
308 HMENU hCPUHistoryMenu;
309
310 hMenu = GetMenu(hMainWnd);
311 hViewMenu = GetSubMenu(hMenu, 2);
312 hCPUHistoryMenu = GetSubMenu(hViewMenu, 3);
313
314 TaskManagerSettings.CPUHistory_OneGraphPerCPU = FALSE;
315 CheckMenuRadioItem(hCPUHistoryMenu, ID_VIEW_CPUHISTORY_ONEGRAPHALL, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, ID_VIEW_CPUHISTORY_ONEGRAPHALL, MF_BYCOMMAND);
316 }
317
318 void PerformancePage_OnViewCPUHistoryOneGraphPerCPU(void)
319 {
320 HMENU hMenu;
321 HMENU hViewMenu;
322 HMENU hCPUHistoryMenu;
323
324 hMenu = GetMenu(hMainWnd);
325 hViewMenu = GetSubMenu(hMenu, 2);
326 hCPUHistoryMenu = GetSubMenu(hViewMenu, 3);
327
328 TaskManagerSettings.CPUHistory_OneGraphPerCPU = TRUE;
329 CheckMenuRadioItem(hCPUHistoryMenu, ID_VIEW_CPUHISTORY_ONEGRAPHALL, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, MF_BYCOMMAND);
330 }
331