[LTP] [PATCH v2 3/3] syscalls/madvise11: new test for madvise(MADV_DONTNEED)

Cyril Hrubis chrubis@suse.cz
Wed Oct 12 10:52:49 CEST 2022


Hi!
> +static void run(void)
> +{
> +	char cmd[BUF_SIZE];
> +	char line[BUF_SIZE];
> +	char vm_area_addr[128];
> +
> +	TEST(madvise(addr, MAP_SIZE, MADV_DONTNEED));
> +	if (TST_RET == -1) {
> +		tst_brk(TBROK | TTERRNO, "madvise(%p, %d, 0x%x)",
> +			addr, MAP_SIZE, MADV_DONTNEED);
> +	}
> +
> +	sprintf(vm_area_addr, "%p", addr);
> +	sprintf(cmd,
> +		"cat /proc/%d/smaps | grep %s -A 4 | grep Rss: | grep '0 kB'",
> +		getpid(), &(vm_area_addr[2]));

This is way too ugly and may break easily too.

If we are going to parse the file we should do it properly in C instead.
Why can't we just read the file line by line until we find the right
address at the start of the line and once we do look for the Rss?

> +	fp = popen(cmd, "r");
> +	if (!fp)
> +		tst_brk(TBROK, "popen failed");
> +
> +	if (fgets(line, sizeof(line), fp) != NULL) {
> +		if (strstr(line, " 0 kB"))
> +			tst_res(TPASS, "RSS is released");
> +		else
> +			tst_res(TFAIL, "RSS is not released");
> +
> +	} else
> +		tst_brk(TBROK, "There is no 'Rss:' in smaps?");
> +}
> +
> +static void setup(void)
> +{
> +	addr = SAFE_MMAP(NULL, MAP_SIZE,
> +			PROT_READ | PROT_WRITE,
> +			MAP_PRIVATE | MAP_ANONYMOUS,
> +			-1, 0);
> +	memset(addr, 1, MAP_SIZE);
> +}
> +
> +static void cleanup(void)
> +{
> +	if (addr)
> +		SAFE_MUNMAP(addr, MAP_SIZE);
> +	if (fp)
> +		pclose(fp);
> +}
> +
> +static struct tst_test test = {
> +	.test_all = run,
> +	.setup = setup,
> +	.cleanup = cleanup,
> +};
> +
> --
> 2.17.1
> 

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list