All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCHSET 0/5] perf kmem: Implement page allocation analysis (v3)
@ 2015-03-23  6:30 Namhyung Kim
  2015-03-23  6:30 ` [PATCH 1/5] perf kmem: Print big numbers using thousands' group Namhyung Kim
                   ` (5 more replies)
  0 siblings, 6 replies; 19+ messages in thread
From: Namhyung Kim @ 2015-03-23  6:30 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Ingo Molnar, Peter Zijlstra, Jiri Olsa, LKML, David Ahern,
	Minchan Kim, Joonsoo Kim

Hello,

Currently perf kmem command only analyzes SLAB memory allocation.  And
I'd like to introduce page allocation analysis also.  Users can use
 --slab and/or --page option to select it.  If none of these options
are used, it does slab allocation analysis for backward compatibility.

 * changes in v3)
  - add live page statistics

 * changes in v2)
  - Use thousand grouping for big numbers - i.e. 12345 -> 12,345  (Ingo)
  - Improve output stat readability  (Ingo)
  - Remove alloc size column as it can be calculated from hits and order

Patch 1 is to support thousand grouping on stat output.  Patch 2
implements basic support for page allocation analysis, patch 3 deals
with the callsite and finally patch 4 implements sorting.

In this patchset, I used two kmem events: kmem:mm_page_alloc and
kmem_page_free for analysis as they can track almost all of memory
allocation/free path AFAIK.  However, unlike slab tracepoint events,
those page allocation events don't provide callsite info directly.  So
I recorded callchains and extracted callsites like below:

Normal page allocation callchains look like this:

  360a7e __alloc_pages_nodemask
  3a711c alloc_pages_current
  357bc7 __page_cache_alloc   <-- callsite
  357cf6 pagecache_get_page
   48b0a prepare_pages
   494d3 __btrfs_buffered_write
   49cdf btrfs_file_write_iter
  3ceb6e new_sync_write
  3cf447 vfs_write
  3cff99 sys_write
  7556e9 system_call
    f880 __write_nocancel
   33eb9 cmd_record
   4b38e cmd_kmem
   7aa23 run_builtin
   27a9a main
   20800 __libc_start_main

But first two are internal page allocation functions so it should be
skipped.  To determine such allocation functions, I used following regex:

  ^_?_?(alloc|get_free|get_zeroed)_pages?

This gave me a following list of functions (you can see this with -v):

  alloc func: __get_free_pages
  alloc func: get_zeroed_page
  alloc func: alloc_pages_exact
  alloc func: __alloc_pages_direct_compact
  alloc func: __alloc_pages_nodemask
  alloc func: alloc_page_interleave
  alloc func: alloc_pages_current
  alloc func: alloc_pages_vma
  alloc func: alloc_page_buffers
  alloc func: alloc_pages_exact_nid

After skipping those function, it got '__page_cache_alloc'.

Other information such as allocation order, migration type and gfp
flags are provided by tracepoint events.

Basically the output will be sorted by total allocation bytes, but you
can change it by using -s/--sort option.  The following sort keys are
added to support page analysis: page, order, mtype, gfp.  Existing
'callsite', 'bytes' and 'hit' sort keys also can be used.

An example follows:

  # perf kmem record --slab --page sleep 1
  [ perf record: Woken up 0 times to write data ]
  [ perf record: Captured and wrote 49.277 MB perf.data (191027 samples) ]

  # perf kmem stat --page --caller -l 10 -s order,hit

  --------------------------------------------------------------------------------------------
   Total alloc (KB) | Hits      | Order | Migration type | GFP flags | Callsite
  --------------------------------------------------------------------------------------------
                 64 |         4 |     2 |    RECLAIMABLE |  00285250 | new_slab
             50,144 |    12,536 |     0 |        MOVABLE |  0102005a | __page_cache_alloc
                 52 |        13 |     0 |      UNMOVABLE |  002084d0 | pte_alloc_one
                 40 |        10 |     0 |        MOVABLE |  000280da | handle_mm_fault
                 28 |         7 |     0 |      UNMOVABLE |  000000d0 | __pollwait
                 20 |         5 |     0 |        MOVABLE |  000200da | do_wp_page
                 20 |         5 |     0 |        MOVABLE |  000200da | do_cow_fault
                 16 |         4 |     0 |      UNMOVABLE |  00000200 | __tlb_remove_page
                 16 |         4 |     0 |      UNMOVABLE |  000084d0 | __pmd_alloc
                  8 |         2 |     0 |      UNMOVABLE |  000084d0 | __pud_alloc
   ...              | ...       | ...   | ...            | ...       | ...
  --------------------------------------------------------------------------------------------

  SUMMARY (page allocator)
  ========================
  Total allocation requests     :           12,594   [           50,420 KB ]
  Total free requests           :              182   [              728 KB ]

  Total alloc+freed requests    :              115   [              460 KB ]
  Total alloc-only requests     :           12,479   [           49,960 KB ]
  Total free-only requests      :               67   [              268 KB ]

  Total allocation failures     :                0   [                0 KB ]
   
  Order     Unmovable   Reclaimable       Movable      Reserved  CMA/Isolated
  -----  ------------  ------------  ------------  ------------  ------------
      0            32             .        12,558             .             .
      1             .             .             .             .             .
      2             .             4             .             .             .
      3             .             .             .             .             .
      4             .             .             .             .             .
      5             .             .             .             .             .
      6             .             .             .             .             .
      7             .             .             .             .             .
      8             .             .             .             .             .
      9             .             .             .             .             .
     10             .             .             .             .             .

I have some idea how to improve it.  But I'd also like to hear other
idea, suggestion, feedback and so on.

This is available at perf/kmem-page-v3 branch on my tree:

  git://git.kernel.org/pub/scm/linux/kernel/git/namhyung/linux-perf.git

Thanks,
Namhyung


Namhyung Kim (5):
  perf kmem: Print big numbers using thousands' group
  perf kmem: Analyze page allocator events also
  perf kmem: Implement stat --page --caller
  perf kmem: Support sort keys on page analysis
  perf kmem: Add --live option for current allocation stat

 tools/perf/Documentation/perf-kmem.txt |   19 +-
 tools/perf/builtin-kmem.c              | 1065 ++++++++++++++++++++++++++++++--
 2 files changed, 1022 insertions(+), 62 deletions(-)

-- 
2.3.3


^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH 1/5] perf kmem: Print big numbers using thousands' group
  2015-03-23  6:30 [PATCHSET 0/5] perf kmem: Implement page allocation analysis (v3) Namhyung Kim
@ 2015-03-23  6:30 ` Namhyung Kim
  2015-03-23 14:08   ` Arnaldo Carvalho de Melo
  2015-03-24 16:31   ` [tip:perf/core] " tip-bot for Namhyung Kim
  2015-03-23  6:30 ` [PATCH 2/5] perf kmem: Analyze page allocator events also Namhyung Kim
                   ` (4 subsequent siblings)
  5 siblings, 2 replies; 19+ messages in thread
From: Namhyung Kim @ 2015-03-23  6:30 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Ingo Molnar, Peter Zijlstra, Jiri Olsa, LKML, David Ahern,
	Minchan Kim, Joonsoo Kim

Like perf stat, this makes easy to read the numbers on stat like below:

  # perf kmem stat

  SUMMARY
  =======
  Total bytes requested: 9,770,900
  Total bytes allocated: 9,782,712
  Total bytes wasted on internal fragmentation: 11,812
  Internal fragmentation: 0.120744%
  Cross CPU allocations: 74/152,819

Suggested-by: Ingo Molnar <mingo@kernel.org>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/builtin-kmem.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c
index 8c85aeb3327a..64d3623d45a0 100644
--- a/tools/perf/builtin-kmem.c
+++ b/tools/perf/builtin-kmem.c
@@ -20,6 +20,7 @@
 
 #include <linux/rbtree.h>
 #include <linux/string.h>
+#include <locale.h>
 
 struct alloc_stat;
 typedef int (*sort_fn_t)(struct alloc_stat *, struct alloc_stat *);
@@ -325,13 +326,13 @@ static void __print_result(struct rb_root *root, struct perf_session *session,
 static void print_summary(void)
 {
 	printf("\nSUMMARY\n=======\n");
-	printf("Total bytes requested: %lu\n", total_requested);
-	printf("Total bytes allocated: %lu\n", total_allocated);
-	printf("Total bytes wasted on internal fragmentation: %lu\n",
+	printf("Total bytes requested: %'lu\n", total_requested);
+	printf("Total bytes allocated: %'lu\n", total_allocated);
+	printf("Total bytes wasted on internal fragmentation: %'lu\n",
 	       total_allocated - total_requested);
 	printf("Internal fragmentation: %f%%\n",
 	       fragmentation(total_requested, total_allocated));
-	printf("Cross CPU allocations: %lu/%lu\n", nr_cross_allocs, nr_allocs);
+	printf("Cross CPU allocations: %'lu/%'lu\n", nr_cross_allocs, nr_allocs);
 }
 
 static void print_result(struct perf_session *session)
@@ -706,6 +707,8 @@ int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
 	symbol__init(&session->header.env);
 
 	if (!strcmp(argv[0], "stat")) {
+		setlocale(LC_ALL, "");
+
 		if (cpu__setup_cpunode_map())
 			goto out_delete;
 
-- 
2.3.3


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 2/5] perf kmem: Analyze page allocator events also
  2015-03-23  6:30 [PATCHSET 0/5] perf kmem: Implement page allocation analysis (v3) Namhyung Kim
  2015-03-23  6:30 ` [PATCH 1/5] perf kmem: Print big numbers using thousands' group Namhyung Kim
@ 2015-03-23  6:30 ` Namhyung Kim
  2015-03-23 17:32   ` Joonsoo Kim
  2015-03-23  6:30 ` [PATCH 3/5] perf kmem: Implement stat --page --caller Namhyung Kim
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 19+ messages in thread
From: Namhyung Kim @ 2015-03-23  6:30 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Ingo Molnar, Peter Zijlstra, Jiri Olsa, LKML, David Ahern,
	Minchan Kim, Joonsoo Kim

The perf kmem command records and analyze kernel memory allocation
only for SLAB objects.  This patch implement a simple page allocator
analyzer using kmem:mm_page_alloc and kmem:mm_page_free events.

It adds two new options of --slab and --page.  The --slab option is
for analyzing SLAB allocator and that's what perf kmem currently does.

The new --page option enables page allocator events and analyze kernel
memory usage in page unit.  Currently, 'stat --alloc' subcommand is
implemented only.

If none of these --slab nor --page is specified, --slab is implied.

  # perf kmem stat --page --alloc --line 10

  -------------------------------------------------------------------------------------
   Page             | Total alloc (KB) | Hits     | Order | Migration type | GFP flags
  -------------------------------------------------------------------------------------
   ffffea0015e48e00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
   ffffea0015e47400 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
   ffffea001440f600 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
   ffffea001440cc00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
   ffffea00140c6300 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
   ffffea00140c5c00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
   ffffea00140c5000 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
   ffffea00140c4f00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
   ffffea00140c4e00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
   ffffea00140c4d00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
   ...              | ...              | ...      | ...   | ...            | ...
  -------------------------------------------------------------------------------------

  SUMMARY (page allocator)
  ========================
  Total allocation requests     :           44,260   [          177,256 KB ]
  Total free requests           :              117   [              468 KB ]

  Total alloc+freed requests    :               49   [              196 KB ]
  Total alloc-only requests     :           44,211   [          177,060 KB ]
  Total free-only requests      :               68   [              272 KB ]

  Total allocation failures     :                0   [                0 KB ]

  Order     Unmovable   Reclaimable       Movable      Reserved  CMA/Isolated
  -----  ------------  ------------  ------------  ------------  ------------
      0            32             .        44,210             .             .
      1             .             .             .             .             .
      2             .            18             .             .             .
      3             .             .             .             .             .
      4             .             .             .             .             .
      5             .             .             .             .             .
      6             .             .             .             .             .
      7             .             .             .             .             .
      8             .             .             .             .             .
      9             .             .             .             .             .
     10             .             .             .             .             .

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/Documentation/perf-kmem.txt |   8 +-
 tools/perf/builtin-kmem.c              | 376 +++++++++++++++++++++++++++++++--
 2 files changed, 368 insertions(+), 16 deletions(-)

diff --git a/tools/perf/Documentation/perf-kmem.txt b/tools/perf/Documentation/perf-kmem.txt
index 150253cc3c97..23219c65c16f 100644
--- a/tools/perf/Documentation/perf-kmem.txt
+++ b/tools/perf/Documentation/perf-kmem.txt
@@ -3,7 +3,7 @@ perf-kmem(1)
 
 NAME
 ----
-perf-kmem - Tool to trace/measure kernel memory(slab) properties
+perf-kmem - Tool to trace/measure kernel memory properties
 
 SYNOPSIS
 --------
@@ -46,6 +46,12 @@ OPTIONS
 --raw-ip::
 	Print raw ip instead of symbol
 
+--slab::
+	Analyze SLAB allocator events.
+
+--page::
+	Analyze page allocator events
+
 SEE ALSO
 --------
 linkperf:perf-record[1]
diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c
index 64d3623d45a0..76a527dc6ba1 100644
--- a/tools/perf/builtin-kmem.c
+++ b/tools/perf/builtin-kmem.c
@@ -22,6 +22,11 @@
 #include <linux/string.h>
 #include <locale.h>
 
+static int	kmem_slab;
+static int	kmem_page;
+
+static long	kmem_page_size;
+
 struct alloc_stat;
 typedef int (*sort_fn_t)(struct alloc_stat *, struct alloc_stat *);
 
@@ -226,6 +231,139 @@ static int perf_evsel__process_free_event(struct perf_evsel *evsel,
 	return 0;
 }
 
+static u64 total_page_alloc_bytes;
+static u64 total_page_free_bytes;
+static u64 total_page_nomatch_bytes;
+static u64 total_page_fail_bytes;
+static unsigned long nr_page_allocs;
+static unsigned long nr_page_frees;
+static unsigned long nr_page_fails;
+static unsigned long nr_page_nomatch;
+
+#define MAX_MIGRATE_TYPES  6
+#define MAX_PAGE_ORDER     11
+
+static int order_stats[MAX_PAGE_ORDER][MAX_MIGRATE_TYPES];
+
+struct page_stat {
+	struct rb_node 	node;
+	u64 		page;
+	int 		order;
+	unsigned 	gfp_flags;
+	unsigned 	migrate_type;
+	u64		alloc_bytes;
+	u64 		free_bytes;
+	int 		nr_alloc;
+	int 		nr_free;
+};
+
+static struct rb_root page_tree;
+static struct rb_root page_alloc_sorted;
+
+static struct page_stat *search_page_stat(unsigned long page, bool create)
+{
+	struct rb_node **node = &page_tree.rb_node;
+	struct rb_node *parent = NULL;
+	struct page_stat *data;
+
+	while (*node) {
+		s64 cmp;
+
+		parent = *node;
+		data = rb_entry(*node, struct page_stat, node);
+
+		cmp = data->page - page;
+		if (cmp < 0)
+			node = &parent->rb_left;
+		else if (cmp > 0)
+			node = &parent->rb_right;
+		else
+			return data;
+	}
+
+	if (!create)
+		return NULL;
+
+	data = zalloc(sizeof(*data));
+	if (data != NULL) {
+		data->page = page;
+
+		rb_link_node(&data->node, parent, node);
+		rb_insert_color(&data->node, &page_tree);
+	}
+
+	return data;
+}
+
+static int perf_evsel__process_page_alloc_event(struct perf_evsel *evsel,
+						struct perf_sample *sample)
+{
+	u64 page = perf_evsel__intval(evsel, sample, "page");
+	unsigned int order = perf_evsel__intval(evsel, sample, "order");
+	unsigned int gfp_flags = perf_evsel__intval(evsel, sample, "gfp_flags");
+	unsigned int migrate_type = perf_evsel__intval(evsel, sample,
+						       "migratetype");
+	u64 bytes = kmem_page_size << order;
+	struct page_stat *stat;
+
+	if (page == 0) {
+		nr_page_fails++;
+		total_page_fail_bytes += bytes;
+
+		return 0;
+	}
+
+	/*
+	 * XXX: We'd better to use PFN instead of page pointer to deal
+	 * with things like partial freeing.  But AFAIK there's no way
+	 * to convert a pointer to struct page into PFN in userspace.
+	 */
+	stat = search_page_stat(page, true);
+	if (stat == NULL)
+		return -1;
+
+	stat->order = order;
+	stat->gfp_flags = gfp_flags;
+	stat->migrate_type = migrate_type;
+
+	stat->nr_alloc++;
+	nr_page_allocs++;
+	stat->alloc_bytes += bytes;
+	total_page_alloc_bytes += bytes;
+
+	order_stats[order][migrate_type]++;
+
+	return 0;
+}
+
+static int perf_evsel__process_page_free_event(struct perf_evsel *evsel,
+						struct perf_sample *sample)
+{
+	u64 page = perf_evsel__intval(evsel, sample, "page");
+	unsigned int order = perf_evsel__intval(evsel, sample, "order");
+	u64 bytes = kmem_page_size << order;
+	struct page_stat *stat;
+
+	nr_page_frees++;
+	total_page_free_bytes += bytes;
+
+	stat = search_page_stat(page, false);
+	if (stat == NULL) {
+		pr_debug2("missing free at page %"PRIx64" (order: %d)\n",
+			  page, order);
+
+		nr_page_nomatch++;
+		total_page_nomatch_bytes += bytes;
+
+		return 0;
+	}
+
+	stat->nr_free++;
+	stat->free_bytes += bytes;
+
+	return 0;
+}
+
 typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
 				  struct perf_sample *sample);
 
@@ -270,8 +408,9 @@ static double fragmentation(unsigned long n_req, unsigned long n_alloc)
 		return 100.0 - (100.0 * n_req / n_alloc);
 }
 
-static void __print_result(struct rb_root *root, struct perf_session *session,
-			   int n_lines, int is_caller)
+static void __print_slab_result(struct rb_root *root,
+				struct perf_session *session,
+				int n_lines, int is_caller)
 {
 	struct rb_node *next;
 	struct machine *machine = &session->machines.host;
@@ -323,9 +462,50 @@ static void __print_result(struct rb_root *root, struct perf_session *session,
 	printf("%.105s\n", graph_dotted_line);
 }
 
-static void print_summary(void)
+static const char * const migrate_type_str[] = {
+	"UNMOVABLE",
+	"RECLAIMABLE",
+	"MOVABLE",
+	"RESERVED",
+	"CMA/ISOLATE",
+	"UNKNOWN",
+};
+
+static void __print_page_result(struct rb_root *root,
+				struct perf_session *session __maybe_unused,
+				int n_lines)
+{
+	struct rb_node *next = rb_first(root);
+
+	printf("\n%.86s\n", graph_dotted_line);
+	printf(" Page             | Total alloc (KB) | Hits      | Order | Migration type | GFP flags\n");
+	printf("%.86s\n", graph_dotted_line);
+
+	while (next && n_lines--) {
+		struct page_stat *data;
+
+		data = rb_entry(next, struct page_stat, node);
+
+		printf(" %016llx | %'16llu | %'9d | %5d | %14s |  %08lx\n",
+		       (unsigned long long)data->page,
+		       (unsigned long long)data->alloc_bytes / 1024,
+		       data->nr_alloc, data->order,
+		       migrate_type_str[data->migrate_type],
+		       (unsigned long)data->gfp_flags);
+
+		next = rb_next(next);
+	}
+
+	if (n_lines == -1)
+		printf(" ...              | ...              | ...       | ...   | ...            | ...     \n");
+
+	printf("%.86s\n", graph_dotted_line);
+}
+
+static void print_slab_summary(void)
 {
-	printf("\nSUMMARY\n=======\n");
+	printf("\nSUMMARY (SLAB allocator)");
+	printf("\n========================\n");
 	printf("Total bytes requested: %'lu\n", total_requested);
 	printf("Total bytes allocated: %'lu\n", total_allocated);
 	printf("Total bytes wasted on internal fragmentation: %'lu\n",
@@ -335,13 +515,73 @@ static void print_summary(void)
 	printf("Cross CPU allocations: %'lu/%'lu\n", nr_cross_allocs, nr_allocs);
 }
 
-static void print_result(struct perf_session *session)
+static void print_page_summary(void)
+{
+	int o, m;
+	u64 nr_alloc_freed = nr_page_frees - nr_page_nomatch;
+	u64 total_alloc_freed_bytes = total_page_free_bytes - total_page_nomatch_bytes;
+
+	printf("\nSUMMARY (page allocator)");
+	printf("\n========================\n");
+	printf("%-30s: %'16lu   [ %'16"PRIu64" KB ]\n", "Total allocation requests",
+	       nr_page_allocs, total_page_alloc_bytes / 1024);
+	printf("%-30s: %'16lu   [ %'16"PRIu64" KB ]\n", "Total free requests",
+	       nr_page_frees, total_page_free_bytes / 1024);
+	printf("\n");
+
+	printf("%-30s: %'16lu   [ %'16"PRIu64" KB ]\n", "Total alloc+freed requests",
+	       nr_alloc_freed, (total_alloc_freed_bytes) / 1024);
+	printf("%-30s: %'16lu   [ %'16"PRIu64" KB ]\n", "Total alloc-only requests",
+	       nr_page_allocs - nr_alloc_freed,
+	       (total_page_alloc_bytes - total_alloc_freed_bytes) / 1024);
+	printf("%-30s: %'16lu   [ %'16"PRIu64" KB ]\n", "Total free-only requests",
+	       nr_page_nomatch, total_page_nomatch_bytes / 1024);
+	printf("\n");
+
+	printf("%-30s: %'16lu   [ %'16"PRIu64" KB ]\n", "Total allocation failures",
+	       nr_page_fails, total_page_fail_bytes / 1024);
+	printf("\n");
+
+	printf("%5s  %12s  %12s  %12s  %12s  %12s\n", "Order",  "Unmovable",
+	       "Reclaimable", "Movable", "Reserved", "CMA/Isolated");
+	printf("%.5s  %.12s  %.12s  %.12s  %.12s  %.12s\n", graph_dotted_line,
+	       graph_dotted_line, graph_dotted_line, graph_dotted_line,
+	       graph_dotted_line, graph_dotted_line);
+
+	for (o = 0; o < MAX_PAGE_ORDER; o++) {
+		printf("%5d", o);
+		for (m = 0; m < MAX_MIGRATE_TYPES - 1; m++) {
+			if (order_stats[o][m])
+				printf("  %'12d", order_stats[o][m]);
+			else
+				printf("  %12c", '.');
+		}
+		printf("\n");
+	}
+}
+
+static void print_slab_result(struct perf_session *session)
 {
 	if (caller_flag)
-		__print_result(&root_caller_sorted, session, caller_lines, 1);
+		__print_slab_result(&root_caller_sorted, session, caller_lines, 1);
 	if (alloc_flag)
-		__print_result(&root_alloc_sorted, session, alloc_lines, 0);
-	print_summary();
+		__print_slab_result(&root_alloc_sorted, session, alloc_lines, 0);
+	print_slab_summary();
+}
+
+static void print_page_result(struct perf_session *session)
+{
+	if (alloc_flag)
+		__print_page_result(&page_alloc_sorted, session, alloc_lines);
+	print_page_summary();
+}
+
+static void print_result(struct perf_session *session)
+{
+	if (kmem_slab)
+		print_slab_result(session);
+	if (kmem_page)
+		print_page_result(session);
 }
 
 struct sort_dimension {
@@ -353,8 +593,8 @@ struct sort_dimension {
 static LIST_HEAD(caller_sort);
 static LIST_HEAD(alloc_sort);
 
-static void sort_insert(struct rb_root *root, struct alloc_stat *data,
-			struct list_head *sort_list)
+static void sort_slab_insert(struct rb_root *root, struct alloc_stat *data,
+			     struct list_head *sort_list)
 {
 	struct rb_node **new = &(root->rb_node);
 	struct rb_node *parent = NULL;
@@ -383,8 +623,8 @@ static void sort_insert(struct rb_root *root, struct alloc_stat *data,
 	rb_insert_color(&data->node, root);
 }
 
-static void __sort_result(struct rb_root *root, struct rb_root *root_sorted,
-			  struct list_head *sort_list)
+static void __sort_slab_result(struct rb_root *root, struct rb_root *root_sorted,
+			       struct list_head *sort_list)
 {
 	struct rb_node *node;
 	struct alloc_stat *data;
@@ -396,26 +636,78 @@ static void __sort_result(struct rb_root *root, struct rb_root *root_sorted,
 
 		rb_erase(node, root);
 		data = rb_entry(node, struct alloc_stat, node);
-		sort_insert(root_sorted, data, sort_list);
+		sort_slab_insert(root_sorted, data, sort_list);
+	}
+}
+
+static void sort_page_insert(struct rb_root *root, struct page_stat *data)
+{
+	struct rb_node **new = &root->rb_node;
+	struct rb_node *parent = NULL;
+
+	while (*new) {
+		struct page_stat *this;
+		int cmp = 0;
+
+		this = rb_entry(*new, struct page_stat, node);
+		parent = *new;
+
+		/* TODO: support more sort key */
+		cmp = data->alloc_bytes - this->alloc_bytes;
+
+		if (cmp > 0)
+			new = &parent->rb_left;
+		else
+			new = &parent->rb_right;
+	}
+
+	rb_link_node(&data->node, parent, new);
+	rb_insert_color(&data->node, root);
+}
+
+static void __sort_page_result(struct rb_root *root, struct rb_root *root_sorted)
+{
+	struct rb_node *node;
+	struct page_stat *data;
+
+	for (;;) {
+		node = rb_first(root);
+		if (!node)
+			break;
+
+		rb_erase(node, root);
+		data = rb_entry(node, struct page_stat, node);
+		sort_page_insert(root_sorted, data);
 	}
 }
 
 static void sort_result(void)
 {
-	__sort_result(&root_alloc_stat, &root_alloc_sorted, &alloc_sort);
-	__sort_result(&root_caller_stat, &root_caller_sorted, &caller_sort);
+	if (kmem_slab) {
+		__sort_slab_result(&root_alloc_stat, &root_alloc_sorted,
+				   &alloc_sort);
+		__sort_slab_result(&root_caller_stat, &root_caller_sorted,
+				   &caller_sort);
+	}
+	if (kmem_page) {
+		__sort_page_result(&page_tree, &page_alloc_sorted);
+	}
 }
 
 static int __cmd_kmem(struct perf_session *session)
 {
 	int err = -EINVAL;
 	const struct perf_evsel_str_handler kmem_tracepoints[] = {
+		/* slab allocator */
 		{ "kmem:kmalloc",		perf_evsel__process_alloc_event, },
     		{ "kmem:kmem_cache_alloc",	perf_evsel__process_alloc_event, },
 		{ "kmem:kmalloc_node",		perf_evsel__process_alloc_node_event, },
     		{ "kmem:kmem_cache_alloc_node", perf_evsel__process_alloc_node_event, },
 		{ "kmem:kfree",			perf_evsel__process_free_event, },
     		{ "kmem:kmem_cache_free",	perf_evsel__process_free_event, },
+		/* page allocator */
+		{ "kmem:mm_page_alloc",		perf_evsel__process_page_alloc_event, },
+		{ "kmem:mm_page_free",		perf_evsel__process_page_free_event, },
 	};
 
 	if (!perf_session__has_traces(session, "kmem record"))
@@ -612,6 +904,22 @@ static int parse_alloc_opt(const struct option *opt __maybe_unused,
 	return 0;
 }
 
+static int parse_slab_opt(const struct option *opt __maybe_unused,
+			  const char *arg __maybe_unused,
+			  int unset __maybe_unused)
+{
+	kmem_slab = (kmem_page + 1);
+	return 0;
+}
+
+static int parse_page_opt(const struct option *opt __maybe_unused,
+			  const char *arg __maybe_unused,
+			  int unset __maybe_unused)
+{
+	kmem_page = (kmem_slab + 1);
+	return 0;
+}
+
 static int parse_line_opt(const struct option *opt __maybe_unused,
 			  const char *arg, int unset __maybe_unused)
 {
@@ -634,6 +942,8 @@ static int __cmd_record(int argc, const char **argv)
 {
 	const char * const record_args[] = {
 	"record", "-a", "-R", "-c", "1",
+	};
+	const char * const slab_events[] = {
 	"-e", "kmem:kmalloc",
 	"-e", "kmem:kmalloc_node",
 	"-e", "kmem:kfree",
@@ -641,10 +951,19 @@ static int __cmd_record(int argc, const char **argv)
 	"-e", "kmem:kmem_cache_alloc_node",
 	"-e", "kmem:kmem_cache_free",
 	};
+	const char * const page_events[] = {
+	"-e", "kmem:mm_page_alloc",
+	"-e", "kmem:mm_page_free",
+	};
 	unsigned int rec_argc, i, j;
 	const char **rec_argv;
 
 	rec_argc = ARRAY_SIZE(record_args) + argc - 1;
+	if (kmem_slab)
+		rec_argc += ARRAY_SIZE(slab_events);
+	if (kmem_page)
+		rec_argc += ARRAY_SIZE(page_events);
+
 	rec_argv = calloc(rec_argc + 1, sizeof(char *));
 
 	if (rec_argv == NULL)
@@ -653,6 +972,15 @@ static int __cmd_record(int argc, const char **argv)
 	for (i = 0; i < ARRAY_SIZE(record_args); i++)
 		rec_argv[i] = strdup(record_args[i]);
 
+	if (kmem_slab) {
+		for (j = 0; j < ARRAY_SIZE(slab_events); j++, i++)
+			rec_argv[i] = strdup(slab_events[j]);
+	}
+	if (kmem_page) {
+		for (j = 0; j < ARRAY_SIZE(page_events); j++, i++)
+			rec_argv[i] = strdup(page_events[j]);
+	}
+
 	for (j = 1; j < (unsigned int)argc; j++, i++)
 		rec_argv[i] = argv[j];
 
@@ -675,6 +1003,10 @@ int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
 		     parse_sort_opt),
 	OPT_CALLBACK('l', "line", NULL, "num", "show n lines", parse_line_opt),
 	OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"),
+	OPT_CALLBACK_NOOPT(0, "slab", NULL, NULL, "Analyze slab allocator",
+			   parse_slab_opt),
+	OPT_CALLBACK_NOOPT(0, "page", NULL, NULL, "Analyze page allocator",
+			   parse_page_opt),
 	OPT_END()
 	};
 	const char *const kmem_subcommands[] = { "record", "stat", NULL };
@@ -695,6 +1027,9 @@ int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
 	if (!argc)
 		usage_with_options(kmem_usage, kmem_options);
 
+	if (kmem_slab == 0 && kmem_page == 0)
+		kmem_slab = 1;  /* for backward compatibility */
+
 	if (!strncmp(argv[0], "rec", 3)) {
 		symbol__init(NULL);
 		return __cmd_record(argc, argv);
@@ -704,6 +1039,17 @@ int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
 	if (session == NULL)
 		return -1;
 
+	if (kmem_page) {
+		struct perf_evsel *evsel = perf_evlist__first(session->evlist);
+
+		if (evsel == NULL || evsel->tp_format == NULL) {
+			pr_err("invalid event found.. aborting\n");
+			return -1;
+		}
+
+		kmem_page_size = pevent_get_page_size(evsel->tp_format->pevent);
+	}
+
 	symbol__init(&session->header.env);
 
 	if (!strcmp(argv[0], "stat")) {
-- 
2.3.3


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 3/5] perf kmem: Implement stat --page --caller
  2015-03-23  6:30 [PATCHSET 0/5] perf kmem: Implement page allocation analysis (v3) Namhyung Kim
  2015-03-23  6:30 ` [PATCH 1/5] perf kmem: Print big numbers using thousands' group Namhyung Kim
  2015-03-23  6:30 ` [PATCH 2/5] perf kmem: Analyze page allocator events also Namhyung Kim
@ 2015-03-23  6:30 ` Namhyung Kim
  2015-03-23  6:30 ` [PATCH 4/5] perf kmem: Support sort keys on page analysis Namhyung Kim
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 19+ messages in thread
From: Namhyung Kim @ 2015-03-23  6:30 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Ingo Molnar, Peter Zijlstra, Jiri Olsa, LKML, David Ahern,
	Minchan Kim, Joonsoo Kim

It perf kmem support caller statistics for page.  Unlike slab case,
the tracepoints in page allocator don't provide callsite info.  So
it records with callchain and extracts callsite info.

Note that the callchain contains several memory allocation functions
which has no meaning for users.  So skip those functions to get proper
callsites.  I used following regex pattern to skip the allocator
functions:

  ^_?_?(alloc|get_free|get_zeroed)_pages?

This gave me a following list of functions:

  # perf kmem record --page sleep 3
  # perf kmem stat --page -v
  ...
  alloc func: __get_free_pages
  alloc func: get_zeroed_page
  alloc func: alloc_pages_exact
  alloc func: __alloc_pages_direct_compact
  alloc func: __alloc_pages_nodemask
  alloc func: alloc_page_interleave
  alloc func: alloc_pages_current
  alloc func: alloc_pages_vma
  alloc func: alloc_page_buffers
  alloc func: alloc_pages_exact_nid
  ...

The output looks mostly same as --alloc (I also added callsite column
to that) but groups entries by callsite.  Currently, the order,
migrate type and GFP flag info is for the last allocation and not
guaranteed to be same for all allocations from the callsite.

  ---------------------------------------------------------------------------------------------
   Total_alloc (KB) | Hits      | Order | Migration type | GFP flags | Callsite
  ---------------------------------------------------------------------------------------------
              1,064 |       266 |     0 |      UNMOVABLE |  000000d0 | __pollwait
                 52 |        13 |     0 |      UNMOVABLE |  002084d0 | pte_alloc_one
                 44 |        11 |     0 |        MOVABLE |  000280da | handle_mm_fault
                 20 |         5 |     0 |        MOVABLE |  000200da | do_cow_fault
                 20 |         5 |     0 |        MOVABLE |  000200da | do_wp_page
                 16 |         4 |     0 |      UNMOVABLE |  000084d0 | __pmd_alloc
                 16 |         4 |     0 |      UNMOVABLE |  00000200 | __tlb_remove_page
                 12 |         3 |     0 |      UNMOVABLE |  000084d0 | __pud_alloc
                  8 |         2 |     0 |      UNMOVABLE |  00000010 | bio_copy_user_iov
                  4 |         1 |     0 |      UNMOVABLE |  000200d2 | pipe_write
                  4 |         1 |     0 |        MOVABLE |  000280da | do_wp_page
                  4 |         1 |     0 |      UNMOVABLE |  002084d0 | pgd_alloc
  ---------------------------------------------------------------------------------------------

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/builtin-kmem.c | 298 ++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 274 insertions(+), 24 deletions(-)

diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c
index 76a527dc6ba1..e4914b3a0213 100644
--- a/tools/perf/builtin-kmem.c
+++ b/tools/perf/builtin-kmem.c
@@ -10,6 +10,7 @@
 #include "util/header.h"
 #include "util/session.h"
 #include "util/tool.h"
+#include "util/callchain.h"
 
 #include "util/parse-options.h"
 #include "util/trace-event.h"
@@ -21,6 +22,7 @@
 #include <linux/rbtree.h>
 #include <linux/string.h>
 #include <locale.h>
+#include <regex.h>
 
 static int	kmem_slab;
 static int	kmem_page;
@@ -240,6 +242,8 @@ static unsigned long nr_page_frees;
 static unsigned long nr_page_fails;
 static unsigned long nr_page_nomatch;
 
+static struct perf_session *kmem_session;
+
 #define MAX_MIGRATE_TYPES  6
 #define MAX_PAGE_ORDER     11
 
@@ -248,6 +252,7 @@ static int order_stats[MAX_PAGE_ORDER][MAX_MIGRATE_TYPES];
 struct page_stat {
 	struct rb_node 	node;
 	u64 		page;
+	u64 		callsite;
 	int 		order;
 	unsigned 	gfp_flags;
 	unsigned 	migrate_type;
@@ -257,12 +262,142 @@ struct page_stat {
 	int 		nr_free;
 };
 
-static struct rb_root page_tree;
+static struct rb_root page_alloc_tree;
 static struct rb_root page_alloc_sorted;
+static struct rb_root page_caller_tree;
+static struct rb_root page_caller_sorted;
+
+struct alloc_func {
+	u64 start;
+	u64 end;
+	char *name;
+};
+
+static int nr_alloc_funcs;
+static struct alloc_func *alloc_func_list;
+
+static int funcmp(const void *a, const void *b)
+{
+	const struct alloc_func *fa = a;
+	const struct alloc_func *fb = b;
+
+	if (fa->start > fb->start)
+		return 1;
+	else
+		return -1;
+}
+
+static int callcmp(const void *a, const void *b)
+{
+	const struct alloc_func *fa = a;
+	const struct alloc_func *fb = b;
+
+	if (fb->start <= fa->start && fa->end < fb->end)
+		return 0;
+
+	if (fa->start > fb->start)
+		return 1;
+	else
+		return -1;
+}
 
-static struct page_stat *search_page_stat(unsigned long page, bool create)
+static int build_alloc_func_list(void)
 {
-	struct rb_node **node = &page_tree.rb_node;
+	int ret;
+	struct map *kernel_map;
+	struct symbol *sym;
+	struct rb_node *node;
+	struct alloc_func *func;
+	struct machine *machine = &kmem_session->machines.host;
+
+	regex_t alloc_func_regex;
+	const char pattern[] = "^_?_?(alloc|get_free|get_zeroed)_pages?";
+
+	ret = regcomp(&alloc_func_regex, pattern, REG_EXTENDED);
+	if (ret) {
+		char err[BUFSIZ];
+
+		regerror(ret, &alloc_func_regex, err, sizeof(err));
+		pr_err("Invalid regex: %s\n%s", pattern, err);
+		return -EINVAL;
+	}
+
+	kernel_map = machine->vmlinux_maps[MAP__FUNCTION];
+	map__load(kernel_map, NULL);
+
+	map__for_each_symbol(kernel_map, sym, node) {
+		if (regexec(&alloc_func_regex, sym->name, 0, NULL, 0))
+			continue;
+
+		func = realloc(alloc_func_list,
+			       (nr_alloc_funcs + 1) * sizeof(*func));
+		if (func == NULL)
+			return -ENOMEM;
+
+		pr_debug("alloc func: %s\n", sym->name);
+		func[nr_alloc_funcs].start = sym->start;
+		func[nr_alloc_funcs].end   = sym->end;
+		func[nr_alloc_funcs].name  = sym->name;
+
+		alloc_func_list = func;
+		nr_alloc_funcs++;
+	}
+
+	qsort(alloc_func_list, nr_alloc_funcs, sizeof(*func), funcmp);
+
+	regfree(&alloc_func_regex);
+	return 0;
+}
+
+/*
+ * Find first non-memory allocation function from callchain.
+ * The allocation functions are in the 'alloc_func_list'.
+ */
+static u64 find_callsite(struct perf_evsel *evsel, struct perf_sample *sample)
+{
+	struct addr_location al;
+	struct machine *machine = &kmem_session->machines.host;
+	struct callchain_cursor_node *node;
+
+	if (alloc_func_list == NULL)
+		build_alloc_func_list();
+
+	al.thread = machine__findnew_thread(machine, sample->pid, sample->tid);
+	sample__resolve_callchain(sample, NULL, evsel, &al, 16);
+
+	callchain_cursor_commit(&callchain_cursor);
+	while (true) {
+		struct alloc_func key, *caller;
+		u64 addr;
+
+		node = callchain_cursor_current(&callchain_cursor);
+		if (node == NULL)
+			break;
+
+		key.start = key.end = node->ip;
+		caller = bsearch(&key, alloc_func_list, nr_alloc_funcs,
+				 sizeof(key), callcmp);
+		if (!caller) {
+			/* found */
+			if (node->map)
+				addr = map__unmap_ip(node->map, node->ip);
+			else
+				addr = node->ip;
+
+			return addr;
+		} else
+			pr_debug3("skipping alloc function: %s\n", caller->name);
+
+		callchain_cursor_advance(&callchain_cursor);
+	}
+
+	pr_debug2("unknown callsite: %"PRIx64, sample->ip);
+	return sample->ip;
+}
+
+static struct page_stat *search_page_alloc_stat(u64 page, bool create)
+{
+	struct rb_node **node = &page_alloc_tree.rb_node;
 	struct rb_node *parent = NULL;
 	struct page_stat *data;
 
@@ -289,7 +424,42 @@ static struct page_stat *search_page_stat(unsigned long page, bool create)
 		data->page = page;
 
 		rb_link_node(&data->node, parent, node);
-		rb_insert_color(&data->node, &page_tree);
+		rb_insert_color(&data->node, &page_alloc_tree);
+	}
+
+	return data;
+}
+
+static struct page_stat *search_page_caller_stat(u64 callsite, bool create)
+{
+	struct rb_node **node = &page_caller_tree.rb_node;
+	struct rb_node *parent = NULL;
+	struct page_stat *data;
+
+	while (*node) {
+		s64 cmp;
+
+		parent = *node;
+		data = rb_entry(*node, struct page_stat, node);
+
+		cmp = data->callsite - callsite;
+		if (cmp < 0)
+			node = &parent->rb_left;
+		else if (cmp > 0)
+			node = &parent->rb_right;
+		else
+			return data;
+	}
+
+	if (!create)
+		return NULL;
+
+	data = zalloc(sizeof(*data));
+	if (data != NULL) {
+		data->callsite = callsite;
+
+		rb_link_node(&data->node, parent, node);
+		rb_insert_color(&data->node, &page_caller_tree);
 	}
 
 	return data;
@@ -304,6 +474,7 @@ static int perf_evsel__process_page_alloc_event(struct perf_evsel *evsel,
 	unsigned int migrate_type = perf_evsel__intval(evsel, sample,
 						       "migratetype");
 	u64 bytes = kmem_page_size << order;
+	u64 callsite;
 	struct page_stat *stat;
 
 	if (page == 0) {
@@ -313,22 +484,39 @@ static int perf_evsel__process_page_alloc_event(struct perf_evsel *evsel,
 		return 0;
 	}
 
+	callsite = find_callsite(evsel, sample);
+
 	/*
 	 * XXX: We'd better to use PFN instead of page pointer to deal
 	 * with things like partial freeing.  But AFAIK there's no way
 	 * to convert a pointer to struct page into PFN in userspace.
 	 */
-	stat = search_page_stat(page, true);
-	if (stat == NULL)
+	stat = search_page_alloc_stat(page, true);
+	if (stat == NULL) {
+		pr_err("cannot create page alloc stat\n");
 		return -1;
+	}
 
 	stat->order = order;
 	stat->gfp_flags = gfp_flags;
 	stat->migrate_type = migrate_type;
+	stat->callsite = callsite;
+	stat->nr_alloc++;
+	stat->alloc_bytes += bytes;
+
+	stat = search_page_caller_stat(callsite, true);
+	if (stat == NULL) {
+		pr_err("cannot create page caller stat\n");
+		return -1;
+	}
 
+	stat->order = order;
+	stat->gfp_flags = gfp_flags;
+	stat->migrate_type = migrate_type;
 	stat->nr_alloc++;
-	nr_page_allocs++;
 	stat->alloc_bytes += bytes;
+
+	nr_page_allocs++;
 	total_page_alloc_bytes += bytes;
 
 	order_stats[order][migrate_type]++;
@@ -347,7 +535,7 @@ static int perf_evsel__process_page_free_event(struct perf_evsel *evsel,
 	nr_page_frees++;
 	total_page_free_bytes += bytes;
 
-	stat = search_page_stat(page, false);
+	stat = search_page_alloc_stat(page, false);
 	if (stat == NULL) {
 		pr_debug2("missing free at page %"PRIx64" (order: %d)\n",
 			  page, order);
@@ -361,6 +549,12 @@ static int perf_evsel__process_page_free_event(struct perf_evsel *evsel,
 	stat->nr_free++;
 	stat->free_bytes += bytes;
 
+	stat = search_page_caller_stat(stat->callsite, false);
+	if (stat != NULL) {
+		stat->nr_free++;
+		stat->free_bytes += bytes;
+	}
+
 	return 0;
 }
 
@@ -471,35 +665,83 @@ static const char * const migrate_type_str[] = {
 	"UNKNOWN",
 };
 
-static void __print_page_result(struct rb_root *root,
-				struct perf_session *session __maybe_unused,
-				int n_lines)
+static void __print_page_alloc_result(struct perf_session *session, int n_lines)
 {
-	struct rb_node *next = rb_first(root);
+	struct rb_node *next = rb_first(&page_alloc_sorted);
+	struct machine *machine = &session->machines.host;
 
-	printf("\n%.86s\n", graph_dotted_line);
-	printf(" Page             | Total alloc (KB) | Hits      | Order | Migration type | GFP flags\n");
-	printf("%.86s\n", graph_dotted_line);
+	printf("\n%.105s\n", graph_dotted_line);
+	printf(" Page             | Total alloc (KB) | Hits      | Order | Migration type | GFP flags | Callsite\n");
+	printf("%.105s\n", graph_dotted_line);
 
 	while (next && n_lines--) {
 		struct page_stat *data;
+		struct symbol *sym;
+		struct map *map;
+		char buf[32];
+		char *caller = buf;
 
 		data = rb_entry(next, struct page_stat, node);
+		sym = machine__find_kernel_function(machine, data->callsite,
+						    &map, NULL);
+		if (sym && sym->name)
+			caller = sym->name;
+		else
+			scnprintf(buf, sizeof(buf), "%"PRIx64, data->callsite);
 
-		printf(" %016llx | %'16llu | %'9d | %5d | %14s |  %08lx\n",
+		printf(" %016llx | %'16llu | %'9d | %5d | %14s |  %08lx | %s\n",
 		       (unsigned long long)data->page,
 		       (unsigned long long)data->alloc_bytes / 1024,
 		       data->nr_alloc, data->order,
 		       migrate_type_str[data->migrate_type],
-		       (unsigned long)data->gfp_flags);
+		       (unsigned long)data->gfp_flags, caller);
 
 		next = rb_next(next);
 	}
 
 	if (n_lines == -1)
-		printf(" ...              | ...              | ...       | ...   | ...            | ...     \n");
+		printf(" ...              | ...              | ...       | ...   | ...            | ...       | ...\n");
 
-	printf("%.86s\n", graph_dotted_line);
+	printf("%.105s\n", graph_dotted_line);
+}
+
+static void __print_page_caller_result(struct perf_session *session, int n_lines)
+{
+	struct rb_node *next = rb_first(&page_caller_sorted);
+	struct machine *machine = &session->machines.host;
+
+	printf("\n%.105s\n", graph_dotted_line);
+	printf(" Total alloc (KB) | Hits      | Order | Migration type | GFP flags | Callsite\n");
+	printf("%.105s\n", graph_dotted_line);
+
+	while (next && n_lines--) {
+		struct page_stat *data;
+		struct symbol *sym;
+		struct map *map;
+		char buf[32];
+		char *caller = buf;
+
+		data = rb_entry(next, struct page_stat, node);
+		sym = machine__find_kernel_function(machine, data->callsite,
+						    &map, NULL);
+		if (sym && sym->name)
+			caller = sym->name;
+		else
+			scnprintf(buf, sizeof(buf), "%"PRIx64, data->callsite);
+
+		printf(" %'16llu | %'9d | %5d | %14s |  %08lx | %s\n",
+		       (unsigned long long)data->alloc_bytes / 1024,
+		       data->nr_alloc, data->order,
+		       migrate_type_str[data->migrate_type],
+		       (unsigned long)data->gfp_flags, caller);
+
+		next = rb_next(next);
+	}
+
+	if (n_lines == -1)
+		printf(" ...              | ...       | ...   | ...            | ...       | ...\n");
+
+	printf("%.105s\n", graph_dotted_line);
 }
 
 static void print_slab_summary(void)
@@ -571,8 +813,10 @@ static void print_slab_result(struct perf_session *session)
 
 static void print_page_result(struct perf_session *session)
 {
+	if (caller_flag)
+		__print_page_caller_result(session, caller_lines);
 	if (alloc_flag)
-		__print_page_result(&page_alloc_sorted, session, alloc_lines);
+		__print_page_alloc_result(session, alloc_lines);
 	print_page_summary();
 }
 
@@ -690,7 +934,8 @@ static void sort_result(void)
 				   &caller_sort);
 	}
 	if (kmem_page) {
-		__sort_page_result(&page_tree, &page_alloc_sorted);
+		__sort_page_result(&page_alloc_tree, &page_alloc_sorted);
+		__sort_page_result(&page_caller_tree, &page_caller_sorted);
 	}
 }
 
@@ -720,8 +965,10 @@ static int __cmd_kmem(struct perf_session *session)
 
 	setup_pager();
 	err = perf_session__process_events(session);
-	if (err != 0)
+	if (err != 0) {
+		pr_err("error during process events: %d\n", err);
 		goto out;
+	}
 	sort_result();
 	print_result(session);
 out:
@@ -962,7 +1209,7 @@ static int __cmd_record(int argc, const char **argv)
 	if (kmem_slab)
 		rec_argc += ARRAY_SIZE(slab_events);
 	if (kmem_page)
-		rec_argc += ARRAY_SIZE(page_events);
+		rec_argc += ARRAY_SIZE(page_events) + 1; /* for -g */
 
 	rec_argv = calloc(rec_argc + 1, sizeof(char *));
 
@@ -977,6 +1224,8 @@ static int __cmd_record(int argc, const char **argv)
 			rec_argv[i] = strdup(slab_events[j]);
 	}
 	if (kmem_page) {
+		rec_argv[i++] = strdup("-g");
+
 		for (j = 0; j < ARRAY_SIZE(page_events); j++, i++)
 			rec_argv[i] = strdup(page_events[j]);
 	}
@@ -1035,7 +1284,7 @@ int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
 		return __cmd_record(argc, argv);
 	}
 
-	session = perf_session__new(&file, false, &perf_kmem);
+	kmem_session = session = perf_session__new(&file, false, &perf_kmem);
 	if (session == NULL)
 		return -1;
 
@@ -1048,6 +1297,7 @@ int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
 		}
 
 		kmem_page_size = pevent_get_page_size(evsel->tp_format->pevent);
+		symbol_conf.use_callchain = true;
 	}
 
 	symbol__init(&session->header.env);
-- 
2.3.3


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 4/5] perf kmem: Support sort keys on page analysis
  2015-03-23  6:30 [PATCHSET 0/5] perf kmem: Implement page allocation analysis (v3) Namhyung Kim
                   ` (2 preceding siblings ...)
  2015-03-23  6:30 ` [PATCH 3/5] perf kmem: Implement stat --page --caller Namhyung Kim
@ 2015-03-23  6:30 ` Namhyung Kim
  2015-03-23 17:27   ` Joonsoo Kim
  2015-03-23  6:30 ` [PATCH 5/5] perf kmem: Add --live option for current allocation stat Namhyung Kim
  2015-03-23 17:23 ` [PATCHSET 0/5] perf kmem: Implement page allocation analysis (v3) Joonsoo Kim
  5 siblings, 1 reply; 19+ messages in thread
From: Namhyung Kim @ 2015-03-23  6:30 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Ingo Molnar, Peter Zijlstra, Jiri Olsa, LKML, David Ahern,
	Minchan Kim, Joonsoo Kim

Add new sort keys for page: page, order, mtype, gfp - existing
'bytes', 'hit' and 'callsite' sort keys also work for page.  Note that
-s/--sort option should be preceded by either of --slab or --page
option to determine where the sort keys applies.

Now it properly groups and sorts allocation stats - so same
page/caller with different order/mtype/gfp will be printed on a
different line.

  # perf kmem stat --page --caller -l 10 -s order,hit

  --------------------------------------------------------------------------------------------
   Total alloc (KB) |  Hits     | Order | Migration type | GFP flags | Callsite
  --------------------------------------------------------------------------------------------
                 64 |         4 |     2 |    RECLAIMABLE |  00285250 | new_slab
             50,144 |    12,536 |     0 |        MOVABLE |  0102005a | __page_cache_alloc
                 52 |        13 |     0 |      UNMOVABLE |  002084d0 | pte_alloc_one
                 40 |        10 |     0 |        MOVABLE |  000280da | handle_mm_fault
                 28 |         7 |     0 |      UNMOVABLE |  000000d0 | __pollwait
                 20 |         5 |     0 |        MOVABLE |  000200da | do_wp_page
                 20 |         5 |     0 |        MOVABLE |  000200da | do_cow_fault
                 16 |         4 |     0 |      UNMOVABLE |  00000200 | __tlb_remove_page
                 16 |         4 |     0 |      UNMOVABLE |  000084d0 | __pmd_alloc
                  8 |         2 |     0 |      UNMOVABLE |  000084d0 | __pud_alloc
   ...              | ...       | ...   | ...            | ...       | ...
  --------------------------------------------------------------------------------------------

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/Documentation/perf-kmem.txt |   6 +-
 tools/perf/builtin-kmem.c              | 399 +++++++++++++++++++++++++++------
 2 files changed, 336 insertions(+), 69 deletions(-)

diff --git a/tools/perf/Documentation/perf-kmem.txt b/tools/perf/Documentation/perf-kmem.txt
index 23219c65c16f..0ebd9c8bfdbf 100644
--- a/tools/perf/Documentation/perf-kmem.txt
+++ b/tools/perf/Documentation/perf-kmem.txt
@@ -37,7 +37,11 @@ OPTIONS
 
 -s <key[,key2...]>::
 --sort=<key[,key2...]>::
-	Sort the output (default: frag,hit,bytes)
+	Sort the output (default: 'frag,hit,bytes' for slab and 'bytes,hit'
+	for page).  Available sort keys are 'ptr, callsite, bytes, hit,
+	pingpong, frag' for slab and 'page, callsite, bytes, hit, order,
+	mtype, gfp' for page.  This option should be preceded by one of the
+	mode selection options - i.e. --slab, --page, --alloc and/or --caller.
 
 -l <num>::
 --line=<num>::
diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c
index e4914b3a0213..fcd0e6f8fbdf 100644
--- a/tools/perf/builtin-kmem.c
+++ b/tools/perf/builtin-kmem.c
@@ -30,7 +30,7 @@ static int	kmem_page;
 static long	kmem_page_size;
 
 struct alloc_stat;
-typedef int (*sort_fn_t)(struct alloc_stat *, struct alloc_stat *);
+typedef int (*sort_fn_t)(void *, void *);
 
 static int			alloc_flag;
 static int			caller_flag;
@@ -181,8 +181,8 @@ static int perf_evsel__process_alloc_node_event(struct perf_evsel *evsel,
 	return ret;
 }
 
-static int ptr_cmp(struct alloc_stat *, struct alloc_stat *);
-static int callsite_cmp(struct alloc_stat *, struct alloc_stat *);
+static int ptr_cmp(void *, void *);
+static int slab_callsite_cmp(void *, void *);
 
 static struct alloc_stat *search_alloc_stat(unsigned long ptr,
 					    unsigned long call_site,
@@ -223,7 +223,8 @@ static int perf_evsel__process_free_event(struct perf_evsel *evsel,
 		s_alloc->pingpong++;
 
 		s_caller = search_alloc_stat(0, s_alloc->call_site,
-					     &root_caller_stat, callsite_cmp);
+					     &root_caller_stat,
+					     slab_callsite_cmp);
 		if (!s_caller)
 			return -1;
 		s_caller->pingpong++;
@@ -395,19 +396,35 @@ static u64 find_callsite(struct perf_evsel *evsel, struct perf_sample *sample)
 	return sample->ip;
 }
 
-static struct page_stat *search_page_alloc_stat(u64 page, bool create)
+struct sort_dimension {
+	const char		name[20];
+	sort_fn_t		cmp;
+	struct list_head	list;
+};
+
+static LIST_HEAD(page_alloc_sort_input);
+static LIST_HEAD(page_caller_sort_input);
+
+static struct page_stat *search_page_alloc_stat(struct page_stat *this,
+						bool create)
 {
 	struct rb_node **node = &page_alloc_tree.rb_node;
 	struct rb_node *parent = NULL;
 	struct page_stat *data;
+	struct sort_dimension *sort;
 
 	while (*node) {
-		s64 cmp;
+		int cmp = 0;
 
 		parent = *node;
 		data = rb_entry(*node, struct page_stat, node);
 
-		cmp = data->page - page;
+		list_for_each_entry(sort, &page_alloc_sort_input, list) {
+			cmp = sort->cmp(this, data);
+			if (cmp)
+				break;
+		}
+
 		if (cmp < 0)
 			node = &parent->rb_left;
 		else if (cmp > 0)
@@ -421,7 +438,10 @@ static struct page_stat *search_page_alloc_stat(u64 page, bool create)
 
 	data = zalloc(sizeof(*data));
 	if (data != NULL) {
-		data->page = page;
+		data->page = this->page;
+		data->order = this->order;
+		data->migrate_type = this->migrate_type;
+		data->gfp_flags = this->gfp_flags;
 
 		rb_link_node(&data->node, parent, node);
 		rb_insert_color(&data->node, &page_alloc_tree);
@@ -430,19 +450,26 @@ static struct page_stat *search_page_alloc_stat(u64 page, bool create)
 	return data;
 }
 
-static struct page_stat *search_page_caller_stat(u64 callsite, bool create)
+static struct page_stat *search_page_caller_stat(struct page_stat *this,
+						 bool create)
 {
 	struct rb_node **node = &page_caller_tree.rb_node;
 	struct rb_node *parent = NULL;
 	struct page_stat *data;
+	struct sort_dimension *sort;
 
 	while (*node) {
-		s64 cmp;
+		int cmp = 0;
 
 		parent = *node;
 		data = rb_entry(*node, struct page_stat, node);
 
-		cmp = data->callsite - callsite;
+		list_for_each_entry(sort, &page_caller_sort_input, list) {
+			cmp = sort->cmp(this, data);
+			if (cmp)
+				break;
+		}
+
 		if (cmp < 0)
 			node = &parent->rb_left;
 		else if (cmp > 0)
@@ -456,7 +483,10 @@ static struct page_stat *search_page_caller_stat(u64 callsite, bool create)
 
 	data = zalloc(sizeof(*data));
 	if (data != NULL) {
-		data->callsite = callsite;
+		data->callsite = this->callsite;
+		data->order = this->order;
+		data->migrate_type = this->migrate_type;
+		data->gfp_flags = this->gfp_flags;
 
 		rb_link_node(&data->node, parent, node);
 		rb_insert_color(&data->node, &page_caller_tree);
@@ -476,6 +506,12 @@ static int perf_evsel__process_page_alloc_event(struct perf_evsel *evsel,
 	u64 bytes = kmem_page_size << order;
 	u64 callsite;
 	struct page_stat *stat;
+	struct page_stat this = {
+		.page = page,
+		.order = order,
+		.gfp_flags = gfp_flags,
+		.migrate_type = migrate_type,
+	};
 
 	if (page == 0) {
 		nr_page_fails++;
@@ -491,28 +527,23 @@ static int perf_evsel__process_page_alloc_event(struct perf_evsel *evsel,
 	 * with things like partial freeing.  But AFAIK there's no way
 	 * to convert a pointer to struct page into PFN in userspace.
 	 */
-	stat = search_page_alloc_stat(page, true);
+	stat = search_page_alloc_stat(&this, true);
 	if (stat == NULL) {
 		pr_err("cannot create page alloc stat\n");
 		return -1;
 	}
 
-	stat->order = order;
-	stat->gfp_flags = gfp_flags;
-	stat->migrate_type = migrate_type;
-	stat->callsite = callsite;
 	stat->nr_alloc++;
 	stat->alloc_bytes += bytes;
+	stat->callsite = callsite;
 
-	stat = search_page_caller_stat(callsite, true);
+	this.callsite = callsite;
+	stat = search_page_caller_stat(&this, true);
 	if (stat == NULL) {
 		pr_err("cannot create page caller stat\n");
 		return -1;
 	}
 
-	stat->order = order;
-	stat->gfp_flags = gfp_flags;
-	stat->migrate_type = migrate_type;
 	stat->nr_alloc++;
 	stat->alloc_bytes += bytes;
 
@@ -531,11 +562,17 @@ static int perf_evsel__process_page_free_event(struct perf_evsel *evsel,
 	unsigned int order = perf_evsel__intval(evsel, sample, "order");
 	u64 bytes = kmem_page_size << order;
 	struct page_stat *stat;
+	struct page_stat this = {
+		.page = page,
+		.order = order,
+		.gfp_flags = -1U,
+		.migrate_type = -1U,
+	};
 
 	nr_page_frees++;
 	total_page_free_bytes += bytes;
 
-	stat = search_page_alloc_stat(page, false);
+	stat = search_page_alloc_stat(&this, false);
 	if (stat == NULL) {
 		pr_debug2("missing free at page %"PRIx64" (order: %d)\n",
 			  page, order);
@@ -549,7 +586,11 @@ static int perf_evsel__process_page_free_event(struct perf_evsel *evsel,
 	stat->nr_free++;
 	stat->free_bytes += bytes;
 
-	stat = search_page_caller_stat(stat->callsite, false);
+	this.callsite = stat->callsite;
+	this.gfp_flags = stat->gfp_flags;
+	this.migrate_type = stat->migrate_type;
+
+	stat = search_page_caller_stat(&this, false);
 	if (stat != NULL) {
 		stat->nr_free++;
 		stat->free_bytes += bytes;
@@ -828,14 +869,10 @@ static void print_result(struct perf_session *session)
 		print_page_result(session);
 }
 
-struct sort_dimension {
-	const char		name[20];
-	sort_fn_t		cmp;
-	struct list_head	list;
-};
-
-static LIST_HEAD(caller_sort);
-static LIST_HEAD(alloc_sort);
+static LIST_HEAD(slab_caller_sort);
+static LIST_HEAD(slab_alloc_sort);
+static LIST_HEAD(page_caller_sort);
+static LIST_HEAD(page_alloc_sort);
 
 static void sort_slab_insert(struct rb_root *root, struct alloc_stat *data,
 			     struct list_head *sort_list)
@@ -884,10 +921,12 @@ static void __sort_slab_result(struct rb_root *root, struct rb_root *root_sorted
 	}
 }
 
-static void sort_page_insert(struct rb_root *root, struct page_stat *data)
+static void sort_page_insert(struct rb_root *root, struct page_stat *data,
+			     struct list_head *sort_list)
 {
 	struct rb_node **new = &root->rb_node;
 	struct rb_node *parent = NULL;
+	struct sort_dimension *sort;
 
 	while (*new) {
 		struct page_stat *this;
@@ -896,8 +935,11 @@ static void sort_page_insert(struct rb_root *root, struct page_stat *data)
 		this = rb_entry(*new, struct page_stat, node);
 		parent = *new;
 
-		/* TODO: support more sort key */
-		cmp = data->alloc_bytes - this->alloc_bytes;
+		list_for_each_entry(sort, sort_list, list) {
+			cmp = sort->cmp(data, this);
+			if (cmp)
+				break;
+		}
 
 		if (cmp > 0)
 			new = &parent->rb_left;
@@ -909,7 +951,8 @@ static void sort_page_insert(struct rb_root *root, struct page_stat *data)
 	rb_insert_color(&data->node, root);
 }
 
-static void __sort_page_result(struct rb_root *root, struct rb_root *root_sorted)
+static void __sort_page_result(struct rb_root *root, struct rb_root *root_sorted,
+			       struct list_head *sort_list)
 {
 	struct rb_node *node;
 	struct page_stat *data;
@@ -921,7 +964,7 @@ static void __sort_page_result(struct rb_root *root, struct rb_root *root_sorted
 
 		rb_erase(node, root);
 		data = rb_entry(node, struct page_stat, node);
-		sort_page_insert(root_sorted, data);
+		sort_page_insert(root_sorted, data, sort_list);
 	}
 }
 
@@ -929,13 +972,15 @@ static void sort_result(void)
 {
 	if (kmem_slab) {
 		__sort_slab_result(&root_alloc_stat, &root_alloc_sorted,
-				   &alloc_sort);
+				   &slab_alloc_sort);
 		__sort_slab_result(&root_caller_stat, &root_caller_sorted,
-				   &caller_sort);
+				   &slab_caller_sort);
 	}
 	if (kmem_page) {
-		__sort_page_result(&page_alloc_tree, &page_alloc_sorted);
-		__sort_page_result(&page_caller_tree, &page_caller_sorted);
+		__sort_page_result(&page_alloc_tree, &page_alloc_sorted,
+				   &page_alloc_sort);
+		__sort_page_result(&page_caller_tree, &page_caller_sorted,
+				   &page_caller_sort);
 	}
 }
 
@@ -975,8 +1020,12 @@ static int __cmd_kmem(struct perf_session *session)
 	return err;
 }
 
-static int ptr_cmp(struct alloc_stat *l, struct alloc_stat *r)
+/* slab sort keys */
+static int ptr_cmp(void *a, void *b)
 {
+	struct alloc_stat *l = a;
+	struct alloc_stat *r = b;
+
 	if (l->ptr < r->ptr)
 		return -1;
 	else if (l->ptr > r->ptr)
@@ -989,8 +1038,11 @@ static struct sort_dimension ptr_sort_dimension = {
 	.cmp	= ptr_cmp,
 };
 
-static int callsite_cmp(struct alloc_stat *l, struct alloc_stat *r)
+static int slab_callsite_cmp(void *a, void *b)
 {
+	struct alloc_stat *l = a;
+	struct alloc_stat *r = b;
+
 	if (l->call_site < r->call_site)
 		return -1;
 	else if (l->call_site > r->call_site)
@@ -1000,11 +1052,14 @@ static int callsite_cmp(struct alloc_stat *l, struct alloc_stat *r)
 
 static struct sort_dimension callsite_sort_dimension = {
 	.name	= "callsite",
-	.cmp	= callsite_cmp,
+	.cmp	= slab_callsite_cmp,
 };
 
-static int hit_cmp(struct alloc_stat *l, struct alloc_stat *r)
+static int hit_cmp(void *a, void *b)
 {
+	struct alloc_stat *l = a;
+	struct alloc_stat *r = b;
+
 	if (l->hit < r->hit)
 		return -1;
 	else if (l->hit > r->hit)
@@ -1017,8 +1072,11 @@ static struct sort_dimension hit_sort_dimension = {
 	.cmp	= hit_cmp,
 };
 
-static int bytes_cmp(struct alloc_stat *l, struct alloc_stat *r)
+static int bytes_cmp(void *a, void *b)
 {
+	struct alloc_stat *l = a;
+	struct alloc_stat *r = b;
+
 	if (l->bytes_alloc < r->bytes_alloc)
 		return -1;
 	else if (l->bytes_alloc > r->bytes_alloc)
@@ -1031,9 +1089,11 @@ static struct sort_dimension bytes_sort_dimension = {
 	.cmp	= bytes_cmp,
 };
 
-static int frag_cmp(struct alloc_stat *l, struct alloc_stat *r)
+static int frag_cmp(void *a, void *b)
 {
 	double x, y;
+	struct alloc_stat *l = a;
+	struct alloc_stat *r = b;
 
 	x = fragmentation(l->bytes_req, l->bytes_alloc);
 	y = fragmentation(r->bytes_req, r->bytes_alloc);
@@ -1050,8 +1110,11 @@ static struct sort_dimension frag_sort_dimension = {
 	.cmp	= frag_cmp,
 };
 
-static int pingpong_cmp(struct alloc_stat *l, struct alloc_stat *r)
+static int pingpong_cmp(void *a, void *b)
 {
+	struct alloc_stat *l = a;
+	struct alloc_stat *r = b;
+
 	if (l->pingpong < r->pingpong)
 		return -1;
 	else if (l->pingpong > r->pingpong)
@@ -1064,7 +1127,135 @@ static struct sort_dimension pingpong_sort_dimension = {
 	.cmp	= pingpong_cmp,
 };
 
-static struct sort_dimension *avail_sorts[] = {
+/* page sort keys */
+static int page_cmp(void *a, void *b)
+{
+	struct page_stat *l = a;
+	struct page_stat *r = b;
+
+	if (l->page < r->page)
+		return -1;
+	else if (l->page > r->page)
+		return 1;
+	return 0;
+}
+
+static struct sort_dimension page_sort_dimension = {
+	.name	= "page",
+	.cmp	= page_cmp,
+};
+
+static int page_callsite_cmp(void *a, void *b)
+{
+	struct page_stat *l = a;
+	struct page_stat *r = b;
+
+	if (l->callsite < r->callsite)
+		return -1;
+	else if (l->callsite > r->callsite)
+		return 1;
+	return 0;
+}
+
+static struct sort_dimension page_callsite_sort_dimension = {
+	.name	= "callsite",
+	.cmp	= page_callsite_cmp,
+};
+
+static int page_hit_cmp(void *a, void *b)
+{
+	struct page_stat *l = a;
+	struct page_stat *r = b;
+
+	if (l->nr_alloc < r->nr_alloc)
+		return -1;
+	else if (l->nr_alloc > r->nr_alloc)
+		return 1;
+	return 0;
+}
+
+static struct sort_dimension page_hit_sort_dimension = {
+	.name	= "hit",
+	.cmp	= page_hit_cmp,
+};
+
+static int page_bytes_cmp(void *a, void *b)
+{
+	struct page_stat *l = a;
+	struct page_stat *r = b;
+
+	if (l->alloc_bytes < r->alloc_bytes)
+		return -1;
+	else if (l->alloc_bytes > r->alloc_bytes)
+		return 1;
+	return 0;
+}
+
+static struct sort_dimension page_bytes_sort_dimension = {
+	.name	= "bytes",
+	.cmp	= page_bytes_cmp,
+};
+
+static int page_order_cmp(void *a, void *b)
+{
+	struct page_stat *l = a;
+	struct page_stat *r = b;
+
+	if (l->order < r->order)
+		return -1;
+	else if (l->order > r->order)
+		return 1;
+	return 0;
+}
+
+static struct sort_dimension page_order_sort_dimension = {
+	.name	= "order",
+	.cmp	= page_order_cmp,
+};
+
+static int migrate_type_cmp(void *a, void *b)
+{
+	struct page_stat *l = a;
+	struct page_stat *r = b;
+
+	/* for internal use to find free'd page */
+	if (l->migrate_type == -1U)
+		return 0;
+
+	if (l->migrate_type < r->migrate_type)
+		return -1;
+	else if (l->migrate_type > r->migrate_type)
+		return 1;
+	return 0;
+}
+
+static struct sort_dimension migrate_type_sort_dimension = {
+	.name	= "mtype",
+	.cmp	= migrate_type_cmp,
+};
+
+static int gfp_flags_cmp(void *a, void *b)
+{
+	struct page_stat *l = a;
+	struct page_stat *r = b;
+
+	/* for internal use to find free'd page */
+	if (l->gfp_flags == -1U)
+		return 0;
+
+	if (l->gfp_flags < r->gfp_flags)
+		return -1;
+	else if (l->gfp_flags > r->gfp_flags)
+		return 1;
+	return 0;
+}
+
+static struct sort_dimension gfp_flags_sort_dimension = {
+	.name	= "gfp",
+	.cmp	= gfp_flags_cmp,
+};
+
+static struct sort_dimension *slab_sorts[] = {
 	&ptr_sort_dimension,
 	&callsite_sort_dimension,
 	&hit_sort_dimension,
@@ -1073,16 +1264,44 @@ static struct sort_dimension *avail_sorts[] = {
 	&pingpong_sort_dimension,
 };
 
-#define NUM_AVAIL_SORTS	((int)ARRAY_SIZE(avail_sorts))
+static struct sort_dimension *page_sorts[] = {
+	&page_sort_dimension,
+	&page_callsite_sort_dimension,
+	&page_hit_sort_dimension,
+	&page_bytes_sort_dimension,
+	&page_order_sort_dimension,
+	&migrate_type_sort_dimension,
+	&gfp_flags_sort_dimension,
+};
+
+static int slab_sort_dimension__add(const char *tok, struct list_head *list)
+{
+	struct sort_dimension *sort;
+	int i;
+
+	for (i = 0; i < (int)ARRAY_SIZE(slab_sorts); i++) {
+		if (!strcmp(slab_sorts[i]->name, tok)) {
+			sort = memdup(slab_sorts[i], sizeof(*slab_sorts[i]));
+			if (!sort) {
+				pr_err("%s: memdup failed\n", __func__);
+				return -1;
+			}
+			list_add_tail(&sort->list, list);
+			return 0;
+		}
+	}
+
+	return -1;
+}
 
-static int sort_dimension__add(const char *tok, struct list_head *list)
+static int page_sort_dimension__add(const char *tok, struct list_head *list)
 {
 	struct sort_dimension *sort;
 	int i;
 
-	for (i = 0; i < NUM_AVAIL_SORTS; i++) {
-		if (!strcmp(avail_sorts[i]->name, tok)) {
-			sort = memdup(avail_sorts[i], sizeof(*avail_sorts[i]));
+	for (i = 0; i < (int)ARRAY_SIZE(page_sorts); i++) {
+		if (!strcmp(page_sorts[i]->name, tok)) {
+			sort = memdup(page_sorts[i], sizeof(*page_sorts[i]));
 			if (!sort) {
 				pr_err("%s: memdup failed\n", __func__);
 				return -1;
@@ -1095,7 +1314,33 @@ static int sort_dimension__add(const char *tok, struct list_head *list)
 	return -1;
 }
 
-static int setup_sorting(struct list_head *sort_list, const char *arg)
+static int setup_slab_sorting(struct list_head *sort_list, const char *arg)
+{
+	char *tok;
+	char *str = strdup(arg);
+	char *pos = str;
+
+	if (!str) {
+		pr_err("%s: strdup failed\n", __func__);
+		return -1;
+	}
+
+	while (true) {
+		tok = strsep(&pos, ",");
+		if (!tok)
+			break;
+		if (slab_sort_dimension__add(tok, sort_list) < 0) {
+			error("Unknown slab --sort key: '%s'", tok);
+			free(str);
+			return -1;
+		}
+	}
+
+	free(str);
+	return 0;
+}
+
+static int setup_page_sorting(struct list_head *sort_list, const char *arg)
 {
 	char *tok;
 	char *str = strdup(arg);
@@ -1110,8 +1355,8 @@ static int setup_sorting(struct list_head *sort_list, const char *arg)
 		tok = strsep(&pos, ",");
 		if (!tok)
 			break;
-		if (sort_dimension__add(tok, sort_list) < 0) {
-			error("Unknown --sort key: '%s'", tok);
+		if (page_sort_dimension__add(tok, sort_list) < 0) {
+			error("Unknown page --sort key: '%s'", tok);
 			free(str);
 			return -1;
 		}
@@ -1127,10 +1372,17 @@ static int parse_sort_opt(const struct option *opt __maybe_unused,
 	if (!arg)
 		return -1;
 
-	if (caller_flag > alloc_flag)
-		return setup_sorting(&caller_sort, arg);
-	else
-		return setup_sorting(&alloc_sort, arg);
+	if (kmem_page > kmem_slab) {
+		if (caller_flag > alloc_flag)
+			return setup_page_sorting(&page_caller_sort, arg);
+		else
+			return setup_page_sorting(&page_alloc_sort, arg);
+	} else {
+		if (caller_flag > alloc_flag)
+			return setup_slab_sorting(&slab_caller_sort, arg);
+		else
+			return setup_slab_sorting(&slab_alloc_sort, arg);
+	}
 
 	return 0;
 }
@@ -1238,7 +1490,8 @@ static int __cmd_record(int argc, const char **argv)
 
 int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
 {
-	const char * const default_sort_order = "frag,hit,bytes";
+	const char * const default_slab_sort = "frag,hit,bytes";
+	const char * const default_page_sort = "bytes,hit";
 	const struct option kmem_options[] = {
 	OPT_STRING('i', "input", &input_name, "file", "input file name"),
 	OPT_INCR('v', "verbose", &verbose,
@@ -1248,8 +1501,8 @@ int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
 	OPT_CALLBACK_NOOPT(0, "alloc", NULL, NULL,
 			   "show per-allocation statistics", parse_alloc_opt),
 	OPT_CALLBACK('s', "sort", NULL, "key[,key2...]",
-		     "sort by keys: ptr, call_site, bytes, hit, pingpong, frag",
-		     parse_sort_opt),
+		     "sort by keys: ptr, callsite, bytes, hit, pingpong, frag, "
+		     "page, order, mtype, gfp", parse_sort_opt),
 	OPT_CALLBACK('l', "line", NULL, "num", "show n lines", parse_line_opt),
 	OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"),
 	OPT_CALLBACK_NOOPT(0, "slab", NULL, NULL, "Analyze slab allocator",
@@ -1308,11 +1561,21 @@ int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
 		if (cpu__setup_cpunode_map())
 			goto out_delete;
 
-		if (list_empty(&caller_sort))
-			setup_sorting(&caller_sort, default_sort_order);
-		if (list_empty(&alloc_sort))
-			setup_sorting(&alloc_sort, default_sort_order);
-
+		if (list_empty(&slab_caller_sort))
+			setup_slab_sorting(&slab_caller_sort, default_slab_sort);
+		if (list_empty(&slab_alloc_sort))
+			setup_slab_sorting(&slab_alloc_sort, default_slab_sort);
+		if (list_empty(&page_caller_sort))
+			setup_page_sorting(&page_caller_sort, default_page_sort);
+		if (list_empty(&page_alloc_sort))
+			setup_page_sorting(&page_alloc_sort, default_page_sort);
+
+		if (kmem_page) {
+			setup_page_sorting(&page_alloc_sort_input,
+					   "page,order,mtype,gfp");
+			setup_page_sorting(&page_caller_sort_input,
+					   "callsite,order,mtype,gfp");
+		}
 		ret = __cmd_kmem(session);
 	} else
 		usage_with_options(kmem_usage, kmem_options);
-- 
2.3.3


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 5/5] perf kmem: Add --live option for current allocation stat
  2015-03-23  6:30 [PATCHSET 0/5] perf kmem: Implement page allocation analysis (v3) Namhyung Kim
                   ` (3 preceding siblings ...)
  2015-03-23  6:30 ` [PATCH 4/5] perf kmem: Support sort keys on page analysis Namhyung Kim
@ 2015-03-23  6:30 ` Namhyung Kim
  2015-03-23 17:23 ` [PATCHSET 0/5] perf kmem: Implement page allocation analysis (v3) Joonsoo Kim
  5 siblings, 0 replies; 19+ messages in thread
From: Namhyung Kim @ 2015-03-23  6:30 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Ingo Molnar, Peter Zijlstra, Jiri Olsa, LKML, David Ahern,
	Minchan Kim, Joonsoo Kim

Currently perf kmem shows total (page) allocation stat by default, but
sometimes one might want to see live (total alloc-only) requests/pages
only.  The new --live option does this by subtracting freed allocation
from the stat.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/Documentation/perf-kmem.txt |   5 ++
 tools/perf/builtin-kmem.c              | 101 ++++++++++++++++++++++++++++++---
 2 files changed, 97 insertions(+), 9 deletions(-)

diff --git a/tools/perf/Documentation/perf-kmem.txt b/tools/perf/Documentation/perf-kmem.txt
index 0ebd9c8bfdbf..5a2d9aaf1933 100644
--- a/tools/perf/Documentation/perf-kmem.txt
+++ b/tools/perf/Documentation/perf-kmem.txt
@@ -56,6 +56,11 @@ OPTIONS
 --page::
 	Analyze page allocator events
 
+--live::
+	Show live page stat.  The perf kmem shows total allocation stat by
+	default, but this option shows live (currently allocated) pages
+	instead.  (This option works with --page option only)
+
 SEE ALSO
 --------
 linkperf:perf-record[1]
diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c
index fcd0e6f8fbdf..de82a1c579d9 100644
--- a/tools/perf/builtin-kmem.c
+++ b/tools/perf/builtin-kmem.c
@@ -243,6 +243,7 @@ static unsigned long nr_page_frees;
 static unsigned long nr_page_fails;
 static unsigned long nr_page_nomatch;
 
+static bool live_page;
 static struct perf_session *kmem_session;
 
 #define MAX_MIGRATE_TYPES  6
@@ -263,6 +264,7 @@ struct page_stat {
 	int 		nr_free;
 };
 
+static struct rb_root page_live_tree;
 static struct rb_root page_alloc_tree;
 static struct rb_root page_alloc_sorted;
 static struct rb_root page_caller_tree;
@@ -405,6 +407,44 @@ struct sort_dimension {
 static LIST_HEAD(page_alloc_sort_input);
 static LIST_HEAD(page_caller_sort_input);
 
+static struct page_stat *search_page_live_stat(struct page_stat *this,
+					       bool create)
+{
+	struct rb_node **node = &page_live_tree.rb_node;
+	struct rb_node *parent = NULL;
+	struct page_stat *data;
+
+	while (*node) {
+		s64 cmp;
+
+		parent = *node;
+		data = rb_entry(*node, struct page_stat, node);
+
+		cmp = data->page - this->page;
+		if (cmp < 0)
+			node = &parent->rb_left;
+		else if (cmp > 0)
+			node = &parent->rb_right;
+		else
+			return data;
+	}
+
+	if (!create)
+		return NULL;
+
+	data = zalloc(sizeof(*data));
+	if (data != NULL) {
+		data->page = this->page;
+		data->order = this->order;
+		data->migrate_type = this->migrate_type;
+		data->gfp_flags = this->gfp_flags;
+
+		rb_link_node(&data->node, parent, node);
+		rb_insert_color(&data->node, &page_live_tree);
+	}
+
+	return data;
+}
 static struct page_stat *search_page_alloc_stat(struct page_stat *this,
 						bool create)
 {
@@ -523,11 +563,14 @@ static int perf_evsel__process_page_alloc_event(struct perf_evsel *evsel,
 	callsite = find_callsite(evsel, sample);
 
 	/*
+	 * This is to find the current page (with correct gfp flags and
+	 * migrate_type) at free event.
+	 *
 	 * XXX: We'd better to use PFN instead of page pointer to deal
 	 * with things like partial freeing.  But AFAIK there's no way
 	 * to convert a pointer to struct page into PFN in userspace.
 	 */
-	stat = search_page_alloc_stat(&this, true);
+	stat = search_page_live_stat(&this, true);
 	if (stat == NULL) {
 		pr_err("cannot create page alloc stat\n");
 		return -1;
@@ -537,6 +580,18 @@ static int perf_evsel__process_page_alloc_event(struct perf_evsel *evsel,
 	stat->alloc_bytes += bytes;
 	stat->callsite = callsite;
 
+	if (!live_page) {
+		stat = search_page_alloc_stat(&this, true);
+		if (stat == NULL) {
+			pr_err("cannot create page alloc stat\n");
+			return -1;
+		}
+
+		stat->nr_alloc++;
+		stat->alloc_bytes += bytes;
+		stat->callsite = callsite;
+	}
+
 	this.callsite = callsite;
 	stat = search_page_caller_stat(&this, true);
 	if (stat == NULL) {
@@ -572,7 +627,7 @@ static int perf_evsel__process_page_free_event(struct perf_evsel *evsel,
 	nr_page_frees++;
 	total_page_free_bytes += bytes;
 
-	stat = search_page_alloc_stat(&this, false);
+	stat = search_page_live_stat(&this, false);
 	if (stat == NULL) {
 		pr_debug2("missing free at page %"PRIx64" (order: %d)\n",
 			  page, order);
@@ -583,17 +638,37 @@ static int perf_evsel__process_page_free_event(struct perf_evsel *evsel,
 		return 0;
 	}
 
-	stat->nr_free++;
-	stat->free_bytes += bytes;
-
 	this.callsite = stat->callsite;
 	this.gfp_flags = stat->gfp_flags;
 	this.migrate_type = stat->migrate_type;
 
+	rb_erase(&stat->node, &page_live_tree);
+	free(stat);
+
+	if (live_page) {
+		order_stats[this.order][this.migrate_type]--;
+	} else {
+		stat = search_page_alloc_stat(&this, false);
+		if (stat != NULL) {
+			stat->nr_free++;
+			stat->free_bytes += bytes;
+		}
+	}
+
 	stat = search_page_caller_stat(&this, false);
 	if (stat != NULL) {
 		stat->nr_free++;
 		stat->free_bytes += bytes;
+
+		if (live_page) {
+			stat->nr_alloc--;
+			stat->alloc_bytes -= bytes;
+
+			if (stat->nr_alloc == 0) {
+				rb_erase(&stat->node, &page_caller_tree);
+				free(stat);
+			}
+		}
 	}
 
 	return 0;
@@ -712,7 +787,8 @@ static void __print_page_alloc_result(struct perf_session *session, int n_lines)
 	struct machine *machine = &session->machines.host;
 
 	printf("\n%.105s\n", graph_dotted_line);
-	printf(" Page             | Total alloc (KB) | Hits      | Order | Migration type | GFP flags | Callsite\n");
+	printf(" Page             | %5s alloc (KB) | Hits      | Order | Migration type | GFP flags | Callsite\n",
+	       live_page ? "Live" : "Total");
 	printf("%.105s\n", graph_dotted_line);
 
 	while (next && n_lines--) {
@@ -752,7 +828,8 @@ static void __print_page_caller_result(struct perf_session *session, int n_lines
 	struct machine *machine = &session->machines.host;
 
 	printf("\n%.105s\n", graph_dotted_line);
-	printf(" Total alloc (KB) | Hits      | Order | Migration type | GFP flags | Callsite\n");
+	printf(" %5s alloc (KB) | Hits      | Order | Migration type | GFP flags | Callsite\n",
+	       live_page ? "Live" : "Total");
 	printf("%.105s\n", graph_dotted_line);
 
 	while (next && n_lines--) {
@@ -977,8 +1054,13 @@ static void sort_result(void)
 				   &slab_caller_sort);
 	}
 	if (kmem_page) {
-		__sort_page_result(&page_alloc_tree, &page_alloc_sorted,
-				   &page_alloc_sort);
+		if (live_page)
+			__sort_page_result(&page_live_tree, &page_alloc_sorted,
+					   &page_alloc_sort);
+		else
+			__sort_page_result(&page_alloc_tree, &page_alloc_sorted,
+					   &page_alloc_sort);
+
 		__sort_page_result(&page_caller_tree, &page_caller_sorted,
 				   &page_caller_sort);
 	}
@@ -1509,6 +1591,7 @@ int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
 			   parse_slab_opt),
 	OPT_CALLBACK_NOOPT(0, "page", NULL, NULL, "Analyze page allocator",
 			   parse_page_opt),
+	OPT_BOOLEAN(0, "live", &live_page, "Show live page stat"),
 	OPT_END()
 	};
 	const char *const kmem_subcommands[] = { "record", "stat", NULL };
-- 
2.3.3


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* Re: [PATCH 1/5] perf kmem: Print big numbers using thousands' group
  2015-03-23  6:30 ` [PATCH 1/5] perf kmem: Print big numbers using thousands' group Namhyung Kim
@ 2015-03-23 14:08   ` Arnaldo Carvalho de Melo
  2015-03-23 23:35     ` Namhyung Kim
  2015-03-24 16:31   ` [tip:perf/core] " tip-bot for Namhyung Kim
  1 sibling, 1 reply; 19+ messages in thread
From: Arnaldo Carvalho de Melo @ 2015-03-23 14:08 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Ingo Molnar, Peter Zijlstra, Jiri Olsa, LKML, David Ahern,
	Minchan Kim, Joonsoo Kim

Em Mon, Mar 23, 2015 at 03:30:40PM +0900, Namhyung Kim escreveu:
> Like perf stat, this makes easy to read the numbers on stat like below:
> 
>   # perf kmem stat
> 
>   SUMMARY
>   =======
>   Total bytes requested: 9,770,900
>   Total bytes allocated: 9,782,712
>   Total bytes wasted on internal fragmentation: 11,812
>   Internal fragmentation: 0.120744%
>   Cross CPU allocations: 74/152,819
> 
> Suggested-by: Ingo Molnar <mingo@kernel.org>
> Signed-off-by: Namhyung Kim <namhyung@kernel.org>
>  static void print_result(struct perf_session *session)
> @@ -706,6 +707,8 @@ int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
>  	symbol__init(&session->header.env);
>  
>  	if (!strcmp(argv[0], "stat")) {
> +		setlocale(LC_ALL, "");
> +

Applying, but I think it is better to have this call in perf's main()
routine, to avoid repeating it in each tool, as 'builtin-stat' already
does this, but as this affects all tools, some further testing is
needed, I think, or does anyone see any problem with that?

- Arnaldo

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCHSET 0/5] perf kmem: Implement page allocation analysis (v3)
  2015-03-23  6:30 [PATCHSET 0/5] perf kmem: Implement page allocation analysis (v3) Namhyung Kim
                   ` (4 preceding siblings ...)
  2015-03-23  6:30 ` [PATCH 5/5] perf kmem: Add --live option for current allocation stat Namhyung Kim
@ 2015-03-23 17:23 ` Joonsoo Kim
  2015-03-23 23:57   ` Namhyung Kim
  5 siblings, 1 reply; 19+ messages in thread
From: Joonsoo Kim @ 2015-03-23 17:23 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Arnaldo Carvalho de Melo, Ingo Molnar, Peter Zijlstra, Jiri Olsa,
	LKML, David Ahern, Minchan Kim

Hello, Namhyung.

2015-03-23 15:30 GMT+09:00 Namhyung Kim <namhyung@kernel.org>:
> Hello,
>
> Currently perf kmem command only analyzes SLAB memory allocation.  And
> I'd like to introduce page allocation analysis also.  Users can use
>  --slab and/or --page option to select it.  If none of these options
> are used, it does slab allocation analysis for backward compatibility.
>
>  * changes in v3)
>   - add live page statistics
>
>  * changes in v2)
>   - Use thousand grouping for big numbers - i.e. 12345 -> 12,345  (Ingo)
>   - Improve output stat readability  (Ingo)
>   - Remove alloc size column as it can be calculated from hits and order
>
> Patch 1 is to support thousand grouping on stat output.  Patch 2
> implements basic support for page allocation analysis, patch 3 deals
> with the callsite and finally patch 4 implements sorting.
>
> In this patchset, I used two kmem events: kmem:mm_page_alloc and
> kmem_page_free for analysis as they can track almost all of memory
> allocation/free path AFAIK.  However, unlike slab tracepoint events,
> those page allocation events don't provide callsite info directly.  So
> I recorded callchains and extracted callsites like below:
>
> Normal page allocation callchains look like this:
>
>   360a7e __alloc_pages_nodemask
>   3a711c alloc_pages_current
>   357bc7 __page_cache_alloc   <-- callsite
>   357cf6 pagecache_get_page
>    48b0a prepare_pages
>    494d3 __btrfs_buffered_write
>    49cdf btrfs_file_write_iter
>   3ceb6e new_sync_write
>   3cf447 vfs_write
>   3cff99 sys_write
>   7556e9 system_call
>     f880 __write_nocancel
>    33eb9 cmd_record
>    4b38e cmd_kmem
>    7aa23 run_builtin
>    27a9a main
>    20800 __libc_start_main
>
> But first two are internal page allocation functions so it should be
> skipped.  To determine such allocation functions, I used following regex:
>
>   ^_?_?(alloc|get_free|get_zeroed)_pages?
>
> This gave me a following list of functions (you can see this with -v):
>
>   alloc func: __get_free_pages
>   alloc func: get_zeroed_page
>   alloc func: alloc_pages_exact
>   alloc func: __alloc_pages_direct_compact
>   alloc func: __alloc_pages_nodemask
>   alloc func: alloc_page_interleave
>   alloc func: alloc_pages_current
>   alloc func: alloc_pages_vma
>   alloc func: alloc_page_buffers
>   alloc func: alloc_pages_exact_nid
>
> After skipping those function, it got '__page_cache_alloc'.

It'd be better to have option for storing more depth of call stack.
Just one call path isn't sufficient to distinguish real caller
for some functions. For example, new_slab(), one of your callsite
example doesn't tell which subsystem try to allocate slab object and
fall through the page allocator.

> Other information such as allocation order, migration type and gfp
> flags are provided by tracepoint events.
>
> Basically the output will be sorted by total allocation bytes, but you
> can change it by using -s/--sort option.  The following sort keys are
> added to support page analysis: page, order, mtype, gfp.  Existing
> 'callsite', 'bytes' and 'hit' sort keys also can be used.
>
> An example follows:
>
>   # perf kmem record --slab --page sleep 1
>   [ perf record: Woken up 0 times to write data ]
>   [ perf record: Captured and wrote 49.277 MB perf.data (191027 samples) ]
>
>   # perf kmem stat --page --caller -l 10 -s order,hit
>
>   --------------------------------------------------------------------------------------------
>    Total alloc (KB) | Hits      | Order | Migration type | GFP flags | Callsite
>   --------------------------------------------------------------------------------------------
>                  64 |         4 |     2 |    RECLAIMABLE |  00285250 | new_slab
>              50,144 |    12,536 |     0 |        MOVABLE |  0102005a | __page_cache_alloc
>                  52 |        13 |     0 |      UNMOVABLE |  002084d0 | pte_alloc_one
>                  40 |        10 |     0 |        MOVABLE |  000280da | handle_mm_fault
>                  28 |         7 |     0 |      UNMOVABLE |  000000d0 | __pollwait
>                  20 |         5 |     0 |        MOVABLE |  000200da | do_wp_page
>                  20 |         5 |     0 |        MOVABLE |  000200da | do_cow_fault
>                  16 |         4 |     0 |      UNMOVABLE |  00000200 | __tlb_remove_page
>                  16 |         4 |     0 |      UNMOVABLE |  000084d0 | __pmd_alloc
>                   8 |         2 |     0 |      UNMOVABLE |  000084d0 | __pud_alloc
>    ...              | ...       | ...   | ...            | ...       | ...
>   --------------------------------------------------------------------------------------------

How about printing GFP flags more intuitively, for example,
GFP_NOFS|GFP_ZERO? Tracepoint on mm_page_alloc already print
output as this format.

Thanks.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 4/5] perf kmem: Support sort keys on page analysis
  2015-03-23  6:30 ` [PATCH 4/5] perf kmem: Support sort keys on page analysis Namhyung Kim
@ 2015-03-23 17:27   ` Joonsoo Kim
  2015-03-24  0:20     ` Namhyung Kim
  0 siblings, 1 reply; 19+ messages in thread
From: Joonsoo Kim @ 2015-03-23 17:27 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Arnaldo Carvalho de Melo, Ingo Molnar, Peter Zijlstra, Jiri Olsa,
	LKML, David Ahern, Minchan Kim

2015-03-23 15:30 GMT+09:00 Namhyung Kim <namhyung@kernel.org>:
> Add new sort keys for page: page, order, mtype, gfp - existing
> 'bytes', 'hit' and 'callsite' sort keys also work for page.  Note that
> -s/--sort option should be preceded by either of --slab or --page
> option to determine where the sort keys applies.
>
> Now it properly groups and sorts allocation stats - so same
> page/caller with different order/mtype/gfp will be printed on a
> different line.

How about storing/printing pid, too?
It would be helpful to distinguish who allocates the page.

Thanks.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 2/5] perf kmem: Analyze page allocator events also
  2015-03-23  6:30 ` [PATCH 2/5] perf kmem: Analyze page allocator events also Namhyung Kim
@ 2015-03-23 17:32   ` Joonsoo Kim
  2015-03-24  0:18     ` Namhyung Kim
  0 siblings, 1 reply; 19+ messages in thread
From: Joonsoo Kim @ 2015-03-23 17:32 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Arnaldo Carvalho de Melo, Ingo Molnar, Peter Zijlstra, Jiri Olsa,
	LKML, David Ahern, Minchan Kim

2015-03-23 15:30 GMT+09:00 Namhyung Kim <namhyung@kernel.org>:
> The perf kmem command records and analyze kernel memory allocation
> only for SLAB objects.  This patch implement a simple page allocator
> analyzer using kmem:mm_page_alloc and kmem:mm_page_free events.
>
> It adds two new options of --slab and --page.  The --slab option is
> for analyzing SLAB allocator and that's what perf kmem currently does.
>
> The new --page option enables page allocator events and analyze kernel
> memory usage in page unit.  Currently, 'stat --alloc' subcommand is
> implemented only.
>
> If none of these --slab nor --page is specified, --slab is implied.
>
>   # perf kmem stat --page --alloc --line 10
>
>   -------------------------------------------------------------------------------------
>    Page             | Total alloc (KB) | Hits     | Order | Migration type | GFP flags
>   -------------------------------------------------------------------------------------
>    ffffea0015e48e00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
>    ffffea0015e47400 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
>    ffffea001440f600 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
>    ffffea001440cc00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
>    ffffea00140c6300 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
>    ffffea00140c5c00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
>    ffffea00140c5000 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
>    ffffea00140c4f00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
>    ffffea00140c4e00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
>    ffffea00140c4d00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
>    ...              | ...              | ...      | ...   | ...            | ...
>   -------------------------------------------------------------------------------------

Tracepoint on mm_page_alloc print out pfn as well as pointer of struct page.
How about printing pfn rather than pointer of struct page?

Thanks.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 1/5] perf kmem: Print big numbers using thousands' group
  2015-03-23 14:08   ` Arnaldo Carvalho de Melo
