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

fix: jit-times - Timestamp arithmetic + add warnings for negative times #9484

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 9 additions & 2 deletions tools/jit-times/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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}`");
jonathanpeppers marked this conversation as resolved.
Show resolved Hide resolved

info.state = MethodInfo.State.Begin;
info.begin = time;
Expand All @@ -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 ();

Expand Down Expand Up @@ -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;
Expand Down
108 changes: 24 additions & 84 deletions tools/jit-times/Timestamp.cs
Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While moving to store only nanoseconds within the Unix epoch is a massive simplification, it also reduces the amount of time that can be tracked. As this is "nanoseconds of Unix epoch" in a signed 64-bit integer, the maximum time this approach can hold is around 2262-Apr-11. Any date after 23h47min16sec on that date results in a negative value:

(long) (new DateTime(2262, 4, 11, 23, 47, 17) - DateTime.UnixEpoch).TotalNanoseconds
// -9223372036854775808

Given that this is still 238 years away, this probably doesn't matter…

Copy link
Author

@alexyakunin alexyakunin Nov 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No disrespect, but if MS folks think it makes sense write this comment to PR + make others read it... I suspect I understand why there is zero progress with some huge issues like the one this PR is related to.

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<long>.Default.Compare(this.nanoseconds, other.nanoseconds);
}
}
}