Skip to content

Commit

Permalink
perf tools: Add unit_number__scnprintf function
Browse files Browse the repository at this point in the history
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 <jolsa@kernel.org>
Cc: David Ahern <dsahern@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/r/1483955520-29063-2-git-send-email-jolsa@kernel.org
[ Rename it to unit_number__scnprintf for consistency ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
  • Loading branch information
Jiri Olsa authored and Arnaldo Carvalho de Melo committed Jan 11, 2017
1 parent e978be9 commit 9808143
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions tools/perf/tests/Build
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ perf-y += is_printable_array.o
perf-y += bitmap.o
perf-y += perf-hooks.o
perf-y += clang.o
perf-y += unit_number__scnprintf.o

$(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build
$(call rule_mkdir)
Expand Down
4 changes: 4 additions & 0 deletions tools/perf/tests/builtin-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ static struct test generic_tests[] = {
.get_desc = test__clang_subtest_get_desc,
}
},
{
.desc = "unit_number__scnprintf",
.func = test__unit_number__scnprint,
},
{
.func = NULL,
},
Expand Down
1 change: 1 addition & 0 deletions tools/perf/tests/tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ int test__perf_hooks(int subtest);
int test__clang(int subtest);
const char *test__clang_subtest_get_desc(int subtest);
int test__clang_subtest_get_nr(void);
int test__unit_number__scnprint(int subtest);

#if defined(__arm__) || defined(__aarch64__)
#ifdef HAVE_DWARF_UNWIND_SUPPORT
Expand Down
37 changes: 37 additions & 0 deletions tools/perf/tests/unit_number__scnprintf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <linux/compiler.h>
#include <linux/types.h>
#include "tests.h"
#include "util.h"
#include "debug.h"

int test__unit_number__scnprint(int subtest __maybe_unused)
{
struct {
u64 n;
const char *str;
} test[] = {
{ 1, "1B" },
{ 10*1024, "10K" },
{ 20*1024*1024, "20M" },
{ 30*1024*1024*1024ULL, "30G" },
{ 0, "0B" },
{ 0, NULL },
};
unsigned i = 0;

while (test[i].str) {
char buf[100];

unit_number__scnprintf(buf, sizeof(buf), test[i].n);

pr_debug("n %" PRIu64 ", str '%s', buf '%s'\n",
test[i].n, test[i].str, buf);

if (strcmp(test[i].str, buf))
return TEST_FAIL;

i++;
}

return TEST_OK;
}
8 changes: 6 additions & 2 deletions tools/perf/util/evlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -1224,12 +1224,16 @@ static long parse_pages_arg(const char *str, unsigned long min,
if (pages == 0 && min == 0) {
/* leave number of pages at 0 */
} else if (!is_power_of_2(pages)) {
char buf[100];

/* round pages up to next power of 2 */
pages = roundup_pow_of_two(pages);
if (!pages)
return -EINVAL;
pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
pages * page_size, pages);

unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
pr_info("rounding mmap pages size to %s (%lu pages)\n",
buf, pages);
}

if (pages > max)
Expand Down
13 changes: 13 additions & 0 deletions tools/perf/util/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -789,3 +789,16 @@ int is_printable_array(char *p, unsigned int len)
}
return 1;
}

int unit_number__scnprintf(char *buf, size_t size, u64 n)
{
char unit[4] = "BKMG";
int i = 0;

while (((n / 1024) > 1) && (i < 3)) {
n /= 1024;
i++;
}

return scnprintf(buf, size, "%" PRIu64 "%c", n, unit[i]);
}
1 change: 1 addition & 0 deletions tools/perf/util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,5 @@ int is_printable_array(char *p, unsigned int len);

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

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

0 comments on commit 9808143

Please sign in to comment.