Skip to contentSkip to conversation

perf report: Recognize hugetlb mapping as anon mapping

PATCH Superseded Wang Nan 3 participants 9 messages Sep 2, 2016 – Sep 6, 2016 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Wang Nan

author
#0 lore raw

cover perf report: Recognize hugetlb mapping as anon mapping

The requirement of this function is first proposed at 2015. Please refer to

http://lkml.iu.edu/hypermail/linux/kernel/1506.2/02372.html
http://lkml.iu.edu/hypermail/linux/kernel/1506.3/02290.html
http://lkml.iu.edu/hypermail/linux/kernel/1506.3/03512.html

For systems which use hugetlbfs, if a sample is captured inside hugetlbfs, perf should not resolve symbols from the file shown in /prof/<pid>/mmap, because 'files' in hugetlbfs are constructed during execution and not ELF files. If user knows positions of symbols, he/she should provide /tmp/perf-<pid>.map file.

This 3 patches makes perf recognize hugetlbfs mapping as anon mapping. Before this 3 patches user has no chance to use his/her own .map file to resolve symbols because perf tries to use hugetlbfs file.

Wang Nan (3):
perf tools: Recognize hugetlb mapping as anon mapping tools lib api fs: Add hugetlbfs filesystem detector perf record: Mark MAP_HUGETLB during synthesizing mmap events

 tools/lib/api/fs/fs.c   | 15 +++++++++++++++
 tools/lib/api/fs/fs.h   |  1 +
 tools/perf/util/event.c |  7 +++++++
 tools/perf/util/map.c   |  8 +++++---
 4 files changed, 28 insertions(+), 3 deletions(-)
Cc Hou Pengyang <[email protected]>
Cc He Kuang <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Signature · 1 line
1.8.3.4
author
#1 lore raw

PATCH 1/3 perf tools: Recognize hugetlb mapping as anon mapping

Hugetlbfs mapping should be recognized as anon mapping so user has a chance to create /tmp/perf-<pid>.map file for symbol resolving. This patch utilizes MAP_HUGETLB to identify hugetlb mapping.

After this patch, if perf is started before the program uses huge pages starting, perf is able to recognize hugetlb mapping as anon mapping.

Cc He Kuang <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
---
 tools/perf/util/map.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/perf/util/map.c +5 -3
index 728129a..a42010d 100644
@@ -6,6 +6,7 @@
 #include <string.h>
 #include <stdio.h>
 #include <unistd.h>
+#include <sys/mman.h>
 #include "map.h"
 #include "thread.h"
 #include "strlist.h"
@@ -24,9 +25,10 @@ const char *map_type__name[MAP__NR_TYPES] = {
 	[MAP__VARIABLE] = "Variables",
 };
 
-static inline int is_anon_memory(const char *filename)
+static inline int is_anon_memory(const char *filename, u32 flags)
 {
-	return !strcmp(filename, "//anon") ||
+	return flags & MAP_HUGETLB ||
+	       !strcmp(filename, "//anon") ||
 	       !strncmp(filename, "/dev/zero", sizeof("/dev/zero") - 1) ||
 	       !strncmp(filename, "/anon_hugepage", sizeof("/anon_hugepage") - 1);
 }
