This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/OPTYap/or.memory.c

266 lines
11 KiB
C
Raw Normal View History

/************************************************************************
** **
** 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 **
** **
************************************************************************/
/**************************************
** Includes & Declarations **
**************************************/
#include "Yap.h"
2011-04-29 14:59:17 +01:00
#if defined(YAPOR_COPY) || defined(YAPOR_COW) || defined(YAPOR_SBA)
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/mman.h>
#include "Yatom.h"
#include "alloc.h"
2011-04-14 19:19:13 +01:00
#include "or.macros.h"
/************************************
** Macros & Declarations **
************************************/
2011-06-02 17:01:00 +01:00
#define GLOBAL_LOCAL_STRUCTS_AREA ADJUST_SIZE_TO_PAGE(sizeof(struct global_data) + MAX_WORKERS * sizeof(struct worker_local))
#ifdef MMAP_MEMORY_MAPPING_SCHEME
#define PATH_MAX 1000
char mapfile_path[PATH_MAX];
2011-06-02 17:01:00 +01:00
#elif SHM_MEMORY_MAPPING_SCHEME
int shm_mapid[MAX_WORKERS + 2];
#endif /* MEMORY_MAPPING_SCHEME */
2011-04-14 19:19:13 +01:00
/******************************************
** Local functions declaration **
******************************************/
2011-06-02 17:01:00 +01:00
#ifdef SHM_MEMORY_MAPPING_SCHEME
2011-04-14 19:19:13 +01:00
void shm_map_memory(int id, int size, void *shmaddr);
2011-06-02 17:01:00 +01:00
void shm_unmap_memory(int id);
#endif /* SHM_MEMORY_MAPPING_SCHEME */
2011-04-14 19:19:13 +01:00
/********************************
** Global functions **
********************************/
void Yap_init_yapor_global_local_memory(void) {
2011-06-02 17:01:00 +01:00
Yap_local = (struct worker_local *)(MMAP_ADDR - GLOBAL_LOCAL_STRUCTS_AREA);
Yap_global = (struct global_data *)(MMAP_ADDR - sizeof(struct global_data));
#ifdef MMAP_MEMORY_MAPPING_SCHEME
{ int fd_mapfile;
if (getcwd(mapfile_path,PATH_MAX) == NULL)
Yap_Error(FATAL_ERROR, TermNil, "getcwd error (Yap_init_yapor_global_local_memory)");
strcat(mapfile_path,"/mapfile");
itos(getpid(), &mapfile_path[strlen(mapfile_path)]);
if (strlen(mapfile_path) >= PATH_MAX)
Yap_Error(FATAL_ERROR, TermNil, "PATH_MAX error (Yap_init_yapor_global_local_memory)");
if ((fd_mapfile = open(mapfile_path, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0)
2011-06-02 17:01:00 +01:00
Yap_Error(FATAL_ERROR, TermNil, "open error (Yap_init_yapor_global_local_memory)");
if (lseek(fd_mapfile, GLOBAL_LOCAL_STRUCTS_AREA, SEEK_SET) < 0)
Yap_Error(FATAL_ERROR, TermNil, "lseek error (Yap_init_yapor_global_local_memory)");
if (write(fd_mapfile, "", 1) < 0)
Yap_Error(FATAL_ERROR, TermNil, "write error (Yap_init_yapor_global_local_memory)");
if (mmap((void *) Yap_local, (size_t) GLOBAL_LOCAL_STRUCTS_AREA, 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)");
if (close(fd_mapfile) == -1)
Yap_Error(FATAL_ERROR, TermNil, "close error (Yap_init_yapor_global_local_memory)");
}
2011-06-02 17:01:00 +01:00
#elif SHM_MEMORY_MAPPING_SCHEME
/* place as segment MAX_WORKERS (0..MAX_WORKERS-1 reserved for worker areas) */
shm_map_memory(MAX_WORKERS, GLOBAL_LOCAL_STRUCTS_AREA, (void *) Yap_local);
#endif /* MEMORY_MAPPING_SCHEME */
return;
}
2011-06-20 10:40:18 +01:00
void Yap_init_yapor_stacks_memory(UInt TrailStackArea, UInt HeapStackArea, UInt GlobalLocalStackArea, int n_workers) {
2011-06-02 17:01:00 +01:00
long StacksArea;
TrailStackArea = ADJUST_SIZE_TO_PAGE(TrailStackArea);
HeapStackArea = ADJUST_SIZE_TO_PAGE(HeapStackArea);
GlobalLocalStackArea = ADJUST_SIZE_TO_PAGE(GlobalLocalStackArea);
Yap_worker_area_size = GlobalLocalStackArea + TrailStackArea;
#if defined(YAPOR_COPY) || defined(YAPOR_SBA)
StacksArea = HeapStackArea + Yap_worker_area_size * n_workers;
#elif defined(YAPOR_COW)
StacksArea = HeapStackArea;
#endif /* YAPOR_COPY || YAPOR_SBA || YAPOR_COW */
Yap_HeapBase = (ADDR) MMAP_ADDR;
2011-06-02 17:01:00 +01:00
LOCAL_GlobalBase = (ADDR) (MMAP_ADDR + HeapStackArea);
#ifdef MMAP_MEMORY_MAPPING_SCHEME
2011-06-02 17:01:00 +01:00
/* map stacks in a single go */
{ int fd_mapfile;
if ((fd_mapfile = open(mapfile_path, O_RDWR)) < 0)
Yap_Error(FATAL_ERROR, TermNil, "open error ( Yap_init_yapor_stacks_memory)");
if (lseek(fd_mapfile, GLOBAL_LOCAL_STRUCTS_AREA + StacksArea, SEEK_SET) < 0)
Yap_Error(FATAL_ERROR, TermNil, "lseek error (Yap_init_yapor_stacks_memory)");
if (write(fd_mapfile, "", 1) < 0)
Yap_Error(FATAL_ERROR, TermNil, "write error (Yap_init_yapor_stacks_memory)");
if (mmap((void *) Yap_HeapBase, (size_t) StacksArea, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd_mapfile, GLOBAL_LOCAL_STRUCTS_AREA) == (void *) -1)
Yap_Error(FATAL_ERROR, TermNil, "mmap error (Yap_init_yapor_stacks_memory)");
if (close(fd_mapfile) == -1)
Yap_Error(FATAL_ERROR, TermNil, "close error (Yap_init_yapor_stacks_memory)");
}
2011-06-02 17:01:00 +01:00
#elif SHM_MEMORY_MAPPING_SCHEME
/* place heap stack segment as MAX_WORKERS+1 */
shm_map_memory(MAX_WORKERS + 1, HeapStackArea, (void *) Yap_HeapBase);
#if defined(YAPOR_COPY) || defined(YAPOR_SBA)
/* map segments for worker areas as 0..MAX_WORKERS-1 */
{ int i;
2011-06-02 17:01:00 +01:00
for (i = 0; i < n_workers; i++)
shm_map_memory(i, Yap_worker_area_size, LOCAL_GlobalBase + Yap_worker_area_size * i);
}
#endif /* YAPOR_COPY || YAPOR_SBA */
#endif /* MEMORY_MAPPING_SCHEME */
#ifdef YAPOR_COW
/* just allocate local space for stacks */
2011-06-02 17:01:00 +01:00
{ int private_fd_mapfile;
if ((private_fd_mapfile = open("/dev/zero", O_RDWR)) < 0)
Yap_Error(FATAL_ERROR, TermNil, "open error (Yap_init_yapor_stacks_memory)");
if (mmap(LOCAL_GlobalBase, Yap_worker_area_size, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_FIXED, private_fd_mapfile, 0) == (void *) -1)
Yap_Error(FATAL_ERROR, TermNil, "mmap error (Yap_init_yapor_stacks_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)
2011-06-02 17:01:00 +01:00
Yap_Error(FATAL_ERROR, TermNil, "malloc error (Yap_init_yapor_stacks_memory)");
if ((CELL)binding_array & MBIT)
Yap_Error(INTERNAL_ERROR, TermNil, "binding_array start address conflicts with tag used in IDB (Yap_init_yapor_stacks_memory)");
sba_offset = binding_array - LOCAL_GlobalBase;
sba_end = (int)binding_array + sba_size;
#endif /* YAPOR_SBA */
2011-06-02 17:01:00 +01:00
LOCAL_TrailBase = LOCAL_GlobalBase + GlobalLocalStackArea;
LOCAL_LocalBase = LOCAL_TrailBase - CellSize;
2011-06-02 17:01:00 +01:00
LOCAL_TrailTop = LOCAL_TrailBase + TrailStackArea;
HeapLim = LOCAL_GlobalBase;
Yap_InitHeap(Yap_HeapBase);
return;
}
2011-06-02 17:01:00 +01:00
void Yap_remap_yapor_memory(void) {
#if defined(YAPOR_COPY)
int i;
void *remap_addr = LOCAL_GlobalBase;
2011-04-14 19:19:13 +01:00
#ifdef MMAP_MEMORY_MAPPING_SCHEME
int fd_mapfile;
long remap_offset = (ADDR) remap_addr - (ADDR) Yap_local;
if ((fd_mapfile = open(mapfile_path, O_RDWR)) < 0)
Yap_Error(FATAL_ERROR, TermNil, "open error (Yap_remap_yapor_memory)");
if (munmap(remap_addr, (size_t)(Yap_worker_area_size * GLOBAL_number_workers)) == -1)
2011-06-02 17:01:00 +01:00
Yap_Error(FATAL_ERROR, TermNil, "munmap error (Yap_remap_yapor_memory)");
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)
2011-06-02 17:01:00 +01:00
Yap_Error(FATAL_ERROR, TermNil, "mmap error (Yap_remap_yapor_memory)");
if (close(fd_mapfile) == -1)
Yap_Error(FATAL_ERROR, TermNil, "close error (Yap_remap_yapor_memory)");
2011-04-14 19:19:13 +01:00
#else /* SHM_MEMORY_MAPPING_SCHEME */
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)
2011-06-02 17:01:00 +01:00
Yap_Error(FATAL_ERROR, TermNil, "shmdt error (Yap_remap_yapor_memory)");
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)
2011-06-02 17:01:00 +01:00
Yap_Error(FATAL_ERROR, TermNil, "shmat error (Yap_remap_yapor_memory)");
2011-04-14 19:19:13 +01:00
#endif /* MEMORY_MAPPING_SCHEME */
2011-06-02 17:01:00 +01:00
#elif defined(YAPOR_SBA)
/* setup workers so that they have different areas */
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;
#endif /* YAPOR_COPY || YAPOR_SBA */
2011-04-14 19:19:13 +01:00
}
2011-06-02 17:01:00 +01:00
void Yap_unmap_yapor_memory (void) {
2011-04-14 19:19:13 +01:00
int i;
INFORMATION_MESSAGE("Worker %d exiting...", worker_id);
2011-06-02 17:01:00 +01:00
for (i = 0; i < GLOBAL_number_workers; i++)
if (i != worker_id && GLOBAL_worker_pid(i) != 0) {
if (kill(GLOBAL_worker_pid(i), SIGKILL) != 0)
INFORMATION_MESSAGE("Can't kill process %d", GLOBAL_worker_pid(i));
else
2011-06-02 17:01:00 +01:00
INFORMATION_MESSAGE("Killing process %d", GLOBAL_worker_pid(i));
}
2011-03-30 16:39:09 +01:00
#ifdef YAPOR_COW
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
INFORMATION_MESSAGE("Killing process %d", GLOBAL_master_worker);
}
2011-04-14 19:19:13 +01:00
#endif /* YAPOR_COW */
#ifdef MMAP_MEMORY_MAPPING_SCHEME
if (remove(mapfile_path) == 0)
INFORMATION_MESSAGE("Removing mapfile \"%s\"", mapfile_path);
else
INFORMATION_MESSAGE("Can't remove mapfile \"%s\"", mapfile_path);
2011-06-02 17:01:00 +01:00
#elif SHM_MEMORY_MAPPING_SCHEME
#if defined(YAPOR_COPY) || defined(YAPOR_SBA)
shm_unmap_memory(MAX_WORKERS);
shm_unmap_memory(MAX_WORKERS + 1);
for (i = 0; i < GLOBAL_number_workers; i++)
shm_unmap_memory(i);
#elif defined(YAPOR_COW)
shm_unmap_memory(0);
#endif /* YAPOR_COPY || YAPOR_SBA || YAPOR_COW */
#endif /* MEMORY_MAPPING_SCHEME */
return;
}
2011-04-14 19:19:13 +01:00
/* ------------------------- **
** Local functions **
** ------------------------- */
2011-06-02 17:01:00 +01:00
#ifdef SHM_MEMORY_MAPPING_SCHEME
2011-04-14 19:19:13 +01:00
void shm_map_memory(int id, int size, void *shmaddr) {
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;
}
2011-06-02 17:01:00 +01:00
void shm_unmap_memory(int id) {
if (shmctl(shm_mapid[id], IPC_RMID, 0) == 0)
INFORMATION_MESSAGE("Removing shared memory segment %d", shm_mapid[id]);
else
INFORMATION_MESSAGE("Can't remove shared memory segment %d", shm_mapid[id]);
return;
}
#endif /* SHM_MEMORY_MAPPING_SCHEME */
2011-04-29 14:59:17 +01:00
#endif /* YAPOR_COPY || YAPOR_COW || YAPOR_SBA */