Skip to content

add option to force cachestat() usage #3602

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

Merged
merged 1 commit into from
Jun 2, 2025

Conversation

teknoraver
Copy link
Contributor

@teknoraver teknoraver commented May 29, 2025

When cachestat() is not available, mmap() and fincore() are used to get cache statistics.
This fallback returns less data than cachestat, and potentially hides read errors by reporting that the file is all mapped in memory:

$ fincore /usr/bin/java
fincore: failed to do cachestat: /usr/bin/java: Operation not permitted
  RES PAGES   SIZE FILE
 208K    13 198,1K /usr/bin/java
$ echo $?
0
$ sudo fincore /usr/bin/java
RES PAGES   SIZE FILE
 0B     0 198,1K /usr/bin/java

Add a --cachestat argument which forces cachestat usage and fails if it's not available:

$ fincore -C /usr/bin/java
fincore: failed to do cachestat: /usr/bin/java: Operation not permitted
$ echo $?
1

Also, on huge files the fallback is very slow and memory consuming, so this flag can be user to avoid doing it by mistake.

@karelzak
Copy link
Collaborator

Please update bash-completion/fincore as well.

It would be helpful to describe in the man page (e.g., the DESCRIPTION section) that we have the cachestat() and mincore() syscalls and how fincore uses them (including the fallback, etc.). This will make --cachestat more understandable for users/readers.

When cachestat() is not available, mmap() and fincore() are used to get
cache statistics.
This fallback returns less data than cachestat, and potentially hides
read errors by reporting that the file is all mapped in memory:

	$ fincore /usr/bin/java
	fincore: failed to do cachestat: /usr/bin/java: Operation not permitted
	  RES PAGES   SIZE FILE
	 208K    13 198,1K /usr/bin/java
	$ echo $?
	0
	$ sudo fincore /usr/bin/java
	RES PAGES   SIZE FILE
	 0B     0 198,1K /usr/bin/java

Add a --cachestat argument which forces cachestat usage and fails if
it's not available:

	$ fincore -C /usr/bin/java
	fincore: failed to do cachestat: /usr/bin/java: Operation not permitted
	$ echo $?
	1

Also, on huge files the fallback is very slow and memory consuming,
so this flag can be user to avoid doing it by mistake.
@karelzak
Copy link
Collaborator

Looks good thanks!

@karelzak karelzak merged commit 3512888 into util-linux:master Jun 2, 2025
35 checks passed
switch (c) {
case 'b':
ctl.bytes = 1;
break;
case 'c':
ctl.total = 1;
break;
case 'C':
#ifndef HAVE_CACHESTAT
Copy link
Member

Choose a reason for hiding this comment

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

This #ifndef can be removed. We have fallback definitions in place.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

and just let the syscall fail in case?

@teknoraver teknoraver deleted the cachestat branch June 2, 2025 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants