tools/testing/selftests/cachestat/test_cachestat.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/cachestat/test_cachestat.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/cachestat/test_cachestat.c- Extension
.c- Size
- 7990 bytes
- Lines
- 366
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
stdio.hstdbool.hlinux/kernel.hlinux/magic.hlinux/mman.hsys/mman.hsys/shm.hsys/syscall.hsys/vfs.hunistd.hstring.hfcntl.herrno.hkselftest.h
Detected Declarations
enum file_typefunction print_cachestatfunction write_exactlyfunction fsyncfunction test_cachestatfunction run_cachestat_testfunction main
Annotated Snippet
if (read_len <= 0) {
ksft_print_msg("Unable to read from urandom.\n");
ret = false;
goto out_free_data;
}
remained -= read_len;
cursor += read_len;
}
/* write random data to fd */
remained = filesize;
cursor = data;
while (remained) {
ssize_t write_len = write(fd, cursor, remained);
if (write_len <= 0) {
ksft_print_msg("Unable write random data to file.\n");
ret = false;
goto out_free_data;
}
remained -= write_len;
cursor += write_len;
}
ret = true;
out_free_data:
free(data);
close_random_fd:
close(random_fd);
out:
return ret;
}
/*
* fsync() is implemented via noop_fsync() on tmpfs. This makes the fsync()
* test fail below, so we need to check for test file living on a tmpfs.
*/
static bool is_on_tmpfs(int fd)
{
struct statfs statfs_buf;
if (fstatfs(fd, &statfs_buf))
return false;
return statfs_buf.f_type == TMPFS_MAGIC;
}
/*
* Open/create the file at filename, (optionally) write random data to it
* (exactly num_pages), then test the cachestat syscall on this file.
*
* If test_fsync == true, fsync the file, then check the number of dirty
* pages.
*/
static int test_cachestat(const char *filename, bool write_random, bool create,
bool test_fsync, unsigned long num_pages,
int open_flags, mode_t open_mode)
{
size_t PS = sysconf(_SC_PAGESIZE);
int filesize = num_pages * PS;
int ret = KSFT_PASS;
long syscall_ret;
struct cachestat cs;
struct cachestat_range cs_range = { 0, filesize };
int fd = open(filename, open_flags, open_mode);
if (fd == -1) {
ksft_print_msg("Unable to create/open file.\n");
ret = KSFT_FAIL;
goto out;
} else {
ksft_print_msg("Create/open %s\n", filename);
}
if (write_random) {
if (!write_exactly(fd, filesize)) {
ksft_print_msg("Unable to access urandom.\n");
ret = KSFT_FAIL;
goto out1;
}
}
syscall_ret = syscall(__NR_cachestat, fd, &cs_range, &cs, 0);
ksft_print_msg("Cachestat call returned %ld\n", syscall_ret);
if (syscall_ret) {
Annotation
- Immediate include surface: `stdio.h`, `stdbool.h`, `linux/kernel.h`, `linux/magic.h`, `linux/mman.h`, `sys/mman.h`, `sys/shm.h`, `sys/syscall.h`.
- Detected declarations: `enum file_type`, `function print_cachestat`, `function write_exactly`, `function fsync`, `function test_cachestat`, `function run_cachestat_test`, `function main`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.