diff --git a/Shared/Cluster/Patches/HitchRecorder.cs b/Shared/Cluster/Patches/HitchRecorder.cs
index 731826b..bff64e3 100644
--- a/Shared/Cluster/Patches/HitchRecorder.cs
+++ b/Shared/Cluster/Patches/HitchRecorder.cs
@@ -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",
@@ -101,6 +106,9 @@ public static class Bucket
"components",
"store_journal",
"wa_wait",
+ "store_journal_lock",
+ "store_journal_append",
+ "store_journal_fsync",
};
}
@@ -111,14 +119,14 @@ public static class Bucket
private static int[] m_bucketCalls;
/// Accumulate elapsed time since into one bucket.
- 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]++;
}
@@ -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('=')
diff --git a/Shared/Cluster/StorePaymentJournal.cs b/Shared/Cluster/StorePaymentJournal.cs
index e55ae6e..86e78d9 100644
--- a/Shared/Cluster/StorePaymentJournal.cs
+++ b/Shared/Cluster/StorePaymentJournal.cs
@@ -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;
@@ -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;
@@ -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);
- }
- }
-
///
/// 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.
diff --git a/Test/ClusterTestPlugin/Source/StorePaymentBurstRoutes.cs b/Test/ClusterTestPlugin/Source/StorePaymentBurstRoutes.cs
index cdcb3b2..cb7b084 100644
--- a/Test/ClusterTestPlugin/Source/StorePaymentBurstRoutes.cs
+++ b/Test/ClusterTestPlugin/Source/StorePaymentBurstRoutes.cs
@@ -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(
diff --git a/Test/scenarios_wa_ops.py b/Test/scenarios_wa_ops.py
index 4c91dec..f71333a 100644
--- a/Test/scenarios_wa_ops.py
+++ b/Test/scenarios_wa_ops.py
@@ -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)