hdparm: make -T -t code smaller (-194 bytes), and output prettier
This commit is contained in:
@@ -432,12 +432,11 @@ static const char * const secu_str[] = {
|
|||||||
|
|
||||||
/* word 255: integrity */
|
/* word 255: integrity */
|
||||||
#define SIG 0x00ff /* signature location */
|
#define SIG 0x00ff /* signature location */
|
||||||
#define SIG_VAL 0x00A5 /* signature value */
|
#define SIG_VAL 0x00a5 /* signature value */
|
||||||
|
|
||||||
#define TIMING_MB 64
|
#define TIMING_MB 64
|
||||||
#define TIMING_BUF_MB 1
|
#define TIMING_BUF_MB 1
|
||||||
#define TIMING_BUF_BYTES (TIMING_BUF_MB * 1024 * 1024)
|
#define TIMING_BUF_BYTES (TIMING_BUF_MB * 1024 * 1024)
|
||||||
#define TIMING_BUF_COUNT (timing_MB / TIMING_BUF_MB)
|
|
||||||
#define BUFCACHE_FACTOR 2
|
#define BUFCACHE_FACTOR 2
|
||||||
|
|
||||||
#undef DO_FLUSHCACHE /* under construction: force cache flush on -W0 */
|
#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;
|
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)
|
static int do_blkgetsize(int fd, unsigned long long *blksize64)
|
||||||
{
|
{
|
||||||
int rc;
|
int rc;
|
||||||
@@ -1300,17 +1291,22 @@ static int do_blkgetsize(int fd, unsigned long long *blksize64)
|
|||||||
return rc;
|
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)
|
static void do_time(int flag, int fd)
|
||||||
/*
|
/* flag = 0 time_cache, 1 time_device */
|
||||||
flag = 0 time_cache
|
|
||||||
flag = 1 time_device
|
|
||||||
*/
|
|
||||||
{
|
{
|
||||||
static const struct itimerval thousand = {{1000, 0}, {1000, 0}};
|
static const struct itimerval thousand = {{1000, 0}, {1000, 0}};
|
||||||
|
|
||||||
struct itimerval e1, e2;
|
struct itimerval itv;
|
||||||
double elapsed, elapsed2;
|
unsigned elapsed, elapsed2;
|
||||||
unsigned max_iterations = 1024, total_MB, iterations;
|
unsigned max_iterations, total_MB, iterations;
|
||||||
unsigned long long blksize;
|
unsigned long long blksize;
|
||||||
RESERVE_CONFIG_BUFFER(buf, TIMING_BUF_BYTES);
|
RESERVE_CONFIG_BUFFER(buf, TIMING_BUF_BYTES);
|
||||||
|
|
||||||
@@ -1319,75 +1315,57 @@ static void do_time(int flag, int fd)
|
|||||||
goto quit2;
|
goto quit2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
max_iterations = 1024;
|
||||||
if (0 == do_blkgetsize(fd, &blksize)) {
|
if (0 == do_blkgetsize(fd, &blksize)) {
|
||||||
max_iterations = blksize / (2 * 1024) / TIMING_BUF_MB;
|
max_iterations = blksize / (2 * 1024) / TIMING_BUF_MB;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Clear out the device request queues & give them time to complete */
|
/* Clear out the device request queues & give them time to complete */
|
||||||
sync();
|
sync();
|
||||||
sleep(3);
|
sleep(2);
|
||||||
|
if (flag == 0) { /* Time cache */
|
||||||
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 {
|
|
||||||
if (seek_to_zero(fd))
|
if (seek_to_zero(fd))
|
||||||
goto quit;
|
goto quit;
|
||||||
getitimer(ITIMER_REAL, &e2);
|
if (read_big_block(fd, buf))
|
||||||
elapsed2 = (e1.it_value.tv_sec - e2.it_value.tv_sec)
|
goto quit;
|
||||||
+ ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
|
printf(" Timing buffer-cache reads: ");
|
||||||
} while (--iterations);
|
} else { /* Time device */
|
||||||
|
|
||||||
elapsed -= elapsed2;
|
|
||||||
print_timing(BUFCACHE_FACTOR * total_MB, elapsed);
|
|
||||||
flush_buffer_cache(fd);
|
|
||||||
sleep(1);
|
|
||||||
} else {
|
|
||||||
/* Time device */
|
|
||||||
|
|
||||||
printf(" Timing buffered disk reads: ");
|
printf(" Timing buffered disk reads: ");
|
||||||
|
}
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
|
iterations = 0;
|
||||||
/*
|
/*
|
||||||
* getitimer() is used rather than gettimeofday() because
|
* getitimer() is used rather than gettimeofday() because
|
||||||
* it is much more consistent (on my machine, at least).
|
* it is much more consistent (on my machine, at least).
|
||||||
*/
|
*/
|
||||||
/* Now do the timings for real */
|
setitimer(ITIMER_REAL, &thousand, NULL);
|
||||||
iterations = 0;
|
/* Now do the timing */
|
||||||
getitimer(ITIMER_REAL, &e1);
|
|
||||||
do {
|
do {
|
||||||
++iterations;
|
++iterations;
|
||||||
|
if ((flag == 0) && seek_to_zero(fd))
|
||||||
|
goto quit;
|
||||||
if (read_big_block(fd, buf))
|
if (read_big_block(fd, buf))
|
||||||
goto quit;
|
goto quit;
|
||||||
getitimer(ITIMER_REAL, &e2);
|
getitimer(ITIMER_REAL, &itv);
|
||||||
elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec)
|
elapsed = (1000 - itv.it_value.tv_sec) * 1000000
|
||||||
+ ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
|
- itv.it_value.tv_usec;
|
||||||
} while (elapsed < 3.0 && iterations < max_iterations);
|
} while (elapsed < 3000000 && iterations < max_iterations);
|
||||||
|
|
||||||
total_MB = iterations * TIMING_BUF_MB;
|
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:
|
quit:
|
||||||
munlock(buf, TIMING_BUF_BYTES);
|
munlock(buf, TIMING_BUF_BYTES);
|
||||||
quit2:
|
quit2:
|
||||||
|
Reference in New Issue
Block a user