2005-05-31 09:24:24 +01:00
|
|
|
/**********************************************************************
|
|
|
|
|
|
|
|
The OPTYap Prolog system
|
|
|
|
OPTYap extends the Yap Prolog system to support or-parallel tabling
|
|
|
|
|
|
|
|
Copyright: R. Rocha and NCC - University of Porto, Portugal
|
|
|
|
File: opt.memory.c
|
2005-06-03 19:28:11 +01:00
|
|
|
version: $Id: opt.memory.c,v 1.9 2005-06-03 18:28:11 ricroc Exp $
|
2005-05-31 09:24:24 +01:00
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
/* -------------------------------------- **
|
|
|
|
** Includes and local variables **
|
|
|
|
** -------------------------------------- */
|
|
|
|
|
|
|
|
#include "Yap.h"
|
2003-11-05 16:12:25 +00:00
|
|
|
#ifdef YAPOR
|
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"
|
|
|
|
#include "Heap.h"
|
|
|
|
#include "alloc.h"
|
|
|
|
#include "heapgc.h"
|
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
|
|
|
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
/* --------------------------- **
|
|
|
|
** Global functions **
|
|
|
|
** --------------------------- */
|
|
|
|
|
|
|
|
#ifdef SHM_MEMORY_MAPPING_SCHEME
|
|
|
|
void shm_map_memory(int id, int size, void *shmaddr) {
|
|
|
|
#define SHMMAX 0x2000000 /* as in <asm/shmparam.h> */
|
|
|
|
if (size > SHMMAX)
|
2005-05-31 01:49:49 +01:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "maximum size for a shm segment exceeded (shm_map_memory)");
|
2001-04-09 20:54:03 +01:00
|
|
|
if ((shm_mapid[id] = shmget(IPC_PRIVATE, size, SHM_R|SHM_W)) == -1)
|
2005-05-31 01:49:49 +01:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "shmget error (shm_map_memory)");
|
2001-04-09 20:54:03 +01:00
|
|
|
if (shmat(shm_mapid[id], shmaddr, 0) == (void *) -1)
|
2005-05-31 01:49:49 +01:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "shmat error (shm_map_memory)");
|
2001-04-09 20:54:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#else /* MMAP_MEMORY_MAPPING_SCHEME */
|
|
|
|
open_mapfile(long TotalArea) {
|
|
|
|
char mapfile[20];
|
|
|
|
strcpy(mapfile,"/tmp/mapfile");
|
|
|
|
itos(getpid(), &mapfile[12]);
|
|
|
|
if ((fd_mapfile = open(mapfile, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0)
|
2005-05-31 01:49:49 +01:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "open error (open_mapfile)");
|
2001-04-09 20:54:03 +01:00
|
|
|
if (lseek(fd_mapfile, TotalArea, SEEK_SET) < 0)
|
2005-05-31 01:49:49 +01:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "lseek error (open_mapfile)");
|
2001-04-09 20:54:03 +01:00
|
|
|
if (write(fd_mapfile, "", 1) < 0)
|
2005-05-31 01:49:49 +01:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "write error (open_mapfile)");
|
2001-04-09 20:54:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-11-05 16:12:25 +00:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
close_mapfile(void) {
|
|
|
|
if (close(fd_mapfile) < 0)
|
2005-05-31 01:49:49 +01:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "close error (close_mapfile)");
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif /* MMAP_MEMORY_MAPPING_SCHEME */
|
|
|
|
|
|
|
|
|
|
|
|
void map_memory(long HeapArea, long GlobalLocalArea, long TrailAuxArea, int n_workers) {
|
|
|
|
void *mmap_addr = (void *)MMAP_ADDR;
|
|
|
|
#ifdef ACOW
|
|
|
|
int private_fd_mapfile;
|
2003-11-05 16:12:25 +00:00
|
|
|
#if MMAP_MEMORY_MAPPING_SCHEME
|
|
|
|
long TotalArea;
|
|
|
|
#endif /* MMAP_MEMORY_MAPPING_SCHEME */
|
|
|
|
#else /* ENV_COPY || SBA */
|
|
|
|
int i;
|
|
|
|
long WorkerArea;
|
|
|
|
long TotalArea;
|
|
|
|
#endif /* YAPOR_MODEL */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2003-11-05 16:12:25 +00:00
|
|
|
/* initial allocation - model independent */
|
2001-04-09 20:54:03 +01:00
|
|
|
HeapArea = ADJUST_SIZE_TO_PAGE(HeapArea * KBYTES);
|
|
|
|
GlobalLocalArea = ADJUST_SIZE(GlobalLocalArea * KBYTES);
|
|
|
|
TrailAuxArea = ADJUST_SIZE(TrailAuxArea * KBYTES);
|
|
|
|
|
|
|
|
/* we'll need this later */
|
2002-11-26 22:19:48 +00:00
|
|
|
Yap_GlobalBase = mmap_addr + HeapArea;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2003-11-05 16:12:25 +00:00
|
|
|
/* shared memory allocation - model dependent */
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef ACOW
|
|
|
|
/* 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 */
|
|
|
|
#else /* ENV_COPY || SBA */
|
2001-04-09 20:54:03 +01:00
|
|
|
/* the others need n stacks */
|
|
|
|
WorkerArea = ADJUST_SIZE_TO_PAGE(GlobalLocalArea + TrailAuxArea);
|
|
|
|
TotalArea = HeapArea + WorkerArea * n_workers;
|
2003-11-05 16:12:25 +00:00
|
|
|
#endif /* YAPOR_MODEL */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2003-11-05 16:12:25 +00:00
|
|
|
/* mmap heap area */
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef MMAP_MEMORY_MAPPING_SCHEME
|
|
|
|
/* map total area in a single go */
|
|
|
|
open_mapfile(TotalArea);
|
|
|
|
if ((mmap_addr = mmap((void *) MMAP_ADDR, (size_t) TotalArea, PROT_READ|PROT_WRITE,
|
|
|
|
MAP_SHARED|MAP_FIXED, fd_mapfile, 0)) == (void *) -1)
|
2005-05-31 01:49:49 +01:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "mmap error (map_memory)");
|
2003-11-05 16:12:25 +00:00
|
|
|
#else /* SHM_MEMORY_MAPPING_SCHEME */
|
2001-04-09 20:54:03 +01:00
|
|
|
/* Most systems are limited regarding what we can allocate */
|
|
|
|
#ifdef ACOW
|
|
|
|
/* single shared segment in ACOW */
|
|
|
|
shm_map_memory(0, HeapArea, mmap_addr);
|
2003-11-05 16:12:25 +00:00
|
|
|
#else /* ENV_COPY || SBA */
|
|
|
|
/* place as segment n otherwise (0..n-1 reserved for worker areas */
|
2001-04-09 20:54:03 +01:00
|
|
|
shm_map_memory(n_workers, HeapArea, mmap_addr);
|
2003-11-05 16:12:25 +00:00
|
|
|
#endif /* YAPOR_MODEL */
|
|
|
|
#endif /* MEMORY_MAPPING_SCHEME */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#ifdef ACOW
|
|
|
|
/* just allocate local space for stacks */
|
|
|
|
if ((private_fd_mapfile = open("/dev/zero", O_RDWR)) < 0)
|
2005-05-31 01:49:49 +01:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "open error (map_memory)");
|
2002-11-26 22:19:48 +00:00
|
|
|
if (mmap(Yap_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)
|
2005-05-31 01:49:49 +01:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "mmap error (map_memory)");
|
2001-04-09 20:54:03 +01:00
|
|
|
close(private_fd_mapfile);
|
2003-11-05 16:12:25 +00:00
|
|
|
#else /* ENV_COPY || SBA */
|
2001-04-09 20:54:03 +01:00
|
|
|
for (i = 0; i < n_workers; i++) {
|
|
|
|
/* initialize worker vars */
|
2002-11-26 22:19:48 +00:00
|
|
|
worker_area(i) = Yap_GlobalBase + i * WorkerArea;
|
2001-04-09 20:54:03 +01:00
|
|
|
worker_offset(i) = worker_area(i) - worker_area(0);
|
|
|
|
#ifdef SHM_MEMORY_MAPPING_SCHEME
|
|
|
|
/* mapping worker area */
|
|
|
|
shm_map_memory(i, WorkerArea, worker_area(i));
|
|
|
|
#endif /* SHM_MEMORY_MAPPING_SCHEME */
|
|
|
|
}
|
2003-11-05 16:12:25 +00:00
|
|
|
#endif /* YAPOR_MODEL */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#ifdef SBA
|
|
|
|
/* alloc space for the sparse binding array */
|
|
|
|
sba_size = WorkerArea * n_workers;
|
|
|
|
if ((binding_array = (char *)malloc(sba_size)) == NULL)
|
2005-05-31 01:49:49 +01:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "malloc error (map_memory)");
|
2001-04-09 20:54:03 +01:00
|
|
|
if ((CELL)binding_array & MBIT) {
|
2005-05-31 01:49:49 +01:00
|
|
|
Yap_Error(INTERNAL_ERROR, TermNil, "binding_array start address conflicts with tag used in IDB (map_memory)");
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2002-11-26 22:19:48 +00:00
|
|
|
sba_offset = binding_array - Yap_GlobalBase;
|
2001-04-09 20:54:03 +01:00
|
|
|
sba_end = (int)binding_array + sba_size;
|
|
|
|
#endif /* SBA */
|
2002-11-26 22:19:48 +00:00
|
|
|
Yap_TrailBase = Yap_GlobalBase + GlobalLocalArea;
|
|
|
|
Yap_LocalBase = Yap_TrailBase - CellSize;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
if (TrailAuxArea > 262144) /* 262144 = 256 * 1024 */
|
2002-11-26 22:19:48 +00:00
|
|
|
Yap_TrailTop = Yap_TrailBase + TrailAuxArea - 131072; /* 131072 = 262144 / 2 */
|
2001-04-09 20:54:03 +01:00
|
|
|
else
|
2002-11-26 22:19:48 +00:00
|
|
|
Yap_TrailTop = Yap_TrailBase + TrailAuxArea / 2;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
HeapMax = Yap_TrailBase + TrailAuxArea - CellSize;
|
2002-11-26 22:19:48 +00:00
|
|
|
Yap_InitHeap(mmap_addr);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void unmap_memory (void) {
|
|
|
|
#ifdef SHM_MEMORY_MAPPING_SCHEME
|
|
|
|
int i;
|
|
|
|
#else /* MMAP_MEMORY_MAPPING_SCHEME */
|
|
|
|
char MapFile[20];
|
|
|
|
#endif /* MEMORY_MAPPING_SCHEME */
|
|
|
|
{
|
|
|
|
int proc;
|
2005-05-31 01:49:49 +01:00
|
|
|
INFORMATION_MESSAGE("Worker %d exiting...", worker_id);
|
2001-04-09 20:54:03 +01:00
|
|
|
for (proc = 0; proc < number_workers; proc++) {
|
|
|
|
if (proc != worker_id && worker_pid(proc) != 0) {
|
|
|
|
if (kill(worker_pid(proc), SIGKILL) != 0)
|
|
|
|
INFORMATION_MESSAGE("Can't kill process %d", worker_pid(proc));
|
|
|
|
else
|
|
|
|
INFORMATION_MESSAGE("Killing process %d", worker_pid(proc));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef ACOW
|
|
|
|
if (number_workers > 1) {
|
|
|
|
if (kill(GLOBAL_master_worker, SIGINT) != 0)
|
|
|
|
INFORMATION_MESSAGE("Can't kill process %d", GLOBAL_master_worker);
|
|
|
|
else
|
|
|
|
INFORMATION_MESSAGE("Killing process %d", GLOBAL_master_worker);
|
|
|
|
}
|
|
|
|
#endif /* ACOW */
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SHM_MEMORY_MAPPING_SCHEME
|
|
|
|
#ifdef ACOW
|
|
|
|
i = 0;
|
|
|
|
#else
|
|
|
|
for (i = 0; i < number_workers + 1; i++)
|
2003-11-05 16:12:25 +00:00
|
|
|
#endif /* ACOW */
|
2001-04-09 20:54:03 +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]);
|
|
|
|
}
|
|
|
|
#else /* MMAP_MEMORY_MAPPING_SCHEME */
|
|
|
|
strcpy(MapFile,"/tmp/mapfile");
|
|
|
|
#ifdef ACOW
|
|
|
|
itos(GLOBAL_master_worker, &MapFile[12]);
|
|
|
|
#else /* ENV_COPY || SBA */
|
|
|
|
itos(worker_pid(0), &MapFile[12]);
|
|
|
|
#endif
|
|
|
|
if (remove(MapFile) == 0)
|
|
|
|
INFORMATION_MESSAGE("Removing mapfile \"%s\"", MapFile);
|
|
|
|
else INFORMATION_MESSAGE("Can't remove mapfile \"%s\"", MapFile);
|
|
|
|
#endif /* MEMORY_MAPPING_SCHEME */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void remap_memory(void) {
|
|
|
|
#ifdef ACOW
|
|
|
|
/* do nothing */
|
|
|
|
#endif /* ACOW */
|
|
|
|
#ifdef SBA
|
|
|
|
/* setup workers so that they have different areas */
|
|
|
|
long WorkerArea = worker_offset(1);
|
|
|
|
|
2002-11-26 22:19:48 +00:00
|
|
|
Yap_GlobalBase += worker_id * WorkerArea;
|
|
|
|
Yap_TrailBase += worker_id * WorkerArea;
|
|
|
|
Yap_LocalBase += worker_id * WorkerArea;
|
|
|
|
Yap_TrailTop += worker_id * WorkerArea;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif /* SBA */
|
|
|
|
#ifdef ENV_COPY
|
|
|
|
void *remap_addr;
|
|
|
|
long remap_offset;
|
|
|
|
long WorkerArea;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
remap_addr = worker_area(0);
|
2005-03-04 20:30:14 +00:00
|
|
|
remap_offset = remap_addr - Yap_HeapBase;
|
2001-04-09 20:54:03 +01:00
|
|
|
WorkerArea = worker_offset(1);
|
|
|
|
#ifdef SHM_MEMORY_MAPPING_SCHEME
|
|
|
|
for (i = 0; i < number_workers; i++) {
|
|
|
|
if (shmdt(worker_area(i)) == -1)
|
2005-05-31 01:49:49 +01:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "shmdt error (remap_memory)");
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
for (i = 0; i < number_workers; i++) {
|
|
|
|
worker_area(i) = remap_addr + ((number_workers + i - worker_id) % number_workers) * WorkerArea;
|
|
|
|
if(shmat(shm_mapid[i], worker_area(i), 0) == (void *) -1)
|
2005-05-31 01:49:49 +01:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "shmat error (remap_memory)");
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#else /* MMAP_MEMORY_MAPPING_SCHEME */
|
|
|
|
if (munmap(remap_addr, (size_t)(WorkerArea * number_workers)) == -1)
|
2005-05-31 01:49:49 +01:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "munmap error (remap_memory)");
|
2001-04-09 20:54:03 +01:00
|
|
|
for (i = 0; i < number_workers; i++) {
|
|
|
|
worker_area(i) = remap_addr + ((number_workers + i - worker_id) % number_workers) * WorkerArea;
|
|
|
|
if (mmap(worker_area(i), (size_t)WorkerArea, PROT_READ|PROT_WRITE,
|
|
|
|
MAP_SHARED|MAP_FIXED, fd_mapfile, remap_offset + i * WorkerArea) == (void *) -1)
|
2005-05-31 01:49:49 +01:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "mmap error (remap_memory)");
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif /* MEMORY_MAPPING_SCHEME */
|
|
|
|
for (i = 0; i < number_workers; i++) {
|
|
|
|
worker_offset(i) = worker_area(i) - worker_area(worker_id);
|
|
|
|
}
|
|
|
|
#endif /* ENV_COPY */
|
|
|
|
}
|
|
|
|
#endif /* YAPOR */
|