@ 2015-03-23 23:35     ` Namhyung Kim
  0 siblings, 0 replies; 19+ messages in thread
From: Namhyung Kim @ 2015-03-23 23:35 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Ingo Molnar, Peter Zijlstra, Jiri Olsa, LKML, David Ahern,
	Minchan Kim, Joonsoo Kim

Hi Arnaldo,

On Mon, Mar 23, 2015 at 11:08:38AM -0300, Arnaldo Carvalho de Melo wrote:
> Em Mon, Mar 23, 2015 at 03:30:40PM +0900, Namhyung Kim escreveu:
> > Like perf stat, this makes easy to read the numbers on stat like below:
> > 
> >   # perf kmem stat
> > 
> >   SUMMARY
> >   =======
> >   Total bytes requested: 9,770,900
> >   Total bytes allocated: 9,782,712
> >   Total bytes wasted on internal fragmentation: 11,812
> >   Internal fragmentation: 0.120744%
> >   Cross CPU allocations: 74/152,819
> > 
> > Suggested-by: Ingo Molnar <mingo@kernel.org>
> > Signed-off-by: Namhyung Kim <namhyung@kernel.org>
> >  static void print_result(struct perf_session *session)
> > @@ -706,6 +707,8 @@ int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
> >  	symbol__init(&session->header.env);
> >  
> >  	if (!strcmp(argv[0], "stat")) {
> > +		setlocale(LC_ALL, "");
> > +
> 
> Applying, but I think it is better to have this call in perf's main()
> routine, to avoid repeating it in each tool, as 'builtin-stat' already
> does this, but as this affects all tools, some further testing is
> needed, I think, or does anyone see any problem with that?

I don't see any explicit problem with it, but as its effect can be
subtle I agree that we need further testing..

Thanks,
Namhyung

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCHSET 0/5] perf kmem: Implement page allocation analysis (v3)
  2015-03-23 17:23 ` [PATCHSET 0/5] perf kmem: Implement page allocation analysis (v3) Joonsoo Kim
