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: timestamp resolution to microseconds on Windows #1039

Merged
merged 3 commits into from
Sep 3, 2024

Conversation

ShawnCZek
Copy link
Contributor

@ShawnCZek ShawnCZek commented Sep 3, 2024

The timestamp resolution was changed to microseconds in #995. However, this has never correctly worked on Windows because SYSTEMTIME, a return value from the underlying call to GetSystemTime(), contains only milliseconds, not microseconds: https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-systemtime.

Instead, GetSystemTimeAsFileTime() can be used as its return value "contains a 64-bit value representing the number of 100-nanosecond intervals". This also helps in reducing two API calls to just one.

Copy link

codecov bot commented Sep 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.78%. Comparing base (4c47d97) to head (dda6e61).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1039      +/-   ##
==========================================
+ Coverage   83.76%   83.78%   +0.01%     
==========================================
  Files          53       53              
  Lines        5512     5512              
  Branches     1198     1198              
==========================================
+ Hits         4617     4618       +1     
  Misses        781      781              
+ Partials      114      113       -1     

@ShawnCZek
Copy link
Contributor Author

Actually, on Windows 8 and later, GetSystemTimePreciseAsFileTime() is available, which is even more precise and is encouraged to be used by Microsoft: https://learn.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps#guidance-for-acquiring-time-stamps

Copy link
Collaborator

@supervacuus supervacuus left a comment

Choose a reason for hiding this comment

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

Thanks!

@supervacuus supervacuus merged commit a6ba26e into getsentry:master Sep 3, 2024
21 checks passed
@ShawnCZek ShawnCZek deleted the fix_usec_time_on_windows branch September 3, 2024 17:59
@dacap
Copy link

dacap commented Oct 7, 2024

This PR made any binary linked to sentry-native depends on GetSystemTimePreciseAsFileTime()/incompatible with Windows 7 by default (aseprite/aseprite#4691).

I know that probably we should not be compiling with _WIN32_WINNT >= 0x0602 to give support for Windows 7 (which is 0x0601), but actually we're compiling with _WIN32_WINNT=0x0A00 just to use Windows 10 APIs dynamically (known the API structures) and load functions dynamically.

Probably an approach like Microsoft's STL should be used for this:

https://github.com/microsoft/STL/blob/faccf0084ed9b8b58df103358174537233b178c7/stl/src/winapisupp.cpp#L161-L170

Where GetSystemTimePreciseAsFileTime is loaded dynamically if _STL_WIN32_WINNT < _WIN32_WINNT_WIN8 (do we need a SENTRY_WIN32_WINNT?).

At the moment we'll revert this change to continue giving support for Windows 7.

dacap added a commit to aseprite/sentry-native that referenced this pull request Oct 8, 2024
@ShawnCZek
Copy link
Contributor Author

I guess we would have to do something similar to the following:

pSetThreadDescription func = (pSetThreadDescription)GetProcAddress(
GetModuleHandleA("kernel32.dll"), "SetThreadDescription");
if (func) {
wchar_t *thread_name_wstr = sentry__string_to_wstr(thread_name);
HRESULT result = SUCCEEDED(func(thread_id, thread_name_wstr)) ? 0 : 1;
sentry_free(thread_name_wstr);
return SUCCEEDED(result) ? 0 : 1;
}

However, what is the stand on supporting operating systems that went EOL 4+ years ago, especially when done non-standardly by changing definitions? For instance, Sentry Native will not work properly for Windows XP using your technique because of instances like this (GetTickCount64 has only been available since Windows Vista):

// Fallback to GetTickCount() on QPC fail
if (!qpc_frequency.QuadPart) {
# if _WIN32_WINNT >= 0x0600
return GetTickCount64();
# else
return GetTickCount();
# endif
}

Frankly, I am not a maintainer of this library; it is up to the Sentry team how they want to resolve this. But I do hope we are not defaulting to the previous solution as it is inefficient and inaccurate, as discussed here: https://devblogs.microsoft.com/oldnewthing/20131101-00/?p=2763.

Likewise, I wish new systems were not disadvantaged because of supporting old systems that <1% of users use; GetSystemTimePreciseAsFileTime() turned out to be extremely precise in our case.

@dacap
Copy link

dacap commented Oct 8, 2024

We are still giving support for Windows Vista and 7 as we still have ~750 users on those platforms. Windows XP support was dropped and probably Vista and 7 will be dropped in a couple of years. But until we can keep working the code on those platforms that will be great.

The MSVC STL library is already doing this job of dynamically loading the function, probably we can just use __crtGetSystemTimePreciseAsFileTime() when compiling with MSVC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants