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

Linux: /proc/<pid>/status refactor no2 #1222

Merged
merged 6 commits into from
Apr 6, 2024
Merged

Conversation

cgzones
Copy link
Member

@cgzones cgzones commented Apr 12, 2023

#1211 showed reading /proc/<pid>/status might have a significant
performance impact. Do not read by default only if actually needed,
since the permitted capabilities are no longer gather from it.

Improves: 8ea144d ("Linux: Refactor /proc//status parsing")
Improves: #1211

@BenBE BenBE added enhancement Extension or improvement to existing feature Linux 🐧 Linux related issues labels Apr 13, 2023
@BenBE BenBE added this to the 3.3.0 milestone Apr 13, 2023
@tanriol
Copy link
Contributor

tanriol commented Apr 13, 2023

Can we just detect container status once per process instead of per thread? According to pid_namespaces(7), threads are required to be in the same PID namespace, so doing that once per thread seems to be extra work with no gain.

Copy link
Member

@BenBE BenBE left a comment

Choose a reason for hiding this comment

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

LGTM.

@BenBE
Copy link
Member

BenBE commented Dec 11, 2023

@cgzones What's missing from this branch to be ready?

@BenBE BenBE modified the milestones: 3.3.0, 3.4.0 Dec 26, 2023
@cgzones cgzones force-pushed the linux_pcaps branch 2 times, most recently from 5234a8d to d5b796d Compare January 9, 2024 22:17
@cgzones cgzones marked this pull request as ready for review January 9, 2024 22:23
@cgzones cgzones requested a review from BenBE January 9, 2024 22:23
Copy link
Member

@BenBE BenBE left a comment

Choose a reason for hiding this comment

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

The various casts from Process* to LinuxProcess* for maintask looks kinda ugly when we know maintask is a LinuxProcess* and thus could already be provided as such; would match with the process argument we usually provide alongside too.

@cgzones cgzones force-pushed the linux_pcaps branch 2 times, most recently from 1bb3ee6 to 00e42ea Compare January 10, 2024 16:51
#include <unistd.h>
#include <linux/capability.h>
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be behind a check for when capabilities are actually needed?

Also, this header is covered by an IWYU rule asking to import sys/capability.h instead. Or is there explicit reason to use linux/capability.h instead here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed the include path.

There is currently no option to disable username coloring for elevated non-root processes, see #1379.

Copy link
Member Author

Choose a reason for hiding this comment

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

Reverted the path change, since <sys/capability.h> is provided by libcap-dev, which we don't require, and <linux/capability.h> is by linux-libc-dev.

Copy link
Member

Choose a reason for hiding this comment

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

AFAICS libcap-dev is checked for in configure.ac if --enable-capabilities is given. And that check looks for libcap(-dev) …

@kartoffelheinz
Copy link

kartoffelheinz commented Mar 19, 2024

@cgzones Thank you very much for these Patches. We would love to try these in production. This PR only applies to current upstream and not latest 3.3.0 release. Since we are unfortunately not as firm with C we can only do so much as to rebuild the debian package for 3.3.0 with a patch applied. This fails when trying this PR for obvious reasons since it has been based on latest upstream master, which apparently includes some significant changes compared to 3.3.0.

So may I ask you in case this is not a lot of effort, could you provide with a patch which includes your improvements and cleanly applies to release 3.3.0? Thanks so much in advance!

@cgzones
Copy link
Member Author

cgzones commented Mar 27, 2024

@kartoffelheinz The code in linux/LinuxProcessTable.c changed quite a lot with conflicting changes, so at the moment I am unable to supply rebased patches for 3.3.0.

cgzones added 6 commits April 5, 2024 19:34
htop-dev#1211 has shown reading /proc/<pid>/status might have a significant
performance impact.  It was started to be read by default to gather the
permitted capabilities of the process.

Gather permitted capabilities via the syscall capget(2) instead.
cap_get_proc(3) is not used to avoid linking with -lcap.
Document for each block gathering information about a task whether the
information is shared for the whole process or specific to a single
userland thread.

Also avoid system calls for process-shared information and reuse the
information from the main task.
Improve maintainability by reordering calls in
LinuxProcessList_recurseProcTree() to group them by side-effect or
interdependency.
htop-dev#1211 showed reading /proc/<pid>/status might have a significant
performance impact.  Do not read by default only if actually needed,
since the permitted capabilities are no longer gather from it.

Improves: 8ea144d ("Linux: Refactor /proc/<pid>/status parsing")
Improves: htop-dev#1211
Container engines like docker and podman rely on Linux namespaces.  If
available check if the target process is inside a different PID
namespace than htop.  This check will mark sandbox'ed applications, e.g.
under bubblewrap, but not light-wight containers, like distrobox.
Might be useful for some users and for debugging the
hideRunningInContainer detection.
@cgzones cgzones merged commit 85c3c3a into htop-dev:main Apr 6, 2024
17 checks passed
@cgzones cgzones deleted the linux_pcaps branch April 6, 2024 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Extension or improvement to existing feature Linux 🐧 Linux related issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants