Skip to contentSkip to conversation

Fix overflow in percentile calculation for Windows

PATCH Superseded Andrzej Jakowski 5 participants 14 messages Oct 10, 2017 – Oct 14, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix overflow in percentile calculation for Windows

This patch fixes percentile calculation for Windows where overflow on 32 bit type happened while calculating actual percentiles.

---
 stat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
stat.c +1 -1
index c5a68ad5..5c758680 100644
@@ -139,7 +139,7 @@ unsigned int calc_clat_percentiles(unsigned int *io_u_plat, unsigned long nr,
 				   fio_fp64_t *plist, unsigned long long **output,
 				   unsigned long long *maxv, unsigned long long *minv)
 {
-	unsigned long sum = 0;
+	unsigned long long sum = 0;
 	unsigned int len, i, j = 0;
 	unsigned int oval_len = 0;
 	unsigned long long *ovals = NULL;
Signature · 1 line
2.13.6
#1 lore raw
Quoted 2 lines · Andrzej Jakowski

This patch fixes percentile calculation for Windows where overflow on 32 bit type happened while calculating actual percentiles.

Just out of idle curiosity what sort of job showed this up - did the storage have to be something fast with low latency and did the job have to run a reasonable length of time?

I have a faint hope that by documenting "problem" jobs in the git commit logs one day in the distant future some soul will be able to mine the logs for jobs and add them to the list of jobs that should be retested (e.g. by running under the undefined,integer sanitizer)...

Signature · 1 line
Sitsofe | http://sucs.org/~sits/
#2 lore raw
Quoted 3 lines

Just out of idle curiosity what sort of job showed this up - did the storage have to be something fast with low latency and did the job have to run a reasonable length of time?

Simple workload with more than 2^32 IOs executed.

Quoted 11 lines

I have a faint hope that by documenting "problem" jobs in the git commit logs one day in the distant future some soul will be able to mine the logs for jobs and add them to the list of jobs that should be retested (e.g. by running under the undefined,integer sanitizer)...

--
Sitsofe | http://sucs.org/~sits/
--
To unsubscribe from this list: send the line "unsubscribe fio" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
In reply to Andrzej Jakowski

Hi Jens,

Quoted 22 lines · Andrzej Jakowski

This patch fixes percentile calculation for Windows where overflow on 32 bit type happened while calculating actual percentiles.

---
 stat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
stat.c +1 -2
index c5a68ad5..5c758680 100644
@@ -139,7 +139,7 @@ unsigned int calc_clat_percentiles(unsigned int *io_u_plat, unsigned long nr,
                                   fio_fp64_t *plist, unsigned long long **output,
                                   unsigned long long *maxv, unsigned long long *minv)
 {
-       unsigned long sum = 0;
+       unsigned long long sum = 0;
        unsigned int len, i, j = 0;
        unsigned int oval_len = 0;
        unsigned long long *ovals = NULL;
--
Quoted 1 line

2.13.6

Just in case you didn't see it, the above looks sensible/necessary...

Signature · 1 line
Sitsofe | http://sucs.org/~sits/
#4 lore raw
Quoted 21 lines · Sitsofe Wheeler

Hi Jens,

Quoted 17 lines · Andrzej Jakowski

This patch fixes percentile calculation for Windows where overflow on 32 bit type happened while calculating actual percentiles.

---
 stat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
stat.c +0 -1
index c5a68ad5..5c758680 100644
@@ -139,7 +139,7 @@ unsigned int calc_clat_percentiles(unsigned int *io_u_plat, unsigned long nr,
                                   fio_fp64_t *plist, unsigned long long **output,
                                   unsigned long long *maxv, unsigned long long *minv)
 {
-       unsigned long sum = 0;
Quoted 8 lines
Quoted 6 lines
stat.c +1 -1
+       unsigned long long sum = 0;
        unsigned int len, i, j = 0;
        unsigned int oval_len = 0;
        unsigned long long *ovals = NULL;
--

2.13.6

Just in case you didn't see it, the above looks sensible/necessary...

Thanks for pointing it my way, I don't always see the mailing list posts quickly.

Looks good to me, I'll get it applied. Thanks Andrzej!

Signature · 1 line
Jens Axboe
#5 lore raw
Quoted 31 lines · Jens Axboe
Quoted 21 lines · Sitsofe Wheeler

Hi Jens,

Quoted 17 lines · Andrzej Jakowski

This patch fixes percentile calculation for Windows where overflow on 32 bit type happened while calculating actual percentiles.

---
  stat.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
stat.c +0 -1
index c5a68ad5..5c758680 100644
@@ -139,7 +139,7 @@ unsigned int calc_clat_percentiles(unsigned int *io_u_plat, unsigned long nr,
                                    fio_fp64_t *plist, unsigned long long **output,
                                    unsigned long long *maxv, unsigned long long *minv)
  {
-       unsigned long sum = 0;
Quoted 8 lines
Quoted 6 lines
stat.c +1 -1
+       unsigned long long sum = 0;
         unsigned int len, i, j = 0;
         unsigned int oval_len = 0;
         unsigned long long *ovals = NULL;
--

2.13.6

Just in case you didn't see it, the above looks sensible/necessary...

Quoted 5 lines

Thanks for pointing it my way, I don't always see the mailing list posts quickly.

Looks good to me, I'll get it applied. Thanks Andrzej!

Andrzej, does this actually resolve the problem you observed? unsigned long nr in the parameter list of calc_clat_percentiles() and show_clat_percentiles() also looks like it will overflow when you have more than 2^32 IOs.

#6 lore raw
Quoted 4 lines · Vincent Fu

Andrzej, does this actually resolve the problem you observed? unsigned long nr in the parameter list of calc_clat_percentiles() and show_clat_percentiles() also looks like it will overflow when you have more than 2^32 IOs.

Yes you are right. That original patch is not sufficient. Will get it fixed and post new version soon.

Quoted 5 lines

--
To unsubscribe from this list: send the line "unsubscribe fio" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#7 lore raw

PATCH v2 1/1 Fix overflow in percentile calculation for Windows

This patch fixes percentile calculation for Windows where overflow on 32 bit type happened while calculating actual percentiles.

---
 gclient.c | 2 +-
 stat.c    | 6 +++---
 stat.h    | 2 +-
 3 files changed, 5 insertions(+), 5 deletions(-)
gclient.c +1 -1
index 43c8a089..daa91538 100644
@@ -1099,7 +1099,7 @@ static void gfio_show_clat_percentiles(struct gfio_client *gc,
 				       int ddir)
 {
 	unsigned int *io_u_plat = ts->io_u_plat[ddir];
-	unsigned long nr = ts->clat_stat[ddir].samples;
+	unsigned long long nr = ts->clat_stat[ddir].samples;
 	fio_fp64_t *plist = ts->percentile_list;
 	unsigned int len, scale_down;
 	unsigned long long *ovals, minv, maxv;
stat.c +3 -3
index c5a68ad5..c8a45dbd 100644
@@ -135,11 +135,11 @@ static int double_cmp(const void *a, const void *b)
 	return cmp;
 }
 
-unsigned int calc_clat_percentiles(unsigned int *io_u_plat, unsigned long nr,
+unsigned int calc_clat_percentiles(unsigned int *io_u_plat, unsigned long long nr,
 				   fio_fp64_t *plist, unsigned long long **output,
 				   unsigned long long *maxv, unsigned long long *minv)
 {
-	unsigned long sum = 0;
+	unsigned long long sum = 0;
 	unsigned int len, i, j = 0;
 	unsigned int oval_len = 0;
 	unsigned long long *ovals = NULL;
@@ -198,7 +198,7 @@ unsigned int calc_clat_percentiles(unsigned int *io_u_plat, unsigned long nr,
 /*
  * Find and display the p-th percentile of clat
  */
-static void show_clat_percentiles(unsigned int *io_u_plat, unsigned long nr,
+static void show_clat_percentiles(unsigned int *io_u_plat, unsigned long long nr,
 				  fio_fp64_t *plist, unsigned int precision,
 				  bool is_clat, struct buf_output *out)
 {
stat.h +1 -1
index 3fda0841..6ddcad25 100644
@@ -293,7 +293,7 @@ extern void init_thread_stat(struct thread_stat *ts);
 extern void init_group_run_stat(struct group_run_stats *gs);
 extern void eta_to_str(char *str, unsigned long eta_sec);
 extern bool calc_lat(struct io_stat *is, unsigned long long *min, unsigned long long *max, double *mean, double *dev);
-extern unsigned int calc_clat_percentiles(unsigned int *io_u_plat, unsigned long nr, fio_fp64_t *plist, unsigned long long **output, unsigned long long *maxv, unsigned long long *minv);
+extern unsigned int calc_clat_percentiles(unsigned int *io_u_plat, unsigned long long nr, fio_fp64_t *plist, unsigned long long **output, unsigned long long *maxv, unsigned long long *minv);
 extern void stat_calc_lat_n(struct thread_stat *ts, double *io_u_lat);
 extern void stat_calc_lat_m(struct thread_stat *ts, double *io_u_lat);
 extern void stat_calc_lat_u(struct thread_stat *ts, double *io_u_lat);
Signature · 1 line
2.13.6
#8 lore raw
Quoted 2 lines · Andrzej Jakowski

This patch fixes percentile calculation for Windows where overflow on 32 bit type happened while calculating actual percentiles.

I already applied the previous one, so I hand edited this one and massaged the changelog somewhat.

Signature · 1 line
Jens Axboe
#9 lore raw
In reply to Andrzej Jakowski
Quoted 7 lines · Andrzej Jakowski
Quoted 4 lines · Vincent Fu

Andrzej, does this actually resolve the problem you observed? unsigned long nr in the parameter list of calc_clat_percentiles() and show_clat_percentiles() also looks like it will overflow when you have more than 2^32 IOs.

Yes you are right. That original patch is not sufficient. Will get it fixed and post new version soon.

I've got a feeling there are few other 32 bit platform integer overflows lurking around. I'm trying a clang sanitizer run...

Signature · 1 line
Sitsofe | http://sucs.org/~sits/
#10 lore raw
Quoted 7 lines · Sitsofe Wheeler
Quoted 5 lines · Andrzej Jakowski
Quoted 4 lines · Vincent Fu

Andrzej, does this actually resolve the problem you observed? unsigned long nr in the parameter list of calc_clat_percentiles() and show_clat_percentiles() also looks like it will overflow when you have more than 2^32 IOs.

Quoted 5 lines
Quoted 2 lines

Yes you are right. That original patch is not sufficient. Will get it fixed and post new version soon.

I've got a feeling there are few other 32 bit platform integer overflows lurking around. I'm trying a clang sanitizer run...

The following results are from a 32 bit Linux build without your patch:
gettime.c:313:28: runtime error: unsigned integer overflow: 3600 * 2600730 cannot be represented in type 'unsigned long' gettime.c:353:53: runtime error: unsigned integer overflow:
8796093022208 * 12901928 cannot be represented in type 'unsigned long long'
overflow: (g=0): rw=read, bs=(R) 32B-32B, (W) 32B-32B, (T) 32B-32B, ioengine=null, iodepth=32
...
fio-3.1-50-g2b6f
Starting 4 threads
io_u.c:1272:28: runtime error: unsigned integer overflow: 0 - 1 cannot be represented in type 'unsigned int'
gettime.c:197:35: runtime error: unsigned integer overflow:
1429766471724 * 12901928 cannot be represented in type 'unsigned long long'
backend.c:345:7: runtime error: unsigned integer overflow: 4294967295
+ 1 cannot be represented in type 'unsigned int'
io_u.c:989:23: runtime error: unsigned integer overflow: 4294967288 + 8 cannot be represented in type 'unsigned int'
io_u.c:1071:24: runtime error: unsigned integer overflow: 4294967295 + 1 cannot be represented in type 'uint32_t' (aka 'unsigned int')
Jobs: 4 (f=4): [R(4)][26.9%][r=102MiB/s,w=0KiB/s][r=3358k,w=0 IOPS][eta 43m:52s]

^C
fio: terminating on signal 2
ioengines.c:270:22: runtime error: unsigned integer overflow: 0 - 7 cannot be represented in type 'unsigned int' stat.c:1452:18: runtime error: unsigned integer overflow: 12246275151
* 12220497954 cannot be represented in type 'unsigned long long' stat.c:1529:20: runtime error: unsigned integer overflow: 3656340552 + 3630563360 cannot be represented in type 'unsigned int' stat.c:1537:22: runtime error: unsigned integer overflow: 3002030688 + 2989903763 cannot be represented in type 'unsigned int' stat.c:1531:23: runtime error: unsigned integer overflow: 3058346638 + 1520207785 cannot be represented in type 'unsigned int' stat.c:1533:25: runtime error: unsigned integer overflow: 3058346638 + 1520207785 cannot be represented in type 'unsigned int'

(I've manually removed most of the warnings that look like unsigned wraparound was expected/desired such as when hashing is being done)

The first gettime.c problem looks like it's easily fixed with "static unsigned long long cycles_per_msec".
The next gettime.c:353:53 looks more problematic. That value is gigantic and would need a 128 bit variable to fit. I don't know if this is spurious because I'm running a VM and manually set --clocksource=cpu but it would be nice to know for sure. io_u.c:1272 looks like the standard decrementing counter whose value is taken before being negated so it's a harmless warning. backend.c:345:7 I think ts_cache_nr is expected to just keep wrapping around so this is harmless?
io_u.c:989:23 io_u_map is a uint32_t which means we can only store 2^32 latency entries before we wraparound - this sounds problematic but I don't know how to handle it (periodically scale everything?). io_u.c:1071:24 see above but for io_u_lat_u .
stat.c:1452:18 - too many samples means (no pun intended) we have an overflow. The rest of the stat.c errors appear to be down to to trying to combine samples when both values are already close to their 32 bit value maximums and the result can't be kept in 32 bits.

Any thoughts?

Signature · 1 line
Sitsofe | http://sucs.org/~sits/
#11 lore raw
Quoted 53 lines · Sitsofe Wheeler
Quoted 7 lines · Sitsofe Wheeler
Quoted 5 lines · Andrzej Jakowski
Quoted 4 lines · Vincent Fu

Andrzej, does this actually resolve the problem you observed? unsigned long nr in the parameter list of calc_clat_percentiles() and show_clat_percentiles() also looks like it will overflow when you have more than 2^32 IOs.

Quoted 5 lines
Quoted 2 lines

Yes you are right. That original patch is not sufficient. Will get it fixed and post new version soon.

I've got a feeling there are few other 32 bit platform integer overflows lurking around. I'm trying a clang sanitizer run...

The following results are from a 32 bit Linux build without your patch:
gettime.c:313:28: runtime error: unsigned integer overflow: 3600 * 2600730 cannot be represented in type 'unsigned long' gettime.c:353:53: runtime error: unsigned integer overflow:
8796093022208 * 12901928 cannot be represented in type 'unsigned long long'
overflow: (g=0): rw=read, bs=(R) 32B-32B, (W) 32B-32B, (T) 32B-32B, ioengine=null, iodepth=32
...
fio-3.1-50-g2b6f
Starting 4 threads
io_u.c:1272:28: runtime error: unsigned integer overflow: 0 - 1 cannot be represented in type 'unsigned int'
gettime.c:197:35: runtime error: unsigned integer overflow:
1429766471724 * 12901928 cannot be represented in type 'unsigned long long'
backend.c:345:7: runtime error: unsigned integer overflow: 4294967295
+ 1 cannot be represented in type 'unsigned int'
io_u.c:989:23: runtime error: unsigned integer overflow: 4294967288 + 8 cannot be represented in type 'unsigned int'
io_u.c:1071:24: runtime error: unsigned integer overflow: 4294967295 + 1 cannot be represented in type 'uint32_t' (aka 'unsigned int')
Jobs: 4 (f=4): [R(4)][26.9%][r=102MiB/s,w=0KiB/s][r=3358k,w=0 IOPS][eta 43m:52s]

^C
fio: terminating on signal 2
ioengines.c:270:22: runtime error: unsigned integer overflow: 0 - 7 cannot be represented in type 'unsigned int' stat.c:1452:18: runtime error: unsigned integer overflow: 12246275151
* 12220497954 cannot be represented in type 'unsigned long long' stat.c:1529:20: runtime error: unsigned integer overflow: 3656340552 + 3630563360 cannot be represented in type 'unsigned int' stat.c:1537:22: runtime error: unsigned integer overflow: 3002030688 + 2989903763 cannot be represented in type 'unsigned int' stat.c:1531:23: runtime error: unsigned integer overflow: 3058346638 + 1520207785 cannot be represented in type 'unsigned int' stat.c:1533:25: runtime error: unsigned integer overflow: 3058346638 + 1520207785 cannot be represented in type 'unsigned int'

Quoted 2 lines

(I've manually removed most of the warnings that look like unsigned wraparound was expected/desired such as when hashing is being done)

Yikes

Quoted 19 lines

The first gettime.c problem looks like it's easily fixed with "static unsigned long long cycles_per_msec".
The next gettime.c:353:53 looks more problematic. That value is gigantic and would need a 128 bit variable to fit. I don't know if this is spurious because I'm running a VM and manually set --clocksource=cpu but it would be nice to know for sure. io_u.c:1272 looks like the standard decrementing counter whose value is taken before being negated so it's a harmless warning. backend.c:345:7 I think ts_cache_nr is expected to just keep wrapping around so this is harmless?
io_u.c:989:23 io_u_map is a uint32_t which means we can only store 2^32 latency entries before we wraparound - this sounds problematic but I don't know how to handle it (periodically scale everything?). io_u.c:1071:24 see above but for io_u_lat_u .
stat.c:1452:18 - too many samples means (no pun intended) we have an overflow. The rest of the stat.c errors appear to be down to to trying to combine samples when both values are already close to their 32 bit value maximums and the result can't be kept in 32 bits.

Quoted 1 line

Any thoughts?

Just go through them one-by-one. I think some of these were likely introduced with the nsec conversion, which I guess isn't too surprising...

In any case, just fix them up individually, that's much better than trying to do one big patch that fixes everything. Or create a mail for each particular case that you don't see an easy fix for, then we can go over them. The easy case are just making counters or values 64-bit, regardless of platform.

Signature · 1 line
Jens Axboe
#12 lore raw
Quoted 4 lines · Jens Axboe

Just go through them one-by-one. I think some of these were likely introduced with the nsec conversion, which I guess isn't too surprising...

With all the latest patches applied these are the only warnings which look faintly legitimate:

io_u.c:1272:28: runtime error: unsigned integer overflow: 0 - 1 cannot be represented in type 'unsigned int'

Might be harmless because we goto out just after td->file_service_left has wrapped around.

ioengines.c:270:22: runtime error: unsigned integer overflow: 59 - 63 cannot be represented in type 'unsigned int'

It does look like td->io_u_in_flight is over decremented but it's not clear how I/Os that weren't already in flight somehow completed...

stat.c:1529:20: runtime error: unsigned integer overflow: 3370715872 + 1125639672 cannot be represented in type 'unsigned int' stat.c:1531:23: runtime error: unsigned integer overflow: 3370715859 + 1125639668 cannot be represented in type 'unsigned int' stat.c:1533:25: runtime error: unsigned integer overflow: 3370715862 + 1125639669 cannot be represented in type 'unsigned int'

The last three are all down to io_u_* structure members being 32 bit. Would we want to make them 64 bit and bump the protocol version?

Signature · 1 line
Sitsofe | http://sucs.org/~sits/
#13 lore raw
Quoted 6 lines · Sitsofe Wheeler
Quoted 4 lines · Jens Axboe

Just go through them one-by-one. I think some of these were likely introduced with the nsec conversion, which I guess isn't too surprising...

Quoted 8 lines

With all the latest patches applied these are the only warnings which look faintly legitimate:

io_u.c:1272:28: runtime error: unsigned integer overflow: 0 - 1 cannot be represented in type 'unsigned int'

Might be harmless because we goto out just after td->file_service_left has wrapped around.

Might be worth it to clean up, I'll do that.

Quoted 5 lines

ioengines.c:270:22: runtime error: unsigned integer overflow: 59 - 63 cannot be represented in type 'unsigned int'

It does look like td->io_u_in_flight is over decremented but it's not clear how I/Os that weren't already in flight somehow completed...

Hmm that is odd. I'll look into that.

Quoted 9 lines

stat.c:1529:20: runtime error: unsigned integer overflow: 3370715872 + 1125639672 cannot be represented in type 'unsigned int' stat.c:1531:23: runtime error: unsigned integer overflow: 3370715859 + 1125639668 cannot be represented in type 'unsigned int' stat.c:1533:25: runtime error: unsigned integer overflow: 3370715862 + 1125639669 cannot be represented in type 'unsigned int'

The last three are all down to io_u_* structure members being 32 bit. Would we want to make them 64 bit and bump the protocol version?

Yes, I think we should make them 64-bit and just bump the protocol version.

Signature · 1 line
Jens Axboe