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.

Dependency Surface

Detected Declarations

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

Implementation Notes