From 27aa2cc2b14ec7b8d3da30b653f8b8abd392ce5a Mon Sep 17 00:00:00 2001 From: Alex Yakunin Date: Mon, 4 Nov 2024 20:05:33 -0800 Subject: [PATCH 1/3] fix: jit-times - Timestamp arithmetics + add warnings for negative times --- tools/jit-times/Program.cs | 11 +++- tools/jit-times/Timestamp.cs | 108 ++++++++--------------------------- 2 files changed, 33 insertions(+), 86 deletions(-) diff --git a/tools/jit-times/Program.cs b/tools/jit-times/Program.cs index 038bc0b6573..277ca96e324 100644 --- a/tools/jit-times/Program.cs +++ b/tools/jit-times/Program.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Numerics; using System.Text.RegularExpressions; using static System.Console; @@ -159,7 +160,7 @@ public static int Main (string [] args) var info = GetMethodInfo (method); if (info.state != MethodInfo.State.None && Verbose) - Warning ($"duplicit begin of `{info.method}`"); + Warning ($"duplicite begin of `{info.method}`"); info.state = MethodInfo.State.Begin; info.begin = time; @@ -183,6 +184,12 @@ public static int Main (string [] args) info.total = info.done - info.begin; info.CalcSelfTime (); + if (Verbose) { + if (info.self.nanoseconds < 0) + Warning ($"negative self time for method {method}: {info.self}"); + if (info.total.nanoseconds < 0) + Warning ($"negative total time for method {method}: {info.total}"); + } jitMethods.Pop (); @@ -229,7 +236,7 @@ static Timestamp PrintSortedMethods () var info = pair.Value; WriteLine ($"{info.total.Milliseconds (),10:F2} | {info.self.Milliseconds (),10:F2} | {info.method}"); - sum += info.self; + sum += info.self.Positive(); } return sum; diff --git a/tools/jit-times/Timestamp.cs b/tools/jit-times/Timestamp.cs index 8f415d6fcee..8cd1582589c 100644 --- a/tools/jit-times/Timestamp.cs +++ b/tools/jit-times/Timestamp.cs @@ -1,113 +1,53 @@ using System; +using System.Collections.Generic; using System.Text.RegularExpressions; namespace jittimes { - public struct Timestamp : IComparable { - public Int64 seconds; - public int milliseconds; - public int nanoseconds; - + public record struct Timestamp(long nanoseconds) : IComparable { static readonly Regex regex = new Regex ("^([0-9]+)s:([0-9]+)::([0-9]+)$"); public static Timestamp Parse (string time) { - Timestamp ts = new Timestamp (); - var match = regex.Match (time); - if (!match.Success || match.Groups.Count <= 3) { - ts.seconds = 0; - ts.milliseconds = 0; - ts.nanoseconds = 0; - return ts; - } + if (!match.Success || match.Groups.Count <= 3) + return default; - ts.seconds = Convert.ToInt64 (match.Groups [1].Value); - ts.milliseconds = Convert.ToInt32 (match.Groups [2].Value); - ts.nanoseconds = Convert.ToInt32 (match.Groups [3].Value); - - return ts; + var s = Convert.ToInt64 (match.Groups [1].Value); + var ms = Convert.ToInt32 (match.Groups [2].Value); + var ns = Convert.ToInt32 (match.Groups [3].Value); + return new Timestamp(1000_000_000*s + 1000_000*ms + ns); } static public Timestamp operator - (Timestamp ts1, Timestamp ts2) - { - Timestamp result = new Timestamp (); - - if (ts1.nanoseconds >= ts2.nanoseconds) - result.nanoseconds = ts1.nanoseconds - ts2.nanoseconds; - else { - result.nanoseconds = 1000000 + ts1.nanoseconds - ts2.nanoseconds; - result.milliseconds--; - } - - if (ts1.milliseconds >= ts2.milliseconds) - result.milliseconds += ts1.milliseconds - ts2.milliseconds; - else { - result.milliseconds += 1000 + ts1.milliseconds - ts2.milliseconds; - result.seconds--; - } - - result.seconds += ts1.seconds - ts2.seconds; - - return result; - } + => new Timestamp(ts1.nanoseconds - ts2.nanoseconds); static public Timestamp operator + (Timestamp ts1, Timestamp ts2) - { - Timestamp result = new Timestamp { - nanoseconds = ts1.nanoseconds + ts2.nanoseconds - }; - - if (result.nanoseconds > 1000000) { - result.milliseconds += result.nanoseconds / 1000000; - result.nanoseconds %= 1000000; - } - - result.milliseconds += ts1.milliseconds + ts2.milliseconds; - - if (result.milliseconds > 1000) { - result.seconds += result.milliseconds / 1000; - result.milliseconds %= 1000; - } - - return result; - } + => new Timestamp(ts1.nanoseconds + ts2.nanoseconds); public override string ToString () { - var sec = seconds != 0 ? $"{seconds}(s):" : ""; - - return $"{sec}{milliseconds}::{nanoseconds}"; + var remainder = Math.Abs(nanoseconds); + var s = remainder / 1000_000_000; + remainder -= 1000_000_000*s; + var ms = remainder / 1000_000; + var ns = remainder - 1000_000*ms; + var sign = nanoseconds < 0 ? "-" : ""; + var sec = s != 0 ? $"{s}(s):" : ""; + return $"{sign}{sec}{ms}::{ns}"; } + public Timestamp Positive () + => new Timestamp(Math.Max(0L, nanoseconds)); + public double Milliseconds () - { - return seconds * 1000.0 + (double)milliseconds + nanoseconds / 1000000.0; - } + => nanoseconds / 1000_000; - public int CompareTo (object o) + public int CompareTo(object o) { if (!(o is Timestamp other)) throw new ArgumentException ("Object is not a Timestamp"); - if (seconds > other.seconds) - return 1; - - if (seconds < other.seconds) - return -1; - - if (milliseconds > other.milliseconds) - return 1; - - if (milliseconds < other.milliseconds) - return -1; - - if (nanoseconds > other.nanoseconds) - return 1; - - if (nanoseconds < other.nanoseconds) - return -1; - - return 0; + return Comparer.Default.Compare(this.nanoseconds, other.nanoseconds); } } } From f8bbaaf7cb1bd9d5596c45e6b78e525e5531c5d4 Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Tue, 5 Nov 2024 09:13:19 -0600 Subject: [PATCH 2/3] Update tools/jit-times/Program.cs --- tools/jit-times/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/jit-times/Program.cs b/tools/jit-times/Program.cs index 277ca96e324..2d18f9e9773 100644 --- a/tools/jit-times/Program.cs +++ b/tools/jit-times/Program.cs @@ -160,7 +160,7 @@ public static int Main (string [] args) var info = GetMethodInfo (method); if (info.state != MethodInfo.State.None && Verbose) - Warning ($"duplicite begin of `{info.method}`"); + Warning ($"duplicate begin of `{info.method}`"); info.state = MethodInfo.State.Begin; info.begin = time; From d1cd94e916ad13e67762e19b3ca38d8c48f9dca1 Mon Sep 17 00:00:00 2001 From: Jonathan Pryor Date: Wed, 13 Nov 2024 10:50:25 -0800 Subject: [PATCH 3/3] Update Timestamp.cs Space ` ` before `(`. Use `1_000_000` instead of `1000_000`. Add `checked()` arithmetic. We don't want Timestamp.operator+() to wrap around to a negative value; that's not useful. --- tools/jit-times/Timestamp.cs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tools/jit-times/Timestamp.cs b/tools/jit-times/Timestamp.cs index 8cd1582589c..1d1bf5c4bd1 100644 --- a/tools/jit-times/Timestamp.cs +++ b/tools/jit-times/Timestamp.cs @@ -3,7 +3,7 @@ using System.Text.RegularExpressions; namespace jittimes { - public record struct Timestamp(long nanoseconds) : IComparable { + public record struct Timestamp (long nanoseconds) : IComparable { static readonly Regex regex = new Regex ("^([0-9]+)s:([0-9]+)::([0-9]+)$"); public static Timestamp Parse (string time) @@ -15,39 +15,39 @@ public static Timestamp Parse (string time) var s = Convert.ToInt64 (match.Groups [1].Value); var ms = Convert.ToInt32 (match.Groups [2].Value); var ns = Convert.ToInt32 (match.Groups [3].Value); - return new Timestamp(1000_000_000*s + 1000_000*ms + ns); + return new Timestamp (1_000_000*s + 1_000_000*ms + ns); } static public Timestamp operator - (Timestamp ts1, Timestamp ts2) - => new Timestamp(ts1.nanoseconds - ts2.nanoseconds); + => new Timestamp (checked (ts1.nanoseconds - ts2.nanoseconds)); static public Timestamp operator + (Timestamp ts1, Timestamp ts2) - => new Timestamp(ts1.nanoseconds + ts2.nanoseconds); + => new Timestamp (checked (ts1.nanoseconds + ts2.nanoseconds)); public override string ToString () { - var remainder = Math.Abs(nanoseconds); - var s = remainder / 1000_000_000; - remainder -= 1000_000_000*s; - var ms = remainder / 1000_000; - var ns = remainder - 1000_000*ms; + var remainder = Math.Abs (nanoseconds); + var s = remainder / 1_000_000_000; + remainder -= 1_000_000_000*s; + var ms = remainder / 1_000_000; + var ns = remainder - 1_000_000*ms; var sign = nanoseconds < 0 ? "-" : ""; var sec = s != 0 ? $"{s}(s):" : ""; return $"{sign}{sec}{ms}::{ns}"; } public Timestamp Positive () - => new Timestamp(Math.Max(0L, nanoseconds)); + => new Timestamp (Math.Max (0L, nanoseconds)); public double Milliseconds () - => nanoseconds / 1000_000; + => nanoseconds / 1_000_000; - public int CompareTo(object o) + public int CompareTo (object o) { if (!(o is Timestamp other)) throw new ArgumentException ("Object is not a Timestamp"); - return Comparer.Default.Compare(this.nanoseconds, other.nanoseconds); + return Comparer.Default.Compare (this.nanoseconds, other.nanoseconds); } } }