Skip to content

Commit 602d4f4

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 f4bd894 commit 602d4f4

File tree

1 file changed

+42
-26
lines changed

1 file changed

+42
-26
lines changed

linux/GPUMeter.c

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,48 +38,64 @@ 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+
// "%.u" prints no digits if (seconds == 0).
67+
return xSnprintf(buffer, size, "%.u.%0*us", seconds, width, fraction);
68+
}
5669

57-
value /= 1000;
70+
value = totalSeconds;
5871

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

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

65-
value /= 1000;
80+
value /= 60; // hours
6681

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

70-
value /= 60;
85+
value /= 24; // days
7186

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

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

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

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

82-
return xSnprintf(buffer, size, "%3llud", value);
98+
return xSnprintf(buffer, size, " inf.");
8399
}
84100

85101
static void GPUMeter_updateValues(Meter* this) {

0 commit comments

Comments
 (0)