Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 141 additions & 43 deletions src/host_envir.c
Original file line number Diff line number Diff line change
Expand Up @@ -4173,6 +4173,30 @@ size on Windows in place of an additional allocation granularity function.
#else /* !EDG_WIN32 */
#include <sys/mman.h>

/*
Test for the variations of the anonymous flag for mmap:

- MAP_ANONYMOUS (Linux)
- MAP_ANON (BSD)

If none are found, use 0 for the value of MMAP_ANONYMOUS_FLAG.
*/
#if defined(MAP_ANONYMOUS)
#define MMAP_ANONYMOUS_FLAG MAP_ANONYMOUS
#elif defined(MAP_ANON)
#define MMAP_ANONYMOUS_FLAG MAP_ANON
#else /* !defined(MAP_ANONYMOUS) && !defined(MAP_ANON) */
#define MMAP_ANONYMOUS_FLAG 0
#endif /* defined(MAP_ANONYMOUS) */

#define MMAP_ANONYMOUS_FD -1
/* The file descriptor number to be used for anonymous
files. */

#define MMAP_ANONYMOUS_FD_OFFSET 0
/* The file offset associated with the file mmap file
descriptor to be used for anonymous files. */

#if defined(__hpux) || defined(__AIX__)
/* HP and IBM require that unistd.h be included, and use _SC_PAGE_SIZE
in place of _SC_PAGESIZE. */
Expand Down Expand Up @@ -4206,17 +4230,63 @@ incremental_size must be a multiple of the page size.
return (size_t)page_size;
} /* get_page_size */

namespace {

static inline caddr_t mmap_posix(a_void_ptr address,
sizeof_t size,
int prot,
int fd,
sizeof_t offset)
/*
Perform a POSIX mmap of the data pointed to by the given file descriptor "fd".
The data is mapped starting at "offset" bytes for "size" bytes, to the address
specified by "address" (or a system-chosen location if "address" is NULL).
The mapping never affects the opened file (due to the use of MAP_PRIVATE).
The source of a POSIX memory mapping.
*/
struct an_mmap_source {
static an_mmap_source for_anonymous();
static an_mmap_source for_file(int fd, sizeof_t offset);

a_boolean is_anonymous_file;
/* The kind of mapping to create. */
int fd; /* The file descriptor to map (MMAP_ANONYMOUS_FD when
is_anonymous_file is TRUE). */
sizeof_t offset; /* The starting offset in the file
(MMAP_ANONYMOUS_FD_OFFSET when is_anonymous_file is
TRUE). */
}; /* an_mmap_source */


an_mmap_source an_mmap_source::for_anonymous()
/*
Return a source for an anonymous memory mapping.
*/
{
an_mmap_source result;

result.is_anonymous_file = TRUE;
result.fd = MMAP_ANONYMOUS_FD;
result.offset = MMAP_ANONYMOUS_FD_OFFSET;
return result;
} /* an_mmap_source::for_anonymous */


an_mmap_source an_mmap_source::for_file(int fd, sizeof_t offset)
/*
Return a source for a mapping of the file descriptor fd starting at offset.
*/
{
an_mmap_source result;

result.is_anonymous_file = FALSE;
result.fd = fd;
result.offset = offset;
return result;
} /* an_mmap_source::for_file */

} /* namespace */

