Skip to content

Commit

Permalink
feat: working save data for message table
Browse files Browse the repository at this point in the history
- making save data a singleton style
- making save data save in finalize, and lazy load
- Adding frames class to handle frame index in buffer
  • Loading branch information
James-Frowen committed Aug 7, 2022
1 parent c9b8917 commit 0ab9197
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 100 deletions.
19 changes: 8 additions & 11 deletions Assets/Mirage.Profiler/CountRecorder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,23 @@ internal class CountRecorder
private readonly ProfilerCounter<int> _profilerBytes;
private readonly ProfilerCounter<int> _profilerPerSecond;
private readonly object _instance;
internal readonly Frame[] _frames;
internal readonly Frames _frames;
private int _count;
private int _bytes;
private int _perSecond;
private readonly Queue<(float time, int bytes)> _perSecondQueue = new Queue<(float time, int bytes)>();
private int _frameIndex = -1;


public CountRecorder(int bufferSize, object instance, ProfilerCounter<int> profilerCount, ProfilerCounter<int> profilerBytes, ProfilerCounter<int> profilerPerSecond)
public CountRecorder(object instance, ProfilerCounter<int> profilerCount, ProfilerCounter<int> profilerBytes, ProfilerCounter<int> profilerPerSecond)
{
_instance = instance;
_profilerCount = profilerCount;
_profilerBytes = profilerBytes;
_profilerPerSecond = profilerPerSecond;
_frames = new Frame[bufferSize];
for (var i = 0; i < _frames.Length; i++)
_frames[i] = new Frame();
_frames = new Frames();
}



public void OnMessage(NetworkDiagnostics.MessageInfo obj)
{
// using the profiler-window branch of mirage to allow NetworkDiagnostics to say which server/client is sent the event
Expand All @@ -39,11 +35,10 @@ public void OnMessage(NetworkDiagnostics.MessageInfo obj)
return;
#endif

// Debug.Log($"{Time.frameCount % frames.Length} {NetworkProfilerModuleViewController.CreateTextForMessageInfo(obj)}");

_count += obj.count;
_bytes += obj.bytes * obj.count;
var frame = _frames[Time.frameCount % _frames.Length];

var frame = _frames.GetFrame(_frameIndex);
frame.Messages.Add(new MessageInfo(obj, frame.Messages.Count));
frame.Bytes++;
}
Expand All @@ -56,7 +51,9 @@ public void EndFrame(int frameIndex)
_count = 0;
_bytes = 0;

_frameIndex = frameIndex;
// +1 so that we set next frame
// otherwise we clear the frame that the savedata wants to grab
_frameIndex = frameIndex + 1;
var frame = _frames.GetFrame(_frameIndex);
frame.Messages.Clear();
frame.Bytes = 0;
Expand Down
57 changes: 9 additions & 48 deletions Assets/Mirage.Profiler/Editor/Messages/MessageViewController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using Mirage.NetworkProfiler.ModuleGUI.UITable;
using Unity.Profiling;
using Unity.Profiling.Editor;
Expand All @@ -13,9 +12,7 @@ namespace Mirage.NetworkProfiler.ModuleGUI.Messages
{
internal sealed class MessageViewController : ProfilerModuleViewController
{
private readonly string _saveDataPath;
private readonly CounterNames _names;
private readonly ICountRecorderProvider _counterProvider;
private readonly Columns _columns = new Columns();
private Label _countLabel;
private Label _bytesLabel;
Expand All @@ -26,20 +23,11 @@ internal sealed class MessageViewController : ProfilerModuleViewController
private MessageView _messageView;
private readonly SavedData _savedData;

public MessageViewController(ProfilerWindow profilerWindow, CounterNames names, string name, ICountRecorderProvider counterProvider) : base(profilerWindow)
public MessageViewController(ProfilerWindow profilerWindow, CounterNames names, SavedData savedData)
: base(profilerWindow)
{
_names = names;
_counterProvider = counterProvider;

var userSettingsFolder = Path.GetFullPath("UserSettings");
if (string.IsNullOrEmpty(name))
throw new ArgumentNullException(nameof(name));

_saveDataPath = Path.Join(userSettingsFolder, "Mirage.Profiler", $"{name}.json");
Debug.Log($"Load from {_saveDataPath}");
_savedData = SaveDataLoader.Load(_saveDataPath);

NetworkProfilerRecorder.AfterSample += AfterUpdate;
_savedData = savedData;
}

protected override VisualElement CreateView()
Expand All @@ -56,22 +44,13 @@ protected override VisualElement CreateView()
}
}

private void ProfilerDriver_profileCleared()
{
Debug.Log($"ProfilerDriver_profileCleared");
}

protected override void Dispose(bool disposing)
{
if (!disposing)
return;

// Unsubscribe from the Profiler window event that we previously subscribed to.
ProfilerWindow.SelectedFrameIndexChanged -= FrameIndexChanged;
ProfilerDriver.profileCleared -= ProfilerDriver_profileCleared;

Debug.Log($"Save to {_saveDataPath}");
SaveDataLoader.Save(_saveDataPath, _savedData);

base.Dispose(disposing);
}
Expand Down Expand Up @@ -139,8 +118,6 @@ private VisualElement CreateViewInternal()

// Be notified when the selected frame index in the Profiler Window changes, so we can update the label.
ProfilerWindow.SelectedFrameIndexChanged += FrameIndexChanged;
ProfilerDriver.profileCleared += ProfilerDriver_profileCleared;


return root;
}
Expand Down Expand Up @@ -223,6 +200,12 @@ private bool TryGetMessages(int frameIndex, out List<MessageInfo> messages)
return true;
}

if (frameIndex == -1)
{
messages = null;
return false;
}

messages = _savedData.Frames.GetFrame(frameIndex).Messages;
return true;
}
Expand Down Expand Up @@ -265,27 +248,5 @@ private void AddNoMessagesLabel()
var ele = AddLabelWithPadding(parent);
ele.text = "No Messages";
}

private void AfterUpdate(int tick)
{
Debug.Assert(_savedData.Frames.Length == NetworkProfilerRecorder.FRAME_COUNT);

{
Debug.Log($"AfterUpdate [tick {tick}, selected {(int)ProfilerWindow.selectedFrameIndex}]");
}

var counter = _counterProvider.GetCountRecorder();
if (counter == null)
{
// no counter, no messages for this frame
// clear old data
_savedData.Frames[tick] = new Frame();

return;
}

// just save frame in save data to be the frame in counter
_savedData.Frames[tick] = counter._frames[tick];
}
}
}
2 changes: 1 addition & 1 deletion Assets/Mirage.Profiler/Editor/ReceivedModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public override ProfilerModuleViewController CreateDetailsViewController()
Names.RECEIVED_PER_SECOND
);

return new MessageViewController(ProfilerWindow, names, "Received", this);
return new MessageViewController(ProfilerWindow, names, SaveDataLoader.ReceiveData);
}

CountRecorder ICountRecorderProvider.GetCountRecorder()
Expand Down
107 changes: 99 additions & 8 deletions Assets/Mirage.Profiler/Editor/SavedData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class SavedData
/// <summary>
/// Message from each frame so they can survive domain reload
/// </summary>
public Frame[] Frames;
public Frames Frames;

/// <summary>
/// Active sort header
Expand All @@ -29,9 +29,7 @@ internal class SavedData

public SavedData()
{
Frames = new Frame[NetworkProfilerRecorder.FRAME_COUNT];
for (var i = 0; i < Frames.Length; i++)
Frames[i] = new Frame();
Frames = new Frames();

Expanded = new List<string>();
}
Expand All @@ -48,6 +46,7 @@ public SavedData()

return (null, SortMode.None);
}

public void SetSortHeader(SortHeader header)
{
if (header == null)
Expand All @@ -60,12 +59,106 @@ public void SetSortHeader(SortHeader header)
SortMode = header.SortMode;
}
}

public void Clear()
{
foreach (var frame in Frames)
{
frame.Bytes = 0;
frame.Messages.Clear();
}
}
}

internal class SaveDataLoader
{
private SavedData _receiveData;
private SavedData _sentData;
private static SaveDataLoader instance;

// private so only we can create one
private SaveDataLoader()
{
NetworkProfilerRecorder.AfterSample += AfterSample;
}

~SaveDataLoader()
{
NetworkProfilerRecorder.AfterSample -= AfterSample;
if (_receiveData != null)
Save(GetFullPath("Receive"), _receiveData);

if (_sentData != null)
Save(GetFullPath("Sent"), _sentData);
}

private static void AfterSample(int tick)
{
Debug.Log($"AfterSample {tick}");
SetFrame(tick, ReceiveData, NetworkProfilerRecorder._receivedCounter);
SetFrame(tick, SentData, NetworkProfilerRecorder._sentCounter);
}

private static void SetFrame(int tick, SavedData data, CountRecorder counter)
{
var saveFrame = data.Frames.GetFrame(tick);

// clear old data
saveFrame.Bytes = 0;
saveFrame.Messages.Clear();

if (counter == null)
return;

var counterFrame = counter._frames.GetFrame(tick);

saveFrame.Bytes = counterFrame.Bytes;
saveFrame.Messages.Clear();
saveFrame.Messages.AddRange(counterFrame.Messages);
}

public static SavedData ReceiveData
{
get
{
if (instance == null)
instance = new SaveDataLoader();

if (instance._receiveData == null)
{
instance._receiveData = Load(GetFullPath("Receive"));
}
return instance._receiveData;
}
}

public static SavedData SentData
{
get
{
if (instance == null)
instance = new SaveDataLoader();

if (instance._sentData == null)
{
instance._sentData = Load(GetFullPath("Sent"));
}
return instance._sentData;
}
}

public static string GetFullPath(string name)
{
var userSettingsFolder = Path.GetFullPath("UserSettings");
if (string.IsNullOrEmpty(name))
throw new ArgumentNullException(nameof(name));

return Path.Join(userSettingsFolder, "Mirage.Profiler", $"{name}.json");
}

public static void Save(string path, SavedData data)
{
Debug.Log($"Save {path}");
CheckDir(path);

var text = JsonUtility.ToJson(data);
Expand All @@ -74,6 +167,7 @@ public static void Save(string path, SavedData data)

public static SavedData Load(string path)
{
Debug.Log($"Load {path}");
CheckDir(path);

if (File.Exists(path))
Expand All @@ -91,10 +185,7 @@ public static SavedData Load(string path)

private static void Validate(SavedData data)
{
if (data.Frames.Length != NetworkProfilerRecorder.FRAME_COUNT)
{
Array.Resize(ref data.Frames, NetworkProfilerRecorder.FRAME_COUNT);
}
data.Frames.ValidateSize();
}

private static void CheckDir(string path)
Expand Down
2 changes: 1 addition & 1 deletion Assets/Mirage.Profiler/Editor/SentModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public override ProfilerModuleViewController CreateDetailsViewController()
Names.SENT_PER_SECOND
);

return new MessageViewController(ProfilerWindow, names, "Sent", this);
return new MessageViewController(ProfilerWindow, names, SaveDataLoader.SentData);
}

CountRecorder ICountRecorderProvider.GetCountRecorder()
Expand Down
35 changes: 34 additions & 1 deletion Assets/Mirage.Profiler/Frame.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Mirage.NetworkProfiler
{
[System.Serializable]
internal class Frame
public class Frames : IEnumerable<Frame>
{
[SerializeField]
private Frame[] _frames;

public Frames()
{
_frames = new Frame[NetworkProfilerRecorder.FRAME_COUNT];
for (var i = 0; i < _frames.Length; i++)
_frames[i] = new Frame();
}

public Frame GetFrame(int frameIndex)
{
return _frames[frameIndex % _frames.Length];
}

public IEnumerator<Frame> GetEnumerator() => ((IEnumerable<Frame>)_frames).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable<Frame>)_frames).GetEnumerator();

internal void ValidateSize()
{
if (_frames.Length != NetworkProfilerRecorder.FRAME_COUNT)
{
Array.Resize(ref _frames, NetworkProfilerRecorder.FRAME_COUNT);
}
}
}

[System.Serializable]
public class Frame
{
public List<MessageInfo> Messages = new List<MessageInfo>();
public int Bytes;
Expand Down
10 changes: 0 additions & 10 deletions Assets/Mirage.Profiler/FramesExtensions.cs

This file was deleted.

Loading

0 comments on commit 0ab9197

Please sign in to comment.