tools/testing/selftests/mincore/mincore_selftest.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/mincore/mincore_selftest.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/mincore/mincore_selftest.c
Extension
.c
Size
8142 bytes
Lines
354
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

ASSERT_EQ(errno, EOPNOTSUPP) {
			TH_LOG("Can't create temporary file: %s",
			       strerror(errno));
		}
		SKIP(goto out_free, "O_TMPFILE not supported by filesystem.");
	}
	errno = 0;
	retval = fallocate(fd, 0, 0, FILE_SIZE);
	if (retval) {
		ASSERT_EQ(errno, EOPNOTSUPP) {
			TH_LOG("Error allocating space for the temporary file: %s",
			       strerror(errno));
		}
		SKIP(goto out_close, "fallocate not supported by filesystem.");
	}

	/*
	 * Map the whole file, the pages shouldn't be fetched yet.
	 */
	errno = 0;
	addr = mmap(NULL, FILE_SIZE, PROT_READ | PROT_WRITE,
			MAP_SHARED, fd, 0);
	ASSERT_NE(MAP_FAILED, addr) {
		TH_LOG("mmap error: %s", strerror(errno));
	}
	retval = mincore(addr, FILE_SIZE, vec);
	ASSERT_EQ(0, retval);
	for (i = 0; i < vec_size; i++) {
		ASSERT_EQ(0, vec[i]) {
			TH_LOG("Unexpected page in memory");
		}
	}

	/*
	 * Touch a page in the middle of the mapping. We expect the next
	 * few pages (the readahead window) to be populated too.
	 */
	addr[FILE_SIZE / 2] = 1;
	retval = mincore(addr, FILE_SIZE, vec);
	ASSERT_EQ(0, retval);
	ASSERT_EQ(1, vec[FILE_SIZE / 2 / page_size]) {
		TH_LOG("Page not found in memory after use");
	}

	i = FILE_SIZE / 2 / page_size + 1;
	while (i < vec_size && vec[i]) {
		ra_pages++;
		i++;
	}
	EXPECT_GT(ra_pages, 0) {
		TH_LOG("No read-ahead pages found in memory");
	}

	/*
	 * End of the readahead window. The rest of the pages shouldn't
	 * be in memory.
	 */
	if (i < vec_size) {
		while (i < vec_size && !vec[i])
			i++;
		EXPECT_EQ(vec_size, i) {
			TH_LOG("Unexpected page in memory beyond readahead window");
		}
	}

	munmap(addr, FILE_SIZE);
out_close:
	close(fd);
out_free:
	free(vec);
}


/*
 * Test mincore() behavior on a page backed by a tmpfs file.  This test
 * performs the same steps as the previous one.
 */
TEST(check_tmpfs_mmap)
{
	unsigned char *vec;
	int vec_size;
	char *addr;
	int retval;
	int page_size;
	int fd;
	int i;

	page_size = sysconf(_SC_PAGESIZE);
	vec_size = FILE_SIZE / page_size;
	if (FILE_SIZE % page_size)

Annotation

Implementation Notes