[LTP] [PATCH v7] Refactor aiocp using new LTP API

Cyril Hrubis chrubis@suse.cz
Mon Aug 15 17:30:34 CEST 2022


Hi!
> -static int dev_block_size_by_path(const char *path)
> +static void fill_with_rand_data(int fd, long long size)
>  {
> -	FILE *f;
> -	struct mntent *mnt;
> -	size_t prefix_len, prefix_max = 0;
> -	char dev_name[1024];
> -	int fd, size;
> +	int lower = 'a';
> +	int upper = 'z';
> +	int bufsize = 1024 * 1024;
> +	char buf[bufsize];
> +	long long i = 0, j;
> +	int start;
>  
> -	if (!path)
> -		return 0;
> +	srand(time(NULL));
>  
> -	f = setmntent("/proc/mounts", "r");
> -	if (!f) {
> -		fprintf(stderr, "Failed to open /proc/mounts\n");
> -		return 0;
> -	}
> -
> -	while ((mnt = getmntent(f))) {
> -		/* Skip pseudo fs */
> -		if (mnt->mnt_fsname[0] != '/')
> -			continue;
> +	for (j = 0; j < bufsize; j++)
> +		buf[j] = (rand() % (upper - lower + 1)) + lower;
>  
> -		prefix_len = strlen(mnt->mnt_dir);
> -
> -		if (prefix_len > prefix_max &&
> -		    !strncmp(path, mnt->mnt_dir, prefix_len)) {
> -			prefix_max = prefix_len;
> -			strncpy(dev_name, mnt->mnt_fsname, sizeof(dev_name));
> -			dev_name[sizeof(dev_name)-1] = '\0';
> -		}
> +	if (size <= bufsize) {
> +		SAFE_WRITE(0, fd, buf, size);
> +		return;
>  	}
>  
> -	endmntent(f);
> +	while (i < size) {
> +		if (!tst_remaining_runtime())
> +			tst_brk(TCONF, "Out of runtime!");
>  
> -	if (!prefix_max) {
> -		fprintf(stderr, "Path '%s' not found in /proc/mounts\n", path);
> -		return 0;
> -	}
> +		start = rand() % (bufsize / 2);
>  
> -	printf("Path '%s' is on device '%s'\n", path, dev_name);
> +		for (j = 0; j < start; j++) {
> +			buf[start - j] = buf[bufsize - start + j];
> +			i++;
>  
> -	fd = open(dev_name, O_RDONLY);
> -	if (!fd) {
> -		fprintf(stderr, "open('%s'): %s\n", dev_name, strerror(errno));
> -		return 0;
> -	}
> +			if (i > size)
> +				break;
> +		}

Huh, why do we change the buffer here at all?

What about much simpler:

#define SIZE (64 * 1024)

static void fill_with_rand_data(int fd, long long size)
{
	char buf[2 * SIZE];
	size_t i, off, to_write;

	for (i = 0; i < sizeof(buf); i++)
                buf[i] = (rand() % (upper - lower + 1)) + lower;

	do {
		if (!tst_remaning_runtime())
			tst_brk(TCONF, "Out of runtime!");

		off = rand() % SIZE;
		to_write = MIN(size, SIZE);

		SAFE_WRITE(1, fd, buf + off, to_write);

		size -= to_write;
	} while (size);

	SAFE_FSYNC(fd);
}

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list