dd: Improve output format
Include written bytes and speed with units
This commit is contained in:
parent
4273f43be1
commit
9213dd13bc
|
@ -1,5 +1,6 @@
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <inttypes.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -7,35 +8,91 @@
|
||||||
|
|
||||||
#define CURRENT_NS() ({ timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); ts.tv_sec * 1'000'000'000 + ts.tv_nsec; })
|
#define CURRENT_NS() ({ timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); ts.tv_sec * 1'000'000'000 + ts.tv_nsec; })
|
||||||
|
|
||||||
int parse_int(const char* val)
|
uint64_t parse_sized_u64(const char* val)
|
||||||
{
|
{
|
||||||
int result = 0;
|
uint64_t result = 0;
|
||||||
for (const char* ptr = val; *ptr; ptr++)
|
|
||||||
|
const char* ptr = val;
|
||||||
|
for (; *ptr && isdigit(*ptr); ptr++)
|
||||||
|
result = (result * 10) + (*ptr - '0');
|
||||||
|
|
||||||
|
switch (*ptr)
|
||||||
{
|
{
|
||||||
if (!isdigit(*ptr))
|
case 'E': result *= 1024; // fall through
|
||||||
|
case 'P': result *= 1024; // fall through
|
||||||
|
case 'T': result *= 1024; // fall through
|
||||||
|
case 'G': result *= 1024; // fall through
|
||||||
|
case 'M': result *= 1024; // fall through
|
||||||
|
case 'K': case 'k': result *= 1024; ptr++; break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*ptr != '\0')
|
||||||
{
|
{
|
||||||
fprintf(stderr, "invalid number: %s\n", val);
|
fprintf(stderr, "invalid number: %s\n", val);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
result = (result * 10) + (*ptr - '0');
|
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_time(uint64_t start_ns, uint64_t end_ns, size_t transfered)
|
void print_value_with_unit(uint64_t value_x10, unsigned base, const char* units[])
|
||||||
|
{
|
||||||
|
unsigned index = 0;
|
||||||
|
while (value_x10 / 10 >= base)
|
||||||
|
{
|
||||||
|
index++;
|
||||||
|
value_x10 /= base;
|
||||||
|
}
|
||||||
|
if (value_x10 < 100)
|
||||||
|
printf("%u.%u %s", (unsigned)value_x10 / 10, (unsigned)value_x10 % 10, units[index]);
|
||||||
|
else
|
||||||
|
printf("%u %s", (unsigned)value_x10 / 10, units[index]);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_time(uint64_t start_ns, uint64_t end_ns, uint64_t transfered, bool last = false)
|
||||||
{
|
{
|
||||||
static bool first = true;
|
static bool first = true;
|
||||||
uint64_t duration_ns = end_ns - start_ns;
|
if (!first)
|
||||||
printf("%s%zu bytes copied, %d.%09d s\e[K\n", (first ? "" : "\e[F"), transfered, (int)(duration_ns / 1'000'000'000), (int)(duration_ns % 1'000'000'000));
|
printf("\e[F");
|
||||||
first = false;
|
first = false;
|
||||||
|
|
||||||
|
printf("%" PRIu64 " bytes", transfered);
|
||||||
|
if (transfered >= 1000)
|
||||||
|
{
|
||||||
|
printf(" (");
|
||||||
|
{
|
||||||
|
const char* units[] { "", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", "RB", "QB" };
|
||||||
|
print_value_with_unit(transfered * 10, 1000, units);
|
||||||
|
}
|
||||||
|
if (transfered >= 1024)
|
||||||
|
{
|
||||||
|
printf(", ");
|
||||||
|
const char* units[] { "", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB", "RiB", "QiB" };
|
||||||
|
print_value_with_unit(transfered * 10, 1024, units);
|
||||||
|
}
|
||||||
|
printf(")");
|
||||||
|
}
|
||||||
|
printf(" copied");
|
||||||
|
|
||||||
|
double duration_s = (end_ns - start_ns) / 1e9;
|
||||||
|
if (last)
|
||||||
|
printf(", %f s, ", duration_s);
|
||||||
|
else
|
||||||
|
printf(", %u s, ", (unsigned)duration_s);
|
||||||
|
|
||||||
|
const char* units[] { "B/s", "kB/s", "MB/s", "GB/s", "TB/s", "PB/s", "EB/s", "ZB/s", "YB/s", "RB/s", "QB/s" };
|
||||||
|
print_value_with_unit(10 * transfered / duration_s, 1000, units);
|
||||||
|
|
||||||
|
printf("\e[K\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
const char* input = nullptr;
|
const char* input = nullptr;
|
||||||
const char* output = nullptr;
|
const char* output = nullptr;
|
||||||
int bs = 512;
|
uint64_t bs = 512;
|
||||||
int count = -1;
|
uint64_t count = ~(uint64_t)0;
|
||||||
bool print_progress = false;
|
bool print_progress = false;
|
||||||
|
|
||||||
for (int i = 1; i < argc; i++)
|
for (int i = 1; i < argc; i++)
|
||||||
|
@ -45,9 +102,9 @@ int main(int argc, char** argv)
|
||||||
else if (strncmp(argv[i], "of=", 3) == 0)
|
else if (strncmp(argv[i], "of=", 3) == 0)
|
||||||
output = argv[i] + 3;
|
output = argv[i] + 3;
|
||||||
else if (strncmp(argv[i], "bs=", 3) == 0)
|
else if (strncmp(argv[i], "bs=", 3) == 0)
|
||||||
bs = parse_int(argv[i] + 3);
|
bs = parse_sized_u64(argv[i] + 3);
|
||||||
else if (strncmp(argv[i], "count=", 6) == 0)
|
else if (strncmp(argv[i], "count=", 6) == 0)
|
||||||
count = parse_int(argv[i] + 6);
|
count = parse_sized_u64(argv[i] + 6);
|
||||||
else if (strcmp(argv[i], "status=progress") == 0)
|
else if (strcmp(argv[i], "status=progress") == 0)
|
||||||
print_progress = true;
|
print_progress = true;
|
||||||
else
|
else
|
||||||
|
@ -86,12 +143,11 @@ int main(int argc, char** argv)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t total_transfered = 0;
|
uint64_t total_transfered = 0;
|
||||||
|
|
||||||
uint64_t start_ns = CURRENT_NS();
|
uint64_t start_ns = CURRENT_NS();
|
||||||
uint64_t last_print_ns = 0;
|
uint64_t last_print_s = 0;
|
||||||
|
|
||||||
for (int i = 0; i != count; i++)
|
for (uint64_t i = 0; i != count; i++)
|
||||||
{
|
{
|
||||||
ssize_t nread = read(ifd, buffer, bs);
|
ssize_t nread = read(ifd, buffer, bs);
|
||||||
if (nread == -1)
|
if (nread == -1)
|
||||||
|
@ -109,21 +165,21 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
total_transfered += nwrite;
|
total_transfered += nwrite;
|
||||||
|
|
||||||
if (nread < bs || nwrite < bs)
|
if ((size_t)nread < bs || (size_t)nwrite < bs)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (print_progress)
|
if (print_progress)
|
||||||
{
|
{
|
||||||
uint64_t current_ns = CURRENT_NS();
|
uint64_t current_ns = CURRENT_NS();
|
||||||
if (current_ns >= last_print_ns + 1'000'000'000)
|
if ((current_ns - start_ns) / 1'000'000'000 > last_print_s)
|
||||||
{
|
{
|
||||||
print_time(start_ns, current_ns, total_transfered);
|
print_time(start_ns, current_ns, total_transfered);
|
||||||
last_print_ns = current_ns;
|
last_print_s = (current_ns - start_ns) / 1'000'000'000;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
print_time(start_ns, CURRENT_NS(), total_transfered);
|
print_time(start_ns, CURRENT_NS(), total_transfered, true);
|
||||||
|
|
||||||
close(ifd);
|
close(ifd);
|
||||||
close(ofd);
|
close(ofd);
|
||||||
|
|
Loading…
Reference in New Issue