Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paprika.Cli provides more stats #351

Merged
merged 3 commits into from
Jun 11, 2024
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
2 changes: 1 addition & 1 deletion src/Paprika.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public override int Execute(CommandContext context, StatisticsSettings settings)

var stats = new Layout("Stats");

AnsiConsole.Write("Gathering stats...");
AnsiConsole.WriteLine("Gathering stats...");

StatisticsForPagedDb.Report(stats, read);

Expand Down
4 changes: 3 additions & 1 deletion src/Paprika.Runner.Pareto/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ public static async Task Main(String[] args)

var state = new StatisticsReporter(TrieType.State);
var storage = new StatisticsReporter(TrieType.Storage);
read.Report(state, storage);
var counting = new PageCountingReporter();

read.Report(state, storage, counting, out _);

spectre.Cancel();
await reportingTask;
Expand Down
17 changes: 13 additions & 4 deletions src/Paprika.Runner/StatisticsForPagedDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@ public static void Report(Layout reportTo, IReporting read)
{
var state = new StatisticsReporter(TrieType.State);
var storage = new StatisticsReporter(TrieType.Storage);
var ids = new PageCountingReporter();

read.Report(state, storage);
read.Report(state, storage, ids, out var totalAbandoned);

var report = new Layout()
.SplitColumns(
BuildReport(state, "State"),
BuildReport(storage, "Storage"));
.SplitRows(
new Layout("top")
.SplitColumns(
BuildReport(state, "State"),
BuildReport(storage, "Storage")),
new Layout("bottom")
.Update(new Panel(new Paragraph(
$"- pages used for id mapping: {Page.FormatAsGb(ids.Count)}\n" +
$"- total pages abandoned: {Page.FormatAsGb(totalAbandoned)}\n" +
"")).Header("Other stats").Expand())
);

reportTo.Update(new Panel(report).Header("Paprika tree statistics").Expand());
}
Expand Down
17 changes: 16 additions & 1 deletion src/Paprika.Tests/Store/PagedDbTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,22 @@ static void Assert(PagedDb db)
var state = new StatisticsReporter(TrieType.State);
var storage = new StatisticsReporter(TrieType.Storage);

read.Report(state, storage);
read.Report(state, storage, new JustLookingReporter(), out _);
}
}

private class JustLookingReporter : IReporter
{
public void ReportDataUsage(PageType type, int pageLevel, int trimmedNibbles, in SlottedArray array)
{
}

public void ReportPage(uint ageInBatches, PageType type)
{
}

public void ReportLeafOverflowCount(byte count)
{
}
}

Expand Down
24 changes: 23 additions & 1 deletion src/Paprika/Store/AbandonedList.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;

namespace Paprika.Store;
Expand Down Expand Up @@ -207,4 +208,25 @@ public void Accept(IPageVisitor visitor, IPageResolver resolver)
}
}
}
}

[Pure]
public long GatherTotalAbandoned(IPageResolver resolver)
{
resolver.Prefetch(Addresses);

long count = 0;

foreach (var addr in Addresses[..(int)EntriesCount])
{
var current = addr;
while (current.IsNull == false)
{
var abandoned = new AbandonedPage(resolver.GetAt(current));
count += abandoned.Count;
current = abandoned.Next;
}
}

return count;
}
}
24 changes: 21 additions & 3 deletions src/Paprika/Store/IReporter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
using HdrHistogram;
using Paprika.Crypto;
using Paprika.Data;
Expand All @@ -26,7 +24,27 @@ public interface IReporter

public interface IReporting
{
void Report(IReporter state, IReporter storage);
void Report(IReporter state, IReporter storage, IReporter ids, out long totalAbandoned);
}

public class PageCountingReporter : IReporter
{
public long Count { get; private set; }

public void ReportDataUsage(PageType type, int pageLevel, int trimmedNibbles, in SlottedArray array)
{
Count++;
}

public void ReportPage(uint ageInBatches, PageType type)
{
throw new NotImplementedException();
}

public void ReportLeafOverflowCount(byte count)
{
throw new NotImplementedException();
}
}

public class StatisticsReporter(TrieType trieType) : IReporter
Expand Down
2 changes: 2 additions & 0 deletions src/Paprika/Store/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,6 @@ public struct PageHeader

public static Page DevOnlyNativeAlloc() =>
new((byte*)NativeMemory.AlignedAlloc(PageSize, PageSize));

public static string FormatAsGb(long pageCount) => $"{(double)pageCount * PageSize / 1024 / 1024 / 1024:F2)}GB";
}
12 changes: 9 additions & 3 deletions src/Paprika/Store/PagedDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -484,14 +484,20 @@ public bool TryGet(scoped in Key key, out ReadOnlySpan<byte> result)
return root.TryGet(key, this, out result);
}

public void Report(IReporter state, IReporter storage)
public void Report(IReporter state, IReporter storage, IReporter ids, out long totalAbandoned)
{
if (root.Data.StateRoot.IsNull == false)
ref readonly var data = ref root.Data;

totalAbandoned = 0;
totalAbandoned = data.AbandonedList.GatherTotalAbandoned(this);

if (data.StateRoot.IsNull == false)
{
new Merkle.StateRootPage(GetAt(root.Data.StateRoot)).Report(state, this, 0, 0);
}

root.Data.Storage.Report(storage, this, 0, 0);
data.Storage.Report(storage, this, 0, 0);
data.Ids.Report(ids, this, 0, 0);
}

public uint BatchId => root.Header.BatchId;
Expand Down
Loading