Skip to content

Commit

Permalink
Rework humanTimeUnit() formats of meters
Browse files Browse the repository at this point in the history
The new time formats will print at most 6 characters (increased from 5)

Signed-off-by: Kang-Che Sung <[email protected]>
  • Loading branch information
Explorer09 committed Jan 23, 2025
1 parent f4bd894 commit 359ae38
Showing 1 changed file with 41 additions and 26 deletions.
67 changes: 41 additions & 26 deletions linux/GPUMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 359ae38

Please sign in to comment.