-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathTaskManagerBitmap.cpp
313 lines (271 loc) · 9.99 KB
/
TaskManagerBitmap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//--------------------------------------------------------------------------------
//
// TaskManagerBitmap
//
// By Mark Russinovich
// December 2020
//
// Takes a bitmap that is the size or width of Task Managers's CPU core view (ideally with single cells
// showing instantaneous percent CPU) and scrolls the bitmap using threads that consume CPU
// based on the pixel's greyscale (black is 100% CPU)
//
//--------------------------------------------------------------------------------
#include <Windows.h>
#include <processthreadsapi.h>
#include <iostream>
// Shades of grey
#define GREYSCALE 8
volatile DWORD* CpuPixels;
typedef struct {
DWORD ProcessId;
HWND hWnd;
} HWND_CONTEXT, * PHWND_CONTEXT;
//--------------------------------------------------------------------------------
//
// IsMainWindow
//
// Is window parentless and visible.
//
//--------------------------------------------------------------------------------
BOOL IsMainWindow(
HWND handle
)
{
return GetWindow( handle, GW_OWNER ) == (HWND)0 && IsWindowVisible( handle );
}
//--------------------------------------------------------------------------------
//
// EnumWindowsCallback
//
// Scan windows of process looking for main one.
//
//--------------------------------------------------------------------------------
BOOL CALLBACK EnumWindowsCallback(
HWND handle,
LPARAM lParam
)
{
PHWND_CONTEXT context = (PHWND_CONTEXT)lParam;
DWORD processId = 0;
GetWindowThreadProcessId( handle, &processId );
if( context->ProcessId != processId || !IsMainWindow( handle ) )
return TRUE;
context->hWnd = handle;
return FALSE;
}
//--------------------------------------------------------------------------------
//
// FindMainWindow
//
// Enumerate windows.
//
//--------------------------------------------------------------------------------
HWND FindMainWindow(
DWORD ProcessId
)
{
HWND_CONTEXT context;
context.ProcessId = ProcessId;
context.hWnd = 0;
EnumWindows( EnumWindowsCallback, (LPARAM)&context );
return context.hWnd;
}
//--------------------------------------------------------------------------------
//
// PixelCpuThread
//
// Thread that runs affinitized to a specific core. It monitors
// its cell in a CpuPixel array to see how much CPU it should burn
//
//--------------------------------------------------------------------------------
DWORD WINAPI PixelCpuThread(
_In_ LPVOID lpParameter
)
{
DWORD cpuNumber = (DWORD)(DWORD_PTR)lpParameter;
ULONGLONG startTick;
while( 1 ) {
startTick = GetTickCount64();
while( GetTickCount64() - startTick < CpuPixels[cpuNumber] * (100 / GREYSCALE) );
Sleep( 100 - CpuPixels[cpuNumber] * (100 / GREYSCALE) );
}
return 0;
}
//--------------------------------------------------------------------------------
//
// LaunchBitmapThreads
//
// Launch a thread pinned to each CPU on the system, following the ordering
// used by Task Manager's CPU view.
//
//--------------------------------------------------------------------------------
DWORD
LaunchBitmapThreads(
volatile DWORD** CpuPixels
)
{
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX curProcInfo, logProcInfo;
PNUMA_NODE_RELATIONSHIP numaRelationship;
DWORD offset, cpuNumber = 0;
GROUP_AFFINITY groupAffinity;
LPPROC_THREAD_ATTRIBUTE_LIST attrList;
SIZE_T attrListSize = 0;
HANDLE hThread;
DWORD returnLength = 0;
ULONG_PTR cpu;
GetLogicalProcessorInformationEx( RelationNumaNode, NULL, &returnLength );
logProcInfo = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)malloc( returnLength );
GetLogicalProcessorInformationEx( RelationNumaNode, logProcInfo, &returnLength );
*CpuPixels = 0;
curProcInfo = logProcInfo;
offset = 0;
while( offset < returnLength ) {
numaRelationship = &curProcInfo->NumaNode;
for( cpu = 0; cpu < sizeof( numaRelationship->GroupMask.Mask ) * 8; cpu++ ) {
if( (1ULL << cpu) & numaRelationship->GroupMask.Mask ) {
*CpuPixels = (DWORD*)realloc( (PVOID)*CpuPixels, (cpuNumber + 1) * sizeof( DWORD ) );
memset( (PVOID)*CpuPixels, 0, (cpuNumber + 1) * sizeof( DWORD ) );
InitializeProcThreadAttributeList( NULL, 1, 0, &attrListSize );
attrList = (LPPROC_THREAD_ATTRIBUTE_LIST)malloc( attrListSize );
InitializeProcThreadAttributeList( attrList, 1, 0, &attrListSize );
memset( &groupAffinity, 0, sizeof( groupAffinity ) );
groupAffinity.Group = numaRelationship->GroupMask.Group;
groupAffinity.Mask = (KAFFINITY)1 << cpu;
UpdateProcThreadAttribute( attrList, 0, PROC_THREAD_ATTRIBUTE_GROUP_AFFINITY,
&groupAffinity, sizeof( groupAffinity ), NULL, NULL );
hThread = CreateRemoteThreadEx( GetCurrentProcess(), 0, 0, PixelCpuThread, (PVOID)(DWORD_PTR)cpuNumber,
0, attrList, NULL );
if( hThread )
{
CloseHandle( hThread );
hThread = NULL;
}
DeleteProcThreadAttributeList( attrList );
free( attrList );
cpuNumber++;
}
}
offset += curProcInfo->Size;
curProcInfo = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)((DWORD_PTR)curProcInfo + curProcInfo->Size);
}
return cpuNumber;
}
//--------------------------------------------------------------------------------
//
// Main
//
// Read bitmap, spawn a thread per core pinned to the core, and then
// update the CPU activity map to display the bitmap on Task Manager
//
//--------------------------------------------------------------------------------
int main( int argc, char* argv[] )
{
DWORD offset;
DWORD maxCpus;
int width, x, y, processId, averageColor;
HDC hScreenDC, hDstDC, hOrigDC;
HWND hWnd = NULL;
BITMAP bitmap;
HBITMAP hBitmap = NULL, hNewBitmap;
RECT rcClient, rcBitmap;
BOOL scrollHorizontal;
COLORREF pixel, greyPixel;
float scaleFactor;
//
// Width is the width of Task Manager's CPU activity array
//
if( argc < 3 ) {
printf( "Usage: %s <bitmap> <width>", argv[0] );
return -1;
}
processId = atoi( argv[1] );
width = atoi( argv[2] );
//
// Spawn a thread pinned to each CPU, identifying them by their CPU number index
// into the CPU array. Task manager shows CPUs ordered by their NUMA node.
//
maxCpus = LaunchBitmapThreads( &CpuPixels );
//
// Load the bitmap
//
hScreenDC = GetDC( NULL );
hOrigDC = CreateCompatibleDC( hScreenDC );
hDstDC = CreateCompatibleDC( hOrigDC );
if( processId != 0 ) {
hWnd = FindMainWindow( processId );
if( hWnd == NULL ) {
printf( "Unable to find windows for process %d\n", processId );
}
hOrigDC = GetDC( hWnd );
GetClientRect( hWnd, &rcClient );
if( width / rcClient.right )
scaleFactor = (float)width / rcClient.right;
else
scaleFactor = (float)(maxCpus / width) / rcClient.bottom;
rcBitmap.right = width;
rcBitmap.bottom = maxCpus / width;
hNewBitmap = CreateCompatibleBitmap( hOrigDC, rcBitmap.right, rcBitmap.bottom );
SelectObject( hDstDC, hNewBitmap );
StretchBlt( hDstDC, 0, 0, rcBitmap.right, rcBitmap.bottom, hOrigDC, 0, 0, rcClient.right, rcClient.bottom, SRCCOPY );
}
else {
hBitmap = (HBITMAP)LoadImageA( NULL, argv[1], IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );
if( hBitmap == NULL ) {
printf( "Error loading %s: %d\n", argv[1], GetLastError() );
return -1;
}
GetObject( hBitmap, sizeof( BITMAP ), &bitmap );
SelectObject( hOrigDC, hBitmap );
if( bitmap.bmWidth > bitmap.bmHeight ) {
scrollHorizontal = TRUE;
scaleFactor = (float)(maxCpus / width) / bitmap.bmHeight;
}
else {
scaleFactor = (float)width / bitmap.bmWidth;
}
rcBitmap.right = (int)((float)bitmap.bmWidth * scaleFactor);
rcBitmap.bottom = (int)(((float)bitmap.bmHeight) * scaleFactor);
hNewBitmap = CreateCompatibleBitmap( hOrigDC, rcBitmap.right, rcBitmap.bottom );
SelectObject( hDstDC, hNewBitmap );
SetStretchBltMode( hDstDC, COLORONCOLOR );
StretchBlt( hDstDC, 0, 0, rcBitmap.right, rcBitmap.bottom, hOrigDC, 0, 0,
bitmap.bmWidth, bitmap.bmHeight, SRCCOPY | CAPTUREBLT );
scrollHorizontal = bitmap.bmWidth > bitmap.bmHeight;
}
//
// Loop the bitmap through the CPU activity array either horizontally or vertically
// depending on dimensions of bitmap
//
offset = 0;
while( TRUE ) {
for( y = 0; y < (int)maxCpus / width; y++ ) {
#if _DEBUG
printf( "\n[%d] ", y );
#endif
for( x = 0; x < width; x++ ) {
if( hBitmap ) {
if( scrollHorizontal )
pixel = GetPixel( hDstDC, (x + offset) % rcBitmap.right, y );
else
pixel = GetPixel( hDstDC, x, (y - offset) % rcBitmap.bottom );
}
else {
pixel = GetPixel( hDstDC, x, y );
}
averageColor = (GetRValue( pixel ) + GetGValue( pixel ) + GetBValue( pixel )) / 3;
greyPixel = RGB( averageColor, averageColor, averageColor );
CpuPixels[y * width + x] = GREYSCALE - greyPixel / (0xffffff / GREYSCALE);
#if _DEBUG
printf( "%d ", CpuPixels[y * width + x] );
#endif
}
}
Sleep( 500 );
if( processId ) {
StretchBlt( hDstDC, 0, 0, width, maxCpus / width, hOrigDC, 0, 0, rcClient.right, rcClient.bottom, SRCCOPY );
}
else {
offset++;
}
}
}