| 457 | } |
| 458 | |
| 459 | string HumanReadableNumBytes(int64 num_bytes) { |
| 460 | if (num_bytes == kint64min) { |
| 461 | // Special case for number with not representable negation. |
| 462 | return "-8E"; |
| 463 | } |
| 464 | |
| 465 | const char* neg_str = (num_bytes < 0) ? "-" : ""; |
| 466 | if (num_bytes < 0) { |
| 467 | num_bytes = -num_bytes; |
| 468 | } |
| 469 | |
| 470 | // Special case for bytes. |
| 471 | if (num_bytes < 1024) { |
| 472 | // No fractions for bytes. |
| 473 | char buf[8]; // Longest possible string is '-XXXXB' |
| 474 | snprintf(buf, sizeof(buf), "%s%lldB", neg_str, |
| 475 | static_cast<int64>(num_bytes)); |
| 476 | return string(buf); |
| 477 | } |
| 478 | |
| 479 | static const char units[] = "KMGTPE"; // int64 only goes up to E. |
| 480 | const char* unit = units; |
| 481 | while (num_bytes >= static_cast<int64>(1024) * 1024) { |
| 482 | num_bytes /= 1024; |
| 483 | ++unit; |
| 484 | CHECK(unit < units + TF_ARRAYSIZE(units)); |
| 485 | } |
| 486 | |
| 487 | // We use SI prefixes. |
| 488 | char buf[16]; |
| 489 | snprintf(buf, sizeof(buf), ((*unit == 'K') ? "%s%.1f%ciB" : "%s%.2f%ciB"), |
| 490 | neg_str, num_bytes / 1024.0, *unit); |
| 491 | return string(buf); |
| 492 | } |
| 493 | |
| 494 | string HumanReadableElapsedTime(double seconds) { |
| 495 | string human_readable; |
no outgoing calls