Skip to content

Commit

Permalink
Added configurable interval values for periodic report. Added console…
Browse files Browse the repository at this point in the history
… shortcut key to display periodic report.
  • Loading branch information
De-Crypted committed Apr 11, 2022
1 parent 2392f32 commit cfb5815
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 93 deletions.
190 changes: 102 additions & 88 deletions Managers/StatusManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ public StatusManager(IConfiguration configuration)
public Task StartAsync(CancellationToken cancellationToken)
{
Watch = new Stopwatch();
Watch.Start();

new Thread(() => ReportProgress(ThreadSource.Token))
new Thread(() => PeriodicReportTimer(ThreadSource.Token))
.UnsafeStart();
new Thread(() => CollectHashrate(ThreadSource.Token))
.UnsafeStart();
Expand Down Expand Up @@ -74,7 +75,11 @@ public static ulong GetHashrate(String type, int id, TimeSpan from)
}

if (snapshot == null) {
return 0;
snapshot = new Snapshot
{
timestamp = DateTime.Now.AddMilliseconds(Watch.ElapsedMilliseconds * -1),
hashrate = 0
};
}

var timeBetween = latest.timestamp - snapshot.timestamp;
Expand All @@ -93,122 +98,131 @@ public static ulong GetHashrate(String type, int id, TimeSpan from)
}
}