static inline caddr_t mmap_posix(a_void_ptr address,
sizeof_t size,
int prot,
an_mmap_source source)
/*
Perform a POSIX mmap for size bytes using source, which specifies either an
anonymous mapping or a file descriptor and starting offset. The data is mapped
to address (or a system-chosen location if address is NULL). The mapping never
affects the opened file (due to the use of MAP_PRIVATE).

If "address" is non-NULL, this function is guaranteed to return an address
equal to the given "address" or NULL. Otherwise, the return value is the
Expand All @@ -4232,13 +4302,21 @@ carefully chosen.
*/
{
caddr_t addr = NULL;
int flags = 0;
int fd = source.fd;
off_t offset = (off_t)source.offset;

if (source.is_anonymous_file) {
flags = MMAP_ANONYMOUS_FLAG;
check_assertion(fd == MMAP_ANONYMOUS_FD);
check_assertion(offset == MMAP_ANONYMOUS_FD_OFFSET);
} /* if */
if (address != NULL) {
#if MMAP_FIXED_NOREPLACE_FLAG
if (try_mmap_fixed_noreplace) {
addr = (caddr_t)mmap((caddr_t)address, size, prot,
MAP_PRIVATE | MAP_FIXED_NOREPLACE,
fd, (off_t)offset);
MAP_PRIVATE | MAP_FIXED_NOREPLACE | flags,
fd, offset);
/* On older Linux kernels MAP_FIXED_NOREPLACE is silently ignored and
mmap may return a system-assigned address rather than the one
requested. Detect this case, disable MAP_FIXED_NOREPLACE for all
Expand All @@ -4252,64 +4330,82 @@ carefully chosen.
try_mmap_fixed_noreplace = FALSE;
(void)munmap(addr, size);
addr = (caddr_t)mmap((caddr_t)address, size, prot,
MAP_PRIVATE | MAP_FIXED, fd, (off_t)offset);
MAP_PRIVATE | MAP_FIXED | flags,
fd, offset);
} /* if */
} else
#endif /* MMAP_FIXED_NOREPLACE_FLAG */
/* Do not add code here. */
{
addr = (caddr_t)mmap((caddr_t)address, size, prot,
MAP_PRIVATE | MAP_FIXED, fd, (off_t)offset);
MAP_PRIVATE | MAP_FIXED | flags,
fd, offset);
} /* if */
} else {
addr = (caddr_t)mmap((char*)0, size, prot, MAP_PRIVATE, fd, (off_t)offset);
addr = (caddr_t)mmap((char*)0, size, prot, MAP_PRIVATE | flags,
fd, offset);
} /* if */
if (addr == (caddr_t)-1) addr = NULL;
return addr;
} /* mmap_posix */


