Skip to content

Commit

Permalink
Update content to new ParallelManager (#21813)
Browse files Browse the repository at this point in the history
  • Loading branch information
metalgearsloth authored Nov 29, 2023
1 parent 03ed3ff commit 7ada1c6
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 83 deletions.
1 change: 0 additions & 1 deletion Content.Client/Instruments/InstrumentSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ public override void SetupRenderer(EntityUid uid, bool fromStateChange, SharedIn

instrument.SequenceDelay = 0;
instrument.SequenceStartTick = 0;
_midiManager.OcclusionCollisionMask = (int) CollisionGroup.Impassable;
instrument.Renderer = _midiManager.GetNewRenderer();

if (instrument.Renderer != null)
Expand Down
193 changes: 124 additions & 69 deletions Content.Server/Atmos/EntitySystems/GasTileOverlaySystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public sealed class GasTileOverlaySystem : SharedGasTileOverlaySystem
[Robust.Shared.IoC.Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
[Robust.Shared.IoC.Dependency] private readonly ChunkingSystem _chunkingSys = default!;

/// <summary>
/// Per-tick cache of sessions.
/// </summary>
private readonly List<ICommonSession> _sessions = new();
private UpdatePlayerJob _updateJob;

private readonly Dictionary<ICommonSession, Dictionary<NetEntity, HashSet<Vector2i>>> _lastSentChunks = new();

// Oh look its more duplicated decal system code!
Expand All @@ -56,6 +62,19 @@ public sealed class GasTileOverlaySystem : SharedGasTileOverlaySystem
public override void Initialize()
{
base.Initialize();

_updateJob = new UpdatePlayerJob()
{
EntManager = EntityManager,
System = this,
ChunkIndexPool = _chunkIndexPool,
Sessions = _sessions,
ChunkingSys = _chunkingSys,
MapManager = _mapManager,
ChunkViewerPool = _chunkViewerPool,
LastSentChunks = _lastSentChunks,
};

_playerManager.PlayerStatusChanged += OnPlayerStatusChanged;
_confMan.OnValueChanged(CCVars.NetGasOverlayTickRate, UpdateTickRate, true);
_confMan.OnValueChanged(CCVars.GasOverlayThresholds, UpdateThresholds, true);
Expand All @@ -69,7 +88,7 @@ private void OnStartup(EntityUid uid, GasTileOverlayComponent component, Compone
{
// This **shouldn't** be required, but just in case we ever get entity prototypes that have gas overlays, we
// need to ensure that we send an initial full state to players.
Dirty(component);
Dirty(uid, component);
}

public override void Shutdown()
Expand Down Expand Up @@ -287,101 +306,137 @@ public override void Update(float frameTime)
// Now we'll go through each player, then through each chunk in range of that player checking if the player is still in range
// If they are, check if they need the new data to send (i.e. if there's an overlay for the gas).
// Afterwards we reset all the chunk data for the next time we tick.
var players = _playerManager.Sessions.Where(x => x.Status == SessionStatus.InGame).ToArray();
var opts = new ParallelOptions { MaxDegreeOfParallelism = _parMan.ParallelProcessCount };
Parallel.ForEach(players, opts, p => UpdatePlayer(p, curTick));
}

private void UpdatePlayer(ICommonSession playerSession, GameTick curTick)
{
var chunksInRange = _chunkingSys.GetChunksForSession(playerSession, ChunkSize, _chunkIndexPool, _chunkViewerPool);
var previouslySent = _lastSentChunks[playerSession];
_sessions.Clear();

var ev = new GasOverlayUpdateEvent();

foreach (var (netGrid, oldIndices) in previouslySent)
foreach (var player in _playerManager.Sessions)
{
// Mark the whole grid as stale and flag for removal.
if (!chunksInRange.TryGetValue(netGrid, out var chunks))
{
previouslySent.Remove(netGrid);
if (player.Status != SessionStatus.InGame)
continue;

// If grid was deleted then don't worry about sending it to the client.
if (!TryGetEntity(netGrid, out var gridId) || !_mapManager.IsGrid(gridId.Value))
ev.RemovedChunks[netGrid] = oldIndices;
else
{
oldIndices.Clear();
_chunkIndexPool.Return(oldIndices);
}
_sessions.Add(player);
}

continue;
}
if (_sessions.Count > 0)
{
_updateJob.CurrentTick = curTick;
_parMan.ProcessNow(_updateJob, _sessions.Count);
}
}

var old = _chunkIndexPool.Get();
DebugTools.Assert(old.Count == 0);
foreach (var chunk in oldIndices)
public void Reset(RoundRestartCleanupEvent ev)
{
foreach (var data in _lastSentChunks.Values)
{
foreach (var previous in data.Values)
{
if (!chunks.Contains(chunk))
old.Add(chunk);
previous.Clear();
_chunkIndexPool.Return(previous);
}

if (old.Count == 0)
_chunkIndexPool.Return(old);
else
ev.RemovedChunks.Add(netGrid, old);
data.Clear();
}
}

foreach (var (netGrid, gridChunks) in chunksInRange)
{
// Not all grids have atmospheres.
if (!TryGetEntity(netGrid, out var grid) || !TryComp(grid, out GasTileOverlayComponent? overlay))
continue;
#region Jobs

/// <summary>
/// Updates per player gas overlay data.
/// </summary>
private record struct UpdatePlayerJob : IParallelRobustJob
{
public int BatchSize => 2;

List<GasOverlayChunk> dataToSend = new();
ev.UpdatedChunks[netGrid] = dataToSend;
public IEntityManager EntManager;
public IMapManager MapManager;
public ChunkingSystem ChunkingSys;
public GasTileOverlaySystem System;
public ObjectPool<HashSet<Vector2i>> ChunkIndexPool;
public ObjectPool<Dictionary<NetEntity, HashSet<Vector2i>>> ChunkViewerPool;

previouslySent.TryGetValue(netGrid, out var previousChunks);
public GameTick CurrentTick;
public Dictionary<ICommonSession, Dictionary<NetEntity, HashSet<Vector2i>>> LastSentChunks;
public List<ICommonSession> Sessions;

foreach (var index in gridChunks)
public void Execute(int index)
{
var playerSession = Sessions[index];
var chunksInRange = ChunkingSys.GetChunksForSession(playerSession, ChunkSize, ChunkIndexPool, ChunkViewerPool);
var previouslySent = LastSentChunks[playerSession];

var ev = new GasOverlayUpdateEvent();

foreach (var (netGrid, oldIndices) in previouslySent)
{
if (!overlay.Chunks.TryGetValue(index, out var value))
// Mark the whole grid as stale and flag for removal.
if (!chunksInRange.TryGetValue(netGrid, out var chunks))
{
previouslySent.Remove(netGrid);

// If grid was deleted then don't worry about sending it to the client.
if (!EntManager.TryGetEntity(netGrid, out var gridId) || !MapManager.IsGrid(gridId.Value))
ev.RemovedChunks[netGrid] = oldIndices;
else
{
oldIndices.Clear();
ChunkIndexPool.Return(oldIndices);
}

continue;
}

if (previousChunks != null &&
previousChunks.Contains(index) &&
value.LastUpdate != curTick)
var old = ChunkIndexPool.Get();
DebugTools.Assert(old.Count == 0);
foreach (var chunk in oldIndices)
{
continue;
if (!chunks.Contains(chunk))
old.Add(chunk);
}

dataToSend.Add(value);
if (old.Count == 0)
ChunkIndexPool.Return(old);
else
ev.RemovedChunks.Add(netGrid, old);
}

previouslySent[netGrid] = gridChunks;
if (previousChunks != null)
foreach (var (netGrid, gridChunks) in chunksInRange)
{
previousChunks.Clear();
_chunkIndexPool.Return(previousChunks);
}
}
// Not all grids have atmospheres.
if (!EntManager.TryGetEntity(netGrid, out var grid) || !EntManager.TryGetComponent(grid, out GasTileOverlayComponent? overlay))
continue;

if (ev.UpdatedChunks.Count != 0 || ev.RemovedChunks.Count != 0)
RaiseNetworkEvent(ev, playerSession.ConnectedClient);
}
List<GasOverlayChunk> dataToSend = new();
ev.UpdatedChunks[netGrid] = dataToSend;

public void Reset(RoundRestartCleanupEvent ev)
{
foreach (var data in _lastSentChunks.Values)
{
foreach (var previous in data.Values)
{
previous.Clear();
_chunkIndexPool.Return(previous);
previouslySent.TryGetValue(netGrid, out var previousChunks);

foreach (var gIndex in gridChunks)
{
if (!overlay.Chunks.TryGetValue(gIndex, out var value))
continue;

if (previousChunks != null &&
previousChunks.Contains(gIndex) &&
value.LastUpdate != CurrentTick)
{
continue;
}

dataToSend.Add(value);
}

previouslySent[netGrid] = gridChunks;
if (previousChunks != null)
{
previousChunks.Clear();
ChunkIndexPool.Return(previousChunks);
}
}

data.Clear();
if (ev.UpdatedChunks.Count != 0 || ev.RemovedChunks.Count != 0)
System.RaiseNetworkEvent(ev, playerSession.Channel);
}
}

#endregion
}
}
45 changes: 42 additions & 3 deletions Content.Server/Decals/DecalSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public sealed class DecalSystem : SharedDecalSystem
private static readonly Vector2 _boundsMinExpansion = new(0.01f, 0.01f);
private static readonly Vector2 _boundsMaxExpansion = new(1.01f, 1.01f);

private UpdatePlayerJob _updateJob;
private List<ICommonSession> _sessions = new();

// If this ever gets parallelised then you'll want to increase the pooled count.
private ObjectPool<HashSet<Vector2i>> _chunkIndexPool =
new DefaultObjectPool<HashSet<Vector2i>>(
Expand All @@ -54,6 +57,12 @@ public override void Initialize()
{
base.Initialize();

_updateJob = new UpdatePlayerJob()
{
System = this,
Sessions = _sessions,
};

_playerManager.PlayerStatusChanged += OnPlayerStatusChanged;
SubscribeLocalEvent<TileChangedEvent>(OnTileChanged);

Expand Down Expand Up @@ -428,9 +437,18 @@ public override void Update(float frameTime)

if (PvsEnabled)
{
var players = _playerManager.Sessions.Where(x => x.Status == SessionStatus.InGame).ToArray();
var opts = new ParallelOptions { MaxDegreeOfParallelism = _parMan.ParallelProcessCount };
Parallel.ForEach(players, opts, UpdatePlayer);
_sessions.Clear();

foreach (var session in _playerManager.Sessions)
{
if (session.Status != SessionStatus.InGame)
continue;

_sessions.Add(session);
}

if (_sessions.Count > 0)
_parMan.ProcessNow(_updateJob, _sessions.Count);
}

_dirtyChunks.Clear();
Expand Down Expand Up @@ -564,5 +582,26 @@ private void SendChunkUpdates(
ReturnToPool(updatedChunks);
ReturnToPool(staleChunks);
}

#region Jobs

/// <summary>
/// Updates per-player data for decals.
/// </summary>
private record struct UpdatePlayerJob : IParallelRobustJob
{
public int BatchSize => 2;

public DecalSystem System;

public List<ICommonSession> Sessions;

public void Execute(int index)
{
System.UpdatePlayer(Sessions[index]);
}
}

#endregion
}
}
2 changes: 1 addition & 1 deletion Content.Server/Power/EntitySystems/PowerNetSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public override void Update(float frameTime)
RaiseLocalEvent(new NetworkBatteryPreSync());

// Run power solver.
_solver.Tick(frameTime, _powerState, _parMan.ParallelProcessCount);
_solver.Tick(frameTime, _powerState, _parMan);

// Synchronize batteries, the other way around.
RaiseLocalEvent(new NetworkBatteryPostSync());
Expand Down
Loading

0 comments on commit 7ada1c6

Please sign in to comment.