summaryrefslogtreecommitdiff
path: root/utils/xbox/vxconsole/mem_profile.cpp
blob: d15aee3e5b1e1eff50ad4bf40e6237665c126d3c (plain) (blame)
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//========= Copyright Valve Corporation, All rights reserved. ============//
//
//	MEM_PROFILE.CPP
//
//	Memory Profiling Display
//=====================================================================================//
#include "vxconsole.h"

#define PROFILE_MAXSAMPLES		512
#define PROFILE_MEMORYHEIGHT	100
#define PROFILE_NUMMINORTICKS	3
#define PROFILE_LABELWIDTH		50
#define PROFILE_SCALESTEPS		8
#define PROFILE_MINSCALE		0.2f
#define PROFILE_MAXSCALE		3.0f
#define PROFILE_NUMMINORTICKS	3
#define PROFILE_MAJORTICKMB		16
#define PROFILE_WARNINGMB		10
#define PROFILE_SEVEREMB		5

#define ID_MEMPROFILE				1

HWND		g_memProfile_hWnd;
RECT		g_memProfile_WindowRect;
int			g_memProfile_tickMarks;
int			g_memProfile_colors;
int			g_memProfile_scale;
UINT_PTR	g_memProfile_Timer;
int			g_memProfile_numSamples;
int			g_memProfile_samples[PROFILE_MAXSAMPLES];

//-----------------------------------------------------------------------------
//	MemProfile_SaveConfig
// 
//-----------------------------------------------------------------------------
void MemProfile_SaveConfig()
{
	char			buff[256];
	WINDOWPLACEMENT wp;

	// profile history
	if ( g_memProfile_hWnd )
	{
		memset( &wp, 0, sizeof( wp ) );
		wp.length = sizeof( WINDOWPLACEMENT );
		GetWindowPlacement( g_memProfile_hWnd, &wp );
		g_memProfile_WindowRect = wp.rcNormalPosition;
		sprintf( buff, "%d %d %d %d", wp.rcNormalPosition.left, wp.rcNormalPosition.top, wp.rcNormalPosition.right, wp.rcNormalPosition.bottom );
		Sys_SetRegistryString( "MemProfileWindowRect", buff );
	}

	Sys_SetRegistryInteger( "MemProfileScale", g_memProfile_scale );
	Sys_SetRegistryInteger( "MemProfileTickMarks", g_memProfile_tickMarks );
	Sys_SetRegistryInteger( "MemProfileColors", g_memProfile_colors );
}

//-----------------------------------------------------------------------------
//	MemProfile_LoadConfig	
// 
//-----------------------------------------------------------------------------
void MemProfile_LoadConfig()
{
	int		numArgs;
	char	buff[256];

	// profile history
	Sys_GetRegistryString( "MemProfileWindowRect", buff, "", sizeof( buff ) );
	numArgs = sscanf( buff, "%d %d %d %d", &g_memProfile_WindowRect.left, &g_memProfile_WindowRect.top, &g_memProfile_WindowRect.right, &g_memProfile_WindowRect.bottom );
	if ( numArgs != 4 )
	{
		memset( &g_memProfile_WindowRect, 0, sizeof( g_memProfile_WindowRect ) );
	}

	Sys_GetRegistryInteger( "MemProfileScale", 0, g_memProfile_scale );
	if ( g_memProfile_scale < -PROFILE_SCALESTEPS || g_memProfile_scale > PROFILE_SCALESTEPS )
	{
		g_memProfile_scale = 0;
	}

	Sys_GetRegistryInteger( "MemProfileTickMarks", 1, g_memProfile_tickMarks );
	Sys_GetRegistryInteger( "MemProfileColors", 1, g_memProfile_colors );
}

//-----------------------------------------------------------------------------
//	MemProfile_SetTitle
// 
//-----------------------------------------------------------------------------
void MemProfile_SetTitle()
{
	char	titleBuff[128];

	if ( g_memProfile_hWnd )
	{
		strcpy( titleBuff, "Free Memory Available" );
		if ( g_memProfile_Timer )
		{
			strcat( titleBuff, " [ON]" );
		}

		SetWindowText( g_memProfile_hWnd, titleBuff );
	}
}

//-----------------------------------------------------------------------------
// MemProfile_EnableProfiling
//
//-----------------------------------------------------------------------------
void MemProfile_EnableProfiling( bool bEnable )
{
	if ( !g_memProfile_hWnd )
	{
		return;
	}

	UINT_PTR timer = TIMERID_MEMPROFILE;
	if ( bEnable && !g_memProfile_Timer )
	{
		// run at 10Hz
		g_memProfile_Timer = SetTimer( g_memProfile_hWnd, timer, 100, NULL );
	}
	else if ( !bEnable && g_memProfile_Timer )
	{
		KillTimer( g_memProfile_hWnd, timer );
		g_memProfile_Timer = NULL;
	}
}

//-----------------------------------------------------------------------------
// MemProfile_UpdateWindow
//
//-----------------------------------------------------------------------------
void MemProfile_UpdateWindow()
{
	if ( g_memProfile_hWnd && !IsIconic( g_memProfile_hWnd ) )
	{
		// visible - force a client repaint
		InvalidateRect( g_memProfile_hWnd, NULL, true );
	}
}

//-----------------------------------------------------------------------------
// rc_FreeMemory
//
//-----------------------------------------------------------------------------
int rc_FreeMemory( char* commandPtr )
{
	int	errCode = -1;
	int	freeMemory;

	char *cmdToken = GetToken( &commandPtr );
	if ( !cmdToken[0] )
	{
		goto cleanUp;
	}
	sscanf( cmdToken, "%x", &freeMemory );

	g_memProfile_samples[g_memProfile_numSamples % PROFILE_MAXSAMPLES] = freeMemory;
	g_memProfile_numSamples++;

	DebugCommand( "FreeMemory( 0x%8.8x )\n", freeMemory );

	MemProfile_UpdateWindow();

	// success
	errCode = 0;

cleanUp:
	return ( errCode );
}

//-----------------------------------------------------------------------------
//	MemProfile_ZoomIn
// 
//-----------------------------------------------------------------------------
void MemProfile_ZoomIn( int& scale, int numSteps )
{
	scale++;
	if ( scale > numSteps )
	{
		scale = numSteps;
		return;
	}
	MemProfile_UpdateWindow();
}

//-----------------------------------------------------------------------------
//	MemProfile_ZoomOut
// 
//-----------------------------------------------------------------------------
void MemProfile_ZoomOut( int& scale, int numSteps )
{
	scale--;
	if ( scale < -numSteps )
	{
		scale = -numSteps;
		return;
	}
	MemProfile_UpdateWindow();
}

//-----------------------------------------------------------------------------
//	MemProfile_CalcScale
// 
//-----------------------------------------------------------------------------
float MemProfile_CalcScale( int scale, int numSteps, float min, float max )
{
	float t;

	// from integral scale [-numSteps..numSteps] to float scale [min..max]
	t = ( float )( scale + numSteps )/( float )( 2*numSteps );
	t = min + t*( max-min );

	return t;
}

