Skip to content
Merged
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
29 changes: 22 additions & 7 deletions Shared/Cluster/Patches/HitchRecorder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ public static class Bucket
public const int SessionComponents = 5;
public const int StoreJournal = 6;
public const int WorldAuthorityWait = 7;
public const int Count = 8;

// These three parts are included in StoreJournal.
public const int StoreJournalLock = 8;
public const int StoreJournalAppend = 9;
public const int StoreJournalFsync = 10;
public const int Count = 11;
public static readonly string[] Names =
{
"physics",
Expand All @@ -101,6 +106,9 @@ public static class Bucket
"components",
"store_journal",
"wa_wait",
"store_journal_lock",
"store_journal_append",
"store_journal_fsync",
};
}

Expand All @@ -111,14 +119,14 @@ public static class Bucket
private static int[] m_bucketCalls;

/// <summary>Accumulate elapsed time since <paramref name="startTimestamp"/> into one bucket.</summary>
public static void Add(int bucket, long startTimestamp)
public static void Add(int bucket, long startTimestamp) =>
AddTicks(bucket, System.Diagnostics.Stopwatch.GetTimestamp() - startTimestamp);

internal static void AddTicks(int bucket, long ticks)
{
double[] buckets = m_buckets ??= new double[Bucket.Count];
int[] calls = m_bucketCalls ??= new int[Bucket.Count];
buckets[bucket] +=
(System.Diagnostics.Stopwatch.GetTimestamp() - startTimestamp)
* 1000.0
/ System.Diagnostics.Stopwatch.Frequency;
buckets[bucket] += ticks * 1000.0 / System.Diagnostics.Stopwatch.Frequency;
calls[bucket]++;
}

Expand All @@ -131,7 +139,14 @@ private static string Breakdown()
return "";
System.Text.StringBuilder text = new System.Text.StringBuilder();
for (int i = 0; i < Bucket.Count; i++)
if (buckets[i] > 0.05)
if (
buckets[i] > 0.05
|| (
i is >= Bucket.StoreJournalLock and <= Bucket.StoreJournalFsync
&& calls[i] > 0
&& buckets[Bucket.StoreJournal] > 0.05
)
)
text.Append(' ')
.Append(Bucket.Names[i])
.Append('=')
Expand Down
72 changes: 49 additions & 23 deletions Shared/Cluster/StorePaymentJournal.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.Json;
Expand Down Expand Up @@ -242,16 +243,59 @@ private static void Append(Entry entry)
{
if (!m_ready)
return;
long started = System.Diagnostics.Stopwatch.GetTimestamp();
long started = Stopwatch.GetTimestamp();
long acquired = 0,
flushStarted = 0,
flushEnded = 0;
try
{
AppendLocked(entry);
lock (Sync)
{
acquired = Stopwatch.GetTimestamp();
Directory.CreateDirectory(Path.GetDirectoryName(m_path));
using FileStream stream = new FileStream(
m_path,
FileMode.Append,
FileAccess.Write,
FileShare.Read
);
using StreamWriter writer = new StreamWriter(stream);
writer.WriteLine(JsonSerializer.Serialize(entry));
writer.Flush();
flushStarted = Stopwatch.GetTimestamp();
try
{
stream.Flush(true);
}
finally
{
flushEnded = Stopwatch.GetTimestamp();
}
}
}
finally
{
// SE-1883: a store trade appends on the game thread, so a slow disk shows in the hitch line as store_journal.
HitchRecorder.Add(HitchRecorder.Bucket.StoreJournal, started);
long ticks = System.Diagnostics.Stopwatch.GetTimestamp() - started;
long ended = Stopwatch.GetTimestamp();
long ticks = ended - started;
HitchRecorder.AddTicks(HitchRecorder.Bucket.StoreJournal, ticks);
HitchRecorder.AddTicks(
HitchRecorder.Bucket.StoreJournalLock,
(acquired != 0 ? acquired : ended) - started
);
if (acquired != 0)
{
// Include directory creation, open, serialization and the buffered write.
long writeEnded = flushStarted != 0 ? flushStarted : ended;
HitchRecorder.AddTicks(
HitchRecorder.Bucket.StoreJournalAppend,
writeEnded - acquired
);
}
if (flushStarted != 0)
HitchRecorder.AddTicks(
HitchRecorder.Bucket.StoreJournalFsync,
flushEnded - flushStarted
);
Interlocked.Increment(ref AppendCount);
Interlocked.Add(ref AppendTicks, ticks);
long max;
Expand All @@ -261,24 +305,6 @@ private static void Append(Entry entry)
}
}

private static void AppendLocked(Entry entry)
{
lock (Sync)
{
Directory.CreateDirectory(Path.GetDirectoryName(m_path));
using FileStream stream = new FileStream(
m_path,
FileMode.Append,
FileAccess.Write,
FileShare.Read
);
using StreamWriter writer = new StreamWriter(stream);
writer.WriteLine(JsonSerializer.Serialize(entry));
writer.Flush();
stream.Flush(true);
}
}

/// <summary>
/// Queues the open legs of every journaled payment for settling, once per process. Reads the journal file, so
/// callers run it off the game thread.
Expand Down
1 change: 1 addition & 0 deletions Test/ClusterTestPlugin/Source/StorePaymentBurstRoutes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ double Percentile(double p) =>
paymentP90Ms = Math.Round(Percentile(0.9), 2),
paymentMaxMs = Math.Round(sorted.LastOrDefault(), 2),
paymentTotalMs = Math.Round(sorted.Sum(), 1),
frameBreakdown = HitchRecorder.Breakdown(),
journalAppends = journalCount,
journalTotalMs = Math.Round(journalMs, 1),
journalMaxMs = Math.Round(
Expand Down
20 changes: 20 additions & 0 deletions Test/scenarios_wa_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1822,6 +1822,26 @@ def burst(label: str) -> dict:
result["paymentP90Ms"] < STORE_BURST_P90_MS, json.dumps(result))
bench.check(f"{label}: a payment journal append took under {STORE_JOURNAL_MEAN_MS} ms on average",
result["journalTotalMs"] < STORE_JOURNAL_MEAN_MS * max(1, result["journalAppends"]), json.dumps(result))
# Read the same frame breakdown the node writes to its hitch log.
parts = {
name: (float(ms), int(calls))
for name, ms, calls in re.findall(
r" (store_journal(?:_\w+)?)=([\d.]+)/(\d+)",
result.get("frameBreakdown", ""),
)
}
total, calls = parts.get("store_journal", (0, 0))
children = [
parts.get("store_journal_" + name, (0, 0))
for name in ("lock", "append", "fsync")
]
bench.check(
f"{label}: each game-thread journal append reports lock, append and fsync time",
calls > 0
and all(n == calls for _, n in children)
and sum(ms for ms, _ in children) <= total + 0.21,
result.get("frameBreakdown", ""),
)
paid = quiet["payments"] + busy["payments"]
started = time.monotonic()
settled = _poll(lambda: _wa_money(bench, payer) == 1_000_000 - paid and _wa_money(bench, payee) == paid, STORE_SETTLE_TIMEOUT)
Expand Down