[LTP] [PATCH 2/2] Avoid zero or negative int results in calculations

Petr Vorel pvorel@suse.cz
Fri Mar 27 16:23:47 CET 2020


Hi Martin,

> Like I said in the previous e-mail, that slowdown is caused by the test
> intentionally randomizing the number of disk writes, not the patch. Run
> the test several times.

> > My concern is about brief explanation where/how is zero or negative result
> > appears. But maybe it's obvious and I just don't see it.

> Let me explain.

> > @@ -60,17 +61,15 @@ static void run(void) {

> >  	double time_delta;

> >  	long int random_number;



> > -	while (max_block <= data_blocks) {

> > -		random_number = rand();

> > -		max_block = random_number % max_blks;

> > -		data_blocks = random_number % 1000 + 1;

> > -	}

> > +	random_number = rand();

> > +	max_block = random_number % max_blks + 1;

> > +	data_blocks = random_number % max_block;


> This fixes a potential infinite loop if max_blks == 1000. This
> calculation is also the reason why the test has random run length.

Thanks for an explanation!

> >  	for (i = 1; i <= data_blocks; i++) {

> >  		offset = i * ((BLOCKSIZE * max_block) / data_blocks);

> > -		offset -= BUFSIZ;

> > +		offset -= BUF_SIZE;


> Here the old calculation could produce negative offset if
> BUFSIZ > BLOCKSIZE and (float)max_block/data_blocks is close to 1.
> BUFSIZ is defined in libc headers so the actual value can be different
> on different systems.
BTW BUFSIZ == BLOCKSIZE for glibc (BUFSIZ is 8192; long time ago was 1024. And
it's 1024 for musl and bionic).


Kind regards,
Petr


More information about the ltp mailing list