Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display speed alongside progress #9

Merged
merged 3 commits into from
Feb 19, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
display speed alongside progress
  • Loading branch information
toy committed Feb 18, 2018
commit 33705fbbaf9d3ba1f0e7a92bd25773961810bbee
37 changes: 28 additions & 9 deletions stressdrive.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
_a < _b ? _a : _b; \
})

#define KILO 1000
#define MEGA 1000000
#define GIGA 1000000000

typedef struct {
uint64_t total;
const char *name;
Expand All @@ -58,7 +62,8 @@ void PROGRESS_Init(PROGRESS_CTX *ctx, uint64_t total, const char *name) {
ctx->last_display = (struct timeval){0};
}

void _PROGRESS_Print(PROGRESS_CTX *ctx, struct timeval *now, uint64_t current) {
void _PROGRESS_Print(PROGRESS_CTX *ctx, struct timeval *now, uint64_t current,
uint32_t blockSize) {
double complete = (double)current / (double)ctx->total;
printf("\r%s %.1f%% (%" PRIu64 " of %" PRIu64 ")", ctx->name,
complete * 100.0, current, ctx->total);
Expand All @@ -67,6 +72,20 @@ void _PROGRESS_Print(PROGRESS_CTX *ctx, struct timeval *now, uint64_t current) {
printf(" %02" PRIu64 ":%02" PRIu64 ":%02" PRIu64 "", elapsed / 3600,
(elapsed / 60) % 60, elapsed % 60);

if (elapsed > 0) {
double speed = (double)current * blockSize / elapsed;

if (speed > GIGA) {
printf(" (%.1f GB/s)", speed / GIGA);
} else if (speed > MEGA) {
printf(" (%.1f MB/s)", speed / MEGA);
} else if (speed > KILO) {
printf(" (%.1f KB/s)", speed / KILO);
} else {
printf(" (%.1f B/s)", speed);
}
}

if (current != ctx->total && elapsed > 10 && complete > 0.001) {
uint64_t eta = (1 / complete - 1) * elapsed;
printf(" (ETA: %02" PRIu64 ":%02" PRIu64 ":%02" PRIu64 ")", eta / 3600,
Expand All @@ -77,20 +96,20 @@ void _PROGRESS_Print(PROGRESS_CTX *ctx, struct timeval *now, uint64_t current) {
fflush(stdout);
}

void PROGRESS_Update(PROGRESS_CTX *ctx, uint64_t current) {
void PROGRESS_Update(PROGRESS_CTX *ctx, uint64_t current, uint32_t blockSize) {
struct timeval now, delta;
gettimeofday(&now, NULL);
timersub(&now, &ctx->last_display, &delta);
if (delta.tv_sec < 1)
return;
ctx->last_display = now;
_PROGRESS_Print(ctx, &now, current);
_PROGRESS_Print(ctx, &now, current, blockSize);
}

void PROGRESS_Finish(PROGRESS_CTX *ctx) {
void PROGRESS_Finish(PROGRESS_CTX *ctx, uint32_t blockSize) {
struct timeval now;
gettimeofday(&now, NULL);
_PROGRESS_Print(ctx, &now, ctx->total);
_PROGRESS_Print(ctx, &now, ctx->total, blockSize);
printf("\n");
}

Expand Down Expand Up @@ -217,9 +236,9 @@ int main(int argc, const char *argv[]) {
exit(EXIT_CALL_FAILED);
}
SHA1_Update(&shaContext, buffer, size);
PROGRESS_Update(&progress, blockIndex);
PROGRESS_Update(&progress, blockIndex, blockSize);
}
PROGRESS_Finish(&progress);
PROGRESS_Finish(&progress, blockSize);

uint8_t writtenShaDigest[SHA_DIGEST_LENGTH];
SHA1_Finish(writtenShaDigest, &shaContext, "written");
Expand All @@ -242,9 +261,9 @@ int main(int argc, const char *argv[]) {
exit(EXIT_CALL_FAILED);
}
SHA1_Update(&shaContext, buffer, size);
PROGRESS_Update(&progress, blockIndex);
PROGRESS_Update(&progress, blockIndex, blockSize);
}
PROGRESS_Finish(&progress);
PROGRESS_Finish(&progress, blockSize);

uint8_t readShaDigest[SHA_DIGEST_LENGTH];
SHA1_Finish(readShaDigest, &shaContext, "read");
Expand Down