Skip to content

Commit d5b9506

Browse files
committed
Rework humanTimeUnit() formats of meters
The new time formats will print at most 6 characters (increased from 5) Signed-off-by: Kang-Che Sung <[email protected]>
1 parent 4e6a781 commit d5b9506

File tree

1 file changed

+41
-26
lines changed

1 file changed

+41
-26
lines changed

linux/GPUMeter.c

+41-26
Original file line numberDiff line numberDiff line change
@@ -38,48 +38,63 @@ static const int GPUMeter_attributes[] = {
3838
GPU_RESIDUE,
3939
};
4040

41-
static int humanTimeUnit(char* buffer, size_t size, unsigned long long int value) {
41+
static int humanTimeUnit(char* buffer, size_t size, unsigned long long totalNanoseconds) {
42+
if (totalNanoseconds < 10000)
43+
return xSnprintf(buffer, size, "%4uns", (unsigned int)totalNanoseconds);
4244

43-
if (value < 1000)
44-
return xSnprintf(buffer, size, "%3lluns", value);
45-
46-
if (value < 10000)
47-
return xSnprintf(buffer, size, "%1llu.%1lluus", value / 1000, (value % 1000) / 100);
48-
49-
value /= 1000;
45+
unsigned long long value = totalNanoseconds / 100;
5046

5147
if (value < 1000)
52-
return xSnprintf(buffer, size, "%3lluus", value);
48+
return xSnprintf(buffer, size, "%u.%uus", (unsigned int)(value / 10), (unsigned int)(value % 10));
49+
50+
value /= 10; // microseconds
5351

5452
if (value < 10000)
55-
return xSnprintf(buffer, size, "%1llu.%1llums", value / 1000, (value % 1000) / 100);
53+
return xSnprintf(buffer, size, "%4uus", (unsigned int)value);
54+
55+
value /= 100;
56+
57+
unsigned long long totalSeconds = value / 10000;
58+
if (totalSeconds < 60) {
59+
int width = 4;
60+
unsigned int seconds = (unsigned int)totalSeconds;
61+
unsigned int fraction = (unsigned int)(value % 10000);
62+
for (unsigned int limit = 1; seconds >= limit; limit *= 10) {
63+
width--;
64+
fraction /= 10;
65+
}
66+
return xSnprintf(buffer, size, "%.u.%0*us", seconds, width, fraction);
67+
}
5668

57-
value /= 1000;
69+
value = totalSeconds;
5870

59-
if (value < 1000)
60-
return xSnprintf(buffer, size, "%3llums", value);
71+
if (value < 3600)
72+
return xSnprintf(buffer, size, "%2um%02us", (unsigned int)value / 60, (unsigned int)value % 60);
6173

62-
if (value < 10000)
63-
return xSnprintf(buffer, size, "%1llu.%1llus", value / 1000, (value % 1000) / 100);
74+
value /= 60; // minutes
75+
76+
if (value < 1440)
77+
return xSnprintf(buffer, size, "%2uh%02um", (unsigned int)value / 60, (unsigned int)value % 60);
6478

65-
value /= 1000;
79+
value /= 60; // hours
6680

67-
if (value < 600)
68-
return xSnprintf(buffer, size, "%3llus", value);
81+
if (value < 2400)
82+
return xSnprintf(buffer, size, "%2ud%02uh", (unsigned int)value / 24, (unsigned int)value % 24);
6983

70-
value /= 60;
84+
value /= 24; // days
7185

72-
if (value < 600)
73-
return xSnprintf(buffer, size, "%3llum", value);
86+
if (value < 365)
87+
return xSnprintf(buffer, size, "%5ud", (unsigned int)value);
7488

75-
value /= 60;
89+
if (value < 3650)
90+
return xSnprintf(buffer, size, "%uy%03ud", (unsigned int)(value / 365), (unsigned int)(value % 365));
7691

77-
if (value < 96)
78-
return xSnprintf(buffer, size, "%3lluh", value);
92+
value /= 365; // years (ignore leap years)
7993

80-
value /= 24;
94+
if (value < 100000)
95+
return xSnprintf(buffer, size, "%5luy", (unsigned long)value);
8196

82-
return xSnprintf(buffer, size, "%3llud", value);
97+
return xSnprintf(buffer, size, " inf.");
8398
}
8499

85100
static void GPUMeter_updateValues(Meter* this) {

0 commit comments

Comments
 (0)