tools/testing/selftests/mm/ksm_tests.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/mm/ksm_tests.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/mm/ksm_tests.c
Extension
.c
Size
26382 bytes
Lines
927
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

struct ksm_sysfs {
	unsigned long max_page_sharing;
	unsigned long merge_across_nodes;
	unsigned long pages_to_scan;
	unsigned long run;
	unsigned long sleep_millisecs;
	unsigned long stable_node_chains_prune_millisecs;
	unsigned long use_zero_pages;
};

enum ksm_merge_type {
	KSM_MERGE_MADVISE,
	KSM_MERGE_PRCTL,
	KSM_MERGE_LAST = KSM_MERGE_PRCTL
};

enum ksm_test_name {
	CHECK_KSM_MERGE,
	CHECK_KSM_UNMERGE,
	CHECK_KSM_GET_MERGE_TYPE,
	CHECK_KSM_ZERO_PAGE_MERGE,
	CHECK_KSM_NUMA_MERGE,
	KSM_MERGE_TIME,
	KSM_MERGE_TIME_HUGE_PAGES,
	KSM_UNMERGE_TIME,
	KSM_COW_TIME
};

int debug;

static int ksm_write_sysfs(const char *file_path, unsigned long val)
{
	return write_sysfs(file_path, val);
}

static int ksm_read_sysfs(const char *file_path, unsigned long *val)
{
	return read_sysfs(file_path, val);
}

static void ksm_print_sysfs(void)
{
	unsigned long max_page_sharing, pages_sharing, pages_shared;
	unsigned long full_scans, pages_unshared, pages_volatile;
	unsigned long stable_node_chains, stable_node_dups;
	long general_profit;

	if (ksm_read_sysfs(KSM_FP("pages_shared"), &pages_shared) ||
	    ksm_read_sysfs(KSM_FP("pages_sharing"), &pages_sharing) ||
	    ksm_read_sysfs(KSM_FP("max_page_sharing"), &max_page_sharing) ||
	    ksm_read_sysfs(KSM_FP("full_scans"), &full_scans) ||
	    ksm_read_sysfs(KSM_FP("pages_unshared"), &pages_unshared) ||
	    ksm_read_sysfs(KSM_FP("pages_volatile"), &pages_volatile) ||
	    ksm_read_sysfs(KSM_FP("stable_node_chains"), &stable_node_chains) ||
	    ksm_read_sysfs(KSM_FP("stable_node_dups"), &stable_node_dups) ||
	    ksm_read_sysfs(KSM_FP("general_profit"), (unsigned long *)&general_profit))
		return;

	printf("pages_shared      : %lu\n", pages_shared);
	printf("pages_sharing     : %lu\n", pages_sharing);
	printf("max_page_sharing  : %lu\n", max_page_sharing);
	printf("full_scans        : %lu\n", full_scans);
	printf("pages_unshared    : %lu\n", pages_unshared);
	printf("pages_volatile    : %lu\n", pages_volatile);
	printf("stable_node_chains: %lu\n", stable_node_chains);
	printf("stable_node_dups  : %lu\n", stable_node_dups);
	printf("general_profit    : %ld\n", general_profit);
}

static void ksm_print_procfs(void)
{
	const char *file_name = "/proc/self/ksm_stat";
	char buffer[512];
	FILE *f = fopen(file_name, "r");

	if (!f) {
		fprintf(stderr, "f %s\n", file_name);
		perror("fopen");
		return;
	}

	while (fgets(buffer, sizeof(buffer), f))
		printf("%s", buffer);

	fclose(f);
}

static int str_to_prot(char *prot_str)
{
	int prot = 0;

Annotation

Implementation Notes