//-----------------------------------------------------------------------------
//	MemProfile_Draw
// 
//-----------------------------------------------------------------------------
void MemProfile_Draw( HDC hdc, RECT* clientRect )
{
	char	labelBuff[128];
	HPEN	hBlackPen;
	HPEN	hPenOld;
	HPEN	hNullPen;
	HPEN	hGreyPen;
	HBRUSH	hColoredBrush;
	HBRUSH	hBrushOld;
	HFONT	hFontOld;
	int		currentSample;
	int		numTicks;
	int		memoryHeight;
	int		windowWidth;
	int		windowHeight;
	int		x;
	int		y;
	int		y0;
	int		i;
	int		j;
	int		h;
	int		numbars;
	RECT	rect;
	float	t;
	float	scale;

	hBlackPen  = CreatePen( PS_SOLID, 1, RGB( 0,0,0 ) );
	hGreyPen   = CreatePen( PS_SOLID, 1, Sys_ColorScale( g_backgroundColor, 0.85f ) );
	hNullPen   = CreatePen( PS_NULL, 0, RGB( 0,0,0 ) );
	hPenOld    = ( HPEN )SelectObject( hdc, hBlackPen );
	hFontOld   = SelectFont( hdc, g_hProportionalFont );

	// zoom
	scale        = MemProfile_CalcScale( g_memProfile_scale, PROFILE_SCALESTEPS, PROFILE_MINSCALE, PROFILE_MAXSCALE );
	memoryHeight = ( int )( PROFILE_MEMORYHEIGHT*scale );
	windowWidth  = clientRect->right-clientRect->left;
	windowHeight = clientRect->bottom-clientRect->top;

	numTicks = windowHeight/memoryHeight + 2;
	if ( numTicks < 0 )
	{
		numTicks = 1;
	}
	else if ( numTicks > 512/PROFILE_MAJORTICKMB + 1 )
	{
		numTicks = 512/PROFILE_MAJORTICKMB + 1;
	}

	SetBkColor( hdc, g_backgroundColor );

	x = 0;
	y = windowHeight;
	for ( i=0; i<numTicks; i++ )
	{
		// major ticks
		SelectObject( hdc, hBlackPen );
		MoveToEx( hdc, 0, y, NULL );
		LineTo( hdc, windowWidth, y );

		if ( g_memProfile_tickMarks )
		{
			// could be very zoomed out, gap must be enough for label, otherwise don't draw
			int gapY = memoryHeight/( PROFILE_NUMMINORTICKS+1 );
			if ( gapY >= 10 )
			{
				// minor ticks
				y0 = y;
				SelectObject( hdc, hGreyPen );
				for ( j=0; j<PROFILE_NUMMINORTICKS; j++ )
				{
					y0 += gapY;
					MoveToEx( hdc, 0, y0, NULL );
					LineTo( hdc, windowWidth, y0 );
				}
			}
		}

		// tick labels
		if ( i )
		{
			rect.left   = windowWidth - 50;
			rect.right  = windowWidth;
			rect.top    = y - 20;
			rect.bottom = y;
			sprintf( labelBuff, "%d MB", i*PROFILE_MAJORTICKMB );
			DrawText( hdc, labelBuff, -1, &rect, DT_RIGHT|DT_SINGLELINE|DT_BOTTOM );
		}

		y -= memoryHeight;
	}

	// vertical bars
	if ( g_memProfile_numSamples )
	{
		SelectObject( hdc, hNullPen );

		numbars = windowWidth-PROFILE_LABELWIDTH;
		currentSample = g_memProfile_numSamples-1;
		for ( x=numbars-1; x>=0; x-=4 )
		{
			float sample = g_memProfile_samples[currentSample % PROFILE_MAXSAMPLES]/( 1024.0f * 1024.0f );

			y = windowHeight;
			t  = sample/(float)PROFILE_MAJORTICKMB;
			h  = ( int )( t * ( float )memoryHeight );
			if ( h )
			{
				if ( h > windowHeight )
					h = windowHeight;
		
				COLORREF barColor;
				if ( sample >= PROFILE_WARNINGMB )
				{
					barColor = RGB( 100, 255, 100 );
				}
				else if ( sample >= PROFILE_SEVEREMB )
				{
					barColor = RGB( 255, 255, 100 );
				}
				else
				{
					barColor = RGB( 255, 0, 0 );
				}

				hColoredBrush = CreateSolidBrush( g_memProfile_colors ? barColor : RGB( 80, 80, 80 ) );
				hBrushOld = ( HBRUSH )SelectObject( hdc, hColoredBrush );

				Rectangle( hdc, x-4, y-h, x, y+1 );
				y -= h;

				SelectObject( hdc, hBrushOld );
				DeleteObject( hColoredBrush );
			}

			currentSample--;
			if ( currentSample < 0 )
			{
				// no data
				break;
			}
		}
	}

	SelectObject( hdc, hFontOld );
	SelectObject( hdc, hPenOld );
	DeleteObject( hBlackPen );
	DeleteObject( hGreyPen );
}

//-----------------------------------------------------------------------------
// MemProfile_TimerProc
//
//-----------------------------------------------------------------------------
void MemProfile_TimerProc( HWND hwnd, UINT_PTR idEvent )
{
	static bool busy = false;

	if ( busy )
	{
		return;
	}

	busy = true;

	if ( g_connectedToApp )
	{
		// send as async
		DmAPI_SendCommand( VXCONSOLE_COMMAND_PREFIX "!" "__memory__ quiet", false );
	}

	busy = false;
}