private void CollectHashrate(CancellationToken token)
public static void DoPeriodicReport()
{
var gpuEnabled = Configuration.GetValue<bool>("gpu:enabled");
var cpuEnabled = Configuration.GetValue<bool>("cpu:enabled");
CollectHashrateSnapshot();

var sb = new StringBuilder();
sb.AppendLine("|---------------------------------------|");
sb.AppendLine("| Periodic Report\t\t\t|");
sb.AppendLine("|---------------------------------------|");
sb.AppendFormat("| Accepted \t\t{0}\t\t|{1}", Interlocked.Read(ref AcceptedShares), Environment.NewLine);
sb.AppendFormat("| Dropped \t\t{0}\t\t|{1}", Interlocked.Read(ref DroppedShares), Environment.NewLine);
sb.AppendFormat("| Rejected \t\t{0}\t\t|{1}", Interlocked.Read(ref RejectedShares), Environment.NewLine);

ulong totalHashes = 0;

if (CpuHashCount.Length > 0) {
var cpuHashes = GetHashrate("CPU", 0, TimeSpan.FromMinutes(1));
CalculateUnit(cpuHashes, out double cpu_hashrate, out string cpu_unit);
sb.AppendFormat("| Hashrate (CPU) \t{0:N2} {1}\t|{2}", cpu_hashrate, cpu_unit, Environment.NewLine);

totalHashes += cpuHashes;
}

while(!token.IsCancellationRequested) {
bool lockTaken = false;
try {
SpinLock.Enter(ref lockTaken);
ulong hashes = 0;
if (GpuHashCount.Length > 0) {
for (int i = 0; i < GpuHashCount.Length; i++) {
var gpuHashes = GetHashrate("GPU", i, TimeSpan.FromMinutes(1));
CalculateUnit(gpuHashes, out double gpu_hashrate, out string gpu_unit);
sb.AppendFormat("| Hashrate (GPU #{0}) \t{1:N2} {2}\t|{3}",
i,
gpu_hashrate,
gpu_unit,
Environment.NewLine);

totalHashes += gpuHashes;
}
}

// keep only snapshots from past 10 minutes
var expiredAt = DateTime.Now.AddMinutes(-10);
HashrateSnapshots.RemoveAll(p => p.timestamp <= expiredAt);
if ((CpuHashCount.Length + GpuHashCount.Length) > 1) {
CalculateUnit(totalHashes, out double hashrate, out string unit);
sb.AppendFormat("| Hashrate (Total) \t{0:N2} {1}\t|{2}", hashrate, unit, Environment.NewLine);
}

if (cpuEnabled) {
for (int i = 0; i < CpuHashCount.Length; i++) {
hashes += CpuHashCount[i];
}
sb.AppendLine("|---------------------------------------|");
sb.AppendFormat("Uptime {0} days, {1} hours, {2} minutes", Watch.Elapsed.Days, Watch.Elapsed.Hours, Watch.Elapsed.Minutes);

SafeConsole.WriteLine(ConsoleColor.White, sb.ToString());
}

HashrateSnapshots.Add(new Snapshot {
timestamp = DateTime.Now,
type = "CPU",
id = 0,
hashrate = hashes
});
}
private static void CollectHashrateSnapshot()
{
bool lockTaken = false;
try {
SpinLock.Enter(ref lockTaken);
ulong hashes = 0;

if (gpuEnabled) {
for (int i = 0; i < GpuHashCount.Length; i++) {
HashrateSnapshots.Add(new Snapshot {
timestamp = DateTime.Now,
type = "GPU",
id = i,
hashrate = GpuHashCount[i]
});

hashes += GpuHashCount[i];
}
// keep only snapshots from past 10 minutes
var expiredAt = DateTime.Now.AddMinutes(-10);
HashrateSnapshots.RemoveAll(p => p.timestamp <= expiredAt);

if (CpuHashCount.Length > 0) {
for (int i = 0; i < CpuHashCount.Length; i++) {
hashes += CpuHashCount[i];
}

HashrateSnapshots.Add(new Snapshot {
timestamp = DateTime.Now,
type = "TOTAL",
type = "CPU",
id = 0,
hashrate = hashes
});
} catch (Exception ex) {
SafeConsole.WriteLine(ConsoleColor.DarkRed, ex.ToString());
}
finally {
if (lockTaken) {
SpinLock.Exit(false);

if (GpuHashCount.Length > 0) {
for (int i = 0; i < GpuHashCount.Length; i++) {
HashrateSnapshots.Add(new Snapshot {
timestamp = DateTime.Now,
type = "GPU",
id = i,
hashrate = GpuHashCount[i]
});

hashes += GpuHashCount[i];
}
}

token.WaitHandle.WaitOne(TimeSpan.FromSeconds(10));
HashrateSnapshots.Add(new Snapshot {
timestamp = DateTime.Now,
type = "TOTAL",
id = 0,
hashrate = hashes
});
} catch (Exception ex) {
SafeConsole.WriteLine(ConsoleColor.DarkRed, ex.ToString());
}
finally {
if (lockTaken) {
SpinLock.Exit(false);
}
}
}

private void ReportProgress(CancellationToken token)
private void CollectHashrate(CancellationToken token)
{
var gpuEnabled = Configuration.GetValue<bool>("gpu:enabled");
var cpuEnabled = Configuration.GetValue<bool>("cpu:enabled");

Watch.Start();
token.WaitHandle.WaitOne(TimeSpan.FromSeconds(30));

while(!token.IsCancellationRequested) {
var sb = new StringBuilder();
sb.AppendLine("|---------------------------------------|");
sb.AppendLine("| Periodic Report\t\t\t|");
sb.AppendLine("|---------------------------------------|");
sb.AppendFormat("| Accepted \t\t{0}\t\t|{1}", Interlocked.Read(ref AcceptedShares), Environment.NewLine);
sb.AppendFormat("| Dropped \t\t{0}\t\t|{1}", Interlocked.Read(ref DroppedShares), Environment.NewLine);
sb.AppendFormat("| Rejected \t\t{0}\t\t|{1}", Interlocked.Read(ref RejectedShares), Environment.NewLine);

ulong totalHashes = 0;

if (cpuEnabled) {
var cpuHashes = GetHashrate("CPU", 0, TimeSpan.FromMinutes(1));
CalculateUnit(cpuHashes, out double cpu_hashrate, out string cpu_unit);
sb.AppendFormat("| Hashrate (CPU) \t{0:N2} {1}\t|{2}", cpu_hashrate, cpu_unit, Environment.NewLine);

totalHashes += cpuHashes;
}

if (gpuEnabled) {
for (int i = 0; i < GpuHashCount.Length; i++) {
var gpuHashes = GetHashrate("GPU", i, TimeSpan.FromMinutes(1));
CalculateUnit(gpuHashes, out double gpu_hashrate, out string gpu_unit);
sb.AppendFormat("| Hashrate (GPU #{0}) \t{1:N2} {2}\t|{3}",
i,
gpu_hashrate,
gpu_unit,
Environment.NewLine);

totalHashes += gpuHashes;
}
}
CollectHashrateSnapshot();
token.WaitHandle.WaitOne(TimeSpan.FromSeconds(10));
}
}

if ((CpuHashCount.Length + GpuHashCount.Length) > 1) {
CalculateUnit(totalHashes, out double hashrate, out string unit);
sb.AppendFormat("| Hashrate (Total) \t{0:N2} {1}\t|{2}", hashrate, unit, Environment.NewLine);
}
private void PeriodicReportTimer(CancellationToken token)
{
var delay = Configuration.GetValue<int>("periodic_report:initial_delay", 30);
var interval = Configuration.GetValue<int>("periodic_report:report_interval", 180);

sb.AppendLine("|---------------------------------------|");
sb.AppendFormat("Uptime {0} days, {1} hours, {2} minutes", Watch.Elapsed.Days, Watch.Elapsed.Hours, Watch.Elapsed.Minutes);

SafeConsole.WriteLine(ConsoleColor.White, sb.ToString());
token.WaitHandle.WaitOne(TimeSpan.FromSeconds(delay));

token.WaitHandle.WaitOne(TimeSpan.FromMinutes(3));
while(!token.IsCancellationRequested) {
DoPeriodicReport();
token.WaitHandle.WaitOne(TimeSpan.FromSeconds(interval));
}
}

private void CalculateUnit(double hashrate, out double adjusted_hashrate, out string unit) {
private static void CalculateUnit(double hashrate, out double adjusted_hashrate, out string unit) {
if (hashrate > 1000000000000) {
adjusted_hashrate = hashrate / 1000000000000;
unit = "TH/s";
Expand Down Expand Up @@ -237,7 +251,7 @@ private void CalculateUnit(double hashrate, out double adjusted_hashrate, out st
unit = "H/s";
}

private class Snapshot {
public class Snapshot {
public DateTime timestamp { get; set; }
public String type { get; set; }
public int id { get; set; }
Expand Down
22 changes: 17 additions & 5 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,24 @@ static async Task Main(string[] args)
.ConfigureServices(ConfigureServices)
.Build();

var version = new Version("0.4-beta".Substring(0, Math.Min("0.4-beta".IndexOf("-"), "0.4-beta".Length)));
Console.WriteLine(version.ToString());
Console.WriteLine(version >= new Version(0, 4));

Console.Title = "dcrptd miner";
await host.RunAsync();
await host.StartAsync();

Console.TreatControlCAsInput = true;
while (true) {
var key = Console.ReadKey(true);

switch (key.Key) {
case ConsoleKey.C:
if (key.Modifiers == ConsoleModifiers.Control) {
Process.GetCurrentProcess().Kill();
}
break;
case ConsoleKey.S:
StatusManager.DoPeriodicReport();
break;
}
}
}

private static void ConfigureServices(IServiceCollection services)
Expand Down
6 changes: 6 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
"_threads": "Set amount of threads to run. -1 = auto detect thread count",
"threads": -1
},
"periodic_report": {
"_initial_delay": "Delay in secodns to wait before first Periodic Report",
"initial_delay": 30,
"_report_interval": "Delay in seconds between Periodic Reports",
"report_interval": 180
},
"Logging": {
"Loglevel": {
"Default": "Warning"
Expand Down

0 comments on commit cfb5815

Please sign in to comment.