diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-09-28 11:21:47 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-09-28 11:21:47 +0000 |
commit | a7bc9ac8f8a398fd11d10104a7aa26edd9b2f5a7 (patch) | |
tree | d5e2b8137c863fc5f4cf3193581051125c1efe7e | |
parent | b9256054419582dc35917b1cb39c7e09f489dfad (diff) | |
download | busybox-w32-a7bc9ac8f8a398fd11d10104a7aa26edd9b2f5a7.tar.gz busybox-w32-a7bc9ac8f8a398fd11d10104a7aa26edd9b2f5a7.tar.bz2 busybox-w32-a7bc9ac8f8a398fd11d10104a7aa26edd9b2f5a7.zip |
hdparm: reduce possibility of numeric overflow in -T.
do_time 386 410 +24
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 24/0) Total: 24 bytes
-rw-r--r-- | miscutils/hdparm.c | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/miscutils/hdparm.c b/miscutils/hdparm.c index 8a7204f29..1f85644d6 100644 --- a/miscutils/hdparm.c +++ b/miscutils/hdparm.c | |||
@@ -1375,10 +1375,13 @@ static void print_timing(unsigned long m, unsigned elapsed_us) | |||
1375 | printf("%5lu MB in %u.%02u seconds = %lu kB/s\n", | 1375 | printf("%5lu MB in %u.%02u seconds = %lu kB/s\n", |
1376 | m, sec, hs, | 1376 | m, sec, hs, |
1377 | /* Trying to not overflow 32-bit arith in m * CONST | 1377 | /* Trying to not overflow 32-bit arith in m * CONST |
1378 | * by keeping CONST not so big. + 1 prevents div-by-0. */ | 1378 | * by keeping CONST not so big. But elapsed_us / CONST2 |
1379 | (m * (1024 * 1000000 / (64*1024))) / (elapsed_us / (64*1024) + 1) | 1379 | * also should not introduce big errors. Currently, |
1380 | // ~= (m * 1024 * 1000000) / elapsed_ms | 1380 | * 16000us is ~1.6% of 1 second. |
1381 | // = (m * 1024) / (elapsed_ms / 1000000) | 1381 | * "+ 1" prevents div-by-0. */ |
1382 | (m * (1024 * 1000000 / (16*1024))) / (elapsed_us / (16*1024) + 1) | ||
1383 | // ~= (m * 1024 * 1000000) / elapsed_us | ||
1384 | // = (m * 1024) / (elapsed_us / 1000000) | ||
1382 | // = kb / elapsed_sec | 1385 | // = kb / elapsed_sec |
1383 | ); | 1386 | ); |
1384 | } | 1387 | } |
@@ -1418,6 +1421,11 @@ static void do_time(int cache /*,int fd*/) | |||
1418 | 1421 | ||
1419 | /* Now do the timing */ | 1422 | /* Now do the timing */ |
1420 | iterations = 0; | 1423 | iterations = 0; |
1424 | /* Max time to run (small for cache, avoids getting | ||
1425 | * huge total_MB which can overlow on print_timing) */ | ||
1426 | elapsed2 = 510000; /* cache */ | ||
1427 | if (!cache) | ||
1428 | elapsed2 = 3000000; /* not cache */ | ||
1421 | start = monotonic_us(); | 1429 | start = monotonic_us(); |
1422 | do { | 1430 | do { |
1423 | if (cache) | 1431 | if (cache) |
@@ -1425,11 +1433,11 @@ static void do_time(int cache /*,int fd*/) | |||
1425 | read_big_block(buf); | 1433 | read_big_block(buf); |
1426 | elapsed = (unsigned)monotonic_us() - start; | 1434 | elapsed = (unsigned)monotonic_us() - start; |
1427 | ++iterations; | 1435 | ++iterations; |
1428 | } while (elapsed < 3000000 && iterations < max_iterations); | 1436 | } while (elapsed < elapsed2 && iterations < max_iterations); |
1429 | total_MB = (unsigned long)iterations * TIMING_BUF_MB; | 1437 | total_MB = (unsigned long)iterations * TIMING_BUF_MB; |
1430 | //printf(" elapsed:%u iterations:%u ", elapsed, iterations); | 1438 | //printf(" elapsed:%u iterations:%u ", elapsed, iterations); |
1431 | if (cache) { | 1439 | if (cache) { |
1432 | /* Now remove the lseek() and monotonic_us() overheads | 1440 | /* Cache: remove lseek() and monotonic_us() overheads |
1433 | * from elapsed */ | 1441 | * from elapsed */ |
1434 | start = monotonic_us(); | 1442 | start = monotonic_us(); |
1435 | do { | 1443 | do { |