Loading files
7f9864208This file is pinned to the selected release. Download the original to open it in your design tool.
using Hast.Communication.Models;
using Hast.Layer;
using Hast.Transformer.SimpleMemory;
using Microsoft.Extensions.Logging;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
namespace Hast.Communication.Services;
public abstract class CommunicationServiceBase : ICommunicationService
{
protected ILogger Logger { get; }
public abstract string ChannelName { get; }
public TextWriter TesterOutput { get; set; }
protected CommunicationServiceBase(ILogger logger) => Logger = logger;
public abstract Task<IHardwareExecutionInformation> ExecuteAsync(
SimpleMemory simpleMemory,
int memberId,
IHardwareExecutionContext executionContext);
protected void EndExecution(CommunicationStateContext context)
{
context.Stopwatch.Stop();
context.HardwareExecutionInformation.FullExecutionTimeMilliseconds = context.Stopwatch.ElapsedMilliseconds;
Logger.LogInformation("Full execution time: {ElapsedMilliseconds} ms", context.Stopwatch.ElapsedMilliseconds);
}
protected async Task<CommunicationStateContext> RunExecutionAsync(
Func<CommunicationStateContext, Task> executionAction)
{
var context = BeginExecution();
await executionAction(context);
EndExecution(context);
return context;
}
protected void SetHardwareExecutionTime(
CommunicationStateContext context,
IHardwareExecutionContext executionContext,
ulong executionTimeClockCycles,
uint? clockFrequency = null)
{
var frequency = clockFrequency ?? executionContext.HardwareRepresentation.DeviceManifest.ClockFrequencyHz;
var milliseconds = 1M / frequency * 1_000 * executionTimeClockCycles;
context.HardwareExecutionInformation.HardwareExecutionTimeMilliseconds = milliseconds;
Logger.LogInformation(
"The {ClockFrequencyType} clock frequency is {Frequency:0.###} MHz",
clockFrequency == null ? "standard" : "specified",
frequency / 1_000_000.0);
Logger.LogInformation("Hardware execution took {Milliseconds:0.0000} ms.", milliseconds);
}
protected static CommunicationStateContext BeginExecution() => new()
{
Stopwatch = Stopwatch.StartNew(),
HardwareExecutionInformation = new HardwareExecutionInformation(),
};
protected class CommunicationStateContext
{
public Stopwatch Stopwatch { get; set; }
public HardwareExecutionInformation HardwareExecutionInformation { get; set; }
}
}