@ 2015-03-23 23:57   ` Namhyung Kim
  0 siblings, 0 replies; 19+ messages in thread
From: Namhyung Kim @ 2015-03-23 23:57 UTC (permalink / raw)
  To: Joonsoo Kim
  Cc: Arnaldo Carvalho de Melo, Ingo Molnar, Peter Zijlstra, Jiri Olsa,
	LKML, David Ahern, Minchan Kim

Hi Joonsoo,

On Tue, Mar 24, 2015 at 02:23:05AM +0900, Joonsoo Kim wrote:
> Hello, Namhyung.
> 
> 2015-03-23 15:30 GMT+09:00 Namhyung Kim <namhyung@kernel.org>:
> > Hello,
> >
> > Currently perf kmem command only analyzes SLAB memory allocation.  And
> > I'd like to introduce page allocation analysis also.  Users can use
> >  --slab and/or --page option to select it.  If none of these options
> > are used, it does slab allocation analysis for backward compatibility.
> >
> >  * changes in v3)
> >   - add live page statistics
> >
> >  * changes in v2)
> >   - Use thousand grouping for big numbers - i.e. 12345 -> 12,345  (Ingo)
> >   - Improve output stat readability  (Ingo)
> >   - Remove alloc size column as it can be calculated from hits and order
> >
> > Patch 1 is to support thousand grouping on stat output.  Patch 2
> > implements basic support for page allocation analysis, patch 3 deals
> > with the callsite and finally patch 4 implements sorting.
> >
> > In this patchset, I used two kmem events: kmem:mm_page_alloc and
> > kmem_page_free for analysis as they can track almost all of memory
> > allocation/free path AFAIK.  However, unlike slab tracepoint events,
> > those page allocation events don't provide callsite info directly.  So
> > I recorded callchains and extracted callsites like below:
> >
> > Normal page allocation callchains look like this:
> >
> >   360a7e __alloc_pages_nodemask
> >   3a711c alloc_pages_current
> >   357bc7 __page_cache_alloc   <-- callsite
> >   357cf6 pagecache_get_page
> >    48b0a prepare_pages
> >    494d3 __btrfs_buffered_write
> >    49cdf btrfs_file_write_iter
> >   3ceb6e new_sync_write
> >   3cf447 vfs_write
> >   3cff99 sys_write
> >   7556e9 system_call
> >     f880 __write_nocancel
> >    33eb9 cmd_record
> >    4b38e cmd_kmem
> >    7aa23 run_builtin
> >    27a9a main
> >    20800 __libc_start_main
> >
> > But first two are internal page allocation functions so it should be
> > skipped.  To determine such allocation functions, I used following regex:
> >
> >   ^_?_?(alloc|get_free|get_zeroed)_pages?
> >
> > This gave me a following list of functions (you can see this with -v):
> >
> >   alloc func: __get_free_pages
> >   alloc func: get_zeroed_page
> >   alloc func: alloc_pages_exact
> >   alloc func: __alloc_pages_direct_compact
> >   alloc func: __alloc_pages_nodemask
> >   alloc func: alloc_page_interleave
> >   alloc func: alloc_pages_current
> >   alloc func: alloc_pages_vma
> >   alloc func: alloc_page_buffers
> >   alloc func: alloc_pages_exact_nid
> >
> > After skipping those function, it got '__page_cache_alloc'.
> 
> It'd be better to have option for storing more depth of call stack.
> Just one call path isn't sufficient to distinguish real caller
> for some functions. For example, new_slab(), one of your callsite
> example doesn't tell which subsystem try to allocate slab object and
> fall through the page allocator.

Agreed.  But it'd require more change in the output format.  The
current table format is fit for single line per data.  So I'd like to
leave it to further work (please see below).


> 
> > Other information such as allocation order, migration type and gfp
> > flags are provided by tracepoint events.
> >
> > Basically the output will be sorted by total allocation bytes, but you
> > can change it by using -s/--sort option.  The following sort keys are
> > added to support page analysis: page, order, mtype, gfp.  Existing
> > 'callsite', 'bytes' and 'hit' sort keys also can be used.
> >
> > An example follows:
> >
> >   # perf kmem record --slab --page sleep 1
> >   [ perf record: Woken up 0 times to write data ]
> >   [ perf record: Captured and wrote 49.277 MB perf.data (191027 samples) ]
> >
> >   # perf kmem stat --page --caller -l 10 -s order,hit
> >
> >   --------------------------------------------------------------------------------------------
> >    Total alloc (KB) | Hits      | Order | Migration type | GFP flags | Callsite
> >   --------------------------------------------------------------------------------------------
> >                  64 |         4 |     2 |    RECLAIMABLE |  00285250 | new_slab
> >              50,144 |    12,536 |     0 |        MOVABLE |  0102005a | __page_cache_alloc
> >                  52 |        13 |     0 |      UNMOVABLE |  002084d0 | pte_alloc_one
> >                  40 |        10 |     0 |        MOVABLE |  000280da | handle_mm_fault
> >                  28 |         7 |     0 |      UNMOVABLE |  000000d0 | __pollwait
> >                  20 |         5 |     0 |        MOVABLE |  000200da | do_wp_page
> >                  20 |         5 |     0 |        MOVABLE |  000200da | do_cow_fault
> >                  16 |         4 |     0 |      UNMOVABLE |  00000200 | __tlb_remove_page
> >                  16 |         4 |     0 |      UNMOVABLE |  000084d0 | __pmd_alloc
> >                   8 |         2 |     0 |      UNMOVABLE |  000084d0 | __pud_alloc
> >    ...              | ...       | ...   | ...            | ...       | ...
> >   --------------------------------------------------------------------------------------------
> 
> How about printing GFP flags more intuitively, for example,
> GFP_NOFS|GFP_ZERO? Tracepoint on mm_page_alloc already print
> output as this format.

That would be great, but it'd also require more column space.  It
already uses 105 characters per line and showing textual gfp flags
will increase it more.. :-/

Actually I'm thinking about 'perf report' style output - with full
callchain, selectable output field and interactive TUI/GUI browser in
the end.  With this change, I'll be able to feel comfortable to show
the textual gfp flags. ;)

Thanks,
Namhyung

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 2/5] perf kmem: Analyze page allocator events also
  2015-03-23 17:32   ` Joonsoo Kim
