tools/testing/selftests/kvm/lib/test_util.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/kvm/lib/test_util.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/kvm/lib/test_util.c- Extension
.c- Size
- 10503 bytes
- Lines
- 445
- 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.hstdarg.hassert.hctype.hlimits.hstdlib.htime.hsys/stat.hsys/syscall.hlinux/mman.hlinux/kernel.htest_util.h
Detected Declarations
function __attribute__function new_guest_random_statefunction guest_random_u32function parse_sizefunction timespec_to_nsfunction timespec_add_nsfunction timespec_addfunction timespec_add_nsfunction timespec_subfunction timespec_add_nsfunction timespec_elapsedfunction timespec_divfunction timespec_add_nsfunction print_skipfunction test_sysfs_pathfunction thp_configuredfunction get_sysfs_valfunction get_trans_hugepageszfunction is_numa_balancing_enabledfunction get_def_hugetlb_pageszfunction get_backing_src_pageszfunction is_backing_src_hugetlbfunction print_available_backing_src_typesfunction backing_src_helpfunction parse_backing_src_typefunction get_run_delayfunction atoi_paranoid
Annotated Snippet
return timespec_add_ns((struct timespec){0}, ns1 + ns2);
}
struct timespec timespec_sub(struct timespec ts1, struct timespec ts2)
{
s64 ns1 = timespec_to_ns(ts1);
s64 ns2 = timespec_to_ns(ts2);
return timespec_add_ns((struct timespec){0}, ns1 - ns2);
}
struct timespec timespec_elapsed(struct timespec start)
{
struct timespec end;
clock_gettime(CLOCK_MONOTONIC, &end);
return timespec_sub(end, start);
}
struct timespec timespec_div(struct timespec ts, int divisor)
{
s64 ns = timespec_to_ns(ts) / divisor;
return timespec_add_ns((struct timespec){0}, ns);
}
void print_skip(const char *fmt, ...)
{
va_list ap;
assert(fmt);
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
puts(", skipping test");
}
static bool test_sysfs_path(const char *path)
{
struct stat statbuf;
int ret;
ret = stat(path, &statbuf);
TEST_ASSERT(ret == 0 || (ret == -1 && errno == ENOENT),
"Error in stat()ing '%s'", path);
return ret == 0;
}
bool thp_configured(void)
{
return test_sysfs_path("/sys/kernel/mm/transparent_hugepage");
}
static size_t get_sysfs_val(const char *path)
{
size_t size;
FILE *f;
int ret;
f = fopen(path, "r");
TEST_ASSERT(f, "Error opening '%s'", path);
ret = fscanf(f, "%ld", &size);
TEST_ASSERT(ret > 0, "Error reading '%s'", path);
/* Re-scan the input stream to verify the entire file was read. */
ret = fscanf(f, "%ld", &size);
TEST_ASSERT(ret < 1, "Error reading '%s'", path);
fclose(f);
return size;
}
size_t get_trans_hugepagesz(void)
{
TEST_ASSERT(thp_configured(), "THP is not configured in host kernel");
return get_sysfs_val("/sys/kernel/mm/transparent_hugepage/hpage_pmd_size");
}
bool is_numa_balancing_enabled(void)
{
if (!test_sysfs_path("/proc/sys/kernel/numa_balancing"))
return false;
return get_sysfs_val("/proc/sys/kernel/numa_balancing") == 1;
}
size_t get_def_hugetlb_pagesz(void)
{
char buf[64];
Annotation
- Immediate include surface: `stdio.h`, `stdarg.h`, `assert.h`, `ctype.h`, `limits.h`, `stdlib.h`, `time.h`, `sys/stat.h`.
- Detected declarations: `function __attribute__`, `function new_guest_random_state`, `function guest_random_u32`, `function parse_size`, `function timespec_to_ns`, `function timespec_add_ns`, `function timespec_add`, `function timespec_add_ns`, `function timespec_sub`, `function timespec_add_ns`.
- 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.