Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Read values from /proc/[pid]/status - #41122

Merged
stephentoub merged 16 commits into
dotnet:masterfrom
shubhamranjan:fix-memory-usage
Nov 4, 2019
Merged

stephentoub merged 16 commits into
dotnet:masterfrom
shubhamranjan:fix-memory-usage

Conversation

@shubhamranjan

@shubhamranjan shubhamranjan commented Sep 15, 2019 •

Copy link
Copy Markdown
Contributor

Add support for:

  • Process.PeakWorkingSet64
  • Process.PrivateMemorySize64
  • Process.PagedMemorySize64
  • Process.PagedSystemMemorySize64
  • Process.PeakVirtualMemorySize64

Improve:

  • Process.VirtualMemorySize64
  • Process.WorkingSet64

Fixes #23449
Fixes #36086

UPDATE: Fixes/Additions/ Supports have only been added on Linux. OSX is still unsupported.

@shubhamranjan

Copy link
Copy Markdown
Contributor Author

I think I will have to modify the tests. Currently, since values are not being returned, they have been set to return 0.

@shubhamranjan

Copy link
Copy Markdown
Contributor Author

This PR fixes an issue which exists in 2.1,2.2 and as well as 3.0. Is there something specific I need to do to make sure this fix makes its way for all supported versions ?

@danmoseley
danmoseley requested a review from krwq October 6, 2019 15:18
@danmoseley

Copy link
Copy Markdown

@krwq as area owner could you please help get this reviewed and merged?

@danmoseley

Copy link
Copy Markdown

Re : versions, this will not be back ported to shipped versions without clear customer need: we minimize churn in shipped releases . It might be a candidate for 3.1 if completed soon though.

@shubhamranjan

Copy link
Copy Markdown
Contributor Author

Re : versions, this will not be back ported to shipped versions without clear customer need: we minimize churn in shipped releases . It might be a candidate for 3.1 if completed soon though.

I see. Currently, I find this required by most monitoring tools which, at the moment, on non-windows platforms are reporting memory usage as 0.

@danmoseley

Copy link
Copy Markdown

@shubhamranjan which monitoring tools? Is there a way to estimate their usage? How did these tools work before?

@shubhamranjan

Copy link
Copy Markdown
Contributor Author

@shubhamranjan which monitoring tools? Is there a way to estimate their usage? How did these tools work before?

Newrelic is one of them. I am not sure about how many dotnet core customers use them. I have tested since 2.x versions of dotnet core and it never worked on non-windows platforms.

Ref: https://docs.newrelic.com/docs/agents/net-agent/installation/compatibility-requirements-net-core-agent#unavailable-features

@danmoseley

Copy link
Copy Markdown

Ah, that Newrelic documentation is a good source. Thank you.

2.1 is LTS so we are trying to minimise churn in it. 2.2 is going out of support soon because 3.0 shipped. 3.1 will ship soon: perhaps we can get this into there in the first instance, and see whether there is more interest in making the case for churn in 2.1.

@maryamariyan
maryamariyan requested a review from wtgodbe October 9, 2019 04:27
@cijothomas

Copy link
Copy Markdown

@danmosemsft This is an issue for Azure Monitor/Application Insights as well. The memory is shown as 0 in Linux in Application Insights Live Metrics feature. It'd be good to get this fixed and ideally ported to 2.1/2.2 as well.

https://github.com/microsoft/ApplicationInsights-dotnet-server/issues/1299

@danmoseley danmoseley added this to the 3.1 milestone Oct 21, 2019
@danmoseley

Copy link
Copy Markdown

@krwq could you please review so we can get this in master? we should keep it open to consider backport to 3.1.

@danmoseley

Copy link
Copy Markdown

@janvorli any concerns about consuming these values, and the mappings being made here? I know we've had various issues using the "correct" memory values on Linux in some cases.

@shubhamranjan
shubhamranjan requested a review from krwq October 22, 2019 19:36

@krwq krwq left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM with questions on tests

return Interop.procfs.TryReadStatFile(pid, out stat, reusableReader) ?
CreateProcessInfo(ref stat, reusableReader) :
null;
return readStatFile || readStatusFile ? CreateProcessInfo(ref stat, ref status, reusableReader) : null;

@stephentoub stephentoub Nov 1, 2019 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

readStatFile || readStatusFile

I don't understand this. How do we create the process info if we don't have a valid stat? We can do so without a valid status, but stat appears necessary, no? (e.g. for the pid, for the process name, etc.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Pid is an input parameter, we already have that, so that's how we access /proc/[pid]/stat. Now what if reading /stat fails and /status doesn't ? You want to fail it as well ?

@stephentoub stephentoub Nov 1, 2019 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That's not how it's populated:


Note that pid currently isn't passed into CreateProcessInfo.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Now what if reading /stat fails and /status doesn't ? You want to fail it as well ?

Yup

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's not how it's populated:

Note that pid currently isn't passed into CreateProcessInfo.

Ah. Ok when you pass it forward, you read from the object itself.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

@shubhamranjan

shubhamranjan commented Nov 1, 2019 •

Copy link
Copy Markdown
Contributor Author

@stephentoub Please re-review. I will be online for a while to do any changes if necessary on urgent basis if you guys plan to take this in your next release (since the cutoff is today I believe) .

@danmoseley

Copy link
Copy Markdown

@shubhamranjan thanks for working on this. At this point, we'll have to defer it from 3.1 I think. It may be possible to service it in later, if appropriate, we'll see. Meantime, let's carry on and get this into master.

@shubhamranjan

Copy link
Copy Markdown
Contributor Author

@shubhamranjan thanks for working on this. At this point, we'll have to defer it from 3.1 I think. It may be possible to service it in later, if appropriate, we'll see. Meantime, let's carry on and get this into master.

@danmosemsft
Cool. Any plans for backporting this to 2.x and 3.0 ? I believe a lot of people wanting metrics would be using those versions and may have difficulty in upgrading to 3.1 asap.

@danmoseley

Copy link
Copy Markdown

Any plans for backporting this to 2.x and 3.0

2.2 and 3.0 will be out of support in not too long, obsoleted by 3.0 and 3.1 respectively. 2.1 is unlikely. That release has a very high bar for changes now.

#endif
}

ReadOnlySpan<char> value = default;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: this local is unnecessary... instead of e.g.

value = statusFileContents.Slice(0, nonUnitSliceLength);
valueParsed = int.TryParse(value, out results.Pid);

it can be:

valueParsed = int.TryParse(statusFileContents.Slice(0, nonUnitSliceLength), out results.Pid);

@stephentoub stephentoub left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@stephentoub
stephentoub merged commit 8086f36 into dotnet:master Nov 4, 2019
@stephentoub

Copy link
Copy Markdown
Member

@danmosemsft, up to you what you want to do about porting to 3.x.

danmoseley pushed a commit to danmoseley/corefx that referenced this pull request Nov 4, 2019
* Fix #23449 - Read values from /proc/[pid]/status

* Fix tests for new returned data from /prod/[pid]/status

* Update tests of process info on OSX

* Rename property ParsedStatus.pid --> ParsedStatus.Pid

* 1. Avoid reallocation of delimiter array
2. Use tryParse instead of Parse to avoid exceptions

* Rename static field and mark it as readonly

* Change pid from /status to be read only from debug mode

* Change status file read implementation using spans

* Remove unecessary debug statement

* Revert "Remove unecessary debug statement"

This reverts commit f60b4f3.

* Refactor logic using if statements with reduction in code redundancy

* Improve condition arrangement to avoid reading subsequent files if process name doesn't match

* Avoid failure of process info creation in case of fail read from one of many files.

* Refactor Interop.ProcFsStat for better understanding

* Change process creation logic to fail on read /stat

* Change logic for unit and non unit values in file
danmoseley pushed a commit to danmoseley/corefx that referenced this pull request Nov 4, 2019
* Fix #23449 - Read values from /proc/[pid]/status

* Fix tests for new returned data from /prod/[pid]/status

* Update tests of process info on OSX

* Rename property ParsedStatus.pid --> ParsedStatus.Pid

* 1. Avoid reallocation of delimiter array
2. Use tryParse instead of Parse to avoid exceptions

* Rename static field and mark it as readonly

* Change pid from /status to be read only from debug mode

* Change status file read implementation using spans

* Remove unecessary debug statement

* Revert "Remove unecessary debug statement"

This reverts commit f60b4f3.

* Refactor logic using if statements with reduction in code redundancy

* Improve condition arrangement to avoid reading subsequent files if process name doesn't match

