[LTP] [RFC PATCH] readahead02: detect cache stats via mincore per testfile

Cyril Hrubis chrubis@suse.cz
Fri Dec 19 15:38:30 CET 2025


Hi!
> -	unsigned long ret;
> +	size_t pagecnt, resident = 0;
> +	size_t map_len;
> +	unsigned char *vec;
> +	void *addr;
> +	int fd;
> +	size_t i;
>  
> -	SAFE_FILE_LINES_SCANF(MEMINFO_FNAME, "Cached: %lu", &ret);
> +	if (!length)
> +		return 0;
>  
> -	return ret;
> +	fd = SAFE_OPEN(path, O_RDONLY);
> +
> +	pagecnt = (length + pagesize - 1) / pagesize;

We have LTP_ALIGN() macro, maybe we should use that one instead.

> +	map_len = pagecnt * pagesize;
> +
> +	addr = SAFE_MMAP(NULL, map_len, PROT_NONE, MAP_SHARED, fd, 0);
> +	vec = SAFE_MALLOC(pagecnt);
> +
> +	if (mincore(addr, map_len, vec) == -1)
> +		tst_brk(TBROK | TERRNO, "mincore");
> +
> +	for (i = 0; i < pagecnt; i++) {
> +		size_t chunk = pagesize;
> +		size_t tail = length % pagesize;
> +
> +		if (i == pagecnt - 1 && tail)
> +			chunk = tail;
> +
> +		if (vec[i] & 1)
> +			resident += chunk;
> +	}
> +
> +	free(vec);
> +	SAFE_MUNMAP(addr, map_len);
> +	SAFE_CLOSE(fd);
> +
> +	return resident;
>  }

Otherwise: Reviewed-by: Cyril Hrubis <chrubis@suse.cz>

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list