@ 2015-03-24  0:18     ` Namhyung Kim
  2015-03-24  5:26       ` Joonsoo Kim
  0 siblings, 1 reply; 19+ messages in thread
From: Namhyung Kim @ 2015-03-24  0:18 UTC (permalink / raw)
  To: Joonsoo Kim
  Cc: Arnaldo Carvalho de Melo, Ingo Molnar, Peter Zijlstra, Jiri Olsa,
	LKML, David Ahern, Minchan Kim

On Tue, Mar 24, 2015 at 02:32:17AM +0900, Joonsoo Kim wrote:
> 2015-03-23 15:30 GMT+09:00 Namhyung Kim <namhyung@kernel.org>:
> > The perf kmem command records and analyze kernel memory allocation
> > only for SLAB objects.  This patch implement a simple page allocator
> > analyzer using kmem:mm_page_alloc and kmem:mm_page_free events.
> >
> > It adds two new options of --slab and --page.  The --slab option is
> > for analyzing SLAB allocator and that's what perf kmem currently does.
> >
> > The new --page option enables page allocator events and analyze kernel
> > memory usage in page unit.  Currently, 'stat --alloc' subcommand is
> > implemented only.
> >
> > If none of these --slab nor --page is specified, --slab is implied.
> >
> >   # perf kmem stat --page --alloc --line 10
> >
> >   -------------------------------------------------------------------------------------
> >    Page             | Total alloc (KB) | Hits     | Order | Migration type | GFP flags
> >   -------------------------------------------------------------------------------------
> >    ffffea0015e48e00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >    ffffea0015e47400 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >    ffffea001440f600 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >    ffffea001440cc00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >    ffffea00140c6300 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >    ffffea00140c5c00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >    ffffea00140c5000 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >    ffffea00140c4f00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >    ffffea00140c4e00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >    ffffea00140c4d00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >    ...              | ...              | ...      | ...   | ...            | ...
> >   -------------------------------------------------------------------------------------
> 
> Tracepoint on mm_page_alloc print out pfn as well as pointer of struct page.
> How about printing pfn rather than pointer of struct page?

I'd really like to have pfn rather than struct page.  But I don't know
how to convert page pointer to pfn in userspace.

The output of tracepoint via $debugfs/tracing/trace file is generated
from kernel-side, so it can easily have pfn from page pointer.  But
tracepoint itself only saves page pointer and we need to convert/print
it in userspace.

Yes, perf script (or libtraceevent) shows pfn when printing those
events.  But that's bogus since it cannot determine the size of the
struct page so the pointer arithmetic in open-coded page_to_pfn()
which is saved in the print_fmt of the tracepoint will end up with an
normal integer arithmatic.

Do I miss something?

Thanks,
Namhyung

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 4/5] perf kmem: Support sort keys on page analysis
  2015-03-23 17:27   ` Joonsoo Kim
@ 2015-03-24  0:20     ` Namhyung Kim
  0 siblings, 0 replies; 19+ messages in thread
From: Namhyung Kim @ 2015-03-24  0:20 UTC (permalink / raw)
  To: Joonsoo Kim
  Cc: Arnaldo Carvalho de Melo, Ingo Molnar, Peter Zijlstra, Jiri Olsa,
	LKML, David Ahern, Minchan Kim

On Tue, Mar 24, 2015 at 02:27:09AM +0900, Joonsoo Kim wrote:
> 2015-03-23 15:30 GMT+09:00 Namhyung Kim <namhyung@kernel.org>:
> > Add new sort keys for page: page, order, mtype, gfp - existing
> > 'bytes', 'hit' and 'callsite' sort keys also work for page.  Note that
> > -s/--sort option should be preceded by either of --slab or --page
> > option to determine where the sort keys applies.
> >
> > Now it properly groups and sorts allocation stats - so same
> > page/caller with different order/mtype/gfp will be printed on a
> > different line.
> 
> How about storing/printing pid, too?
> It would be helpful to distinguish who allocates the page.

That's also in my TODO list. :)

Thanks,
Namhyung

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 2/5] perf kmem: Analyze page allocator events also
  2015-03-24  0:18     ` Namhyung Kim
@ 2015-03-24  5:26       ` Joonsoo Kim
  2015-03-24  6:05         ` Namhyung Kim
  2015-03-24  7:08         ` Ingo Molnar
  0 siblings, 2 replies; 19+ messages in thread
From: Joonsoo Kim @ 2015-03-24  5:26 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Arnaldo Carvalho de Melo, Ingo Molnar, Peter Zijlstra, Jiri Olsa,
	LKML, David Ahern, Minchan Kim

2015-03-24 9:18 GMT+09:00 Namhyung Kim <namhyung@kernel.org>:
> On Tue, Mar 24, 2015 at 02:32:17AM +0900, Joonsoo Kim wrote:
>> 2015-03-23 15:30 GMT+09:00 Namhyung Kim <namhyung@kernel.org>:
>> > The perf kmem command records and analyze kernel memory allocation
>> > only for SLAB objects.  This patch implement a simple page allocator
>> > analyzer using kmem:mm_page_alloc and kmem:mm_page_free events.
>> >
>> > It adds two new options of --slab and --page.  The --slab option is
>> > for analyzing SLAB allocator and that's what perf kmem currently does.
>> >
>> > The new --page option enables page allocator events and analyze kernel
>> > memory usage in page unit.  Currently, 'stat --alloc' subcommand is
>> > implemented only.
>> >
>> > If none of these --slab nor --page is specified, --slab is implied.
>> >
>> >   # perf kmem stat --page --alloc --line 10
>> >
>> >   -------------------------------------------------------------------------------------
>> >    Page             | Total alloc (KB) | Hits     | Order | Migration type | GFP flags
>> >   -------------------------------------------------------------------------------------
>> >    ffffea0015e48e00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
>> >    ffffea0015e47400 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
>> >    ffffea001440f600 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
>> >    ffffea001440cc00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
>> >    ffffea00140c6300 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
>> >    ffffea00140c5c00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
>> >    ffffea00140c5000 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
>> >    ffffea00140c4f00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
>> >    ffffea00140c4e00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
>> >    ffffea00140c4d00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
>> >    ...              | ...              | ...      | ...   | ...            | ...
>> >   -------------------------------------------------------------------------------------
>>
>> Tracepoint on mm_page_alloc print out pfn as well as pointer of struct page.
>> How about printing pfn rather than pointer of struct page?
>
> I'd really like to have pfn rather than struct page.  But I don't know
> how to convert page pointer to pfn in userspace.
>
> The output of tracepoint via $debugfs/tracing/trace file is generated
> from kernel-side, so it can easily have pfn from page pointer.  But
> tracepoint itself only saves page pointer and we need to convert/print
> it in userspace.

Ah...I didn't realize that perf don't use output of $debugfs/tracing/trace
file. So, perf just uses raw trace buffer directly? If pfn is saved to
the trace buffer, perf can print pfn rather than pointer of struct page?

> Yes, perf script (or libtraceevent) shows pfn when printing those
> events.  But that's bogus since it cannot determine the size of the
> struct page so the pointer arithmetic in open-coded page_to_pfn()
> which is saved in the print_fmt of the tracepoint will end up with an
> normal integer arithmatic.

How about following change and making 'perf kmem' print pfn?
If we store pfn on the trace buffer, we can print $debugfs/tracing/trace
as is and 'perf kmem' can also print pfn.

Thanks.