//-----------------------------------------------------------------------------
//	MemProfile_WndProc
// 
//-----------------------------------------------------------------------------
LRESULT CALLBACK MemProfile_WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
	WORD			wID = LOWORD( wParam );
	HDC				hdc;
	PAINTSTRUCT		ps; 
	RECT			rect;
	CREATESTRUCT	*createStructPtr;

	switch ( message )
	{
	case WM_CREATE:
		// set the window identifier
		createStructPtr = ( CREATESTRUCT* )lParam;
		SetWindowLong( hwnd, GWL_USERDATA+0, ( LONG )createStructPtr->lpCreateParams );

		// clear samples
		g_memProfile_numSamples = 0;
		memset( g_memProfile_samples, 0, sizeof( g_memProfile_samples ) );
		return 0L;

	case WM_DESTROY:
		MemProfile_SaveConfig();
		MemProfile_EnableProfiling( false );
		g_memProfile_hWnd = NULL;
		return 0L;

	case WM_INITMENU:
		CheckMenuItem( ( HMENU )wParam, IDM_MEMPROFILE_TICKMARKS, MF_BYCOMMAND | ( g_memProfile_tickMarks ? MF_CHECKED : MF_UNCHECKED ) );
		CheckMenuItem( ( HMENU )wParam, IDM_MEMPROFILE_COLORS, MF_BYCOMMAND | ( g_memProfile_colors ? MF_CHECKED : MF_UNCHECKED ) );
		CheckMenuItem( ( HMENU )wParam, IDM_MEMPROFILE_ENABLE, MF_BYCOMMAND | ( g_memProfile_Timer != NULL ? MF_CHECKED : MF_UNCHECKED ) );
		return 0L;

	case WM_PAINT:
		GetClientRect( hwnd, &rect );
		hdc = BeginPaint( hwnd, &ps ); 
		MemProfile_Draw( hdc, &rect );
		EndPaint( hwnd, &ps ); 
		return 0L;

	case WM_SIZE:
		// force a redraw
		MemProfile_UpdateWindow();
		return 0L;

	case WM_TIMER:
		if ( wID == TIMERID_MEMPROFILE )
		{
			MemProfile_TimerProc( hwnd, TIMERID_MEMPROFILE );
			return 0L;
		}
		break;

	case WM_KEYDOWN:
		switch ( wParam )
		{
		case VK_INSERT:
			MemProfile_ZoomIn( g_memProfile_scale, PROFILE_SCALESTEPS );
			return 0L;

		case VK_DELETE:
			MemProfile_ZoomOut( g_memProfile_scale, PROFILE_SCALESTEPS );
			return 0L;
		}
		break;

	case WM_COMMAND:
		switch ( wID )
		{
		case IDM_MEMPROFILE_TICKMARKS:
			g_memProfile_tickMarks ^= 1;
			MemProfile_UpdateWindow();
			return 0L;

		case IDM_MEMPROFILE_COLORS:
			g_memProfile_colors ^= 1;
			MemProfile_UpdateWindow();
			return 0L;

		case IDM_MEMPROFILE_ZOOMIN:
			MemProfile_ZoomIn( g_memProfile_scale, PROFILE_SCALESTEPS );
			return 0L;

		case IDM_MEMPROFILE_ZOOMOUT:
			MemProfile_ZoomOut( g_memProfile_scale, PROFILE_SCALESTEPS );
			return 0L;

		case IDM_MEMPROFILE_ENABLE:
			bool bEnable = ( g_memProfile_Timer != NULL );
			bEnable ^= 1;
			MemProfile_EnableProfiling( bEnable );
			MemProfile_SetTitle();
			return 0L;
		}
		break;
	}	
	return ( DefWindowProc( hwnd, message, wParam, lParam ) );
}

//-----------------------------------------------------------------------------
//	MemProfile_Open
// 
//-----------------------------------------------------------------------------
void MemProfile_Open()
{
	HWND	hWnd;
	
	if ( g_memProfile_hWnd )
	{
		// only one profile instance
		if ( IsIconic( g_memProfile_hWnd ) )
			ShowWindow( g_memProfile_hWnd, SW_RESTORE );
		SetForegroundWindow( g_memProfile_hWnd );
		return;
	}

	hWnd = CreateWindowEx( 
				WS_EX_CLIENTEDGE,
				"MEMPROFILECLASS",
				"",
				WS_POPUP|WS_CAPTION|WS_SYSMENU|WS_SIZEBOX|WS_MINIMIZEBOX|WS_MAXIMIZEBOX,
				0,
				0,
				600,
				500,
				g_hDlgMain,
				NULL,
				g_hInstance,
				( void* )ID_MEMPROFILE );
	g_memProfile_hWnd = hWnd;

	MemProfile_EnableProfiling( true );
	MemProfile_SetTitle();

	if ( g_memProfile_WindowRect.right && g_memProfile_WindowRect.bottom )
		MoveWindow( g_memProfile_hWnd, g_memProfile_WindowRect.left, g_memProfile_WindowRect.top, g_memProfile_WindowRect.right-g_memProfile_WindowRect.left, g_memProfile_WindowRect.bottom-g_memProfile_WindowRect.top, FALSE );
	ShowWindow( g_memProfile_hWnd, SHOW_OPENWINDOW );
}

//-----------------------------------------------------------------------------
//	MemProfile_Init
// 
//-----------------------------------------------------------------------------
bool MemProfile_Init()
{
	WNDCLASS wndclass;

	// set up our window class
	memset( &wndclass, 0, sizeof( wndclass ) );
	wndclass.style         = 0;
	wndclass.lpfnWndProc   = MemProfile_WndProc;
	wndclass.cbClsExtra    = 0;
	wndclass.cbWndExtra    = 0;
	wndclass.hInstance     = g_hInstance;
	wndclass.hIcon         = g_hIcons[ICON_APPLICATION];
	wndclass.hCursor       = LoadCursor( NULL, IDC_ARROW );
	wndclass.hbrBackground = g_hBackgroundBrush;
	wndclass.lpszMenuName  = MAKEINTRESOURCE( MENU_MEMPROFILE );
	wndclass.lpszClassName = "MEMPROFILECLASS";
	if ( !RegisterClass( &wndclass ) )
		return false;
	
	MemProfile_LoadConfig();

	return true;
}