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

Process.StartTime on macOS returns different values #30241

Closed
mdolan92869 opened this issue Jul 12, 2019 · 10 comments · Fixed by dotnet/corefx#39572
Closed

Process.StartTime on macOS returns different values #30241

mdolan92869 opened this issue Jul 12, 2019 · 10 comments · Fixed by dotnet/corefx#39572

Comments

@mdolan92869
Copy link

When getting the StartTime for a process, each time it's called for a new instance of the same process, it has a different value. I believe that the issue is in StartTimeCore where the native "now" time is retrieved, a bunch of arithmetic is performed, then the C# "now" time is retrieved. Since operations were between the two "now" calls, the "now" values will obviously be different, but they're used as if they're the same. I've attached a sample program that will show the deltas between StartTime accesses.

System: macOS 10.14.5 Mojave

ProcessStartTime.zip

@mdolan92869
Copy link
Author

In the attached code, the PID I was using was for Microsoft Outlook on my laptop. It was launched one time, but the StartTime drifted and no 2 consecutive calls got the same value.

@jpfreyen
Copy link

@danmosemsft I'm reading this and I am concerned that there is a similar issue in Linux not being addressed by just fixing Process.OSX.cs

@danmoseley
Copy link
Member

We likely won't have time to look at this for a a little while, so if either of you feel motivated to work on PR(s) that would be welcome.

@watfordgnf
Copy link
Contributor

watfordgnf commented Jul 15, 2019

I've been away from OS X programming for some time (and my MBP is too old to run .NET Core), but would proc_bsdinfo.pbi_start_tvsec be a good start? Interop.libproc.GetProcessInfoById already has a call which retrieves it within proc_taskallinfo.pbsd. https://github.com/dotnet/corefx/blob/f7539b726c4bc2385b7f49e5751c1cff2f2c7368/src/Common/src/Interop/OSX/Interop.libproc.cs#L310-L323

Untested:

        /// <summary>Gets the time the associated process was started.</summary>
        private int StartTimeCore
        {
            get
            {
                EnsureState(State.HaveNonExitedId);
                Interop.libproc.proc_taskallinfo? info = Interop.libproc.GetProcessInfoById(Id);

                if (info == null)
                    throw new Win32Exception(SR.ProcessInformationUnavailable);

                // ignores microseconds
                return DateTime.UnixEpoch + TimeSpan.FromSeconds(info.pbsd.pbi_start_tvsec);
            }
        }

@watfordgnf
Copy link
Contributor

@mdolan92869 I've put up a branch which attempts a fix if you can test (my MBP cannot run .NET Core).

@jpfreyen it looks like the Linux code falls back on UtcNow if it can't read the boot time from /proc/stat. It would be nice if /proc/{pid}/psinfo was available on *nix, as its start time is relative to the epoch rather than ticks since boot.

@jpfreyen
Copy link

jpfreyen commented Jul 17, 2019

@watfordgnf I would agree, pbi_start_tvsec/pbi_start_tvusec would be a good place to start for macOS.

And @watfordgnf do you mean /proc/{pid}/stat instead of /proc/stat? Or wait, if /proc/stat somehow can't be read then .NETCore doesn't bother with individual process stats at /proc/{pid}/stat, right?

@watfordgnf
Copy link
Contributor

It pulls starttime from /proc/{pid}/stat, which is in ticks since boot. Boot time is then pulled from /proc/stat. It falls back to adding starttime to DateTime.UtcNow in the event /proc/stat parsing fails (for whatever reason).

@watfordgnf
Copy link
Contributor

watfordgnf commented Jul 17, 2019

I've opened draft PR dotnet/corefx#39572 to iterate on a possible implementation.

@jpfreyen
Copy link

Thanks for the fix @watfordgnf; I can't help test, my work environment is totally not setup to build .NETCore code changes and try them.

stephentoub referenced this issue in dotnet/corefx Aug 6, 2019
* Use pbi_start for StartTime on OS X fixes #39434

* Add test to ensure StartTime remains stable

* Reduce delay in test based on feedback

- Use the correct Assert.Equal too.

* Ensure StartTime is in Local Time

* Remove unnecessary test and reenable other test for OS X

* Remove unused OS X interop methods

- SystemNative_GetAbsoluteTime
- SystemNative_GetTimebaseInfo

* Remove cmake entries for unused HAVE_MACH_TIMEBASE_INFO

* Address review comment regarding constants
@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the 5.0 milestone Feb 1, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants