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

Add and use Environment.ProcessId #38908

Merged
merged 2 commits into from Jul 9, 2020

Conversation

stephentoub
Copy link
Member

@stephentoub stephentoub commented Jul 8, 2020

Fixes #38388

dotnet/roslyn-analyzers#3838 provides a Process.GetCurrentProcess().Id => Environment.ProcessId analyzer and fixer

cc: @jkotas, @ericstj, @jeffhandley

@Dotnet-GitSync-Bot
Copy link
Collaborator

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@Dotnet-GitSync-Bot
Copy link
Collaborator

I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label.

Copy link
Member

@wfurt wfurt left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -10,24 +10,6 @@
<Compile Include="System\Diagnostics\DelimitedListTraceListener.cs" />
Copy link
Member

Choose a reason for hiding this comment

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

This does not need to have OS specific build anymore.

<TargetFrameworks>$(NetCoreAppCurrent)<TargetFrameworks>

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I tried that, but I got errors that I wasn't sure what to do with, and so decided to leave that for a follow-up, e.g.

C:\Users\stoub\.nuget\packages\microsoft.dotnet.build.tasks.targetframework.sdk\5.0.0-beta.20316.1\build\Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk.targets(74,5): error : Not able to find a compatible supported target framework for net5.0 in Project System.Diagnostics.Process.csproj. The Supported Configurations are net5.0-Windows_NT, net5.0-FreeBSD, net5.0-Linux, net5.0-OSX, net5.0-iOS, net5.0-tvOS [D:\repos\runtime\src\libraries\System.Diagnostics.TraceSource\src\System.Diagnostics.TraceSource.csproj]

Copy link
Member

Choose a reason for hiding this comment

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

Can you remove the reference to System.Diagnostics.Process? Is its still used anywhere? IIRC there was a case for Process.GetCurrentProcess().ProcessName which was unused in some cases. Perhaps we can use a different pattern to obtain the name of the process, like AppDomain.CurrentDomain.FriendlyName 🤮 or add Environment.ProcessName.

Copy link
Member Author

Choose a reason for hiding this comment

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

@ericstj, I can remove the Process reference, but then the error just becomes:

C:\Users\stoub\.nuget\packages\microsoft.dotnet.build.tasks.targetframework.sdk\5.0.0-beta.20316.1\build\Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk.targets(74,5): error : Not able to find a compatible supported target framework for net5.0 in Project System.IO.FileSystem.csproj. The Supported Configurations are net5.0-Windows_NT, net5.0-Unix [D:\repos\runtime\src\libraries\System.Diagnostics.TraceSource\src\System.Diagnostics.TraceSource.csproj]

for FileSystem.

Copy link
Member

Choose a reason for hiding this comment

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

It looks to me like you're talking about a different project? This comment is on TextWriterTraceListener and the error mentions System.Diagnostics.TraceSource.csproj and I don't see any project references in TextWriterTraceListener.

In the case of TraceSource you might be able to get away with changing from a ProjectReference to a simple Reference. ProjectReferences are only needed when you need to use a type that lives in System.Private.CoreLib at runtime (to avoid the duplicate type error from compiler).

Copy link
Member Author

Choose a reason for hiding this comment

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

It looks to me like you're talking about a different project? This comment is on TextWriterTraceListener and the error mentions System.Diagnostics.TraceSource.csproj and I don't see any project references in TextWriterTraceListener.

I was talking about TraceSource (the below "Same here" comment was applying to TraceSource as well, so I've just been using that as a guinea pig). For TextWriterTraceListener, it does still use Process, to get the current process name.

In the case of TraceSource you might be able to get away with changing from a ProjectReference to a simple Reference.

The purpose of this PR is to add Environment.ProcessId. I'm not going to hold it up right now fiddling with the csproj. Please open an issue about consolidating the target frameworks if there isn't already one tracking that. Thanks!

Copy link
Member

Choose a reason for hiding this comment

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

Ok, that's fine by me. /cc @Anipik

Copy link
Member Author

Choose a reason for hiding this comment

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

Great, thanks.

Copy link
Contributor

Choose a reason for hiding this comment

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

I can follow up with this and see where we can leverage this api and remove cross targeting

@stephentoub stephentoub force-pushed the environmentprocessid branch 3 times, most recently from 343573f to fe0a403 Compare July 8, 2020 22:13
@GSPP
Copy link

GSPP commented Jul 10, 2020

Could s_processId be a static readonly field on a new static class? That way there would not need to be an initialization check at all. Obtaining the process id would amount to reading a static field. Maybe the JIT could even inline the value.

@stephentoub
Copy link
Member Author

Could s_processId be a static readonly field on a new static class?

It could. I'm not sure it's worth it. @jkotas, do you have an opinion?

@jkotas
Copy link
Member

jkotas commented Jul 10, 2020

I do not have an opinion. This optimization is not free - it adds static footprint. Is it a good tradeoff in this case? Unclear.

@stephentoub
Copy link
Member Author

stephentoub commented Jul 10, 2020

This optimization is not free - it adds static footprint. Is it a good tradeoff in this case? Unclear.

Right, thanks. That's why I went with the simple solution of just doing lazy initialization. If we get enough evidence to suggest this is so critical path that it's worth it, we can change it in the future. As of now, though, we're making something that was super slow way faster already.

@dotnet dotnet locked as resolved and limited conversation to collaborators Dec 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

API Proposal: Add Environment.ProcessId property
10 participants