Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 0 additions & 132 deletions Profiler/Core/FastConcurrentQueue.cs

This file was deleted.

50 changes: 32 additions & 18 deletions Profiler/Core/ProfilerResultQueue.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
using NLog;
Expand All @@ -13,12 +14,12 @@ namespace Profiler.Core
public static class ProfilerResultQueue
{
static readonly ILogger Log = LogManager.GetCurrentClassLogger();
static readonly FastConcurrentQueue<ProfilerResult> _profilerResults;
static readonly ConcurrentQueue<ProfilerResult> _profilerResults;
static readonly ConcurrentCachingList<IProfiler> _profilers;

static ProfilerResultQueue()
{
_profilerResults = new FastConcurrentQueue<ProfilerResult>();
_profilerResults = new ConcurrentQueue<ProfilerResult>();
_profilers = new ConcurrentCachingList<IProfiler>();
}

Expand All @@ -35,35 +36,48 @@ public static IDisposable Profile(IProfiler observer)

internal static void Enqueue(in ProfilerResult result)
{
if (_profilers.Count == 0) return;

_profilerResults.Enqueue(result);
}

internal static async Task Start(CancellationToken canceller)
{
while (!canceller.IsCancellationRequested)
{
_profilerResults.Alternate();
_profilers.ApplyChanges();

var index = 0;
while (_profilerResults.TryDequeue(ref index, out var result))
try
{
foreach (var profiler in _profilers)
_profilers.ApplyChanges();

var dequeued = false;
while (_profilerResults.TryDequeue(out var result))
{
try
dequeued = true;
foreach (var profiler in _profilers)
{
profiler.ReceiveProfilerResult(result);
}
catch (Exception e)
{
Log.Error($"{profiler}: {e.Message}");
try
{
profiler.ReceiveProfilerResult(result);
}
catch (Exception e)
{
Log.Error($"{profiler}: {e.Message}");
}
}
}
}

await Task.Delay(TimeSpan.FromSeconds(.1f), canceller);
if (!dequeued)
{
await Task.Delay(TimeSpan.FromSeconds(.1f), canceller);
}
}
catch (OperationCanceledException) when (canceller.IsCancellationRequested)
{
break;
}
catch (Exception e)
{
Log.Warn(e, "Profiler consumer loop crashed; restarting after delay");
await Task.Delay(TimeSpan.FromSeconds(1), canceller);
}
}
}

Expand Down
23 changes: 15 additions & 8 deletions Profiler/Core/StringIndexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ internal sealed class StringIndexer
public static readonly StringIndexer Instance = new StringIndexer();

readonly List<string> _mapping;
readonly object _lock = new object();

StringIndexer()
{
Expand All @@ -24,21 +25,27 @@ public int IndexOf(string methodName)
throw new Exception("method name null");
}

var existingIndex = _mapping.IndexOf(methodName);
if (existingIndex >= 0) return existingIndex;
lock (_lock)
{
var existingIndex = _mapping.IndexOf(methodName);
if (existingIndex >= 0) return existingIndex;

_mapping.Add(methodName);
return _mapping.Count - 1;
_mapping.Add(methodName);
return _mapping.Count - 1;
}
}

public string StringAt(int index)
{
if (index >= _mapping.Count)
lock (_lock)
{
throw new IndexOutOfRangeException($"length: {_mapping.Count}, given index: {index}");
}
if (index >= _mapping.Count)
{
throw new IndexOutOfRangeException($"length: {_mapping.Count}, given index: {index}");
}

return _mapping[index];
return _mapping[index];
}
}
}
}
2 changes: 0 additions & 2 deletions Profiler/Interactive/PhysicsEntitySnapshot.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Utils.General;
using Sandbox.Game.Entities;
using Sandbox.Game.Entities.Cube;
using Sandbox.Game.Entities.EnvironmentItems;
using Sandbox.Game.World;
using VRage.Game.Entity;
using VRage.Game.ModAPI;
Expand Down
33 changes: 21 additions & 12 deletions Profiler/Profiler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>..\GameBinaries\Newtonsoft.Json.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="NLog">
<HintPath>..\TorchBinaries\NLog.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="WindowsBase" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
Expand All @@ -46,14 +54,6 @@
<Reference Include="PresentationFramework" />
</ItemGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>$(SolutionDir)\TorchBinaries\Newtonsoft.Json.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="NLog">
<HintPath>$(SolutionDir)\TorchBinaries\Nlog.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Sandbox.Common">
<HintPath>$(SolutionDir)\GameBinaries\Sandbox.Common.dll</HintPath>
<Private>False</Private>
Expand Down Expand Up @@ -198,7 +198,6 @@
<Compile Include="Core.Patches\MySession_UpdateComponents_Transpile.cs" />
<Compile Include="Core.Patches\MyTransportLayer_Tick.cs" />
<Compile Include="Core\CustomProfiling.cs" />
<Compile Include="Core\FastConcurrentQueue.cs" />
<Compile Include="Core\IProfiler.cs" />
<Compile Include="Core\StringIndexer.cs" />
<Compile Include="Core\ProfilerCategory.cs" />
Expand All @@ -220,13 +219,23 @@
<Compile Include="Utils\BlockTypeIdPool.cs" />
<Compile Include="ProfilerPlugin.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Page Include="ProfilerControl.xaml" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<Content Include="..\manifest.xml" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
<Target Name="Zip" BeforeTargets="AfterBuild">
<MakeDir Directories="$(SolutionDir)\Build\" />
<RemoveDir Directories="$(SolutionDir)\Build\tmp\" Condition="Exists('$(SolutionDir)\Build\tmp\')" />
<MakeDir Directories="$(SolutionDir)\Build\tmp\" />
<Copy SourceFiles="$(OutputPath)$(AssemblyName).dll" DestinationFolder="$(SolutionDir)\Build\tmp\" />
<Copy SourceFiles="$(OutputPath)$(AssemblyName).pdb" DestinationFolder="$(SolutionDir)\Build\tmp\" Condition="Exists('$(OutputPath)$(AssemblyName).pdb')" />
<Copy SourceFiles="$(OutputPath)manifest.xml" DestinationFolder="$(SolutionDir)\Build\tmp\" />
<Exec Command="powershell -NoProfile -Command &quot;Compress-Archive -Path '$(SolutionDir)\Build\tmp\*' -DestinationPath '$(SolutionDir)\Build\$(MSBuildProjectName).zip' -Force&quot;" />
<RemoveDir Directories="$(SolutionDir)\Build\tmp\" />
</Target>
<Import Project="..\TorchUtils\Utils.General\Utils.General.projitems" Label="Shared" />
<Import Project="..\TorchUtils\Utils.Torch\Utils.Torch.projitems" Label="Shared" />
</Project>
Loading