From 263e66c5739fde2140a7e97e5ae06f556fa0be02 Mon Sep 17 00:00:00 2001 From: tznind Date: Sun, 29 Dec 2024 17:52:03 +0000 Subject: [PATCH] Warnings and nrt --- .../ConsoleDrivers/V2/ConsoleInput.cs | 5 +- Terminal.Gui/ConsoleDrivers/V2/IMainLoop.cs | 26 ++++++---- Terminal.Gui/ConsoleDrivers/V2/Logging.cs | 17 ++++++ Terminal.Gui/ConsoleDrivers/V2/MainLoop.cs | 52 ++++++++++++++----- 4 files changed, 74 insertions(+), 26 deletions(-) diff --git a/Terminal.Gui/ConsoleDrivers/V2/ConsoleInput.cs b/Terminal.Gui/ConsoleDrivers/V2/ConsoleInput.cs index c1633f754e..3c93608fc4 100644 --- a/Terminal.Gui/ConsoleDrivers/V2/ConsoleInput.cs +++ b/Terminal.Gui/ConsoleDrivers/V2/ConsoleInput.cs @@ -18,8 +18,7 @@ public abstract class ConsoleInput : IConsoleInput /// public Func Now { get; set; } = () => DateTime.Now; - private readonly Histogram drainInputStream = Logging.Meter.CreateHistogram ("Drain Input (ms)"); - + /// public virtual void Dispose () { } @@ -51,7 +50,7 @@ public void Run (CancellationToken token) TimeSpan took = Now () - dt; TimeSpan sleepFor = TimeSpan.FromMilliseconds (20) - took; - drainInputStream.Record (took.Milliseconds); + Logging.DrainInputStream.Record (took.Milliseconds); if (sleepFor.Milliseconds > 0) { diff --git a/Terminal.Gui/ConsoleDrivers/V2/IMainLoop.cs b/Terminal.Gui/ConsoleDrivers/V2/IMainLoop.cs index 5d5f9ea407..73493eb7ea 100644 --- a/Terminal.Gui/ConsoleDrivers/V2/IMainLoop.cs +++ b/Terminal.Gui/ConsoleDrivers/V2/IMainLoop.cs @@ -4,35 +4,40 @@ namespace Terminal.Gui; /// -/// Interface for main loop that runs the core Terminal.Gui UI loop. +/// Interface for main loop that runs the core Terminal.Gui UI loop. /// /// public interface IMainLoop : IDisposable { /// - /// Gets the class responsible for servicing user timeouts and idles + /// Gets the class responsible for servicing user timeouts and idles /// public ITimedEvents TimedEvents { get; } /// - /// Gets the class responsible for writing final rendered output to the console + /// Gets the class responsible for writing final rendered output to the console /// public IOutputBuffer OutputBuffer { get; } /// - /// Gets the class responsible for processing buffered console input and translating - /// it into events on the UI thread. + /// Class for writing output to the console. + /// + public IConsoleOutput Out { get; } + + /// + /// Gets the class responsible for processing buffered console input and translating + /// it into events on the UI thread. /// public IInputProcessor InputProcessor { get; } /// - /// Gets the class responsible for sending ANSI escape requests which expect a response - /// from the remote terminal e.g. Device Attribute Request + /// Gets the class responsible for sending ANSI escape requests which expect a response + /// from the remote terminal e.g. Device Attribute Request /// public AnsiRequestScheduler AnsiRequestScheduler { get; } /// - /// Gets the class responsible for determining the current console size + /// Gets the class responsible for determining the current console size /// public IWindowSizeMonitor WindowSizeMonitor { get; } @@ -46,7 +51,8 @@ public interface IMainLoop : IDisposable void Initialize (ITimedEvents timedEvents, ConcurrentQueue inputBuffer, IInputProcessor inputProcessor, IConsoleOutput consoleOutput); /// - /// Perform a single iteration of the main loop without blocking/sleeping anywhere. + /// Perform a single iteration of the main loop then blocks for a fixed length + /// of time, this method is designed to be run in a loop. /// public void Iteration (); -} \ No newline at end of file +} diff --git a/Terminal.Gui/ConsoleDrivers/V2/Logging.cs b/Terminal.Gui/ConsoleDrivers/V2/Logging.cs index 300dac1c5a..b8034ebbe4 100644 --- a/Terminal.Gui/ConsoleDrivers/V2/Logging.cs +++ b/Terminal.Gui/ConsoleDrivers/V2/Logging.cs @@ -27,4 +27,21 @@ public static class Logging /// create your own static instrument e.g. CreateCounter, CreateHistogram etc /// internal static readonly Meter Meter = new ("Terminal.Gui"); + + /// + /// Metric for how long it takes each full iteration of the main loop to occur + /// + public static readonly Histogram TotalIterationMetric = Logging.Meter.CreateHistogram ("Iteration (ms)"); + + /// + /// Metric for how long it took to do the 'timeouts and invokes' section of main loop. + /// + public static readonly Histogram IterationInvokesAndTimeouts = Logging.Meter.CreateHistogram ("Invokes & Timers (ms)"); + + /// + /// Metric for how long it takes to read all available input from the input stream - at which + /// point input loop will sleep. + /// + public static readonly Histogram DrainInputStream = Logging.Meter.CreateHistogram ("Drain Input (ms)"); + } diff --git a/Terminal.Gui/ConsoleDrivers/V2/MainLoop.cs b/Terminal.Gui/ConsoleDrivers/V2/MainLoop.cs index 8ea9c98548..1bed07e671 100644 --- a/Terminal.Gui/ConsoleDrivers/V2/MainLoop.cs +++ b/Terminal.Gui/ConsoleDrivers/V2/MainLoop.cs @@ -1,7 +1,6 @@ #nullable enable using System.Collections.Concurrent; using System.Diagnostics; -using System.Diagnostics.Metrics; namespace Terminal.Gui; @@ -9,6 +8,11 @@ namespace Terminal.Gui; public class MainLoop : IMainLoop { private ITimedEvents? _timedEvents; + private ConcurrentQueue? _inputBuffer; + private IInputProcessor? _inputProcessor; + private IConsoleOutput? _out; + private AnsiRequestScheduler? _ansiRequestScheduler; + private IWindowSizeMonitor? _windowSizeMonitor; /// public ITimedEvents TimedEvents @@ -24,16 +28,42 @@ public ITimedEvents TimedEvents /// thread by a . Is drained as part of each /// /// - public ConcurrentQueue InputBuffer { get; private set; } + public ConcurrentQueue InputBuffer + { + get => _inputBuffer ?? throw new NotInitializedException (nameof (InputBuffer)); + private set => _inputBuffer = value; + } - public IInputProcessor InputProcessor { get; private set; } + /// + public IInputProcessor InputProcessor + { + get => _inputProcessor ?? throw new NotInitializedException (nameof (InputProcessor)); + private set => _inputProcessor = value; + } + /// public IOutputBuffer OutputBuffer { get; } = new OutputBuffer (); - public IConsoleOutput Out { get; private set; } - public AnsiRequestScheduler AnsiRequestScheduler { get; private set; } + /// + public IConsoleOutput Out + { + get => _out ?? throw new NotInitializedException (nameof (Out)); + private set => _out = value; + } + + /// + public AnsiRequestScheduler AnsiRequestScheduler + { + get => _ansiRequestScheduler ?? throw new NotInitializedException (nameof (AnsiRequestScheduler)); + private set => _ansiRequestScheduler = value; + } - public IWindowSizeMonitor WindowSizeMonitor { get; private set; } + /// + public IWindowSizeMonitor WindowSizeMonitor + { + get => _windowSizeMonitor ?? throw new NotInitializedException (nameof (WindowSizeMonitor)); + private set => _windowSizeMonitor = value; + } /// /// Determines how to get the current system type, adjust @@ -41,10 +71,6 @@ public ITimedEvents TimedEvents /// public Func Now { get; set; } = () => DateTime.Now; - private static readonly Histogram totalIterationMetric = Logging.Meter.CreateHistogram ("Iteration (ms)"); - - private static readonly Histogram iterationInvokesAndTimeouts = Logging.Meter.CreateHistogram ("Invokes & Timers (ms)"); - public void Initialize (ITimedEvents timedEvents, ConcurrentQueue inputBuffer, IInputProcessor inputProcessor, IConsoleOutput consoleOutput) { InputBuffer = inputBuffer; @@ -67,7 +93,7 @@ public void Iteration () TimeSpan took = Now () - dt; TimeSpan sleepFor = TimeSpan.FromMilliseconds (50) - took; - totalIterationMetric.Record (took.Milliseconds); + Logging.TotalIterationMetric.Record (took.Milliseconds); if (sleepFor.Milliseconds > 0) { @@ -75,7 +101,7 @@ public void Iteration () } } - public void IterationImpl () + internal void IterationImpl () { InputProcessor.ProcessQueue (); @@ -100,7 +126,7 @@ public void IterationImpl () TimedEvents.LockAndRunIdles (); - iterationInvokesAndTimeouts.Record (swCallbacks.Elapsed.Milliseconds); + Logging.IterationInvokesAndTimeouts.Record (swCallbacks.Elapsed.Milliseconds); } private bool AnySubviewsNeedDrawn (View v)