xref: /openbmc/linux/tools/perf/util/units.c (revision 1c2dd16a)
1 #include "units.h"
2 #include <inttypes.h>
3 #include <linux/kernel.h>
4 #include <linux/time64.h>
5 
6 unsigned long convert_unit(unsigned long value, char *unit)
7 {
8 	*unit = ' ';
9 
10 	if (value > 1000) {
11 		value /= 1000;
12 		*unit = 'K';
13 	}
14 
15 	if (value > 1000) {
16 		value /= 1000;
17 		*unit = 'M';
18 	}
19 
20 	if (value > 1000) {
21 		value /= 1000;
22 		*unit = 'G';
23 	}
24 
25 	return value;
26 }
27 
28 int unit_number__scnprintf(char *buf, size_t size, u64 n)
29 {
30 	char unit[4] = "BKMG";
31 	int i = 0;
32 
33 	while (((n / 1024) > 1) && (i < 3)) {
34 		n /= 1024;
35 		i++;
36 	}
37 
38 	return scnprintf(buf, size, "%" PRIu64 "%c", n, unit[i]);
39 }
40