From d5b950622a15ca9749fdec0e2b89f8bb20cb6380 Mon Sep 17 00:00:00 2001 From: Explorer09 Date: Fri, 20 Dec 2024 04:34:21 +0800 Subject: [PATCH] Rework humanTimeUnit() formats of meters The new time formats will print at most 6 characters (increased from 5) Signed-off-by: Kang-Che Sung --- linux/GPUMeter.c | 67 +++++++++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/linux/GPUMeter.c b/linux/GPUMeter.c index 628d4c71c..6854eaf37 100644 --- a/linux/GPUMeter.c +++ b/linux/GPUMeter.c @@ -38,48 +38,63 @@ static const int GPUMeter_attributes[] = { GPU_RESIDUE, }; -static int humanTimeUnit(char* buffer, size_t size, unsigned long long int value) { +static int humanTimeUnit(char* buffer, size_t size, unsigned long long totalNanoseconds) { + if (totalNanoseconds < 10000) + return xSnprintf(buffer, size, "%4uns", (unsigned int)totalNanoseconds); - if (value < 1000) - return xSnprintf(buffer, size, "%3lluns", value); - - if (value < 10000) - return xSnprintf(buffer, size, "%1llu.%1lluus", value / 1000, (value % 1000) / 100); - - value /= 1000; + unsigned long long value = totalNanoseconds / 100; if (value < 1000) - return xSnprintf(buffer, size, "%3lluus", value); + return xSnprintf(buffer, size, "%u.%uus", (unsigned int)(value / 10), (unsigned int)(value % 10)); + + value /= 10; // microseconds if (value < 10000) - return xSnprintf(buffer, size, "%1llu.%1llums", value / 1000, (value % 1000) / 100); + return xSnprintf(buffer, size, "%4uus", (unsigned int)value); + + value /= 100; + + unsigned long long totalSeconds = value / 10000; + if (totalSeconds < 60) { + int width = 4; + unsigned int seconds = (unsigned int)totalSeconds; + unsigned int fraction = (unsigned int)(value % 10000); + for (unsigned int limit = 1; seconds >= limit; limit *= 10) { + width--; + fraction /= 10; + } + return xSnprintf(buffer, size, "%.u.%0*us", seconds, width, fraction); + } - value /= 1000; + value = totalSeconds; - if (value < 1000) - return xSnprintf(buffer, size, "%3llums", value); + if (value < 3600) + return xSnprintf(buffer, size, "%2um%02us", (unsigned int)value / 60, (unsigned int)value % 60); - if (value < 10000) - return xSnprintf(buffer, size, "%1llu.%1llus", value / 1000, (value % 1000) / 100); + value /= 60; // minutes + + if (value < 1440) + return xSnprintf(buffer, size, "%2uh%02um", (unsigned int)value / 60, (unsigned int)value % 60); - value /= 1000; + value /= 60; // hours - if (value < 600) - return xSnprintf(buffer, size, "%3llus", value); + if (value < 2400) + return xSnprintf(buffer, size, "%2ud%02uh", (unsigned int)value / 24, (unsigned int)value % 24); - value /= 60; + value /= 24; // days - if (value < 600) - return xSnprintf(buffer, size, "%3llum", value); + if (value < 365) + return xSnprintf(buffer, size, "%5ud", (unsigned int)value); - value /= 60; + if (value < 3650) + return xSnprintf(buffer, size, "%uy%03ud", (unsigned int)(value / 365), (unsigned int)(value % 365)); - if (value < 96) - return xSnprintf(buffer, size, "%3lluh", value); + value /= 365; // years (ignore leap years) - value /= 24; + if (value < 100000) + return xSnprintf(buffer, size, "%5luy", (unsigned long)value); - return xSnprintf(buffer, size, "%3llud", value); + return xSnprintf(buffer, size, " inf."); } static void GPUMeter_updateValues(Meter* this) {