Skip to content

Commit 9808143

Browse files
olsajiriacmel
authored andcommitted
perf tools: Add unit_number__scnprintf function
Add unit_number__scnprintf function to display size units and use it in -m option info message. Before: $ perf record -m 10M ls rounding mmap pages size to 16777216 bytes (4096 pages) ... After: $ perf record -m 10M ls rounding mmap pages size to 16M (4096 pages) ... Signed-off-by: Jiri Olsa <[email protected]> Cc: David Ahern <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Wang Nan <[email protected]> Link: http://lkml.kernel.org/r/[email protected] [ Rename it to unit_number__scnprintf for consistency ] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent e978be9 commit 9808143

File tree

7 files changed

+63
-2
lines changed

7 files changed

+63
-2
lines changed

tools/perf/tests/Build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ perf-y += is_printable_array.o
4444
perf-y += bitmap.o
4545
perf-y += perf-hooks.o
4646
perf-y += clang.o
47+
perf-y += unit_number__scnprintf.o
4748

4849
$(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build
4950
$(call rule_mkdir)

tools/perf/tests/builtin-test.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ static struct test generic_tests[] = {
246246
.get_desc = test__clang_subtest_get_desc,
247247
}
248248
},
249+
{
250+
.desc = "unit_number__scnprintf",
251+
.func = test__unit_number__scnprint,
252+
},
249253
{
250254
.func = NULL,
251255
},

tools/perf/tests/tests.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ int test__perf_hooks(int subtest);
9696
int test__clang(int subtest);
9797
const char *test__clang_subtest_get_desc(int subtest);
9898
int test__clang_subtest_get_nr(void);
99+
int test__unit_number__scnprint(int subtest);
99100

100101
#if defined(__arm__) || defined(__aarch64__)
101102
#ifdef HAVE_DWARF_UNWIND_SUPPORT
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
7+
int 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+
}

tools/perf/util/evlist.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,12 +1224,16 @@ static long parse_pages_arg(const char *str, unsigned long min,
12241224
if (pages == 0 && min == 0) {
12251225
/* leave number of pages at 0 */
12261226
} else if (!is_power_of_2(pages)) {
1227+
char buf[100];
1228+
12271229
/* round pages up to next power of 2 */
12281230
pages = roundup_pow_of_two(pages);
12291231
if (!pages)
12301232
return -EINVAL;
1231-
pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
1232-
pages * page_size, pages);
1233+
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);
12331237
}
12341238

12351239
if (pages > max)

tools/perf/util/util.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,3 +789,16 @@ int is_printable_array(char *p, unsigned int len)
789789
}
790790
return 1;
791791
}
792+
793+
int 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+
}

tools/perf/util/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,4 +363,5 @@ int is_printable_array(char *p, unsigned int len);
363363

364364
int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz);
365365

366+
int unit_number__scnprintf(char *buf, size_t size, u64 n);
366367
#endif /* GIT_COMPAT_UTIL_H */

0 commit comments

Comments
 (0)