* Avoid failure of process info creation in case of fail read from one of many files.

* Refactor Interop.ProcFsStat for better understanding

* Change process creation logic to fail on read /stat

* Change logic for unit and non unit values in file
return true;
}
}
catch (IOException)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

when reading /proc/<pid>/maps we also catch UnauthorizedAccessException . should we do that here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In case you want a specific behaviour for UnauthorizedAccessException , we can add one.

danmoseley pushed a commit that referenced this pull request Nov 5, 2019
* Fix #23449 - Read values from /proc/[pid]/status

* Fix tests for new returned data from /prod/[pid]/status

* Update tests of process info on OSX

* Rename property ParsedStatus.pid --> ParsedStatus.Pid

* 1. Avoid reallocation of delimiter array
2. Use tryParse instead of Parse to avoid exceptions

* Rename static field and mark it as readonly

* Change pid from /status to be read only from debug mode

* Change status file read implementation using spans

* Remove unecessary debug statement

* Revert "Remove unecessary debug statement"

This reverts commit f60b4f3.

* Refactor logic using if statements with reduction in code redundancy

* Improve condition arrangement to avoid reading subsequent files if process name doesn't match

* Avoid failure of process info creation in case of fail read from one of many files.

* Refactor Interop.ProcFsStat for better understanding

* Change process creation logic to fail on read /stat

* Change logic for unit and non unit values in file
@cijothomas

Copy link
Copy Markdown

@danmosemsft What version of .NET Core will this be part of ? 3.1 and higher only? Any scope of porting to 2.1/2.2 ?

@danmoseley

Copy link
Copy Markdown

@cijothomas it will be in 3.1 only. That is expected to be released next month. We don't plan to back port. 3.1 should be a suitable upgrade for most people, as it is expected to become a long term support release.

@danmoseley

Copy link
Copy Markdown

@cijothomas as you probably noticed, 3.1 is now released. Please let us know (in a new issue) if this does not work correctly for you.

@cijothomas

Copy link
Copy Markdown

@danmosemsft Yes! I validated this works and unblocks a key customer scenario for application insights customers.

@danmoseley

Copy link
Copy Markdown

Yay! Thanks @cijothomas for circling back. That makes it worth the rush to get this in.

@shubhamranjan you pointed to New Relic. Do you have a contact there who can update their documentation?

@karelz karelz modified the milestones: 3.1, 5.0 Dec 19, 2019
@usr-sse2

usr-sse2 commented Jul 7, 2021 •

Copy link
Copy Markdown

It seems to be working differently than on Windows.
On Windows I've never seen PrivateMemorySize64 differ from PagedMemorySize64 (my program throws an exception if it happens, and it haven't happened for two years).
With this change on Linux I see PrivateMemorySize64 6013288448 and PagedMemorySize64 8766259. From the code I see that paged on Linux shows only memory in a swap file.

The point of this assertion in my program is that I really need PeakPrivateMemorySize64, but there is no such field, and I use PeakPagedMemorySize64 instead because PagedMemorySize64 is always equal to PrivateMemorySize64.

Linux and Windows machines are identical and both have 32 GB RAM.

@danmoseley

Copy link
Copy Markdown

@usr-sse2 this PR is merged, so if you have an issue I suggest to open one in dotnet/runtime and link here if relevant.

picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
* Fix dotnet/corefx#23449 - Read values from /proc/[pid]/status

* Fix tests for new returned data from /prod/[pid]/status

* Update tests of process info on OSX

* Rename property ParsedStatus.pid --> ParsedStatus.Pid

* 1. Avoid reallocation of delimiter array
2. Use tryParse instead of Parse to avoid exceptions

* Rename static field and mark it as readonly

* Change pid from /status to be read only from debug mode

* Change status file read implementation using spans

* Remove unecessary debug statement

* Revert "Remove unecessary debug statement"

This reverts commit dotnet/corefx@f60b4f3.

* Refactor logic using if statements with reduction in code redundancy

* Improve condition arrangement to avoid reading subsequent files if process name doesn't match

* Avoid failure of process info creation in case of fail read from one of many files.

* Refactor Interop.ProcFsStat for better understanding

* Change process creation logic to fail on read /stat

* Change logic for unit and non unit values in file


Commit migrated from dotnet/corefx@8086f36
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.

10 participants