2004-11-22 03:37:56 +03:00
|
|
|
/*
|
2004-11-22 04:38:40 +03:00
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University.
|
|
|
|
* All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The Trustees of the University of Tennessee.
|
|
|
|
* All rights reserved.
|
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
2004-11-22 03:37:56 +03:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
2004-10-20 05:03:09 +04:00
|
|
|
#include "ompi_config.h"
|
2004-06-16 19:41:29 +04:00
|
|
|
#include <errno.h>
|
2004-10-22 20:06:05 +04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2004-06-16 19:41:29 +04:00
|
|
|
#include <unistd.h>
|
2004-10-22 20:06:05 +04:00
|
|
|
#endif
|
2004-06-16 19:41:29 +04:00
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <time.h>
|
2004-10-22 20:06:05 +04:00
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
2004-06-16 19:41:29 +04:00
|
|
|
#include <sys/types.h>
|
2004-10-22 20:06:05 +04:00
|
|
|
#endif
|
2004-06-16 19:41:29 +04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/errno.h>
|
2004-10-22 20:06:05 +04:00
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
2004-06-16 19:41:29 +04:00
|
|
|
#include <sys/mman.h>
|
2004-10-22 20:06:05 +04:00
|
|
|
#endif
|
2004-06-16 19:41:29 +04:00
|
|
|
|
2004-06-17 23:16:51 +04:00
|
|
|
#include "include/constants.h"
|
2004-06-16 19:41:29 +04:00
|
|
|
#include "util/output.h"
|
2004-06-17 20:13:20 +04:00
|
|
|
#include "util/sys_info.h"
|
2004-07-15 22:42:02 +04:00
|
|
|
#include "util/proc_info.h"
|
2004-06-16 19:41:29 +04:00
|
|
|
#include "mca/pcm/pcm.h"
|
2004-08-06 23:35:57 +04:00
|
|
|
#include "common_sm_mmap.h"
|
2004-06-16 19:41:29 +04:00
|
|
|
|
|
|
|
|
|
|
|
OBJ_CLASS_INSTANCE(
|
2004-08-06 23:35:57 +04:00
|
|
|
mca_common_sm_mmap_t,
|
2004-06-16 19:41:29 +04:00
|
|
|
ompi_object_t,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
|
2004-08-06 23:35:57 +04:00
|
|
|
/*
|
|
|
|
* Instance that is shared between components that use shared memory
|
|
|
|
*/
|
|
|
|
mca_common_sm_mmap_t *mca_common_sm_mmap = NULL;
|
|
|
|
|
2004-06-16 19:41:29 +04:00
|
|
|
|
2004-08-06 23:35:57 +04:00
|
|
|
static int mca_common_sm_mmap_open(char* path)
|
2004-06-16 19:41:29 +04:00
|
|
|
{
|
|
|
|
int fd = -1;
|
|
|
|
|
2004-08-04 21:22:16 +04:00
|
|
|
/* loop until file can be opened, or until an erro, other than
|
|
|
|
* access error, occurs */
|
2004-06-16 19:41:29 +04:00
|
|
|
while(fd < 0) {
|
|
|
|
struct timespec ts;
|
|
|
|
fd = open(path, O_CREAT|O_RDWR, 0000);
|
|
|
|
if(fd < 0 && errno != EACCES) {
|
2004-07-15 22:42:02 +04:00
|
|
|
ompi_output(0,
|
|
|
|
"mca_ptl_sm_mmap_open: open %s failed with errno=%d\n", path, errno);
|
2004-08-04 21:22:16 +04:00
|
|
|
return -1;
|
2004-06-16 19:41:29 +04:00
|
|
|
}
|
|
|
|
ts.tv_sec = 0;
|
|
|
|
ts.tv_nsec = 500000;
|
|
|
|
nanosleep(&ts,NULL);
|
|
|
|
}
|
|
|
|
|
2004-08-04 21:22:16 +04:00
|
|
|
/* return */
|
|
|
|
return fd;
|
2004-06-17 00:01:19 +04:00
|
|
|
|
2004-06-16 19:41:29 +04:00
|
|
|
}
|
|
|
|
|
2004-08-04 21:22:16 +04:00
|
|
|
/**
|
|
|
|
* This routine is used to set up a shared memory file, backed
|
|
|
|
* by a specified file. It is assumed that the file does not
|
|
|
|
* exist before any of the current set of processes try and open
|
|
|
|
* it.
|
|
|
|
*
|
|
|
|
* @param size - size of the file, in bytes (IN)
|
|
|
|
*
|
|
|
|
* @param file_name name of file to be opened. (IN)
|
|
|
|
*
|
|
|
|
* @param size_ctl_structure size of the control structure at
|
|
|
|
* the head of the file. The control structure
|
2004-08-18 19:02:21 +04:00
|
|
|
* is assumed to have mca_common_sm_file_header_t
|
2004-08-04 21:22:16 +04:00
|
|
|
* as its first segment (IN)
|
|
|
|
*
|
|
|
|
* @param data_set_alignment alignment of the data segment. this
|
|
|
|
* follows the control structure (IN)
|
|
|
|
*/
|
|
|
|
|
2004-08-06 23:35:57 +04:00
|
|
|
mca_common_sm_mmap_t* mca_common_sm_mmap_init(size_t size, char *file_name,
|
2004-08-04 21:22:16 +04:00
|
|
|
size_t size_ctl_structure, size_t data_seg_alignment)
|
2004-06-16 19:41:29 +04:00
|
|
|
{
|
2004-08-04 21:22:16 +04:00
|
|
|
int fd,return_code=OMPI_SUCCESS;
|
|
|
|
bool file_previously_opened;
|
2004-08-18 19:02:21 +04:00
|
|
|
mca_common_sm_file_header_t* seg;
|
2004-08-06 23:35:57 +04:00
|
|
|
mca_common_sm_mmap_t* map;
|
2004-08-04 21:22:16 +04:00
|
|
|
struct stat s_stat;
|
|
|
|
unsigned char *addr;
|
2004-08-11 20:06:14 +04:00
|
|
|
size_t tmp,mem_offset;
|
2004-08-04 21:22:16 +04:00
|
|
|
|
|
|
|
/* input parameter error checks */
|
2004-08-18 19:02:21 +04:00
|
|
|
if( (size < sizeof(mca_common_sm_file_header_t) ) ||
|
2004-08-04 21:22:16 +04:00
|
|
|
( file_name == NULL ) ||
|
2004-08-18 19:02:21 +04:00
|
|
|
( size_ctl_structure <
|
|
|
|
sizeof(mca_common_sm_file_header_t ) ) ||
|
2004-08-04 21:22:16 +04:00
|
|
|
( data_seg_alignment == 0 ) ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-06-16 19:41:29 +04:00
|
|
|
|
2004-08-04 21:22:16 +04:00
|
|
|
/* open the backing file - only the first process accessing this
|
|
|
|
* file will succeed. */
|
2004-08-06 23:35:57 +04:00
|
|
|
fd=mca_common_sm_mmap_open(file_name);
|
2004-08-04 21:22:16 +04:00
|
|
|
if( -1 == fd ) {
|
2004-08-06 23:35:57 +04:00
|
|
|
ompi_output(0, "mca_common_sm_mmap_init: mca_common_sm_mmap_open failed \n");
|
2004-06-16 19:41:29 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-08-04 21:22:16 +04:00
|
|
|
/* figure out if I am first to attach to file */
|
|
|
|
file_previously_opened=false;
|
|
|
|
return_code=fstat(fd,&s_stat);
|
|
|
|
if( 0 > return_code ) {
|
2004-08-06 23:35:57 +04:00
|
|
|
ompi_output(0, "mca_common_sm_mmap_init: fstat failed with errno=%d\n", errno);
|
2004-08-04 21:22:16 +04:00
|
|
|
close(fd);
|
2004-06-16 19:41:29 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2004-08-04 21:22:16 +04:00
|
|
|
if(0 < s_stat.st_size){
|
|
|
|
file_previously_opened=true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* first process to open the file, so needs to
|
|
|
|
* initialize it */
|
|
|
|
if( !file_previously_opened ) {
|
|
|
|
/* truncate the file to the requested size */
|
|
|
|
if(ftruncate(fd, size) != 0) {
|
|
|
|
ompi_output(0,
|
2004-08-06 23:35:57 +04:00
|
|
|
"mca_common_sm_mmap_init: ftruncate failed with errno=%d\n",
|
2004-08-04 21:22:16 +04:00
|
|
|
errno);
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2004-06-16 19:41:29 +04:00
|
|
|
|
|
|
|
/* map the file and initialize segment state */
|
|
|
|
seg = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
|
2004-08-04 21:22:16 +04:00
|
|
|
if( (void*)-1 == seg ) {
|
2004-08-06 23:35:57 +04:00
|
|
|
ompi_output(0, "mca_common_sm_mmap_init: mmap failed with errno=%d\n",
|
2004-08-04 21:22:16 +04:00
|
|
|
errno);
|
|
|
|
close(fd);
|
2004-06-16 19:41:29 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-08-04 21:22:16 +04:00
|
|
|
/* set up the map object */
|
2004-08-06 23:35:57 +04:00
|
|
|
map = OBJ_NEW(mca_common_sm_mmap_t);
|
2004-08-04 21:22:16 +04:00
|
|
|
strncpy(map->map_path, file_name, PATH_MAX);
|
2004-08-11 20:06:14 +04:00
|
|
|
/* the first entry in the file is the control strcuture. the first
|
2004-08-18 19:02:21 +04:00
|
|
|
entry in the control structure is an mca_common_sm_file_header_t
|
2004-08-11 20:06:14 +04:00
|
|
|
element */
|
2004-06-16 19:41:29 +04:00
|
|
|
map->map_seg = seg;
|
2004-08-11 20:06:14 +04:00
|
|
|
|
2004-08-04 21:22:16 +04:00
|
|
|
/* allign start of data segment */
|
|
|
|
addr=(unsigned char *)seg+size_ctl_structure;
|
2004-08-11 20:06:14 +04:00
|
|
|
tmp=(size_t)(addr+1);
|
2004-08-04 21:22:16 +04:00
|
|
|
addr=(unsigned char *)( (tmp/data_seg_alignment)*data_seg_alignment);
|
|
|
|
/* is addr past end of file ? */
|
|
|
|
if( (unsigned char*)seg+size < addr ){
|
2004-08-11 20:06:14 +04:00
|
|
|
ompi_output(0, "mca_common_sm_mmap_init: memory region too small len %d addr %p\n",
|
|
|
|
size,addr);
|
|
|
|
fchmod(fd, 0600);
|
2004-08-04 21:22:16 +04:00
|
|
|
close(fd);
|
|
|
|
munmap(seg,size);
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-08-11 20:06:14 +04:00
|
|
|
mem_offset=addr-(unsigned char *)seg;
|
2004-08-13 20:01:51 +04:00
|
|
|
map->map_addr = (unsigned char *)seg;
|
2004-08-11 20:06:14 +04:00
|
|
|
map->data_addr = addr;
|
|
|
|
map->map_size = size;
|
2004-08-04 21:22:16 +04:00
|
|
|
|
|
|
|
/* initialize the segment - only the first process to open the file */
|
|
|
|
if( !file_previously_opened ) {
|
2004-08-19 03:24:27 +04:00
|
|
|
ompi_atomic_unlock(&seg->seg_lock);
|
2004-08-04 21:22:16 +04:00
|
|
|
seg->seg_inited = false;
|
2004-08-11 20:06:14 +04:00
|
|
|
seg->seg_offset = mem_offset;
|
2004-08-04 21:22:16 +04:00
|
|
|
seg->seg_size = size;
|
|
|
|
}
|
2004-06-16 19:41:29 +04:00
|
|
|
|
|
|
|
/* enable access by other processes on this host */
|
|
|
|
if(fchmod(fd, 0600) != 0) {
|
2004-08-07 02:47:33 +04:00
|
|
|
ompi_output(0, "mca_common_sm_mmap_init: fchmod failed with errno=%d :: fd %d\n",
|
|
|
|
errno,fd);
|
2004-06-16 19:41:29 +04:00
|
|
|
OBJ_RELEASE(map);
|
2004-08-07 02:47:33 +04:00
|
|
|
close(fd);
|
2004-06-16 19:41:29 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2004-08-07 02:47:33 +04:00
|
|
|
close(fd);
|
2004-08-04 21:22:16 +04:00
|
|
|
|
2004-06-16 19:41:29 +04:00
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-11 20:06:14 +04:00
|
|
|
/**
|
|
|
|
* allocate memory from a previously allocated shared memory
|
|
|
|
* block.
|
|
|
|
*
|
|
|
|
* @param size size of request, in bytes (IN)
|
|
|
|
*
|
|
|
|
* @retval addr virtual address
|
|
|
|
*/
|
2004-08-06 23:35:57 +04:00
|
|
|
void* mca_common_sm_mmap_alloc(size_t* size)
|
2004-06-16 19:41:29 +04:00
|
|
|
{
|
2004-08-06 23:35:57 +04:00
|
|
|
mca_common_sm_mmap_t* map = mca_common_sm_mmap;
|
2004-08-18 19:02:21 +04:00
|
|
|
mca_common_sm_file_header_t* seg = map->map_seg;
|
2004-06-16 19:41:29 +04:00
|
|
|
void* addr;
|
|
|
|
|
2004-08-19 03:24:27 +04:00
|
|
|
ompi_atomic_lock(&seg->seg_lock);
|
2004-06-17 20:13:20 +04:00
|
|
|
if(seg->seg_offset + *size > map->map_size) {
|
2004-06-16 19:41:29 +04:00
|
|
|
addr = NULL;
|
|
|
|
} else {
|
2004-08-11 20:06:14 +04:00
|
|
|
/* add base address to segment offset */
|
|
|
|
addr = map->data_addr + seg->seg_offset;
|
2004-06-16 19:41:29 +04:00
|
|
|
seg->seg_offset += *size;
|
|
|
|
}
|
2004-08-19 03:24:27 +04:00
|
|
|
ompi_atomic_unlock(&seg->seg_lock);
|
2004-06-16 19:41:29 +04:00
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
2004-06-18 00:33:52 +04:00
|
|
|
|