Skip to content

ActivitySource started activities aren't using an accurate utcnow on framework. #45111

Closed

Description

@cg110

Description

Activity source started events on .net framework don't have accurate duration. This appears to be due to the use of DateTime.UtcNow:

activity.StartTimeUtc = startTime == default ? DateTime.UtcNow : startTime.UtcDateTime;

In other code paths GetUtcNow() is used to get an accurate timestamp.

This can be observed by durations being 00:00:00.0000001 on .net framework.

Configuration

.net framework 4.8
Windows 10 2004.
x64

Regression?

Not a regression, as activity source is new, but diagnostic source had accurate durations.

Other information

I suspect the only workaround is to provide an accurate timestamp to the calls to StartActivity.

Activity

  1. added
    untriaged
    on Nov 23, 2020
  2. cijothomas commented on Nov 23, 2020

    @cijothomas
    Contributor
  3. Clockwork-Muse commented on Nov 23, 2020

    @Clockwork-Muse
    Contributor

    On corefx:

    internal static DateTime GetUtcNow()
    {
        // .NET Core CLR gives accurate UtcNow
        return DateTime.UtcNow;
    }

    On netfx:

    internal static DateTime GetUtcNow()
    {
        // DateTime.UtcNow accuracy on .NET Framework is ~16ms, this method
        // uses combination of Stopwatch and DateTime to calculate accurate UtcNow.
    
        var tmp = timeSync;
    
        // Timer ticks need to be converted to DateTime ticks
        long dateTimeTicksDiff = (long)((Stopwatch.GetTimestamp() - tmp.SyncStopwatchTicks) * 10000000L /
                                        (double)Stopwatch.Frequency);
    
        // DateTime.AddSeconds (or Milliseconds) rounds value to 1 ms, use AddTicks to prevent it
        return tmp.SyncUtcNow.AddTicks(dateTimeTicksDiff);
    }

    ... My first question is going to be "are we sure this is still the case"? The comment implies that netfx is still pulling from a lower-accuracy clock in Windows (since this used to be a common limitation for all OSs/languages). Is switching to the better clock something on the table for netfx, if it is?

  4. added
    question
    and removed
    untriaged
    on Nov 23, 2020
  5. added this to the Future milestone on Nov 23, 2020
  6. tarekgh commented on Nov 23, 2020

    @tarekgh
    Member

    @cg110 although we maintain and support the Full Framework, but we are not actively doing any development there. Also, I don't think Activity timestamps is meant to be accurate to leas than 1 milliseconds.

    @Clockwork-Muse we know on Windows UtcNow is taking sometime to execute, this is because we have to ensure the time is syned with the system when having a leap seconds. Otherwise it'll be shifted.

    Closing it as a duplicate to #13091

  7. tarekgh commented on Nov 23, 2020

    @tarekgh
    Member

    @cg110 I don't think this will make much difference. I have pointed at what is the cause of that. Also, as I mentioned this is unlikely to be fixed in the full framework but we are looking at that in the .NET future releases. Thanks!

  8. cg110 commented on Nov 23, 2020

    @cg110
    Author

    I'm puzzled, I'd just like the DateTime.utcnow changed to GetUtcNow() to match the diagnosticsource behaviour. Is that not doable to give better accuracy? It's already used in the stop/dispose.

    Is that not going to be fixed because it's framework? Utcnow is inaccurate to 16ms so bad for timing things

  9. tarekgh commented on Nov 23, 2020

    @tarekgh
    Member

    @cg110 in general it depends on where we can fix this. If we need to fix UtcNow, then the fix will not be in Full Framework. If the fix would be in System.Diagnostics.DiagnosticSource, then the fix will be spanned all frameworks and you'll get it as this is NuGet package which can be used in the Full Framework too. I'll reactivate this issue to track if anything can be done in System.Diagnostics.DiagnosticSource not depending on UtcNow.

  10. 28 remaining items

  11. ericstj commented on Dec 11, 2020

    @ericstj
    Member

    Looks like the package wasn't added to the build for servicing. @Anipik can you do this for 5.0.2?

  12. Anipik commented on Dec 11, 2020

    @Anipik
    Contributor

    Looks like the package wasn't added to the build for servicing. @Anipik can you do this for 5.0.2?

    yep i will add that

  13. Anipik commented on Dec 14, 2020

    @Anipik
    Contributor

    I added the package, we can keep this open until we ship the package

  14. cijothomas commented on Dec 15, 2020

    @cijothomas
    Contributor

    @Anipik When does 5.0.2 gets released to nuget?

  15. Anipik commented on Dec 15, 2020

    @Anipik
    Contributor

    It will be shipped around second week of january

  16. modified the milestones: Future, 6.0.0 on Jan 21, 2021
  17. ghost locked as resolved and limited conversation to collaborators on Feb 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Diagnostics.Activityenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions