-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfilerLogger.cs
More file actions
159 lines (134 loc) · 5.3 KB
/
ProfilerLogger.cs
File metadata and controls
159 lines (134 loc) · 5.3 KB
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
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using Cysharp.Text;
using Cysharp.Threading.Tasks;
using UnityEngine;
using Unity.Jobs.LowLevel.Unsafe;
using Unity.Profiling;
namespace Tests
{
public class ProfilerLogger : MonoBehaviour
{
private ProfilerRecorder _frameTimeRecorder;
private ProfilerRecorder _idleTimeRecorder;
private ProfilerRecorder _systemMemoryRecorder;
private ProfilerRecorder _gcAllocRecorder;
private StreamWriter _writer;
private float _startWriteTime;
// Accumulators
private double _frameTimeSum;
private double _idleTimeSum;
private long _gcAllocSum;
private double _systemMemorySum;
private int _frameCount;
public void Init(List<string> selectedScenes, int numberOfBoids, string logFileArg)
{
_frameTimeRecorder = ProfilerRecorder.StartNew(
ProfilerCategory.Render,
"CPU Total Frame Time"
);
_idleTimeRecorder = ProfilerRecorder.StartNew(
ProfilerCategory.Internal,
"Idle"
);
_systemMemoryRecorder = ProfilerRecorder.StartNew(
ProfilerCategory.Memory,
"System Used Memory"
);
_gcAllocRecorder = ProfilerRecorder.StartNew(
ProfilerCategory.Memory,
"GC Allocated In Frame"
);
string filePath;
using (var sb = ZString.CreateStringBuilder())
{
sb.Append("EEProfilerLog_");
selectedScenes.Sort();
for (var i = 0; i < selectedScenes.Count; i++)
{
sb.Append(selectedScenes[i]);
if (i != selectedScenes.Count - 1)
{
sb.Append('+');
}
}
sb.Append('_');
sb.Append(numberOfBoids);
sb.Append("boids_");
sb.Append(logFileArg);
sb.Append(".csv");
filePath = Path.Combine(Application.persistentDataPath, sb.ToString());
}
_writer = new StreamWriter(filePath, false, Encoding.UTF8, 4096);
// Header
_writer.WriteLine(
"Time(s),FrameTimeAvg(ns),IdleTimeAvg(ns),SystemMemoryAvg(bytes),GCAllocTotal(bytes),JobWorkerCount"
);
}
private async UniTaskVoid Accumulator(CancellationTokenSource cts)
{
while (!cts.Token.IsCancellationRequested)
{
// Clamping idle time is required as during lag frames it may go into negatives or exceed max value
var idleTimeClamped = Mathf.Clamp(_idleTimeRecorder.LastValue, 0f,
_frameTimeRecorder.LastValue * JobsUtility.JobWorkerCount);
// Accumulate per frame
_frameTimeSum += _frameTimeRecorder.LastValue;
_idleTimeSum += idleTimeClamped;
_systemMemorySum += _systemMemoryRecorder.LastValue;
_gcAllocSum += _gcAllocRecorder.LastValue;
_frameCount++;
await UniTask.Yield(PlayerLoopTiming.Update);
}
}
public async UniTaskVoid RunLogger(CancellationTokenSource cts, float logInterval)
{
_startWriteTime = Time.unscaledTime;
Accumulator(cts).Forget();
while (!cts.Token.IsCancellationRequested)
{
await UniTask.WaitForSeconds(logInterval, true, PlayerLoopTiming.LastUpdate, cancellationToken: cts.Token);
WriteLogEntry();
}
}
private void WriteLogEntry()
{
var writeTime = Time.unscaledTime - _startWriteTime;
var mainThreadTimeAvg = _frameTimeSum / _frameCount;
var idleTimeAvg = _idleTimeSum / _frameCount;
var systemMemoryAvg = _systemMemorySum / _frameCount;
var gcAllocTotal = _gcAllocSum; // Total over an interval
using(var sb = ZString.CreateStringBuilder())
{
sb.Append(writeTime); sb.Append(',');
sb.Append(mainThreadTimeAvg); sb.Append(',');
sb.Append(idleTimeAvg); sb.Append(',');
sb.Append(systemMemoryAvg); sb.Append(',');
sb.Append(gcAllocTotal); sb.Append(',');
sb.Append(JobsUtility.JobWorkerCount);
_writer.WriteLine(sb.AsSpan());
}
// Reset accumulators
_frameTimeSum = 0;
_idleTimeSum = 0;
_systemMemorySum = 0;
_gcAllocSum = 0;
_frameCount = 0;
}
public void Shutdown()
{
WriteLogEntry(); // final entry
_frameTimeRecorder.Dispose();
_idleTimeRecorder.Dispose();
_gcAllocRecorder.Dispose();
_systemMemoryRecorder.Dispose();
if (_writer != null)
{
_writer.Flush();
_writer.Close();
}
}
}
}