tools/mm/thp_swap_allocator_test.c

Source file repositories/reference/linux-study-clean/tools/mm/thp_swap_allocator_test.c

File Facts

System
Linux kernel
Corpus path
tools/mm/thp_swap_allocator_test.c
Extension
.c
Size
6370 bytes
Lines
235
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

if (mem2 == NULL) {
			fprintf(stderr, "Failed to allocate small folios memory\n");
			free(mem1);
			return EXIT_FAILURE;
		}

		if (madvise(mem2, MEMSIZE_SMALLFOLIO, MADV_NOHUGEPAGE) != 0) {
			perror("madvise nohugepage for mem2");
			free(mem1);
			free(mem2);
			return EXIT_FAILURE;
		}
	}

	/* warm-up phase to occupy the swapfile */
	memset(mem1, 0x11, MEMSIZE_MTHP);
	madvise(mem1, MEMSIZE_MTHP, MADV_PAGEOUT);
	if (use_small_folio) {
		memset(mem2, 0x11, MEMSIZE_SMALLFOLIO);
		madvise(mem2, MEMSIZE_SMALLFOLIO, MADV_PAGEOUT);
	}

	/* iterations with newly created mTHP, swap-in, and swap-out */
	for (i = 0; i < 100; ++i) {
		unsigned long initial_swpout;
		unsigned long initial_swpout_fallback;
		unsigned long final_swpout;
		unsigned long final_swpout_fallback;
		unsigned long swpout_inc;
		unsigned long swpout_fallback_inc;
		double fallback_percentage;

		initial_swpout = read_stat(SWPOUT_PATH);
		initial_swpout_fallback = read_stat(SWPOUT_FALLBACK_PATH);

		/*
		 * The following setup creates a 1:1 ratio of mTHP to small folios
		 * since large folio swap-in isn't supported yet. Once we support
		 * mTHP swap-in, we'll likely need to reduce MEMSIZE_MTHP and
		 * increase MEMSIZE_SMALLFOLIO to maintain the ratio.
		 */
		random_swapin(mem1, MEMSIZE_MTHP,
				aligned_swapin ? ALIGNMENT_MTHP : ALIGNMENT_SMALLFOLIO,
				TOTAL_DONTNEED_MTHP);
		random_madvise_dontneed(mem1, MEMSIZE_MTHP, ALIGNMENT_MTHP,
				TOTAL_DONTNEED_MTHP);

		if (use_small_folio) {
			random_swapin(mem2, MEMSIZE_SMALLFOLIO,
					ALIGNMENT_SMALLFOLIO,
					TOTAL_DONTNEED_SMALLFOLIO);
		}

		if (madvise(mem1, MEMSIZE_MTHP, MADV_PAGEOUT) != 0) {
			perror("madvise pageout for mem1");
			free(mem1);
			if (mem2 != NULL)
				free(mem2);
			return EXIT_FAILURE;
		}

		if (use_small_folio) {
			if (madvise(mem2, MEMSIZE_SMALLFOLIO, MADV_PAGEOUT) != 0) {
				perror("madvise pageout for mem2");
				free(mem1);
				free(mem2);
				return EXIT_FAILURE;
			}
		}

		final_swpout = read_stat(SWPOUT_PATH);
		final_swpout_fallback = read_stat(SWPOUT_FALLBACK_PATH);

		swpout_inc = final_swpout - initial_swpout;
		swpout_fallback_inc = final_swpout_fallback - initial_swpout_fallback;

		fallback_percentage = (double)swpout_fallback_inc /
			(swpout_fallback_inc + swpout_inc) * 100;

		printf("Iteration %d: swpout inc: %lu, swpout fallback inc: %lu, Fallback percentage: %.2f%%\n",
				i + 1, swpout_inc, swpout_fallback_inc, fallback_percentage);
	}

	free(mem1);
	if (mem2 != NULL)
		free(mem2);

	return EXIT_SUCCESS;
}

Annotation

Implementation Notes