@@ -155,7 +157,7 @@ struct map *map__new(struct machine *machine, u64 start, u64 len,
 		int anon, no_dso, vdso, android;
 
 		android = is_android_lib(filename);
-		anon = is_anon_memory(filename);
+		anon = is_anon_memory(filename, flags);
 		vdso = is_vdso_map(filename);
 		no_dso = is_no_dso_memory(filename);
 
Signature · 1 line
1.8.3.4
#2 lore raw

Re: [PATCH 1/3] perf tools: Recognize hugetlb mapping as anon mapping

Quoted 6 lines · Wang Nan

Hugetlbfs mapping should be recognized as anon mapping so user has a chance to create /tmp/perf-<pid>.map file for symbol resolving. This patch utilizes MAP_HUGETLB to identify hugetlb mapping.

After this patch, if perf is started before the program uses huge pages starting, perf is able to recognize hugetlb mapping as anon mapping.

Wang, everything in the patch series looks good to me. Just a slight change that I would suggest for the summary above: replace "program uses huge pages starting" with "program starts using huge pages".

--
Nilay

author
#3 lore raw

PATCH 1/3 perf tools: Recognize hugetlb mapping as anon mapping

Hugetlbfs mapping should be recognized as anon mapping so user has a chance to create /tmp/perf-<pid>.map file for symbol resolving. This patch utilizes MAP_HUGETLB to identify hugetlb mapping.

After this patch, if perf is started before the program starts using huge pages (so perf gets MMAP2 events from kernel), perf is able to recognize hugetlb mapping as anon mapping.

Cc He Kuang <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Nilay Vaish <[email protected]>
---
 tools/perf/util/map.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/perf/util/map.c +5 -3
index 728129a..a42010d 100644
@@ -6,6 +6,7 @@
 #include <string.h>
 #include <stdio.h>
 #include <unistd.h>
+#include <sys/mman.h>
 #include "map.h"
 #include "thread.h"
 #include "strlist.h"
@@ -24,9 +25,10 @@ const char *map_type__name[MAP__NR_TYPES] = {
 	[MAP__VARIABLE] = "Variables",
 };
 
-static inline int is_anon_memory(const char *filename)
+static inline int is_anon_memory(const char *filename, u32 flags)
 {
-	return !strcmp(filename, "//anon") ||
+	return flags & MAP_HUGETLB ||
+	       !strcmp(filename, "//anon") ||
 	       !strncmp(filename, "/dev/zero", sizeof("/dev/zero") - 1) ||
 	       !strncmp(filename, "/anon_hugepage", sizeof("/anon_hugepage") - 1);
 }
@@ -155,7 +157,7 @@ struct map *map__new(struct machine *machine, u64 start, u64 len,
 		int anon, no_dso, vdso, android;
 
 		android = is_android_lib(filename);
-		anon = is_anon_memory(filename);
+		anon = is_anon_memory(filename, flags);
 		vdso = is_vdso_map(filename);
 		no_dso = is_no_dso_memory(filename);
 
Signature · 1 line
1.8.3.4
#4 lore raw

Re: [PATCH 1/3 (fix commit message)] perf tools: Recognize hugetlb mapping as anon mapping

Em Sun, Sep 04, 2016 at 05:04:38AM +0000, Wang Nan escreveu:

Quoted 7 lines

Hugetlbfs mapping should be recognized as anon mapping so user has a chance to create /tmp/perf-<pid>.map file for symbol resolving. This patch utilizes MAP_HUGETLB to identify hugetlb mapping.

After this patch, if perf is started before the program starts using huge pages (so perf gets MMAP2 events from kernel), perf is able to recognize hugetlb mapping as anon mapping.

Fixing build on older distros (Centos5, Ubuntu 12.04.5, etc)

CC /tmp/build/perf/arch/x86/util/perf_regs.o
util/event.c: In function 'perf_event__synthesize_mmap_events':
util/event.c:350: error: 'MAP_HUGETLB' undeclared (first use in this function) util/event.c:350: error: (Each undeclared identifier is reported only once util/event.c:350: error: for each function it appears in.)

Quoted 45 lines
Cc He Kuang <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Nilay Vaish <[email protected]>
---
 tools/perf/util/map.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/perf/util/map.c +5 -3
index 728129a..a42010d 100644
@@ -6,6 +6,7 @@
 #include <string.h>
 #include <stdio.h>
 #include <unistd.h>
+#include <sys/mman.h>
 #include "map.h"
 #include "thread.h"
 #include "strlist.h"
@@ -24,9 +25,10 @@ const char *map_type__name[MAP__NR_TYPES] = {
 	[MAP__VARIABLE] = "Variables",
 };
 
-static inline int is_anon_memory(const char *filename)
+static inline int is_anon_memory(const char *filename, u32 flags)
 {
-	return !strcmp(filename, "//anon") ||
+	return flags & MAP_HUGETLB ||
+	       !strcmp(filename, "//anon") ||
 	       !strncmp(filename, "/dev/zero", sizeof("/dev/zero") - 1) ||
 	       !strncmp(filename, "/anon_hugepage", sizeof("/anon_hugepage") - 1);
 }
@@ -155,7 +157,7 @@ struct map *map__new(struct machine *machine, u64 start, u64 len,
 		int anon, no_dso, vdso, android;
 
 		android = is_android_lib(filename);
-		anon = is_anon_memory(filename);
+		anon = is_anon_memory(filename, flags);
 		vdso = is_vdso_map(filename);
 		no_dso = is_no_dso_memory(filename);
 
Signature · 1 line
1.8.3.4
#5 lore raw

Re: [PATCH 1/3 (fix commit message)] perf tools: Recognize hugetlb mapping as anon mapping

Quoted 8 lines · Arnaldo Carvalho de Melo

Em Sun, Sep 04, 2016 at 05:04:38AM +0000, Wang Nan escreveu:

Quoted 7 lines

Hugetlbfs mapping should be recognized as anon mapping so user has a chance to create /tmp/perf-<pid>.map file for symbol resolving. This patch utilizes MAP_HUGETLB to identify hugetlb mapping.

After this patch, if perf is started before the program starts using huge pages (so perf gets MMAP2 events from kernel), perf is able to recognize hugetlb mapping as anon mapping.

Quoted 10 lines

Fixing build on older distros (Centos5, Ubuntu 12.04.5, etc)

CC /tmp/build/perf/arch/x86/util/perf_regs.o
util/event.c: In function 'perf_event__synthesize_mmap_events':
util/event.c:350: error: 'MAP_HUGETLB' undeclared (first use in this function) util/event.c:350: error: (Each undeclared identifier is reported only once util/event.c:350: error: for each function it appears in.)

Fixing MAP_HUGETLB is not as easy as it seems to be. Its value is different in each arch:

./arch/powerpc/include/uapi/asm/mman.h:#define MAP_HUGETLB 0x40000

    /* create a huge page mapping */

./arch/alpha/include/uapi/asm/mman.h:#define MAP_HUGETLB 0x100000 /* create a huge page mapping */
./arch/xtensa/include/uapi/asm/mman.h:#define MAP_HUGETLB 0x80000 /* create a huge page mapping */
./arch/tile/include/uapi/asm/mman.h:#define MAP_HUGETLB 0x4000 /* create a huge page mapping */
./arch/parisc/include/uapi/asm/mman.h:#define MAP_HUGETLB 0x80000 /* create a huge page mapping */
./arch/sparc/include/uapi/asm/mman.h:#define MAP_HUGETLB 0x40000 /* create a huge page mapping */
./arch/mips/include/uapi/asm/mman.h:#define MAP_HUGETLB 0x80000 /* create a huge page mapping */
./include/uapi/asm-generic/mman.h:#define MAP_HUGETLB 0x40000 /* create a huge page mapping */

So we need to introduce another header into tools for this macro. Sigh...

Thank you.

Quoted 45 lines
Quoted 45 lines
Cc He Kuang <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Nilay Vaish <[email protected]>
---
  tools/perf/util/map.c | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
tools/perf/util/map.c +5 -3
index 728129a..a42010d 100644
@@ -6,6 +6,7 @@
  #include <string.h>
  #include <stdio.h>
  #include <unistd.h>
+#include <sys/mman.h>
  #include "map.h"
  #include "thread.h"
  #include "strlist.h"
@@ -24,9 +25,10 @@ const char *map_type__name[MAP__NR_TYPES] = {
  	[MAP__VARIABLE] = "Variables",
  };
 
-static inline int is_anon_memory(const char *filename)
+static inline int is_anon_memory(const char *filename, u32 flags)
  {
-	return !strcmp(filename, "//anon") ||
+	return flags & MAP_HUGETLB ||
+	       !strcmp(filename, "//anon") ||
  	       !strncmp(filename, "/dev/zero", sizeof("/dev/zero") - 1) ||
  	       !strncmp(filename, "/anon_hugepage", sizeof("/anon_hugepage") - 1);
  }
@@ -155,7 +157,7 @@ struct map *map__new(struct machine *machine, u64 start, u64 len,
  		int anon, no_dso, vdso, android;
 
  		android = is_android_lib(filename);
-		anon = is_anon_memory(filename);
+		anon = is_anon_memory(filename, flags);
  		vdso = is_vdso_map(filename);
  		no_dso = is_no_dso_memory(filename);
 
Signature · 1 line
1.8.3.4
author
#6 lore raw

PATCH 2/3 tools lib api fs: Add hugetlbfs filesystem detector

In reply to Wang Nan

Detect hugetlbfs. hugetlbfs__mountpoint() will be used during recording to help recorder identifying hugetlb mmaps: which should be recognized as anon mapping.

Cc Hou Pengyang <[email protected]>
Cc He Kuang <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
---
 tools/lib/api/fs/fs.c | 15 +++++++++++++++
 tools/lib/api/fs/fs.h |  1 +
 2 files changed, 16 insertions(+)
tools/lib/api/fs/fs.c +15 -0
index ba7094b..f99f49e4 100644
@@ -34,6 +34,10 @@
 #define TRACEFS_MAGIC          0x74726163
 #endif
 
+#ifndef HUGETLBFS_MAGIC
+#define HUGETLBFS_MAGIC        0x958458f6
+#endif
+
 static const char * const sysfs__fs_known_mountpoints[] = {
 	"/sys",
 	0,
@@ -67,6 +71,10 @@ static const char * const tracefs__known_mountpoints[] = {
 	0,
 };
 
+static const char * const hugetlbfs__known_mountpoints[] = {
+	0,
+};
+
 struct fs {
 	const char		*name;
 	const char * const	*mounts;
@@ -80,6 +88,7 @@ enum {
 	FS__PROCFS  = 1,
 	FS__DEBUGFS = 2,
 	FS__TRACEFS = 3,
+	FS__HUGETLBFS = 4,
 };
 
 #ifndef TRACEFS_MAGIC
@@ -107,6 +116,11 @@ static struct fs fs__entries[] = {
 		.mounts	= tracefs__known_mountpoints,
 		.magic	= TRACEFS_MAGIC,
 	},
+	[FS__HUGETLBFS] = {
+		.name	= "hugetlbfs",
+		.mounts = hugetlbfs__known_mountpoints,
+		.magic	= HUGETLBFS_MAGIC,
+	},
 };
 
 static bool fs__read_mounts(struct fs *fs)
@@ -265,6 +279,7 @@ FS(sysfs,   FS__SYSFS);
 FS(procfs,  FS__PROCFS);
 FS(debugfs, FS__DEBUGFS);
 FS(tracefs, FS__TRACEFS);
+FS(hugetlbfs, FS__HUGETLBFS);
 
 int filename__read_int(const char *filename, int *value)
 {
tools/lib/api/fs/fs.h +1 -0
index 16c9c2e..a63269f 100644
@@ -21,6 +21,7 @@ FS(sysfs)
 FS(procfs)
 FS(debugfs)
 FS(tracefs)
+FS(hugetlbfs)
 
 #undef FS
 
Signature · 1 line
1.8.3.4
author
#7 lore raw

PATCH 3/3 perf record: Mark MAP_HUGETLB during synthesizing mmap events

In reply to Wang Nan

During synthesizing mmap events, add MAP_HUGETLB map flag if the source of mapping is file in hugetlbfs.

After this patch, perf can identify hugetlb mapping even if perf is started after the mapping of huge pages (like perf top).

Cc Hou Pengyang <[email protected]>
Cc He Kuang <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
---
 tools/perf/util/event.c | 7 +++++++
 1 file changed, 7 insertions(+)
tools/perf/util/event.c +7 -0
index e20438b..4c2abdf 100644
@@ -1,5 +1,6 @@
 #include <linux/types.h>
 #include <sys/mman.h>
+#include <api/fs/fs.h>
 #include "event.h"
 #include "debug.h"
 #include "hist.h"
@@ -248,6 +249,8 @@ int perf_event__synthesize_mmap_events(struct perf_tool *tool,
 	bool truncation = false;
 	unsigned long long timeout = proc_map_timeout * 1000000ULL;
 	int rc = 0;
+	const char *hugetlbfs_mnt = hugetlbfs__mountpoint();
+	int hugetlbfs_mnt_len = hugetlbfs_mnt ? strlen(hugetlbfs_mnt) : 0;
 
 	if (machine__is_default_guest(machine))
 		return 0;
@@ -342,6 +345,10 @@ out:
 
 		if (!strcmp(execname, ""))
 			strcpy(execname, anonstr);
+		if (!strncmp(execname, hugetlbfs_mnt, hugetlbfs_mnt_len)) {
+			strcpy(execname, anonstr);
+			event->mmap2.flags |= MAP_HUGETLB;
+		}
 
 		size = strlen(execname) + 1;
 		memcpy(event->mmap2.filename, execname, size);
Signature · 1 line
1.8.3.4
Reviewed-by on the v1 series
#8 lore raw
In reply to Wang Nan
Quoted 28 lines · Wang Nan

The requirement of this function is first proposed at 2015. Please refer to

http://lkml.iu.edu/hypermail/linux/kernel/1506.2/02372.html
http://lkml.iu.edu/hypermail/linux/kernel/1506.3/02290.html
http://lkml.iu.edu/hypermail/linux/kernel/1506.3/03512.html

For systems which use hugetlbfs, if a sample is captured inside hugetlbfs, perf should not resolve symbols from the file shown in /prof/<pid>/mmap, because 'files' in hugetlbfs are constructed during execution and not ELF files. If user knows positions of symbols, he/she should provide /tmp/perf-<pid>.map file.

This 3 patches makes perf recognize hugetlbfs mapping as anon mapping. Before this 3 patches user has no chance to use his/her own .map file to resolve symbols because perf tries to use hugetlbfs file.

Wang Nan (3):
perf tools: Recognize hugetlb mapping as anon mapping tools lib api fs: Add hugetlbfs filesystem detector perf record: Mark MAP_HUGETLB during synthesizing mmap events

 tools/lib/api/fs/fs.c   | 15 +++++++++++++++
 tools/lib/api/fs/fs.h   |  1 +
 tools/perf/util/event.c |  7 +++++++
 tools/perf/util/map.c   |  8 +++++---
 4 files changed, 28 insertions(+), 3 deletions(-)
Quoted 4 lines
Cc Hou Pengyang <[email protected]>
Cc He Kuang <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>