-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Get-Uptime is very slightly but increasingly inaccurate relative to Task Manager's up time #24432
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
Comments
We've found some similar issues:
If any of the above are duplicates, please consider closing this issue out and adding additional context in the original issue.
|
I disagree that this is a duplicate:
This issue is about high performance counters being unsuitable (inaccurate) for timing long running things, such as the uptime of a system. The linked StackOverflow page gives some ideas for alternatives. |
The @PowerShell/wg-powershell-cmdlets discussed this. In our ad hoc testing, we also observed on some systems a drift as compared to using WMI. We would approve of a change to the cmdlet specific to Windows to use WMI to get the LastBootUpTime and subtracting from curren time as |
I did a quick research on where WMI gets this information (as I assumed it might be just a registry key). However it turns out the boot time information is coming from some un(der)documented APIs. The well documented WMI route seems to be the best approach. |
The best we can do is describe the drift problem in the documentation. I found a deviation of GetUptime from WMI of several minutes on one system and no deviation on the other. I do not know what this is due to. But this is not important for support scenarios like "Has the user rebooted the system now?" or "Has the CU (requires a reboot) been installed on the system in the last month?"
|
MacOS API to get boot time
|
I have put together a proof of concept module getting the boot time from Windows, Linux, MacOS and hopefully FreeBSD For Windows it uses
For Linux it uses either
or
For MacOS and the *BSD it uses
Linux Docker images typically don't have uptime so if it is not present it gets the creation date of process 1. |
I'd prefer binary examples for supported OS-s. |
I also, but as a proof of concept it is about is it getting the information from the correct place and consistent. |
Rewritten in C# SinceYouAsk.cs using the example OSX DllImport
Running 7.4.5 on FreeBSD 13.3
|
@rhubarb-geek-nz Thanks! Some notices for your code.
|
True, it is very much work in progress at the moment! ReadLine does not work well at all. But the core SDK does give it an entry in OSPlatform, but not in netstandard2.0 Historically much of OSX/Darwin derives from FreeBSD.
Does it take into account hibernate/suspend times? If it counts clock tick interrupts then it will drift from true boot time. |
No.
|
Prerequisites
Steps to reproduce
Open Task Manager to the Performance tab and click on the CPU item on the left, and open a PowerShell command window simultaneously next to it. In the PowerShell window run
Get-Uptime
, note how the time doesn't match that given by Task Manager (listed at the bottom of the CPU page), it can be off by seconds to minutes or more depending on the uptime of the system. You can see in this StackOverflow answer how various methods of getting uptime have a drift relative to the "real" uptime as given by Task Manager (let's all assume Microsoft got it correct in Task Manager and it's the other methods that are wrong): https://stackoverflow.com/a/34010110/15743074, some methods have no drift while others do.More info: The C# code behind
Get-Uptime
usesStopWatch.GetTimeStamp()
which uses QueryPerformanceCounter, hidden behind a shim but the fact it uses QPC is also indicated on this Microsoft Learn page about high-resolution timestamps:Which also states,
And the page goes into examples of typical crystal oscillators used in computers and how they will drift relative to the real world by seconds per week. I suspect that when Windows synchronizes its time to a timeserver, any corrections to the time are applied to other time sources which is one of the ones Task Manager uses, so if the clock was fast by a second when it gets updated then some kernel counter has 1 second subtracted which causes Task Manager to show an accurate uptime, but the "number of ticks" does not get adjusted to remove a second--which is all the QPC timers use, an unsynchronized number of ticks.
High-resolution timestamps are accurate to microseconds, but microsecond accuracy doesn't hold over days and weeks, these high-resolution timestamps are intended for short profiling events.
Get-Uptime
is expected to return long times, sometimes even years if you have a server up forever, therefore it should not be using a high resolution timestamp under the hood because it has known issues with drift against real world clocks when used over days or longer.Expected behavior
Task Manager "up time" and PowerShell `Get-Uptime` should match each other to a second or better.
Actual behavior
Task Manager "up time" and PowerShell `Get-Uptime` are different from each other by seconds or more depending on the uptime of the system. My system has 13 day uptime and the difference is 8 seconds right now, which is admittedly very small at only 0.0007% off after 13 days. The accumulated error will be system dependent and represents an unsynchronized time.
Error details
No response
Environment data
Visuals
No response
The text was updated successfully, but these errors were encountered: