2010-04-03 05:58:14 +01:00
|
|
|
/************************************************************************
|
|
|
|
** **
|
|
|
|
** The YapTab/YapOr/OPTYap systems **
|
|
|
|
** **
|
|
|
|
** YapTab extends the Yap Prolog engine to support sequential tabling **
|
|
|
|
** YapOr extends the Yap Prolog engine to support or-parallelism **
|
|
|
|
** OPTYap extends the Yap Prolog engine to support or-parallel tabling **
|
|
|
|
** **
|
|
|
|
** **
|
|
|
|
** Yap Prolog was developed at University of Porto, Portugal **
|
|
|
|
** **
|
|
|
|
************************************************************************/
|
2005-05-31 09:24:24 +01:00
|
|
|
|
2010-04-03 05:58:14 +01:00
|
|
|
/**************************************
|
|
|
|
** Includes & Declarations **
|
|
|
|
**************************************/
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#include "Yap.h"
|
2011-04-29 14:59:17 +01:00
|
|
|
#if defined(YAPOR_COPY) || defined(YAPOR_COW) || defined(YAPOR_SBA)
|
2001-04-09 20:54:03 +01:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/shm.h>
|
|
|
|
#include <sys/mman.h>
|
2005-06-03 19:28:11 +01:00
|
|
|
#include "Yatom.h"
|
2009-10-23 14:22:17 +01:00
|
|
|
#include "YapHeap.h"
|
2005-06-03 19:28:11 +01:00
|
|
|
#include "alloc.h"
|
|
|
|
#include "heapgc.h"
|
2011-04-14 19:19:13 +01:00
|
|
|
#include "or.macros.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/************************************
|
|
|
|
** Macros & Declarations **
|
|
|
|
************************************/
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2003-11-05 16:12:25 +00:00
|
|
|
#define KBYTES 1024
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#ifdef MMAP_MEMORY_MAPPING_SCHEME
|
|
|
|
int fd_mapfile;
|
|
|
|
#else /* SHM_MEMORY_MAPPING_SCHEME */
|
2003-11-05 16:12:25 +00:00
|
|
|
int shm_mapid[MAX_WORKERS + 1];
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif /* MEMORY_MAPPING_SCHEME */
|
|
|
|
|
2003-11-05 16:12:25 +00:00
|
|
|
|
|
|
|
|
2011-04-14 19:19:13 +01:00
|
|
|
/******************************************
|
|
|
|
** Local functions declaration **
|
|
|
|
******************************************/
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2011-04-14 19:19:13 +01:00
|
|
|
#ifdef MMAP_MEMORY_MAPPING_SCHEME
|
|
|
|
void open_mapfile(long TotalArea);
|
|
|
|
#else /* SHM_MEMORY_MAPPING_SCHEME */
|
|
|
|
void shm_map_memory(int id, int size, void *shmaddr);
|
|
|
|
#endif /* MEMORY_MAPPING_SCHEME */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2003-11-05 16:12:25 +00:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2011-04-14 19:19:13 +01:00
|
|
|
/********************************
|
|
|
|
** Global functions **
|
|
|
|
********************************/
|
|
|
|
|
2011-05-23 16:19:47 +01:00
|
|
|
void Yap_init_yapor_global_local_memory(void) {
|
|
|
|
#ifdef YAPOR_COW
|
|
|
|
int private_fd_mapfile;
|
|
|
|
#endif /* YAPOR_COW */
|
|
|
|
long ExtraArea = ADJUST_SIZE_TO_PAGE(sizeof(struct global_data) + MAX_WORKERS * sizeof(struct worker_local));
|
|
|
|
|
|
|
|
Yap_local = (struct worker_local *)(MMAP_ADDR - ExtraArea);
|
|
|
|
Yap_global = (struct global_data *)(MMAP_ADDR - sizeof(struct global_data));
|
|
|
|
|
|
|
|
#ifdef MMAP_MEMORY_MAPPING_SCHEME
|
|
|
|
//open_mapfile(ExtraArea);
|
|
|
|
char mapfile[20];
|
|
|
|
strcpy(mapfile,"./mapfile");
|
|
|
|
itos(getpid(), &mapfile[9]);
|
|
|
|
if ((fd_mapfile = open(mapfile, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0)
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "open error (open_mapfile)");
|
|
|
|
if (lseek(fd_mapfile, ExtraArea, SEEK_SET) < 0)
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "lseek error (open_mapfile)");
|
|
|
|
if (write(fd_mapfile, "", 1) < 0)
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "write error (open_mapfile)");
|
|
|
|
|
|
|
|
if (mmap((void *) Yap_local, (size_t) ExtraArea, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd_mapfile, 0) == (void *) -1)
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "mmap error (Yap_init_global_local_memory)");
|
|
|
|
#else /* SHM_MEMORY_MAPPING_SCHEME */
|
|
|
|
/* most systems are limited regarding what we can allocate */
|
|
|
|
#ifdef YAPOR_COW
|
|
|
|
/* single shared segment in ACOW */
|
|
|
|
shm_map_memory(0, ExtraArea + HeapArea, (void *) MMAP_ADDR);
|
|
|
|
#else /* YAPOR_COPY || YAPOR_SBA */
|
|
|
|
/* place as segment n otherwise (0..n-1 reserved for worker areas */
|
|
|
|
shm_map_memory(n_workers, ExtraArea + HeapArea, (void *) Yap_local);
|
|
|
|
{ int i;
|
|
|
|
for (i = 0; i < n_workers; i++)
|
|
|
|
shm_map_memory(i, Yap_worker_area_size, GlobalBase + Yap_worker_area_size * i);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* MEMORY_MAPPING_SCHEME */
|
|
|
|
|
|
|
|
#ifdef YAPOR_COW
|
|
|
|
/* just allocate local space for stacks */
|
|
|
|
if ((private_fd_mapfile = open("/dev/zero", O_RDWR)) < 0)
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "open error (Yap_init_optyap_memory)");
|
|
|
|
if (mmap(GlobalBase, GlobalLocalArea + TrailAuxArea, PROT_READ|PROT_WRITE,
|
|
|
|
MAP_PRIVATE|MAP_FIXED, private_fd_mapfile, 0) == (void *) -1)
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "mmap error (Yap_init_optyap_memory)");
|
|
|
|
close(private_fd_mapfile);
|
|
|
|
#endif /* YAPOR_COW */
|
|
|
|
|
|
|
|
#ifdef YAPOR_SBA
|
|
|
|
/* alloc space for the sparse binding array */
|
|
|
|
sba_size = Yap_worker_area_size * n_workers;
|
|
|
|
if ((binding_array = (char *)malloc(sba_size)) == NULL)
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "malloc error (Yap_init_optyap_memory)");
|
|
|
|
if ((CELL)binding_array & MBIT) {
|
|
|
|
Yap_Error(INTERNAL_ERROR, TermNil, "binding_array start address conflicts with tag used in IDB (Yap_init_optyap_memory)");
|
|
|
|
}
|
|
|
|
sba_offset = binding_array - GlobalBase;
|
|
|
|
sba_end = (int)binding_array + sba_size;
|
|
|
|
#endif /* YAPOR_SBA */
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Yap_init_yapor_stacks_memory(long TrailAuxArea, long HeapArea, long GlobalLocalArea, int n_workers) {
|
|
|
|
#ifdef YAPOR_COW
|
|
|
|
int private_fd_mapfile;
|
|
|
|
#if MMAP_MEMORY_MAPPING_SCHEME
|
|
|
|
long TotalArea;
|
|
|
|
#endif /* MMAP_MEMORY_MAPPING_SCHEME */
|
|
|
|
#else /* YAPOR_COPY || YAPOR_SBA */
|
|
|
|
long TotalArea;
|
|
|
|
#endif
|
|
|
|
long ExtraArea = ADJUST_SIZE_TO_PAGE(sizeof(struct global_data) + MAX_WORKERS * sizeof(struct worker_local));
|
|
|
|
|
|
|
|
TrailAuxArea = ADJUST_SIZE(TrailAuxArea);
|
|
|
|
HeapArea = ADJUST_SIZE_TO_PAGE(HeapArea);
|
|
|
|
GlobalLocalArea = ADJUST_SIZE(GlobalLocalArea);
|
|
|
|
Yap_HeapBase = (ADDR) MMAP_ADDR;
|
|
|
|
LOCAL_GlobalBase = (ADDR) (MMAP_ADDR + HeapArea);
|
|
|
|
/* shared memory allocation - model dependent */
|
|
|
|
#ifdef YAPOR_COW
|
|
|
|
/* acow just needs one stack */
|
|
|
|
#ifdef MMAP_MEMORY_MAPPING_SCHEME
|
|
|
|
/* I need this for MMAP to know what it must allocate */
|
|
|
|
TotalArea = HeapArea;
|
|
|
|
#endif /* MMAP_MEMORY_MAPPING_SCHEME */
|
|
|
|
#else /* YAPOR_COPY || YAPOR_SBA */
|
|
|
|
/* the others need n stacks */
|
|
|
|
Yap_worker_area_size = ADJUST_SIZE_TO_PAGE(GlobalLocalArea + TrailAuxArea);
|
|
|
|
TotalArea = ExtraArea + HeapArea + Yap_worker_area_size * n_workers;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MMAP_MEMORY_MAPPING_SCHEME
|
|
|
|
/* map total area in a single go */
|
|
|
|
//open_mapfile(TotalArea);
|
|
|
|
if (lseek(fd_mapfile, TotalArea, SEEK_SET) < 0)
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "lseek error (open_mapfile)");
|
|
|
|
if (write(fd_mapfile, "", 1) < 0)
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "write error (open_mapfile)");
|
|
|
|
if (mmap((void *) Yap_HeapBase, (size_t) TotalArea, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd_mapfile, ExtraArea) == (void *) -1)
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "mmap error (Yap_init_yapor_memory)");
|
|
|
|
#else /* SHM_MEMORY_MAPPING_SCHEME */
|
|
|
|
/* most systems are limited regarding what we can allocate */
|
|
|
|
#ifdef YAPOR_COW
|
|
|
|
/* single shared segment in ACOW */
|
|
|
|
shm_map_memory(0, ExtraArea + HeapArea, (void *) MMAP_ADDR);
|
|
|
|
#else /* YAPOR_COPY || YAPOR_SBA */
|
|
|
|
/* place as segment n otherwise (0..n-1 reserved for worker areas */
|
|
|
|
shm_map_memory(n_workers, ExtraArea + HeapArea, (void *) Yap_local);
|
|
|
|
{ int i;
|
|
|
|
for (i = 0; i < n_workers; i++)
|
|
|
|
shm_map_memory(i, Yap_worker_area_size, LOCAL_GlobalBase + Yap_worker_area_size * i);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* MEMORY_MAPPING_SCHEME */
|
|
|
|
|
|
|
|
#ifdef YAPOR_COW
|
|
|
|
/* just allocate local space for stacks */
|
|
|
|
if ((private_fd_mapfile = open("/dev/zero", O_RDWR)) < 0)
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "open error (Yap_init_optyap_memory)");
|
|
|
|
if (mmap(LOCAL_GlobalBase, GlobalLocalArea + TrailAuxArea, PROT_READ|PROT_WRITE,
|
|
|
|
MAP_PRIVATE|MAP_FIXED, private_fd_mapfile, 0) == (void *) -1)
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "mmap error (Yap_init_optyap_memory)");
|
|
|
|
close(private_fd_mapfile);
|
|
|
|
#endif /* YAPOR_COW */
|
|
|
|
|
|
|
|
#ifdef YAPOR_SBA
|
|
|
|
/* alloc space for the sparse binding array */
|
|
|
|
sba_size = Yap_worker_area_size * n_workers;
|
|
|
|
if ((binding_array = (char *)malloc(sba_size)) == NULL)
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "malloc error (Yap_init_optyap_memory)");
|
|
|
|
if ((CELL)binding_array & MBIT) {
|
|
|
|
Yap_Error(INTERNAL_ERROR, TermNil, "binding_array start address conflicts with tag used in IDB (Yap_init_optyap_memory)");
|
|
|
|
}
|
|
|
|
sba_offset = binding_array - LOCAL_GlobalBase;
|
|
|
|
sba_end = (int)binding_array + sba_size;
|
|
|
|
#endif /* YAPOR_SBA */
|
|
|
|
|
|
|
|
LOCAL_TrailBase = LOCAL_GlobalBase + GlobalLocalArea;
|
|
|
|
LOCAL_LocalBase = LOCAL_TrailBase - CellSize;
|
|
|
|
LOCAL_TrailTop = LOCAL_TrailBase + (TrailAuxArea /2);
|
|
|
|
Yap_InitHeap(Yap_HeapBase);
|
|
|
|
//HeapMax = (CELL)(LOCAL_TrailBase + (TrailAuxArea - CellSize));
|
|
|
|
// HeapLim = LOCAL_GlobalBase;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void OLD_Yap_init_optyap_memory(long TrailAuxArea, long HeapArea, long GlobalLocalArea, int n_workers) {
|
2011-03-30 16:39:09 +01:00
|
|
|
#ifdef YAPOR_COW
|
2001-04-09 20:54:03 +01:00
|
|
|
int private_fd_mapfile;
|
2003-11-05 16:12:25 +00:00
|
|
|
#if MMAP_MEMORY_MAPPING_SCHEME
|
|
|
|
long TotalArea;
|
|
|
|
#endif /* MMAP_MEMORY_MAPPING_SCHEME */
|
2011-03-30 15:32:59 +01:00
|
|
|
#else /* YAPOR_COPY || YAPOR_SBA */
|
2003-11-05 16:12:25 +00:00
|
|
|
long TotalArea;
|
2011-04-29 14:59:17 +01:00
|
|
|
#endif
|
2011-04-14 19:19:13 +01:00
|
|
|
long ExtraArea;
|
2011-05-23 16:19:47 +01:00
|
|
|
ADDR GlobalBase;
|
|
|
|
|
2008-03-25 16:45:53 +00:00
|
|
|
HeapArea = ADJUST_SIZE_TO_PAGE(HeapArea);
|
|
|
|
GlobalLocalArea = ADJUST_SIZE(GlobalLocalArea);
|
|
|
|
TrailAuxArea = ADJUST_SIZE(TrailAuxArea);
|
2011-04-14 19:19:13 +01:00
|
|
|
|
|
|
|
/* initial allocation - model independent */
|
|
|
|
ExtraArea = ADJUST_SIZE_TO_PAGE(sizeof(struct global_data) + MAX_WORKERS * sizeof(struct worker_local));
|
2011-05-09 19:36:51 +01:00
|
|
|
Yap_local = (struct worker_local *)(MMAP_ADDR - ExtraArea);
|
2011-04-14 19:19:13 +01:00
|
|
|
Yap_global = (struct global_data *)(MMAP_ADDR - sizeof(struct global_data));
|
|
|
|
Yap_HeapBase = (ADDR) MMAP_ADDR;
|
2011-05-23 16:19:47 +01:00
|
|
|
GlobalBase = (ADDR) (MMAP_ADDR + HeapArea);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2003-11-05 16:12:25 +00:00
|
|
|
/* shared memory allocation - model dependent */
|
2011-03-30 16:39:09 +01:00
|
|
|
#ifdef YAPOR_COW
|
2001-04-09 20:54:03 +01:00
|
|
|
/* acow just needs one stack */
|
|
|
|
#ifdef MMAP_MEMORY_MAPPING_SCHEME
|
|
|
|
/* I need this for MMAP to know what it must allocate */
|
|
|
|
TotalArea = HeapArea;
|
2003-11-05 16:12:25 +00:00
|
|
|
#endif /* MMAP_MEMORY_MAPPING_SCHEME */
|
2011-03-30 15:32:59 +01:00
|
|
|
#else /* YAPOR_COPY || YAPOR_SBA */
|
2001-04-09 20:54:03 +01:00
|
|
|
/* the others need n stacks */
|
2011-04-14 19:19:13 +01:00
|
|
|
Yap_worker_area_size = ADJUST_SIZE_TO_PAGE(GlobalLocalArea + TrailAuxArea);
|
|
|
|
TotalArea = ExtraArea + HeapArea + Yap_worker_area_size * n_workers;
|
2011-04-29 14:59:17 +01:00
|
|
|
#endif
|
2011-05-23 16:19:47 +01:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef MMAP_MEMORY_MAPPING_SCHEME
|
|
|
|
/* map total area in a single go */
|
2011-04-14 19:19:13 +01:00
|
|
|
open_mapfile(TotalArea);
|
2011-05-09 19:36:51 +01:00
|
|
|
if (mmap((void *) Yap_local, (size_t) TotalArea, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd_mapfile, 0) == (void *) -1)
|
2011-04-14 19:19:13 +01:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "mmap error (Yap_init_optyap_memory)");
|
2003-11-05 16:12:25 +00:00
|
|
|
#else /* SHM_MEMORY_MAPPING_SCHEME */
|
2011-04-14 19:19:13 +01:00
|
|
|
/* most systems are limited regarding what we can allocate */
|
2011-03-30 16:39:09 +01:00
|
|
|
#ifdef YAPOR_COW
|
2001-04-09 20:54:03 +01:00
|
|
|
/* single shared segment in ACOW */
|
2011-04-14 19:19:13 +01:00
|
|
|
shm_map_memory(0, ExtraArea + HeapArea, (void *) MMAP_ADDR);
|
2011-03-30 15:32:59 +01:00
|
|
|
#else /* YAPOR_COPY || YAPOR_SBA */
|
2003-11-05 16:12:25 +00:00
|
|
|
/* place as segment n otherwise (0..n-1 reserved for worker areas */
|
2011-05-09 19:36:51 +01:00
|
|
|
shm_map_memory(n_workers, ExtraArea + HeapArea, (void *) Yap_local);
|
2011-04-14 19:19:13 +01:00
|
|
|
{ int i;
|
|
|
|
for (i = 0; i < n_workers; i++)
|
2011-05-23 16:19:47 +01:00
|
|
|
shm_map_memory(i, Yap_worker_area_size, GlobalBase + Yap_worker_area_size * i);
|
2011-04-14 19:19:13 +01:00
|
|
|
}
|
2011-04-29 14:59:17 +01:00
|
|
|
#endif
|
2003-11-05 16:12:25 +00:00
|
|
|
#endif /* MEMORY_MAPPING_SCHEME */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2011-03-30 16:39:09 +01:00
|
|
|
#ifdef YAPOR_COW
|
2001-04-09 20:54:03 +01:00
|
|
|
/* just allocate local space for stacks */
|
|
|
|
if ((private_fd_mapfile = open("/dev/zero", O_RDWR)) < 0)
|
2011-04-14 19:19:13 +01:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "open error (Yap_init_optyap_memory)");
|
2011-05-23 16:19:47 +01:00
|
|
|
if (mmap(GlobalBase, GlobalLocalArea + TrailAuxArea, PROT_READ|PROT_WRITE,
|
2001-04-09 20:54:03 +01:00
|
|
|
MAP_PRIVATE|MAP_FIXED, private_fd_mapfile, 0) == (void *) -1)
|
2011-04-14 19:19:13 +01:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "mmap error (Yap_init_optyap_memory)");
|
2001-04-09 20:54:03 +01:00
|
|
|
close(private_fd_mapfile);
|
2011-04-14 19:19:13 +01:00
|
|
|
#endif /* YAPOR_COW */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2011-03-30 15:32:59 +01:00
|
|
|
#ifdef YAPOR_SBA
|
2001-04-09 20:54:03 +01:00
|
|
|
/* alloc space for the sparse binding array */
|
2011-04-14 19:19:13 +01:00
|
|
|
sba_size = Yap_worker_area_size * n_workers;
|
2001-04-09 20:54:03 +01:00
|
|
|
if ((binding_array = (char *)malloc(sba_size)) == NULL)
|
2011-04-14 19:19:13 +01:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "malloc error (Yap_init_optyap_memory)");
|
2001-04-09 20:54:03 +01:00
|
|
|
if ((CELL)binding_array & MBIT) {
|
2011-04-14 19:19:13 +01:00
|
|
|
Yap_Error(INTERNAL_ERROR, TermNil, "binding_array start address conflicts with tag used in IDB (Yap_init_optyap_memory)");
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2011-05-23 16:19:47 +01:00
|
|
|
sba_offset = binding_array - GlobalBase;
|
2001-04-09 20:54:03 +01:00
|
|
|
sba_end = (int)binding_array + sba_size;
|
2011-03-30 15:32:59 +01:00
|
|
|
#endif /* YAPOR_SBA */
|
2011-04-14 19:19:13 +01:00
|
|
|
Yap_InitHeap(Yap_HeapBase);
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL = REMOTE(0); /* point to the first area */
|
|
|
|
LOCAL_GlobalBase = GlobalBase;
|
|
|
|
LOCAL_TrailBase = LOCAL_GlobalBase + GlobalLocalArea;
|
|
|
|
LOCAL_LocalBase = LOCAL_TrailBase - CellSize;
|
|
|
|
LOCAL_TrailTop = LOCAL_TrailBase + (TrailAuxArea /2);
|
|
|
|
//HeapMax = (CELL)(LOCAL_TrailBase + (TrailAuxArea - CellSize));
|
|
|
|
// HeapLim = LOCAL_GlobalBase;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-14 19:19:13 +01:00
|
|
|
void Yap_remap_optyap_memory(void) {
|
|
|
|
#ifdef YAPOR_SBA
|
|
|
|
/* setup workers so that they have different areas */
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_GlobalBase += worker_id * Yap_worker_area_size;
|
|
|
|
LOCAL_TrailBase += worker_id * Yap_worker_area_size;
|
|
|
|
LOCAL_LocalBase += worker_id * Yap_worker_area_size;
|
|
|
|
LOCAL_TrailTop += worker_id * Yap_worker_area_size;
|
2011-04-14 19:19:13 +01:00
|
|
|
#endif /* YAPOR_SBA */
|
|
|
|
|
|
|
|
#ifdef YAPOR_COPY
|
2001-04-09 20:54:03 +01:00
|
|
|
int i;
|
2011-05-23 16:19:47 +01:00
|
|
|
void *remap_addr = LOCAL_GlobalBase;
|
2011-04-14 19:19:13 +01:00
|
|
|
#ifdef MMAP_MEMORY_MAPPING_SCHEME
|
2011-05-09 19:36:51 +01:00
|
|
|
long remap_offset = (ADDR) remap_addr - (ADDR) Yap_local;
|
2011-05-10 11:47:18 +01:00
|
|
|
if (munmap(remap_addr, (size_t)(Yap_worker_area_size * GLOBAL_number_workers)) == -1)
|
2011-04-14 19:19:13 +01:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "munmap error (Yap_remap_optyap_memory)");
|
2011-05-10 11:47:18 +01:00
|
|
|
for (i = 0; i < GLOBAL_number_workers; i++)
|
2011-04-14 19:19:13 +01:00
|
|
|
if (mmap(remap_addr + worker_offset(i), (size_t)Yap_worker_area_size, PROT_READ|PROT_WRITE,
|
|
|
|
MAP_SHARED|MAP_FIXED, fd_mapfile, remap_offset + i * Yap_worker_area_size) == (void *) -1)
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "mmap error (Yap_remap_optyap_memory)");
|
|
|
|
#else /* SHM_MEMORY_MAPPING_SCHEME */
|
2011-05-10 11:47:18 +01:00
|
|
|
for (i = 0; i < GLOBAL_number_workers; i++)
|
2011-04-14 19:19:13 +01:00
|
|
|
if (shmdt(remap_addr + Yap_worker_area_size * i) == -1)
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "shmdt error (Yap_remap_optyap_memory)");
|
2011-05-10 11:47:18 +01:00
|
|
|
for (i = 0; i < GLOBAL_number_workers; i++)
|
2011-04-14 19:19:13 +01:00
|
|
|
if(shmat(shm_mapid[i], remap_addr + worker_offset(i), 0) == (void *) -1)
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "shmat error (Yap_remap_optyap_memory)");
|
|
|
|
#endif /* MEMORY_MAPPING_SCHEME */
|
|
|
|
#endif /* YAPOR_COPY */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Yap_unmap_optyap_memory (void) {
|
|
|
|
#ifdef MMAP_MEMORY_MAPPING_SCHEME
|
2001-04-09 20:54:03 +01:00
|
|
|
char MapFile[20];
|
2011-04-14 19:19:13 +01:00
|
|
|
#else /* SHM_MEMORY_MAPPING_SCHEME */
|
|
|
|
int i;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif /* MEMORY_MAPPING_SCHEME */
|
2011-04-14 19:19:13 +01:00
|
|
|
int proc;
|
|
|
|
|
|
|
|
INFORMATION_MESSAGE("Worker %d exiting...", worker_id);
|
2011-05-10 11:47:18 +01:00
|
|
|
for (proc = 0; proc < GLOBAL_number_workers; proc++) {
|
|
|
|
if (proc != worker_id && GLOBAL_worker_pid(proc) != 0) {
|
|
|
|
if (kill(GLOBAL_worker_pid(proc), SIGKILL) != 0)
|
|
|
|
INFORMATION_MESSAGE("Can't kill process %d", GLOBAL_worker_pid(proc));
|
2001-04-09 20:54:03 +01:00
|
|
|
else
|
2011-05-10 11:47:18 +01:00
|
|
|
INFORMATION_MESSAGE("Killing process %d", GLOBAL_worker_pid(proc));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
2011-04-14 19:19:13 +01:00
|
|
|
|
2011-03-30 16:39:09 +01:00
|
|
|
#ifdef YAPOR_COW
|
2011-05-10 11:47:18 +01:00
|
|
|
if (GLOBAL_number_workers > 1) {
|
|
|
|
if (kill(GLOBAL_master_worker, SIGINT) != 0)
|
|
|
|
INFORMATION_MESSAGE("Can't kill process %d", GLOBAL_master_worker);
|
2011-04-14 19:19:13 +01:00
|
|
|
else
|
2011-05-10 11:47:18 +01:00
|
|
|
INFORMATION_MESSAGE("Killing process %d", GLOBAL_master_worker);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2011-04-14 19:19:13 +01:00
|
|
|
#endif /* YAPOR_COW */
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef MMAP_MEMORY_MAPPING_SCHEME
|
2010-02-03 22:54:24 +00:00
|
|
|
strcpy(MapFile,"./mapfile");
|
2011-03-30 16:39:09 +01:00
|
|
|
#ifdef YAPOR_COW
|
2011-05-10 11:47:18 +01:00
|
|
|
itos(GLOBAL_master_worker, &MapFile[9]);
|
2011-03-30 15:32:59 +01:00
|
|
|
#else /* YAPOR_COPY || YAPOR_SBA */
|
2011-05-10 11:47:18 +01:00
|
|
|
itos(GLOBAL_worker_pid(0), &MapFile[9]);
|
2011-04-29 14:59:17 +01:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
if (remove(MapFile) == 0)
|
|
|
|
INFORMATION_MESSAGE("Removing mapfile \"%s\"", MapFile);
|
2011-04-14 19:19:13 +01:00
|
|
|
else
|
|
|
|
INFORMATION_MESSAGE("Can't remove mapfile \"%s\"", MapFile);
|
|
|
|
#else /* SHM_MEMORY_MAPPING_SCHEME */
|
|
|
|
#ifdef YAPOR_COW
|
|
|
|
i = 0;
|
|
|
|
#else /* YAPOR_COPY || YAPOR_SBA */
|
2011-05-10 11:47:18 +01:00
|
|
|
for (i = 0; i < GLOBAL_number_workers + 1; i++)
|
2011-04-29 14:59:17 +01:00
|
|
|
#endif
|
2011-04-14 19:19:13 +01:00
|
|
|
{
|
|
|
|
if (shmctl(shm_mapid[i], IPC_RMID, 0) == 0)
|
|
|
|
INFORMATION_MESSAGE("Removing shared memory segment %d", shm_mapid[i]);
|
|
|
|
else
|
|
|
|
INFORMATION_MESSAGE("Can't remove shared memory segment %d", shm_mapid[i]);
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif /* MEMORY_MAPPING_SCHEME */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-04-14 19:19:13 +01:00
|
|
|
/* ------------------------- **
|
|
|
|
** Local functions **
|
|
|
|
** ------------------------- */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2011-04-14 19:19:13 +01:00
|
|
|
#ifdef MMAP_MEMORY_MAPPING_SCHEME
|
|
|
|
void open_mapfile(long TotalArea) {
|
|
|
|
char mapfile[20];
|
|
|
|
strcpy(mapfile,"./mapfile");
|
|
|
|
itos(getpid(), &mapfile[9]);
|
2011-05-23 16:19:47 +01:00
|
|
|
printf(" file %s \n", mapfile);
|
2011-04-14 19:19:13 +01:00
|
|
|
if ((fd_mapfile = open(mapfile, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0)
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "open error (open_mapfile)");
|
|
|
|
if (lseek(fd_mapfile, TotalArea, SEEK_SET) < 0)
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "lseek error (open_mapfile)");
|
|
|
|
if (write(fd_mapfile, "", 1) < 0)
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "write error (open_mapfile)");
|
|
|
|
return;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2011-04-14 19:19:13 +01:00
|
|
|
#else /* SHM_MEMORY_MAPPING_SCHEME */
|
|
|
|
void shm_map_memory(int id, int size, void *shmaddr) {
|
|
|
|
if (size > SHMMAX)
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "maximum size for a shm segment exceeded (shm_map_memory)");
|
|
|
|
if ((shm_mapid[id] = shmget(IPC_PRIVATE, size, SHM_R|SHM_W)) == -1)
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "shmget error (shm_map_memory)");
|
|
|
|
if (shmat(shm_mapid[id], shmaddr, 0) == (void *) -1)
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "shmat error (shm_map_memory)");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif /* MMAP_MEMORY_MAPPING_SCHEME */
|
2011-04-29 14:59:17 +01:00
|
|
|
#endif /* YAPOR_COPY || YAPOR_COW || YAPOR_SBA */
|