Skip to content

Commit 6255a31

Browse files
author
wanderer
committed
添加了profiler的内存分析
1 parent f8f6ec0 commit 6255a31

File tree

3 files changed

+267
-105
lines changed

3 files changed

+267
-105
lines changed

GameFramework/Runtime/Debugger/GuiUtility.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ public static void DrawItem(string key, string value)
1313
GUILayout.Label(value);
1414
GUILayout.EndVertical();
1515
}
16+
17+
1618
}
1719
}

GameFramework/Runtime/Debugger/ProfilerWindow.cs

Lines changed: 200 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
using System.Collections;
1+
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
4+
using System.Linq;
35
using UnityEngine;
6+
using UnityEngine.Profiling;
47

58
namespace Wanderer.GameFramework
69
{
@@ -24,27 +27,52 @@ public override void OnInit(params object[] args)
2427
}
2528

2629
//概要分析
27-
internal class SummaryProfiler : IDebuggerWindow
30+
internal class SummaryProfiler : DebuggerWindowBase
2831
{
29-
public void OnInit(params object[] args)
30-
{
31-
}
32-
33-
public void OnClose()
34-
{
35-
}
36-
37-
public void OnDraw()
38-
{
39-
}
40-
41-
public void OnEnter()
32+
public override void OnDraw()
4233
{
34+
GUILayout.Label("<b>Profiler Information</b>");
35+
GUILayout.BeginVertical("box");
36+
{
37+
GuiUtility.DrawItem("Supported", Profiler.supported.ToString());
38+
GuiUtility.DrawItem("Enabled", Profiler.enabled.ToString());
39+
GuiUtility.DrawItem("Enable Binary Log", Profiler.enableBinaryLog ? string.Format("True, {0}", Profiler.logFile) : "False");
40+
#if UNITY_2018_3_OR_NEWER
41+
GuiUtility.DrawItem("Area Count", Profiler.areaCount.ToString());
42+
#endif
43+
#if UNITY_5_3 || UNITY_5_4
44+
GuiUtility.DrawItem("Max Samples Number Per Frame", Profiler.maxNumberOfSamplesPerFrame.ToString());
45+
#endif
46+
#if UNITY_2018_3_OR_NEWER
47+
GuiUtility.DrawItem("Max Used Memory", Profiler.maxUsedMemory.ToByteLengthString());
48+
#endif
49+
#if UNITY_5_6_OR_NEWER
50+
GuiUtility.DrawItem("Mono Used Size", Profiler.GetMonoUsedSizeLong().ToByteLengthString());
51+
GuiUtility.DrawItem("Mono Heap Size", Profiler.GetMonoHeapSizeLong().ToByteLengthString());
52+
GuiUtility.DrawItem("Used Heap Size", Profiler.usedHeapSizeLong.ToByteLengthString());
53+
GuiUtility.DrawItem("Total Allocated Memory", Profiler.GetTotalAllocatedMemoryLong().ToByteLengthString());
54+
GuiUtility.DrawItem("Total Reserved Memory", Profiler.GetTotalReservedMemoryLong().ToByteLengthString());
55+
GuiUtility.DrawItem("Total Unused Reserved Memory", Profiler.GetTotalUnusedReservedMemoryLong().ToByteLengthString());
56+
#else
57+
GuiUtility.DrawItem("Mono Used Size", GetByteLengthString(Profiler.GetMonoUsedSize()));
58+
GuiUtility.DrawItem("Mono Heap Size", GetByteLengthString(Profiler.GetMonoHeapSize()));
59+
GuiUtility.DrawItem("Used Heap Size", GetByteLengthString(Profiler.usedHeapSize));
60+
GuiUtility.DrawItem("Total Allocated Memory", GetByteLengthString(Profiler.GetTotalAllocatedMemory()));
61+
GuiUtility.DrawItem("Total Reserved Memory", GetByteLengthString(Profiler.GetTotalReservedMemory()));
62+
GuiUtility.DrawItem("Total Unused Reserved Memory", GetByteLengthString(Profiler.GetTotalUnusedReservedMemory()));
63+
#endif
64+
#if UNITY_2018_1_OR_NEWER
65+
GuiUtility.DrawItem("Allocated Memory For Graphics Driver", Profiler.GetAllocatedMemoryForGraphicsDriver().ToByteLengthString());
66+
#endif
67+
#if UNITY_5_5_OR_NEWER
68+
GuiUtility.DrawItem("Temp Allocator Size", Profiler.GetTempAllocatorSize().ToByteLengthString());
69+
// GuiUtility.DrawItem("Marshal Cached HGlobal Size", Utility.Marshal.CachedHGlobalSize.);
70+
// GuiUtility.DrawItem("Data Provider Cached Bytes Size", DataProviderCreator.CachedBytesSize);
71+
#endif
72+
}
73+
GUILayout.EndVertical();
4374
}
4475

45-
public void OnExit()
46-
{
47-
}
4876
}
4977

5078
//内存分析
@@ -68,9 +96,13 @@ public override void OnInit(params object[] args)
6896
}
6997

7098
//内存分析基类
71-
internal abstract class MemoryProfilerBase : IDebuggerWindow
99+
internal abstract class MemoryProfilerBase<T> : IDebuggerWindow where T : UnityEngine.Object
72100
{
73101
Vector2 _scrollPos = Vector2.zero;
102+
protected List<ProfilerSample> _samples = new List<ProfilerSample>();
103+
protected long _sampleSize = 0;
104+
protected DateTime _sampleDateTime = DateTime.MinValue;
105+
74106
public virtual void OnInit(params object[] args)
75107
{
76108
}
@@ -90,6 +122,8 @@ public virtual void OnExit()
90122
public virtual void OnDraw()
91123
{
92124
_scrollPos = GUILayout.BeginScrollView(_scrollPos, "box");
125+
string typeName = typeof(T).Name;
126+
GUILayout.Label(string.Format("<b>{0} Runtime Memory Information</b>", typeName));
93127
if (GUILayout.Button("Take Sample"))
94128
{
95129
TakeSample();
@@ -98,131 +132,208 @@ public virtual void OnDraw()
98132
GUILayout.EndScrollView();
99133
}
100134

101-
protected abstract void OnScrollViewDraw();
135+
protected virtual void OnScrollViewDraw()
136+
{
137+
// if (m_SampleTime <= DateTime.MinValue)
138+
// {
139+
// GUILayout.Label(Utility.Text.Format("<b>Please take sample for {0} first.</b>", typeName));
140+
// }
141+
// else
142+
{
143+
// if (m_DuplicateSimpleCount > 0)
144+
// {
145+
// GUILayout.Label(Utility.Text.Format("<b>{0} {1}s ({2}) obtained at {3}, while {4} {1}s ({5}) might be duplicated.</b>", m_Samples.Count.ToString(), typeName, GetByteLengthString(m_SampleSize), m_SampleTime.ToString("yyyy-MM-dd HH:mm:ss"), m_DuplicateSimpleCount.ToString(), GetByteLengthString(m_DuplicateSampleSize)));
146+
// }
147+
// else
148+
// {
149+
// GUILayout.Label(Utility.Text.Format("<b>{0} {1}s ({2}) obtained at {3}.</b>", m_Samples.Count.ToString(), typeName, GetByteLengthString(m_SampleSize), m_SampleTime.ToString("yyyy-MM-dd HH:mm:ss")));
150+
// }
151+
string typeName = typeof(T).Name;
152+
GUILayout.Label(string.Format("<b>{0} {1}s ({2}) obtained at {3}.</b>", _samples.Count.ToString(), typeName, _sampleSize.ToByteLengthString(), _sampleDateTime.ToString("yyyy-MM-dd HH:mm:ss")));
153+
if (_samples.Count > 0)
154+
{
155+
GUILayout.BeginHorizontal();
156+
{
157+
GUILayout.Label(string.Format("<b>{0} Name</b>", typeName));
158+
GUILayout.Label("<b>Type</b>", GUILayout.Width(240f));
159+
GUILayout.Label("<b>Size</b>", GUILayout.Width(80f));
160+
}
161+
GUILayout.EndHorizontal();
162+
}
102163

103-
protected abstract void TakeSample();
164+
for (int i = 0; i < _samples.Count; i++)
165+
{
166+
GUILayout.BeginHorizontal();
167+
{
168+
GUILayout.Label(_samples[i].Highlight ? string.Format("<color=yellow>{0}</color>", _samples[i].Name) : _samples[i].Name);
169+
GUILayout.Label(_samples[i].Highlight ? string.Format("<color=yellow>{0}</color>", _samples[i].TypeName) : _samples[i].TypeName, GUILayout.Width(240f));
170+
GUILayout.Label(_samples[i].Highlight ? string.Format("<color=yellow>{0}</color>", _samples[i].Size.ToByteLengthString()) : _samples[i].Size.ToByteLengthString(), GUILayout.Width(80f));
171+
}
172+
GUILayout.EndHorizontal();
173+
}
174+
}
175+
}
104176

105-
}
106-
//概要内存分析
107-
internal class SummaryMemoryProfiler : MemoryProfilerBase
108-
{
109-
protected override void OnScrollViewDraw()
177+
protected virtual void TakeSample()
110178
{
179+
ReleaseSamples();
180+
181+
_sampleSize = 0;
182+
_sampleDateTime = DateTime.Now;
183+
//整理所有的数据
184+
T[] samples = Resources.FindObjectsOfTypeAll<T>();
185+
for (int i = 0; i < samples.Length; i++)
186+
{
187+
long sampleSize = Profiler.GetRuntimeMemorySizeLong(samples[i]);
188+
var sample = ProfilerSamplePool.Get(samples[i].name, samples[i].GetType().Name, sampleSize);
189+
_samples.Add(sample);
190+
191+
_sampleSize += sampleSize;
192+
}
193+
//sort
194+
_samples = _samples.OrderByDescending(x => x.Size).ToList();
111195
}
112196

113-
protected override void TakeSample()
197+
//释放所有的数据
198+
protected void ReleaseSamples()
114199
{
200+
foreach (var item in _samples)
201+
{
202+
ProfilerSamplePool.Release(item);
203+
}
204+
_samples.Clear();
115205
}
116206
}
117-
//所有的内存分析
118-
internal class AllMemoryProfiler : MemoryProfilerBase
207+
//概要内存分析
208+
internal class SummaryMemoryProfiler : MemoryProfilerBase<UnityEngine.Object>
119209
{
120-
protected override void OnScrollViewDraw()
121-
{
122-
}
123210

124211
protected override void TakeSample()
125212
{
213+
base.TakeSample();
214+
Dictionary<string, ProfilerSample> newSamples = new Dictionary<string, ProfilerSample>();
215+
ProfilerSample tempSample;
216+
for (int i = 0; i < _samples.Count; i++)
217+
{
218+
tempSample = _samples[i];
219+
ProfilerSample sample;
220+
if (!newSamples.TryGetValue(tempSample.TypeName, out sample))
221+
{
222+
sample = ProfilerSamplePool.Get(tempSample.TypeName, tempSample.TypeName, tempSample.Size, tempSample.Highlight);
223+
newSamples.Add(tempSample.TypeName, sample);
224+
}
225+
else
226+
{
227+
sample.Size += tempSample.Size;
228+
}
229+
ProfilerSamplePool.Release(tempSample);
230+
tempSample = null;
231+
}
232+
_samples.Clear();
233+
foreach (var item in newSamples.Values)
234+
{
235+
_samples.Add(item);
236+
}
237+
newSamples.Clear();
126238
}
127239
}
128-
//Texture内存分析
129-
internal class TextureMemoryProfiler : MemoryProfilerBase
240+
//所有的内存分析
241+
internal class AllMemoryProfiler : MemoryProfilerBase<UnityEngine.Object>
130242
{
131-
protected override void OnScrollViewDraw()
132-
{
133-
}
134-
135243
protected override void TakeSample()
136244
{
245+
base.TakeSample();
246+
for (int i = 0; i < _samples.Count; i++)
247+
{
248+
if (_samples[i].Size < 1024)
249+
{
250+
for (int j = i; j < _samples.Count; j++)
251+
{
252+
ProfilerSamplePool.Release(_samples[j]);
253+
}
254+
_samples.RemoveRange(i, _samples.Count - i);
255+
break;
256+
}
257+
}
137258
}
138259
}
260+
//Texture内存分析
261+
internal class TextureMemoryProfiler : MemoryProfilerBase<Texture>
262+
{
263+
}
139264
//Mesh内存分析
140-
internal class MeshMemoryProfiler : MemoryProfilerBase
265+
internal class MeshMemoryProfiler : MemoryProfilerBase<Mesh>
141266
{
142-
protected override void OnScrollViewDraw()
143-
{
144-
}
145267

146-
protected override void TakeSample()
147-
{
148-
}
149268
}
150269
//Material内存分析
151-
internal class MaterialMemoryProfiler : MemoryProfilerBase
270+
internal class MaterialMemoryProfiler : MemoryProfilerBase<Material>
152271
{
153-
protected override void OnScrollViewDraw()
154-
{
155-
}
156272

157-
protected override void TakeSample()
158-
{
159-
}
160273
}
161274
//Shader内存分析
162-
internal class ShaderMemoryProfiler : MemoryProfilerBase
275+
internal class ShaderMemoryProfiler : MemoryProfilerBase<Shader>
163276
{
164-
protected override void OnScrollViewDraw()
165-
{
166-
}
167277

168-
protected override void TakeSample()
169-
{
170-
}
171278
}
172279
//AnimationClip内存分析
173-
internal class AnimationClipMemoryProfiler : MemoryProfilerBase
280+
internal class AnimationClipMemoryProfiler : MemoryProfilerBase<AnimationClip>
174281
{
175-
protected override void OnScrollViewDraw()
176-
{
177-
}
178282

179-
protected override void TakeSample()
180-
{
181-
}
182283
}
183284
//AudioClip内存分析
184-
internal class AudioClipMemoryProfiler : MemoryProfilerBase
285+
internal class AudioClipMemoryProfiler : MemoryProfilerBase<AudioClip>
185286
{
186-
protected override void OnScrollViewDraw()
187-
{
188-
}
189287

190-
protected override void TakeSample()
191-
{
192-
}
193288
}
194289
// Font内存分析
195-
internal class FontMemoryProfiler : MemoryProfilerBase
290+
internal class FontMemoryProfiler : MemoryProfilerBase<Font>
196291
{
197-
protected override void OnScrollViewDraw()
198-
{
199-
}
200292

201-
protected override void TakeSample()
202-
{
203-
}
204293
}
205294
//TextAsset内存分析
206-
internal class TextAssetMemoryProfiler : MemoryProfilerBase
295+
internal class TextAssetMemoryProfiler : MemoryProfilerBase<TextAsset>
207296
{
208-
protected override void OnScrollViewDraw()
209-
{
210-
}
211297

212-
protected override void TakeSample()
298+
}
299+
//ScriptableObject内存分析
300+
internal class ScriptableObjectMemoryProfiler : MemoryProfilerBase<ScriptableObject>
301+
{
302+
303+
}
304+
305+
// profiler
306+
internal class ProfilerSample
307+
{
308+
public string Name { get; private set; }
309+
public string TypeName { get; private set; }
310+
public long Size { get; set; }
311+
public bool Highlight { get; set; }
312+
313+
public ProfilerSample Set(string name, string type, long size, bool highlight)
213314
{
315+
Name = name;
316+
TypeName = type;
317+
Size = size;
318+
Highlight = highlight;
319+
return this;
214320
}
215321
}
216-
//ScriptableObject内存分析
217-
internal class ScriptableObjectMemoryProfiler : MemoryProfilerBase
322+
// profiler pool
323+
internal class ProfilerSamplePool
218324
{
219-
protected override void OnScrollViewDraw()
325+
static ObjectPool<ProfilerSample> _pool = new ObjectPool<ProfilerSample>(null, null);
326+
327+
public static ProfilerSample Get(string name, string type, long size, bool highlight = false)
220328
{
329+
return _pool.Get().Set(name, type, size, highlight);
221330
}
222331

223-
protected override void TakeSample()
332+
public static void Release(ProfilerSample sample)
224333
{
334+
_pool.Release(sample);
225335
}
336+
226337
}
227338

228339
}

0 commit comments

Comments
 (0)