Skip to content

Commit dbd7a77

Browse files
committed
more cleanup
1 parent e7b8e1f commit dbd7a77

6 files changed

Lines changed: 328 additions & 212 deletions

File tree

Basis Server/BasisNetworkServer/BasisNetworkingReductionSystem/BasisServerReductionSystemEvents.cs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -571,28 +571,28 @@ private static void RebalanceCpuBudget(long nowTick)
571571
// one machine; on hardware with a different core count or per-core speed this line is
572572
// what tells an operator whether the shipped default is wrong for them, and which of
573573
// BSRMaxDegreeOfParallelism / PeerUpdateParallelism to reach for.
574+
//
575+
// The delivery side rides the same line rather than a second one: undeliverable packets
576+
// used to be visible only on the health endpoint, so a server shedding a third of its
577+
// output looked identical in the log to one running clean. Drop pressure is per player
578+
// per control window; sustained values above the escalate figure are what drive the
579+
// shedding shown alongside. Worker counts are what is actually running, not just what
580+
// each pool is allowed — the gap between those two is what hid a server using a quarter
581+
// of a large host.
574582
if (WriteLoadLog && nowTick - _lastPoolLoadLogTick >= PoolLoadLogIntervalTicks)
575583
{
576584
_lastPoolLoadLogTick = nowTick;
577585
int peerWorkers = lnl?.PeerUpdateWorkers ?? 0;
578-
// Workers actually running, not just what each pool is allowed — the gap between
579-
// those two is what hid a server using a quarter of a large host.
580-
BNL.Log(
581-
$"[CPU] send {parallelOptions.MaxDegreeOfParallelism}/{BasisCpuBudget.ReductionSendCap} workers, " +
582-
$"peer-update {peerWorkers}/{BasisCpuBudget.PeerUpdateCap} workers " +
583-
$"(pass {lnl?.PeerUpdatePassMs ?? 0:F1} ms, target {LiteNetLib.NetManager.PeerPassTargetMs:F0} ms), machine {BasisCpuBudget.Utilization * 100:F0}% of {BasisCpuBudget.TotalCores} cores.");
584-
585-
// The delivery side of the same question. Undeliverable packets used to be visible
586-
// only on the health endpoint, so a server shedding a third of its output looked
587-
// identical in the log to one running clean — this is the line that would have made
588-
// that obvious. Drop pressure is per player per control window; sustained values
589-
// above 1 are what drive the shedding shown alongside.
590586
int pop = NetworkServer.Server?.ConnectedPeersCount ?? 0;
591587
BNL.Log(
592-
$"[POP] {pop} peers: drop pressure {_dropsPerPlayerWindow:F2}/player " +
593-
$"(escalate above {DropEscalatePerPlayer:F2}), slicing {_sliceCount}/{MaxSliceCount()}, " +
594-
$"shed tier {_loadShedTier} ({LoadShedTierName(_loadShedTier)}), " +
595-
$"unreliable queue {(lnl != null ? lnl.EffectiveUnreliableQueuePerPeer : 0)}/peer.");
588+
$"[CPU/POP] {pop} peers | send {parallelOptions.MaxDegreeOfParallelism}/{BasisCpuBudget.ReductionSendCap} wkr, " +
589+
$"peer-upd {peerWorkers}/{BasisCpuBudget.PeerUpdateCap} wkr " +
590+
$"(pass {lnl?.PeerUpdatePassMs ?? 0:F1}/{LiteNetLib.NetManager.PeerPassTargetMs:F0} ms), " +
591+
$"machine {BasisCpuBudget.Utilization * 100:F0}% of {BasisCpuBudget.TotalCores} cores | " +
592+
$"drops {_dropsPerPlayerWindow:F2}/player (esc {DropEscalatePerPlayer:F2}), " +
593+
$"slice {_sliceCount}/{MaxSliceCount()}, " +
594+
$"tier {_loadShedTier} {LoadShedTierName(_loadShedTier)}, " +
595+
$"unrel q {(lnl != null ? lnl.EffectiveUnreliableQueuePerPeer : 0)}/peer");
596596
}
597597
}
598598

@@ -781,6 +781,7 @@ private static void MaybeGrowSendSockets(LiteNetLib.NetManager lnl, long nowTick
781781
// Smoothed tick duration driving the load controller, and a rate limit for its log line.
782782
private static double _tickMsEma;
783783
private static long _lastSliceLogTick;
784+
private static bool _loadLegendWritten;
784785

785786
// Overrun-ratio control signal. Evaluated once per window rather than per tick so the
786787
// controller cannot chatter, and so a single slow tick cannot move it.
@@ -1398,12 +1399,19 @@ private static void RunTick(long startTick)
13981399
if (nowLog - _lastSliceLogTick > Stopwatch.Frequency * 5)
13991400
{
14001401
_lastSliceLogTick = nowLog;
1401-
BNL.Log($"[BSR] Load: {_tickOverrunRatio:P0} of ticks over budget " +
1402+
// How to read the line is worth saying once per run, not on every change: it was
1403+
// 118 characters of unchanging prose repeated all through a busy session, more
1404+
// than the numbers it was explaining.
1405+
if (!_loadLegendWritten)
1406+
{
1407+
_loadLegendWritten = true;
1408+
BNL.Log("[BSR] Load legend: period alone is harmless; tier > 0 means distant " +
1409+
"players stop updating; slicing > 1 means everyone's rate is reduced.");
1410+
}
1411+
BNL.Log($"[BSR] Load: {_tickOverrunRatio:P0} ticks over budget " +
14021412
$"(mean {_tickMsEma:F2} ms), period {intervalMs} ms " +
1403-
$"({1000 / Math.Max(1, intervalMs)} Hz), shed tier {_loadShedTier} " +
1404-
$"({LoadShedTierName(_loadShedTier)}), slicing {_sliceCount}. " +
1405-
$"Period alone is harmless; tier > 0 means distant players stop updating; " +
1406-
$"slicing > 1 means everyone's rate is reduced.");
1413+
$"({1000 / Math.Max(1, intervalMs)} Hz), " +
1414+
$"tier {_loadShedTier} {LoadShedTierName(_loadShedTier)}, slicing {_sliceCount}");
14071415
}
14081416
}
14091417
}

Basis Server/BasisNetworkServer/BasisNetworkingReductionSystem/Profiling.cs

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Text;
45
using System.Threading;
56

67
namespace BasisNetworkServer.BasisNetworkingReductionSystem
@@ -308,30 +309,45 @@ public static void TryPrint()
308309

309310
if (!WriteToLog) return;
310311

311-
BNL.Log($"\n[BSR Profile] {ticks} ticks, {msgs} msgs, {sends} sends, preSer {preSer}/{preSer + preSkip}");
312-
BNL.Log($" drain: {drain / ticks:F3} ms/tick ({drain / total * 100:F1}%)");
313-
BNL.Log($" process: {process / ticks:F3} ms/tick ({process / total * 100:F1}%)");
314-
BNL.Log($" distance: {distance / ticks:F3} ms/tick ({distance / total * 100:F1}%)");
315-
BNL.Log($" update: {update / ticks:F3} ms/tick ({update / total * 100:F1}%)");
316-
BNL.Log($" trigger: {trigger / ticks:F3} ms/tick ({trigger / total * 100:F1}%)");
317-
BNL.Log($" total: {total / ticks:F3} ms/tick");
312+
// One line, not eleven. A window carries the same 26 figures it always did, but every
313+
// BNL.Log costs a console lock, a DateTime.Now, an open/write/close on the log file and
314+
// a fistful of console attribute syscalls — the cost of reporting a window was set by
315+
// the number of calls, not by anything being measured. Percentages are dropped to whole
316+
// numbers where the ms/tick figure they derive from sits beside them.
317+
double pctOfTotal = total > 0 ? 100.0 / total : 0;
318+
319+
StringBuilder line = new StringBuilder(288);
320+
AppendInv(line, $"[BSR] {ticks}t {total / ticks:F3}ms/t");
321+
AppendPhase(line, "drain", drain, ticks, pctOfTotal);
322+
AppendPhase(line, "proc", process, ticks, pctOfTotal);
323+
AppendPhase(line, "dist", distance, ticks, pctOfTotal);
324+
AppendPhase(line, "upd", update, ticks, pctOfTotal);
325+
AppendPhase(line, "trig", trigger, ticks, pctOfTotal);
326+
AppendInv(line, $" | {msgs}msg {sends}send preser {preSer}/{preSer + preSkip}");
318327

319328
if (bEmit > 0 || bTail > 0 || bFallback > 0)
320329
{
321330
double ratio = bRaw > 0 ? (double)bComp / bRaw : 0;
322-
double avgMsgsPerBundle = bEmit > 0 ? (double)bMsg / bEmit : 0;
323-
double avgRawPerBundle = bEmit > 0 ? (double)bRaw / bEmit : 0;
324-
double avgCompPerBundle = bEmit > 0 ? (double)bComp / bEmit : 0;
331+
double savedPct = bRaw > 0 ? (ratio - 1) * 100.0 : 0; // negative: bundled bytes shrank
325332
double deflateMs = bDeflate / MsToTick;
326-
double avgDeflateUs = bEmit > 0 ? (deflateMs * 1000.0) / bEmit : 0;
327-
double bundlesPerTick = (double)bEmit / ticks;
328-
double retryRate = bEmit > 0 ? (double)bRetry / bEmit * 100.0 : 0;
329-
long savedBytes = bRaw - bComp; // raw input vs compressed output
330-
BNL.Log($" bundles: {bEmit} emitted ({bundlesPerTick:F2}/tick), {bMsg} msgs in bundles, {bTail} msgs tail-uncompressed, {bFallback} fallbacks");
331-
BNL.Log($" ratio {ratio:F3} ({(1 - ratio) * 100:F1}% saved on bundled bytes), avg {avgMsgsPerBundle:F1} msgs/bundle ({avgRawPerBundle:F0} B raw → {avgCompPerBundle:F0} B compressed)");
332-
BNL.Log($" deflate {deflateMs / ticks:F3} ms/tick ({deflateMs / total * 100:F1}% of tick), {avgDeflateUs:F1} µs/bundle, retries {bRetry} ({retryRate:F1}%)");
333-
BNL.Log($" saved ~{savedBytes / 1024.0:F1} KB this window before per-message wire overhead");
333+
double perBundle = bEmit > 0 ? 1.0 / bEmit : 0; // 0 collapses every per-bundle average to 0
334+
335+
AppendInv(line, $" | bundles {bEmit} {bEmit / (double)ticks:F2}/t {bMsg}msg {bTail}tail {bFallback}fb");
336+
AppendInv(line, $" {bMsg * perBundle:F1}msg/b {bRaw * perBundle:F0}{bComp * perBundle:F0}B {ratio:F3} {savedPct:F1}% {(bRaw - bComp) / 1024.0:F1}KB");
337+
AppendInv(line, $" deflate {deflateMs / ticks:F3}ms/t {deflateMs * pctOfTotal:F1}% {deflateMs * 1000.0 * perBundle:F1}µs/b retry {bRetry} {bRetry * perBundle * 100.0:F1}%");
334338
}
339+
340+
BNL.Log(line.ToString());
335341
}
342+
343+
/// <summary>
344+
/// Invariant culture so a comma-decimal host still emits numbers a log scraper can read —
345+
/// the same reason the health endpoint pins it.
346+
/// </summary>
347+
private static void AppendInv(StringBuilder sb, FormattableString text) =>
348+
sb.Append(FormattableString.Invariant(text));
349+
350+
private static void AppendPhase(StringBuilder sb, string name, double ms, long ticks, double pctOfTotal) =>
351+
AppendInv(sb, $" {name} {ms / ticks:F3} {ms * pctOfTotal:F0}%");
336352
}
337353
}

0 commit comments

Comments
 (0)