hdparm: make -T -t code smaller (-194 bytes), and output prettier

This commit is contained in:
Denis Vlasenko 2007-05-22 21:46:11 +00:00
parent 0681033918
commit 6963eb53a1

View File

@ -432,12 +432,11 @@ static const char * const secu_str[] = {
/* word 255: integrity */
#define SIG 0x00ff /* signature location */
#define SIG_VAL 0x00A5 /* signature value */
#define SIG_VAL 0x00a5 /* signature value */
#define TIMING_MB 64
#define TIMING_BUF_MB 1
#define TIMING_BUF_BYTES (TIMING_BUF_MB * 1024 * 1024)
#define TIMING_BUF_COUNT (timing_MB / TIMING_BUF_MB)
#define BUFCACHE_FACTOR 2
#undef DO_FLUSHCACHE /* under construction: force cache flush on -W0 */
@ -1276,14 +1275,6 @@ static int read_big_block(int fd, char *buf)
return 0;
}
static void print_timing(int t, double e)
{
if (t >= e) /* more than 1MB/s */
printf("%2d MB in %5.2f seconds =%6.2f %cB/sec\n", t, e, t / e, 'M');
else
printf("%2d MB in %5.2f seconds =%6.2f %cB/sec\n", t, e, t / e * 1024, 'k');
}
static int do_blkgetsize(int fd, unsigned long long *blksize64)
{
int rc;
@ -1300,17 +1291,22 @@ static int do_blkgetsize(int fd, unsigned long long *blksize64)
return rc;
}
static void print_timing(unsigned t, double e)
{
if (t >= e) /* more than 1MB/s */
printf("%4d MB in %.2f seconds = %.2f %cB/sec\n", t, e, t / e, 'M');
else
printf("%4d MB in %.2f seconds = %.2f %cB/sec\n", t, e, t / e * 1024, 'k');
}
static void do_time(int flag, int fd)
/*
flag = 0 time_cache
flag = 1 time_device
*/
/* flag = 0 time_cache, 1 time_device */
{
static const struct itimerval thousand = {{1000, 0}, {1000, 0}};
struct itimerval e1, e2;
double elapsed, elapsed2;
unsigned max_iterations = 1024, total_MB, iterations;
struct itimerval itv;
unsigned elapsed, elapsed2;
unsigned max_iterations, total_MB, iterations;
unsigned long long blksize;
RESERVE_CONFIG_BUFFER(buf, TIMING_BUF_BYTES);
@ -1319,75 +1315,57 @@ static void do_time(int flag, int fd)
goto quit2;
}
max_iterations = 1024;
if (0 == do_blkgetsize(fd, &blksize)) {
max_iterations = blksize / (2 * 1024) / TIMING_BUF_MB;
}
/* Clear out the device request queues & give them time to complete */
sync();
sleep(3);
setitimer(ITIMER_REAL, &thousand, NULL);
if (flag == 0) {
/* Time cache */
if (seek_to_zero(fd)) return;
if (read_big_block(fd, buf)) return;
printf(" Timing cached reads: ");
fflush(stdout);
/* Now do the timing */
iterations = 0;
getitimer(ITIMER_REAL, &e1);
do {
++iterations;
if (seek_to_zero(fd) || read_big_block(fd, buf))
goto quit;
getitimer(ITIMER_REAL, &e2);
elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec)
+ ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
} while (elapsed < 2.0);
total_MB = iterations * TIMING_BUF_MB;
/* Now remove the lseek() and getitimer() overheads from the elapsed time */
getitimer(ITIMER_REAL, &e1);
do {
sleep(2);
if (flag == 0) { /* Time cache */
if (seek_to_zero(fd))
goto quit;
getitimer(ITIMER_REAL, &e2);
elapsed2 = (e1.it_value.tv_sec - e2.it_value.tv_sec)
+ ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
} while (--iterations);
elapsed -= elapsed2;
print_timing(BUFCACHE_FACTOR * total_MB, elapsed);
flush_buffer_cache(fd);
sleep(1);
} else {
/* Time device */
if (read_big_block(fd, buf))
goto quit;
printf(" Timing buffer-cache reads: ");
} else { /* Time device */
printf(" Timing buffered disk reads: ");
}
fflush(stdout);
iterations = 0;
/*
* getitimer() is used rather than gettimeofday() because
* it is much more consistent (on my machine, at least).
*/
/* Now do the timings for real */
iterations = 0;
getitimer(ITIMER_REAL, &e1);
setitimer(ITIMER_REAL, &thousand, NULL);
/* Now do the timing */
do {
++iterations;
if ((flag == 0) && seek_to_zero(fd))
goto quit;
if (read_big_block(fd, buf))
goto quit;
getitimer(ITIMER_REAL, &e2);
elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec)
+ ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
} while (elapsed < 3.0 && iterations < max_iterations);
getitimer(ITIMER_REAL, &itv);
elapsed = (1000 - itv.it_value.tv_sec) * 1000000
- itv.it_value.tv_usec;
} while (elapsed < 3000000 && iterations < max_iterations);
total_MB = iterations * TIMING_BUF_MB;
print_timing(total_MB, elapsed);
if (flag == 0) {
/* Now remove the lseek() and getitimer() overheads from the elapsed time */
setitimer(ITIMER_REAL, &thousand, NULL);
do {
if (seek_to_zero(fd))
goto quit;
getitimer(ITIMER_REAL, &itv);
elapsed2 = (1000 - itv.it_value.tv_sec) * 1000000
- itv.it_value.tv_usec;
} while (--iterations);
elapsed -= elapsed2;
total_MB *= BUFCACHE_FACTOR;
flush_buffer_cache(fd);
}
print_timing(total_MB, elapsed / 1000000.0);
quit:
munlock(buf, TIMING_BUF_BYTES);
quit2: