Game Programming 13 - Debugging & Performance Optimization
-
Upload
nick-pruehs -
Category
Technology
-
view
180 -
download
0
Transcript of Game Programming 13 - Debugging & Performance Optimization
![Page 1: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/1.jpg)
Game ProgrammingDebugging & Performance Optimization
Nick Prühs
![Page 2: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/2.jpg)
Objectives
• To get an overview of techniques for preventing bugs beforehand
• To learn how to track down and properly remove bugs from your code
• To understand possible performance bottlenecks
2 / 49
![Page 3: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/3.jpg)
Why you should alwaysstart debugging immediately• Code entropy says your code will get worse, all the
time, unless you actively invest in preventing that
• Broken windows theory says the worse your code is, the worse it will become
• Tracking down bugs is harder in a larger code base
• Tracking down bugs is harder in a buggy code base
3 / 12
![Page 4: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/4.jpg)
Code Quality Tools
4 / 12
![Page 5: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/5.jpg)
Code Quality Tools
5 / 12
![Page 6: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/6.jpg)
Code Quality Tools
6 / 12
/// <summary>
/// Attaches the passed component to the entity with the specified id.
/// Note that this manager does not check whether the specified id is valid.
/// </summary>
/// <exception cref="ArgumentNullException">
/// Passed component is null.
/// </exception>
/// <exception cref="InvalidOperationException">
/// There is already a component of the same type attached.
/// </exception>
public void AddComponent(int entityId, IEntityComponent component)
{
if (component == null)
{
throw new ArgumentNullException("component");
}
if (this.components.ContainsKey(entityId))
{
throw new InvalidOperationException(
"There is already a component of type " + component.GetType() + " attached to entity with id "
+ entityId + ".");
}
this.components.Add(entityId, component);
this.OnComponentAdded(entityId, component);
}
![Page 7: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/7.jpg)
You need a repro. Period.
How can you be sure you’ve fixed it?
7 / 12
![Page 8: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/8.jpg)
Stack Traces
Object reference not set to an instance of an object
at LifeApplication.Initializer.CreateManagers () [0x00488] in Initializer.cs:481
at LifeApplication.Initializer.OnLoad () [0x00016] in Initializer.cs:235
8 / 12
![Page 9: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/9.jpg)
What’s null here?
// Initialize progress manager.
var progressConfig = newProgressConfig(this.unityLoader.Version.Code);
if (this.config.Progress.Encrypt)
{
progressConfig.SetEncryption(
this.config.Progress.Encryption.EncryptKey,
this.config.Progress.Encryption.EncryptIv);
}
9 / 12
![Page 10: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/10.jpg)
What’s null here?
// Initialize progress manager.
var progressConfig = newProgressConfig(this.unityLoader.Version.Code);
if (this.config.Progress.Encrypt)
{
progressConfig.SetEncryption(
this.config.Progress.Encryption.EncryptKey,
this.config.Progress.Encryption.EncryptIv);
}
10 / 12
![Page 11: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/11.jpg)
What’s null here?
// Initialize progress manager.
var progressConfig = newProgressConfig(this.unityLoader.Version.Code);
if (this.config.Progress.Encrypt)
{
progressConfig.SetEncryption(
this.config.Progress.Encryption.EncryptKey,
this.config.Progress.Encryption.EncryptIv);
}
11 / 12
![Page 12: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/12.jpg)
What’s null here?
// Initialize progress manager.
var progressConfig = newProgressConfig(this.unityLoader.Version.Code);
if (this.config.Progress.Encrypt)
{
progressConfig.SetEncryption(
this.config.Progress.Encryption.EncryptKey,
this.config.Progress.Encryption.EncryptIv);
}
12 / 12
![Page 13: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/13.jpg)
What’s null here?
// Initialize progress manager.
var progressConfig = newProgressConfig(this.unityLoader.Version.Code);
if (this.config.Progress.Encrypt)
{
progressConfig.SetEncryption(
this.config.Progress.Encryption.EncryptKey,
this.config.Progress.Encryption.EncryptIv);
}
13 / 12
![Page 14: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/14.jpg)
What’s null here?
// Initialize progress manager.
var progressConfig = newProgressConfig(this.unityLoader.Version.Code);
if (this.config.Progress.Encrypt)
{
progressConfig.SetEncryption(
this.config.Progress.Encryption.EncryptKey,
this.config.Progress.Encryption.EncryptIv);
}
14 / 12
![Page 15: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/15.jpg)
Divide-and-conquer
15 / 12
![Page 16: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/16.jpg)
Divide-and-conquer
16 / 12
![Page 17: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/17.jpg)
Divide-and-conquer
17 / 12
![Page 18: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/18.jpg)
Divide-and-conquer
18 / 12
![Page 19: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/19.jpg)
Divide-and-conquer
19 / 12
![Page 20: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/20.jpg)
Divide-and-conquer
20 / 12
![Page 21: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/21.jpg)
Conditional Breakpoints
21 / 12
![Page 22: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/22.jpg)
Logging
22 / 12
![Page 23: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/23.jpg)
On-Screen
23 / 12
![Page 24: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/24.jpg)
Crash Dump Analaysis
24 / 43
C:\Program Files\Procdump>procdump.exe -ma -i D:\Temp\Dumps
ProcDump v7.0 - Writes process dump files
Copyright (C) 2009-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
With contributions from Andrew Richards
Set to:
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AeDebug
(REG_SZ) Auto = 1
(REG_SZ) Debugger = "C:\Program Files\Procdump\procdump.exe" -accepteula -ma
-j "D:\Temp\Dumps" %ld %ld %p
Set to:
HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\AeDebug
(REG_SZ) Auto = 1
(REG_SZ) Debugger = "C:\Program Files\Procdump\procdump.exe" -accepteula -ma
-j "D:\Temp\Dumps" %ld %ld %p
ProcDump is now set as the Just-in-time (AeDebug) debugger.
![Page 25: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/25.jpg)
Crash Dump Analaysis
25 / 43
C:\Program Files\Procdump>procdump.exe -u
ProcDump v7.0 - Writes process dump files
Copyright (C) 2009-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
With contributions from Andrew Richards
Reset to:
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AeDebug
(REG_SZ) Auto = <deleted>
(REG_SZ) Debugger = "C:\WINDOWS\system32\vsjitdebugger.exe" -p %ld -e %ld
Reset to:
HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\AeDebug
(REG_SZ) Auto = <deleted>
(REG_SZ) Debugger = "C:\WINDOWS\system32\vsjitdebugger.exe" -p %ld -e %ld
ProcDump is no longer the Just-in-time (AeDebug) debugger.
![Page 26: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/26.jpg)
Crash Dump Analaysis
26 / 43
Deferencing a nullptr that will cause a crash
![Page 27: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/27.jpg)
Crash Dump Analaysis
27 / 43
Minidump File Summary in Visual Studio
![Page 28: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/28.jpg)
Crash Dump Analaysis
28 / 43
Debugging a Minidump in Visual Studio
![Page 29: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/29.jpg)
Pair Programming
29 / 12Source: University of Utah, UIT
![Page 30: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/30.jpg)
And if nothings helps …
30 / 12
![Page 31: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/31.jpg)
And if nothings helps …
31 / 12
TRY AGAIN TOMORROW!
![Page 32: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/32.jpg)
Some are really nasty …
• Remote systems
• Race conditions
• Mobile development, embedded systems, drivers
• “Release Mode only” bugs
32 / 12
![Page 33: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/33.jpg)
Quoting My Tutor
“If the bug is not where you expect it to be,
you better start looking for it where you’re not expecting it to be.”
- Hagen Peters
33 / 12
![Page 34: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/34.jpg)
Why you should neverstart optimizing immediately• Your code base will change over time, a lot, most
likely removing some of the code you’ve spent time on optimizing
• Optimized code tends to be hard to read• … and thus, hard to debug.
34 / 12
![Page 35: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/35.jpg)
Performance Optimization
1. Profile first!
35 / 12
![Page 36: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/36.jpg)
Performance Optimization
1. Profile first!
2. Profile again!
36 / 12
![Page 37: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/37.jpg)
Performance Optimization
1. Profile first!
2. Profile again!
3. Identify the bottlenecks: CPU vs. GPU vs. Memory
37 / 12
![Page 38: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/38.jpg)
Fighting CPU Bottlenecks
Pooling
Trades memory for CPU performance.
Re-uses objects to prevent costly construction and destruction.
Requires proper (cheap) reset of pooled objects.
38 / 12
![Page 39: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/39.jpg)
Fighting CPU Bottlenecks
Caching
Trades memory for CPU performance.
Stores computed values for later use.
Requires proper cache invalidation whenever the input changes.
39 / 12
![Page 40: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/40.jpg)
Fighting CPU Bottlenecks
Bucketing
Trades accuracy for CPU performance.
Distributes computations across multiple frames by dividing operation into multiple input sets.
Can only be applied if player doesn’t notice difference immediately (e.g. updating AI just twice per second).
40 / 12
![Page 41: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/41.jpg)
Memory Bottleneck
41 / 12
![Page 42: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/42.jpg)
Memory Bottleneck
42 / 12
![Page 43: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/43.jpg)
Memory Bottleneck
43 / 12
![Page 44: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/44.jpg)
GPU Bottleneck
44 / 12
![Page 45: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/45.jpg)
Frame Debugger
45 / 12
![Page 46: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/46.jpg)
Frame Debugger
46 / 12
![Page 47: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/47.jpg)
Frame Debugger
47 / 12
![Page 48: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/48.jpg)
Frame Debugger
48 / 12
![Page 49: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/49.jpg)
Frame Debugger
49 / 12
![Page 50: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/50.jpg)
Gotcha!
Always turn off logging before profiling!
Otherwise, disk I/O will lead to false results.
50 / 28
![Page 51: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/51.jpg)
Hint
If no native profiling tools are available (or applicable), you can always fall back to utility classes
such as System.Diagnostics.Stopwatch.
51 / 28
![Page 52: Game Programming 13 - Debugging & Performance Optimization](https://reader031.fdocuments.net/reader031/viewer/2022021500/58786ec41a28ab497b8b596d/html5/thumbnails/52.jpg)
References
• McShaffry. Debugging Your Game … or “That’s not supposed to happen!”. Austin Game Conference, 2003.
52 / 28