HPCToolkit
mmap.c
Go to the documentation of this file.
1 // interface to unix (anonymous) mmap operations
2 
3 //------------------------------------------------------------------
4 // System includes
5 //------------------------------------------------------------------
6 #include <sys/mman.h>
7 #include <sys/stat.h>
8 #include <sys/types.h>
9 
10 #include <errno.h>
11 #include <stdbool.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15 
16 //------------------------------------------------------------------
17 // Local includes
18 //------------------------------------------------------------------
19 #include "mmap.h"
20 #include <messages/messages.h>
21 
22 //------------------------------------------------------------------
23 // Internal constants
24 //------------------------------------------------------------------
25 
26 #define DEFAULT_PAGESIZE 4096;
27 static size_t pagesize = DEFAULT_PAGESIZE;
28 
29 //------------------------------------------------------------------
30 // Internal functions
31 //------------------------------------------------------------------
32 static inline size_t
34 {
35  return ((size + pagesize - 1)/pagesize) * pagesize;
36 }
37 
38 //------------------------------------------------------------------
39 // Interface functions
40 //------------------------------------------------------------------
41 
42 //
43 // Returns: address of mmap-ed region, else NULL on failure.
44 //
45 // Note: we leak fds in the rare case of a process that creates many
46 // threads running on a system that doesn't allow MAP_ANON.
47 //
48 void*
49 hpcrun_mmap_anon(size_t size)
50 {
51  int prot, flags, fd;
52  char* str;
53  void* addr;
54 
55  size = hpcrun_align_pagesize(size);
56  prot = PROT_READ | PROT_WRITE;
57  fd = -1;
58 
59 #if defined(MAP_ANON)
60  flags = MAP_PRIVATE | MAP_ANON;
61 #elif defined(MAP_ANONYMOUS)
62  flags = MAP_PRIVATE | MAP_ANONYMOUS;
63 #else
64  flags = MAP_PRIVATE;
65  fd = open("/dev/zero", O_RDWR);
66  if (fd < 0) {
67  str = strerror(errno);
68  EMSG("%s: open /dev/null failed: %s", __func__, str);
69  return NULL;
70  }
71 #endif
72 
73  addr = mmap(NULL, size, prot, flags, fd, 0);
74  if (addr == MAP_FAILED) {
75  str = strerror(errno);
76  EMSG("%s: mmap failed: %s", __func__, str);
77  addr = NULL;
78  }
79 
80 #ifdef FIXME
81 #endif
82  //
83  // *** MUST use NMSG,
84  // as this function is called before thread_id is established
85  //
86  TMSG(MMAP, "%s: size = %ld, fd = %d, addr = %p",
87  __func__, size, fd, addr);
88  return addr;
89 }
90 
91 // Look up pagesize if this is possible
92 //
93 void
95 {
96  static bool init_done = false;
97  long ans;
98 
99  if (init_done)
100  return;
101 
102 #ifdef _SC_PAGESIZE
103  if ((ans = sysconf(_SC_PAGESIZE)) > 0) {
104  TMSG(MMAP, "sysconf gives pagesize = %ld", ans);
105  pagesize = ans;
106  }
107 #endif
108  TMSG(MMAP, "pagesize = %ld", pagesize);
109  init_done = true;
110 }
void hpcrun_mmap_init(void)
Definition: mmap.c:94
#define DEFAULT_PAGESIZE
Definition: mmap.c:26
static size_t hpcrun_align_pagesize(size_t size)
Definition: mmap.c:33
#define EMSG
Definition: messages.h:70
void * hpcrun_mmap_anon(size_t size)
Definition: mmap.c:49
static size_t pagesize
Definition: mmap.c:27
#define TMSG(f,...)
Definition: messages.h:93
#define NULL
Definition: ElfHelper.cpp:85
cct_addr_t * addr
Definition: cct.c:130