a_void_ptr map_memory_region_file(an_mmap_handle handle,
void *base_addr,
sizeof_t curr_size,
sizeof_t incremental_size,
sizeof_t file_offset)
a_void_ptr map_memory_region_file(ARG_UNUSED an_mmap_handle handle,
void *base_addr,
sizeof_t curr_size,
sizeof_t incremental_size,
ARG_UNUSED sizeof_t file_offset)
/*
Expand the memory region file represented by the given handle. This routine
assumes that curr_size bytes have already been allocated and mapped starting at
base_addr, and that incremental_size bytes should be added. If base_addr is
NULL, the memory region mapped will be picked by the system; otherwise, the
memory mapped region starts at base_addr. incremental_size must be a multiple
of the host page size.
of the host page size. Anonymous mappings are used when available; in that
case, handle and file_offset are unused.
*/
{
caddr_t addr = NULL;
sizeof_t size;
a_void_ptr map_address = (base_addr != NULL) ?
(a_void_ptr)((char*)base_addr + curr_size) : NULL;

db_enter(4, "map_memory_region_file");
size = file_offset + incremental_size;
/* The file must be large enough to contain the mapped area. */
if (fseek(handle, (long)size, SEEK_SET) == 0) {
/* Write a character at the last allocated position and
make sure the write to the file is actually done. */
if (fputc(0, handle) != EOF && fflush(handle) == 0) {
addr = mmap_posix(map_address, incremental_size, PROT_WRITE | PROT_READ,
fileno(handle), file_offset);
#if DEBUG
if (db_flag_is_set("mmap") || debug_level >= 4) {
fprintf(f_debug,
"map_memory_region_file: allocated %lu bytes of mmap memory "
"at %p\n",
(unsigned long)incremental_size, (a_void_ptr)addr);
if (map_address != NULL) {
fprintf(f_debug, " requested address was: %p\n", map_address);
} /* if */
#if MMAP_ANONYMOUS_FLAG
addr = mmap_posix(map_address, incremental_size, PROT_WRITE | PROT_READ,
an_mmap_source::for_anonymous());
#else /* !MMAP_ANONYMOUS_FLAG */
{ /* The file must be large enough to contain the mapped area. */
sizeof_t size = file_offset + incremental_size;

if (fseek(handle, (long)size, SEEK_SET) == 0) {
/* Write a character at the last allocated position and make sure the
write to the file is actually done. */
if (fputc(0, handle) != EOF && fflush(handle) == 0) {
addr = mmap_posix(map_address, incremental_size,
PROT_WRITE | PROT_READ,
an_mmap_source::for_file(fileno(handle),
file_offset));
} /* if */
#endif /* DEBUG */
} /* if */
}
#endif /* MMAP_ANONYMOUS_FLAG */
#if DEBUG
if (db_flag_is_set("mmap") || debug_level >= 4) {
if (addr != NULL) {
fprintf(f_debug,
"map_memory_region_file: allocated %lu bytes of mmap memory "
"at %p\n",
(unsigned long)incremental_size, (a_void_ptr)addr);
} else {
fprintf(f_debug,
"map_memory_region_file: failed to allocate %lu bytes of "
"mmap memory\n", (unsigned long)incremental_size);
} /* if */
if (map_address != NULL) {
fprintf(f_debug, " requested address was: %p\n", map_address);
} /* if */
} /* if */
#endif /* DEBUG */
db_exit();
return addr;
} /* map_memory_region_file */
Expand Down Expand Up @@ -4359,7 +4455,7 @@ error).
result_addr = (a_void_ptr)mmap_posix(address, size,
read_only ? PROT_READ :
PROT_READ | PROT_WRITE,
fd, offset);
an_mmap_source::for_file(fd, offset));
#if DEBUG
if (db_flag_is_set("mmap") || debug_level >= 4) {
fprintf(f_debug,
Expand Down Expand Up @@ -4394,16 +4490,18 @@ Unmap a block of previously mapped memory.

an_mmap_handle open_memory_region_tmp_file()
/*
Open a temporary file to be used for allocation of file mapped
memory for IL memory blocks.
Open a temporary file to be used for allocation of file mapped memory for IL
memory blocks; if anonymous mappings are used, instead return NULL.
*/
{
an_mmap_handle file;
an_mmap_handle file = NULL;

db_enter(3, "open_memory_region_tmp_file");
#if !MMAP_ANONYMOUS_FLAG
file = open_temp_file(/*binary_file=*/TRUE);
/*lint -e{530}*/ /* Lint bug PCLP-804 */
check_assertion(file != NULL);
#endif /* !MMAP_ANONYMOUS_FLAG */
db_exit();
return file;
} /* open_memory_region_tmp_file */
Expand Down
10 changes: 5 additions & 5 deletions src/mem_manage.c
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,7 @@ preallocated memory that has not been used.
#if USE_MMAP_FOR_MEMORY_REGIONS
STATIC_THREAD a_boolean
mmap_initialized;
/* TRUE when the file used for mmap has been
opened. */
/* TRUE when memory mapping has been initialized. */

STATIC_THREAD sizeof_t
mmap_size_allocated;
Expand All @@ -459,7 +458,8 @@ STATIC_THREAD sizeof_t
STATIC_THREAD an_mmap_handle
memory_region_file;
/* The temporary file used to back the memory mapped
region blocks. */
region blocks (NULL when anonymous mappings
are used). */

void record_mapped_mem_block(a_void_ptr addr,
sizeof_t size)
Expand Down Expand Up @@ -514,7 +514,7 @@ Unmap the memory blocks that have been mapped.
a_void_ptr alloc_new_mem_block(sizeof_t size)
/*
Allocate a block of memory to be used for memory region storage. This
version uses a memory mapped file to obtain the storage. This is done
version uses memory mapping to obtain the storage. This is done
so that the memory region storage may be obtained in a separate
range of addresses. This, in turn, simplifies the processing needed
to ensure that the precompiled header processing routines can read
Expand All @@ -525,7 +525,7 @@ PCH was created.
a_void_ptr addr = NULL;

if (!mmap_initialized) {
/* On the first call, open the file that will be mapped. */
/* On the first call, open a backing file if the host requires one. */
memory_region_file = open_memory_region_tmp_file();
mmap_size_allocated = 0;
mmap_initialized = TRUE;
Expand Down
7 changes: 7 additions & 0 deletions src/pending_changes/gh-648-anonymous-mmap.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[GH #648]
Improved memory allocation performance on POSIX

On Linux and BSD, in configurations where USE_MMAP_FOR_MEMORY_REGIONS is TRUE,
the memory used for the IL when a precompiled header is created or used is now
obtained with anonymous memory mappings instead of mappings of a temporary
file, which makes such compilations faster.
Loading