aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--tools/perf/tests/Build1
-rw-r--r--tools/perf/tests/builtin-test.c4
-rw-r--r--tools/perf/tests/tests.h1
-rw-r--r--tools/perf/tests/unit_number__scnprintf.c37
-rw-r--r--tools/perf/util/evlist.c8
-rw-r--r--tools/perf/util/util.c13
-rw-r--r--tools/perf/util/util.h1
7 files changed, 63 insertions, 2 deletions
diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index 6676c2dd6dcb..1cb3d9b540e9 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -44,6 +44,7 @@ perf-y += is_printable_array.o
44perf-y += bitmap.o 44perf-y += bitmap.o
45perf-y += perf-hooks.o 45perf-y += perf-hooks.o
46perf-y += clang.o 46perf-y += clang.o
47perf-y += unit_number__scnprintf.o
47 48
48$(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build 49$(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build
49 $(call rule_mkdir) 50 $(call rule_mkdir)
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index a77dcc0d24e3..37e326bfd2dc 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -247,6 +247,10 @@ static struct test generic_tests[] = {
247 } 247 }
248 }, 248 },
249 { 249 {
250 .desc = "unit_number__scnprintf",
251 .func = test__unit_number__scnprint,
252 },
253 {
250 .func = NULL, 254 .func = NULL,
251 }, 255 },
252}; 256};
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index a512f0c8ff5b..1fa9b9d83aa5 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -96,6 +96,7 @@ int test__perf_hooks(int subtest);
96int test__clang(int subtest); 96int test__clang(int subtest);
97const char *test__clang_subtest_get_desc(int subtest); 97const char *test__clang_subtest_get_desc(int subtest);
98int test__clang_subtest_get_nr(void); 98int test__clang_subtest_get_nr(void);
99int test__unit_number__scnprint(int subtest);
99 100
100#if defined(__arm__) || defined(__aarch64__) 101#if defined(__arm__) || defined(__aarch64__)
101#ifdef HAVE_DWARF_UNWIND_SUPPORT 102#ifdef HAVE_DWARF_UNWIND_SUPPORT
diff --git a/tools/perf/tests/unit_number__scnprintf.c b/tools/perf/tests/unit_number__scnprintf.c
new file mode 100644
index 000000000000..623c2aa53c4a
--- /dev/null
+++ b/tools/perf/tests/unit_number__scnprintf.c
@@ -0,0 +1,37 @@
1#include <linux/compiler.h>
2#include <linux/types.h>
3#include "tests.h"
4#include "util.h"
5#include "debug.h"
6
7int test__unit_number__scnprint(int subtest __maybe_unused)
8{
9 struct {
10 u64 n;
11 const char *str;
12 } test[] = {
13 { 1, "1B" },
14 { 10*1024, "10K" },
15 { 20*1024*1024, "20M" },
16 { 30*1024*1024*1024ULL, "30G" },
17 { 0, "0B" },
18 { 0, NULL },
19 };
20 unsigned i = 0;
21
22 while (test[i].str) {
23 char buf[100];
24
25 unit_number__scnprintf(buf, sizeof(buf), test[i].n);
26
27 pr_debug("n %" PRIu64 ", str '%s', buf '%s'\n",
28 test[i].n, test[i].str, buf);
29
30 if (strcmp(test[i].str, buf))
31 return TEST_FAIL;
32
33 i++;
34 }
35
36 return TEST_OK;
37}
diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index 23e6f33edcf2..dc4df3d2660e 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -1224,12 +1224,16 @@ static long parse_pages_arg(const char *str, unsigned long min,
1224 if (pages == 0 && min == 0) { 1224 if (pages == 0 && min == 0) {
1225 /* leave number of pages at 0 */ 1225 /* leave number of pages at 0 */
1226 } else if (!is_power_of_2(pages)) { 1226 } else if (!is_power_of_2(pages)) {
1227 char buf[100];
1228
1227 /* round pages up to next power of 2 */ 1229 /* round pages up to next power of 2 */
1228 pages = roundup_pow_of_two(pages); 1230 pages = roundup_pow_of_two(pages);
1229 if (!pages) 1231 if (!pages)
1230 return -EINVAL; 1232 return -EINVAL;
1231 pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n", 1233
1232 pages * page_size, pages); 1234 unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
1235 pr_info("rounding mmap pages size to %s (%lu pages)\n",
1236 buf, pages);
1233 } 1237 }
1234 1238
1235 if (pages > max) 1239 if (pages > max)
diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index 9ddd98827d12..bf29aed16bd6 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -789,3 +789,16 @@ int is_printable_array(char *p, unsigned int len)
789 } 789 }
790 return 1; 790 return 1;
791} 791}
792
793int unit_number__scnprintf(char *buf, size_t size, u64 n)
794{
795 char unit[4] = "BKMG";
796 int i = 0;
797
798 while (((n / 1024) > 1) && (i < 3)) {
799 n /= 1024;
800 i++;
801 }
802
803 return scnprintf(buf, size, "%" PRIu64 "%c", n, unit[i]);
804}
diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
index 1d639e38aa82..6e8be174ec0b 100644
--- a/tools/perf/util/util.h
+++ b/tools/perf/util/util.h
@@ -363,4 +363,5 @@ int is_printable_array(char *p, unsigned int len);
363 363
364int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz); 364int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz);
365 365
366int unit_number__scnprintf(char *buf, size_t size, u64 n);
366#endif /* GIT_COMPAT_UTIL_H */ 367#endif /* GIT_COMPAT_UTIL_H */