diff --git a/include/trace/events/kmem.h b/include/trace/events/kmem.h
index 4ad10ba..9dcfd0b 100644
--- a/include/trace/events/kmem.h
+++ b/include/trace/events/kmem.h
@@ -199,22 +199,22 @@ TRACE_EVENT(mm_page_alloc,
        TP_ARGS(page, order, gfp_flags, migratetype),

        TP_STRUCT__entry(
-               __field(        struct page *,  page            )
+               __field(        unsigned long,  pfn             )
                __field(        unsigned int,   order           )
                __field(        gfp_t,          gfp_flags       )
                __field(        int,            migratetype     )
        ),

        TP_fast_assign(
-               __entry->page           = page;
+               __entry->pfn            = page ? page_to_pfn(page) : -1;
                __entry->order          = order;
                __entry->gfp_flags      = gfp_flags;
                __entry->migratetype    = migratetype;
        ),

        TP_printk("page=%p pfn=%lu order=%d migratetype=%d gfp_flags=%s",
-               __entry->page,
-               __entry->page ? page_to_pfn(__entry->page) : 0,
+               __entry->pfn != -1 ? pfn_to_page(__entry->pfn) : NULL,
+               __entry->pfn != -1 ? __entry->pfn : 0,
                __entry->order,
                __entry->migratetype,
                show_gfp_flags(__entry->gfp_flags))

^ permalink raw reply related	[flat|nested] 19+ messages in thread

* Re: [PATCH 2/5] perf kmem: Analyze page allocator events also
  2015-03-24  5:26       ` Joonsoo Kim
@ 2015-03-24  6:05         ` Namhyung Kim
  2015-03-24  7:08         ` Ingo Molnar
  1 sibling, 0 replies; 19+ messages in thread
From: Namhyung Kim @ 2015-03-24  6:05 UTC (permalink / raw)
  To: Joonsoo Kim
  Cc: Arnaldo Carvalho de Melo, Ingo Molnar, Peter Zijlstra, Jiri Olsa,
	LKML, David Ahern, Minchan Kim

Hi Joonsoo,

On Tue, Mar 24, 2015 at 02:26:39PM +0900, Joonsoo Kim wrote:
> 2015-03-24 9:18 GMT+09:00 Namhyung Kim <namhyung@kernel.org>:
> > On Tue, Mar 24, 2015 at 02:32:17AM +0900, Joonsoo Kim wrote:
> >> 2015-03-23 15:30 GMT+09:00 Namhyung Kim <namhyung@kernel.org>:
> >> > The perf kmem command records and analyze kernel memory allocation
> >> > only for SLAB objects.  This patch implement a simple page allocator
> >> > analyzer using kmem:mm_page_alloc and kmem:mm_page_free events.
> >> >
> >> > It adds two new options of --slab and --page.  The --slab option is
> >> > for analyzing SLAB allocator and that's what perf kmem currently does.
> >> >
> >> > The new --page option enables page allocator events and analyze kernel
> >> > memory usage in page unit.  Currently, 'stat --alloc' subcommand is
> >> > implemented only.
> >> >
> >> > If none of these --slab nor --page is specified, --slab is implied.
> >> >
> >> >   # perf kmem stat --page --alloc --line 10
> >> >
> >> >   -------------------------------------------------------------------------------------
> >> >    Page             | Total alloc (KB) | Hits     | Order | Migration type | GFP flags
> >> >   -------------------------------------------------------------------------------------
> >> >    ffffea0015e48e00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >> >    ffffea0015e47400 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >> >    ffffea001440f600 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >> >    ffffea001440cc00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >> >    ffffea00140c6300 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >> >    ffffea00140c5c00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >> >    ffffea00140c5000 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >> >    ffffea00140c4f00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >> >    ffffea00140c4e00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >> >    ffffea00140c4d00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >> >    ...              | ...              | ...      | ...   | ...            | ...
> >> >   -------------------------------------------------------------------------------------
> >>
> >> Tracepoint on mm_page_alloc print out pfn as well as pointer of struct page.
> >> How about printing pfn rather than pointer of struct page?
> >
> > I'd really like to have pfn rather than struct page.  But I don't know
> > how to convert page pointer to pfn in userspace.
> >
> > The output of tracepoint via $debugfs/tracing/trace file is generated
> > from kernel-side, so it can easily have pfn from page pointer.  But
> > tracepoint itself only saves page pointer and we need to convert/print
> > it in userspace.
> 
> Ah...I didn't realize that perf don't use output of $debugfs/tracing/trace
> file. So, perf just uses raw trace buffer directly? If pfn is saved to
> the trace buffer, perf can print pfn rather than pointer of struct page?

Yes, perf uses raw (binary) trace data..  If the tracepoint saves pfn
directly it would be nice!


> 
> > Yes, perf script (or libtraceevent) shows pfn when printing those
> > events.  But that's bogus since it cannot determine the size of the
> > struct page so the pointer arithmetic in open-coded page_to_pfn()
> > which is saved in the print_fmt of the tracepoint will end up with an
> > normal integer arithmatic.
> 
> How about following change and making 'perf kmem' print pfn?
> If we store pfn on the trace buffer, we can print $debugfs/tracing/trace
> as is and 'perf kmem' can also print pfn.

I'm very happy with this change.  The textual output via
debugfs/tracefs will be same, binary data will be changed but
libtraceevent will handle it seamlessly.

Thanks,
Namhyung


> 
> diff --git a/include/trace/events/kmem.h b/include/trace/events/kmem.h
> index 4ad10ba..9dcfd0b 100644
> --- a/include/trace/events/kmem.h
> +++ b/include/trace/events/kmem.h
> @@ -199,22 +199,22 @@ TRACE_EVENT(mm_page_alloc,
>         TP_ARGS(page, order, gfp_flags, migratetype),
> 
>         TP_STRUCT__entry(
> -               __field(        struct page *,  page            )
> +               __field(        unsigned long,  pfn             )
>                 __field(        unsigned int,   order           )
>                 __field(        gfp_t,          gfp_flags       )
>                 __field(        int,            migratetype     )
>         ),
> 
>         TP_fast_assign(
> -               __entry->page           = page;
> +               __entry->pfn            = page ? page_to_pfn(page) : -1;
>                 __entry->order          = order;
>                 __entry->gfp_flags      = gfp_flags;
>                 __entry->migratetype    = migratetype;
>         ),
> 
>         TP_printk("page=%p pfn=%lu order=%d migratetype=%d gfp_flags=%s",
> -               __entry->page,
> -               __entry->page ? page_to_pfn(__entry->page) : 0,
> +               __entry->pfn != -1 ? pfn_to_page(__entry->pfn) : NULL,
> +               __entry->pfn != -1 ? __entry->pfn : 0,
>                 __entry->order,
>                 __entry->migratetype,
>                 show_gfp_flags(__entry->gfp_flags))
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 2/5] perf kmem: Analyze page allocator events also
  2015-03-24  5:26       ` Joonsoo Kim
  2015-03-24  6:05         ` Namhyung Kim
@ 2015-03-24  7:08         ` Ingo Molnar
  2015-03-24 13:17           ` Namhyung Kim
  1 sibling, 1 reply; 19+ messages in thread
From: Ingo Molnar @ 2015-03-24  7:08 UTC (permalink / raw)
  To: Joonsoo Kim
  Cc: Namhyung Kim, Arnaldo Carvalho de Melo, Peter Zijlstra,
	Jiri Olsa, LKML, David Ahern, Minchan Kim


* Joonsoo Kim <js1304@gmail.com> wrote:

> 2015-03-24 9:18 GMT+09:00 Namhyung Kim <namhyung@kernel.org>:
> > On Tue, Mar 24, 2015 at 02:32:17AM +0900, Joonsoo Kim wrote:
> >> 2015-03-23 15:30 GMT+09:00 Namhyung Kim <namhyung@kernel.org>:
> >> > The perf kmem command records and analyze kernel memory allocation
> >> > only for SLAB objects.  This patch implement a simple page allocator
> >> > analyzer using kmem:mm_page_alloc and kmem:mm_page_free events.
> >> >
> >> > It adds two new options of --slab and --page.  The --slab option is
> >> > for analyzing SLAB allocator and that's what perf kmem currently does.
> >> >
> >> > The new --page option enables page allocator events and analyze kernel
> >> > memory usage in page unit.  Currently, 'stat --alloc' subcommand is
> >> > implemented only.
> >> >
> >> > If none of these --slab nor --page is specified, --slab is implied.
> >> >
> >> >   # perf kmem stat --page --alloc --line 10
> >> >
> >> >   -------------------------------------------------------------------------------------
> >> >    Page             | Total alloc (KB) | Hits     | Order | Migration type | GFP flags
> >> >   -------------------------------------------------------------------------------------
> >> >    ffffea0015e48e00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >> >    ffffea0015e47400 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >> >    ffffea001440f600 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >> >    ffffea001440cc00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >> >    ffffea00140c6300 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >> >    ffffea00140c5c00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >> >    ffffea00140c5000 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >> >    ffffea00140c4f00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >> >    ffffea00140c4e00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >> >    ffffea00140c4d00 |               16 |        1 |     2 |    RECLAIMABLE |  00285250
> >> >    ...              | ...              | ...      | ...   | ...            | ...
> >> >   -------------------------------------------------------------------------------------
> >>
> >> Tracepoint on mm_page_alloc print out pfn as well as pointer of struct page.
> >> How about printing pfn rather than pointer of struct page?
> >
> > I'd really like to have pfn rather than struct page.  But I don't know
> > how to convert page pointer to pfn in userspace.
> >
> > The output of tracepoint via $debugfs/tracing/trace file is generated
> > from kernel-side, so it can easily have pfn from page pointer.  But
> > tracepoint itself only saves page pointer and we need to convert/print
> > it in userspace.
> 
> Ah...I didn't realize that perf don't use output of $debugfs/tracing/trace
> file. So, perf just uses raw trace buffer directly? If pfn is saved to
> the trace buffer, perf can print pfn rather than pointer of struct page?
> 
> > Yes, perf script (or libtraceevent) shows pfn when printing those
> > events.  But that's bogus since it cannot determine the size of the
> > struct page so the pointer arithmetic in open-coded page_to_pfn()
> > which is saved in the print_fmt of the tracepoint will end up with an
> > normal integer arithmatic.
> 
> How about following change and making 'perf kmem' print pfn?
> If we store pfn on the trace buffer, we can print $debugfs/tracing/trace
> as is and 'perf kmem' can also print pfn.
> 
> Thanks.
> 
> diff --git a/include/trace/events/kmem.h b/include/trace/events/kmem.h
> index 4ad10ba..9dcfd0b 100644
> --- a/include/trace/events/kmem.h
> +++ b/include/trace/events/kmem.h
> @@ -199,22 +199,22 @@ TRACE_EVENT(mm_page_alloc,
>         TP_ARGS(page, order, gfp_flags, migratetype),
> 
>         TP_STRUCT__entry(
> -               __field(        struct page *,  page            )
> +               __field(        unsigned long,  pfn             )
>                 __field(        unsigned int,   order           )
>                 __field(        gfp_t,          gfp_flags       )
>                 __field(        int,            migratetype     )
>         ),
> 
>         TP_fast_assign(
> -               __entry->page           = page;
> +               __entry->pfn            = page ? page_to_pfn(page) : -1;
>                 __entry->order          = order;
>                 __entry->gfp_flags      = gfp_flags;
>                 __entry->migratetype    = migratetype;
>         ),
> 
>         TP_printk("page=%p pfn=%lu order=%d migratetype=%d gfp_flags=%s",
> -               __entry->page,
> -               __entry->page ? page_to_pfn(__entry->page) : 0,
> +               __entry->pfn != -1 ? pfn_to_page(__entry->pfn) : NULL,
> +               __entry->pfn != -1 ? __entry->pfn : 0,
>                 __entry->order,
>                 __entry->migratetype,
>                 show_gfp_flags(__entry->gfp_flags))

Acked-by: Ingo Molnar <mingo@kernel.org>

It would be very nice to make all the other page granular tracepoints 
output pfn (which is a physical address that can be resolved to 'node' 
and other properties), not 'struct page *' (which is a kernel resource 
with little meaning to user-space tooling).

I.e. the following tracepoints:

triton:~/tip> git grep -E '__field.*struct page *' include/trace/
include/trace/events/filemap.h:         __field(struct page *, page)
include/trace/events/kmem.h:            __field(        struct page *,  page            )
include/trace/events/kmem.h:            __field(        struct page *,  page            )
include/trace/events/kmem.h:            __field(        struct page *,  page            )
include/trace/events/kmem.h:            __field(        struct page *,  page            )
include/trace/events/kmem.h:            __field(        struct page *,  page                    )
include/trace/events/pagemap.h:         __field(struct page *,  page    )
include/trace/events/pagemap.h:         __field(struct page *,  page    )
include/trace/events/vmscan.h:          __field(struct page *, page)

there's very little breakage I can imagine: they have traced pointers 
to 'struct page', which is a pretty opaque page identifier to 
user-space, and they'll trace pfn's in the future, which still serves 
as a page identifier.

One thing would be important: to do all these changes at once, to make 
sure that the various page identifiers can be compared.

Also, we might keep the 'page' field name if anything relies on that - 
but 'pfn' is even better.

Thanks,

	Ingo

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 2/5] perf kmem: Analyze page allocator events also
  2015-03-24  7:08         ` Ingo Molnar
@ 2015-03-24 13:17           ` Namhyung Kim
  0 siblings, 0 replies; 19+ messages in thread
From: Namhyung Kim @ 2015-03-24 13:17 UTC (permalink / raw)
  To: Ingo Molnar
  Cc: Joonsoo Kim, Arnaldo Carvalho de Melo, Peter Zijlstra, Jiri Olsa,
	LKML, David Ahern, Minchan Kim

Hi Ingo,

On Tue, Mar 24, 2015 at 08:08:03AM +0100, Ingo Molnar wrote:
> * Joonsoo Kim <js1304@gmail.com> wrote:
> > How about following change and making 'perf kmem' print pfn?
> > If we store pfn on the trace buffer, we can print $debugfs/tracing/trace
> > as is and 'perf kmem' can also print pfn.
> > 
> > Thanks.
> > 
> > diff --git a/include/trace/events/kmem.h b/include/trace/events/kmem.h
> > index 4ad10ba..9dcfd0b 100644
> > --- a/include/trace/events/kmem.h
> > +++ b/include/trace/events/kmem.h
> > @@ -199,22 +199,22 @@ TRACE_EVENT(mm_page_alloc,
> >         TP_ARGS(page, order, gfp_flags, migratetype),
> > 
> >         TP_STRUCT__entry(
> > -               __field(        struct page *,  page            )
> > +               __field(        unsigned long,  pfn             )
> >                 __field(        unsigned int,   order           )
> >                 __field(        gfp_t,          gfp_flags       )
> >                 __field(        int,            migratetype     )
> >         ),
> > 
> >         TP_fast_assign(
> > -               __entry->page           = page;
> > +               __entry->pfn            = page ? page_to_pfn(page) : -1;
> >                 __entry->order          = order;
> >                 __entry->gfp_flags      = gfp_flags;
> >                 __entry->migratetype    = migratetype;
> >         ),
> > 
> >         TP_printk("page=%p pfn=%lu order=%d migratetype=%d gfp_flags=%s",
> > -               __entry->page,
> > -               __entry->page ? page_to_pfn(__entry->page) : 0,
> > +               __entry->pfn != -1 ? pfn_to_page(__entry->pfn) : NULL,
> > +               __entry->pfn != -1 ? __entry->pfn : 0,
> >                 __entry->order,
> >                 __entry->migratetype,
> >                 show_gfp_flags(__entry->gfp_flags))
> 
> Acked-by: Ingo Molnar <mingo@kernel.org>
> 
> It would be very nice to make all the other page granular tracepoints 
> output pfn (which is a physical address that can be resolved to 'node' 
> and other properties), not 'struct page *' (which is a kernel resource 
> with little meaning to user-space tooling).
> 
> I.e. the following tracepoints:
> 
> triton:~/tip> git grep -E '__field.*struct page *' include/trace/
> include/trace/events/filemap.h:         __field(struct page *, page)
> include/trace/events/kmem.h:            __field(        struct page *,  page            )
> include/trace/events/kmem.h:            __field(        struct page *,  page            )
> include/trace/events/kmem.h:            __field(        struct page *,  page            )
> include/trace/events/kmem.h:            __field(        struct page *,  page            )
> include/trace/events/kmem.h:            __field(        struct page *,  page                    )
> include/trace/events/pagemap.h:         __field(struct page *,  page    )
> include/trace/events/pagemap.h:         __field(struct page *,  page    )
> include/trace/events/vmscan.h:          __field(struct page *, page)

Okay, will do.

> 
> there's very little breakage I can imagine: they have traced pointers 
> to 'struct page', which is a pretty opaque page identifier to 
> user-space, and they'll trace pfn's in the future, which still serves 
> as a page identifier.

Agreed.

> 
> One thing would be important: to do all these changes at once, to make 
> sure that the various page identifiers can be compared.

OK

> 
> Also, we might keep the 'page' field name if anything relies on that - 
> but 'pfn' is even better.

Another option is to keep page field and add new pfn field.  The
events in pagemap.h already do this way.  This will minimize the
possible breakage but increase the trace size somewhat.

Thanks,
Namhyung

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [tip:perf/core] perf kmem: Print big numbers using thousands' group
  2015-03-23  6:30 ` [PATCH 1/5] perf kmem: Print big numbers using thousands' group Namhyung Kim
  2015-03-23 14:08   ` Arnaldo Carvalho de Melo
@ 2015-03-24 16:31   ` tip-bot for Namhyung Kim
  1 sibling, 0 replies; 19+ messages in thread
From: tip-bot for Namhyung Kim @ 2015-03-24 16:31 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: hpa, js1304, tglx, minchan, linux-kernel, jolsa, namhyung, acme,
	mingo, dsahern, a.p.zijlstra

Commit-ID:  77cfe388767572586b7d4fd533c38f902d020f17
Gitweb:     http://git.kernel.org/tip/77cfe388767572586b7d4fd533c38f902d020f17
Author:     Namhyung Kim <namhyung@kernel.org>
AuthorDate: Mon, 23 Mar 2015 15:30:40 +0900
Committer:  Arnaldo Carvalho de Melo <acme@redhat.com>
CommitDate: Tue, 24 Mar 2015 12:07:06 -0300

perf kmem: Print big numbers using thousands' group

Like perf stat, this makes easy to read the numbers on stat like below:

  # perf kmem stat

  SUMMARY
  =======
  Total bytes requested: 9,770,900
  Total bytes allocated: 9,782,712
  Total bytes wasted on internal fragmentation: 11,812
  Internal fragmentation: 0.120744%
  Cross CPU allocations: 74/152,819

Suggested-by: Ingo Molnar <mingo@kernel.org>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Joonsoo Kim <js1304@gmail.com>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Link: http://lkml.kernel.org/r/1427092244-22764-2-git-send-email-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/builtin-kmem.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c
index 8c85aeb..64d3623 100644
--- a/tools/perf/builtin-kmem.c
+++ b/tools/perf/builtin-kmem.c
@@ -20,6 +20,7 @@
 
 #include <linux/rbtree.h>
 #include <linux/string.h>
+#include <locale.h>
 
 struct alloc_stat;
 typedef int (*sort_fn_t)(struct alloc_stat *, struct alloc_stat *);
@@ -325,13 +326,13 @@ static void __print_result(struct rb_root *root, struct perf_session *session,
 static void print_summary(void)
 {
 	printf("\nSUMMARY\n=======\n");
-	printf("Total bytes requested: %lu\n", total_requested);
-	printf("Total bytes allocated: %lu\n", total_allocated);
-	printf("Total bytes wasted on internal fragmentation: %lu\n",
+	printf("Total bytes requested: %'lu\n", total_requested);
+	printf("Total bytes allocated: %'lu\n", total_allocated);
+	printf("Total bytes wasted on internal fragmentation: %'lu\n",
 	       total_allocated - total_requested);
 	printf("Internal fragmentation: %f%%\n",
 	       fragmentation(total_requested, total_allocated));
-	printf("Cross CPU allocations: %lu/%lu\n", nr_cross_allocs, nr_allocs);
+	printf("Cross CPU allocations: %'lu/%'lu\n", nr_cross_allocs, nr_allocs);
 }
 
 static void print_result(struct perf_session *session)
@@ -706,6 +707,8 @@ int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
 	symbol__init(&session->header.env);
 
 	if (!strcmp(argv[0], "stat")) {
+		setlocale(LC_ALL, "");
+
 		if (cpu__setup_cpunode_map())
 			goto out_delete;
 

^ permalink raw reply related	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2015-03-24 16:32 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-23  6:30 [PATCHSET 0/5] perf kmem: Implement page allocation analysis (v3) Namhyung Kim
2015-03-23  6:30 ` [PATCH 1/5] perf kmem: Print big numbers using thousands' group Namhyung Kim
2015-03-23 14:08   ` Arnaldo Carvalho de Melo
2015-03-23 23:35     ` Namhyung Kim
2015-03-24 16:31   ` [tip:perf/core] " tip-bot for Namhyung Kim
2015-03-23  6:30 ` [PATCH 2/5] perf kmem: Analyze page allocator events also Namhyung Kim
2015-03-23 17:32   ` Joonsoo Kim
2015-03-24  0:18     ` Namhyung Kim
2015-03-24  5:26       ` Joonsoo Kim
2015-03-24  6:05         ` Namhyung Kim
2015-03-24  7:08         ` Ingo Molnar
2015-03-24 13:17           ` Namhyung Kim
2015-03-23  6:30 ` [PATCH 3/5] perf kmem: Implement stat --page --caller Namhyung Kim
2015-03-23  6:30 ` [PATCH 4/5] perf kmem: Support sort keys on page analysis Namhyung Kim
2015-03-23 17:27   ` Joonsoo Kim
2015-03-24  0:20     ` Namhyung Kim
2015-03-23  6:30 ` [PATCH 5/5] perf kmem: Add --live option for current allocation stat Namhyung Kim
2015-03-23 17:23 ` [PATCHSET 0/5] perf kmem: Implement page allocation analysis (v3) Joonsoo Kim
2015-03-23 23:57   ` Namhyung Kim

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.