From: Antti Louko <alo@iki.fi>
Subject: panda-dist patch
Date: Mon, 30 Jul 2012 19:19:41 +0300 [thread overview]
Message-ID: <jv6c6t$5c6$1@dont-email.me> (raw)
[-- Attachment #1: Type: text/plain, Size: 439 bytes --]
Hello,
I decided to post here my panda-dist klh10 patch. If you don't want to
use SYSV shared memory segments, you can apply this patch and use mmap
over normal file. The advantage is that mmap doesn't leave memory locked
or unclaimed in case of crashes.
mmap directory is needed under the directory whele klh10 is being run.
I have been trying to contact Mark Crispin to integrate this but he
still seems to be unreachable.
[-- Attachment #2: klh10-mmap.patch --]
[-- Type: text/x-patch, Size: 13677 bytes --]
diff -r d480ad380981 -r 5fedd998fc73 src/Makefile.mk
--- a/src/Makefile.mk Mon Jul 30 13:32:39 2012 +0300
+++ b/src/Makefile.mk Mon Jul 30 15:58:08 2012 +0300
@@ -163,7 +163,8 @@
vdisk.o dvrpxx.o dvrh20.o \
vmtape.o dvtm03.o \
dvni20.o dpsup.o \
- dvhost.o dvlites.o
+ dvhost.o dvlites.o \
+ mmap_fun.o
# Modules needed for KS10 version.
@@ -485,8 +486,8 @@
dprpxx.o: $(SRC)/dprpxx.c $(SRC)/dprpxx.h $(SRC)/dpsup.h $(SRC)/vdisk.c
$(BUILDMOD) $(SRC)/dprpxx.c
-dprpxx: dprpxx.o dpsup.o
- $(LINKER) $(LDFLAGS) $(LDOUTF) dprpxx dprpxx.o dpsup.o $(LIBS)
+dprpxx: dprpxx.o dpsup.o mmap_fun.o
+ $(LINKER) $(LDFLAGS) $(LDOUTF) dprpxx dprpxx.o dpsup.o mmap_fun.o $(LIBS)
# --------- TM03 tape drive subprocess
@@ -494,7 +495,7 @@
dptm03.o: $(SRC)/dptm03.c $(SRC)/dptm03.h $(SRC)/dpsup.h $(SRC)/vmtape.c
$(BUILDMOD) $(SRC)/dptm03.c
-OFILES_DPTM03=dptm03.o dpsup.o wfio.o prmstr.o
+OFILES_DPTM03=dptm03.o dpsup.o wfio.o prmstr.o mmap_fun.o
dptm03: $(OFILES_DPTM03)
$(LINKER) $(LDFLAGS) $(LDOUTF) dptm03 $(OFILES_DPTM03) $(LIBS)
@@ -505,8 +506,8 @@
dpni20.o: $(SRC)/dpni20.c $(SRC)/dpni20.h $(SRC)/dpsup.h
$(BUILDMOD) $(SRC)/dpni20.c
-dpni20: dpni20.o dpsup.o
- $(LINKER) $(LDFLAGS) $(LDOUTF) dpni20 dpni20.o dpsup.o $(LIBS)
+dpni20: dpni20.o dpsup.o mmap_fun.o
+ $(LINKER) $(LDFLAGS) $(LDOUTF) dpni20 dpni20.o dpsup.o mmap_fun.o $(LIBS)
# --------- IMP subprocess (ITS KS only; counterpart for dvlhdh)
@@ -712,6 +713,9 @@
wfio.o: $(SRC)/wfio.c $(SRC)/wfio.h
$(BUILDMOD) $(SRC)/wfio.c
+mmap_fun.o: $(SRC)/mmap_fun.c $(SRC)/mmap_fun.h
+ $(BUILDMOD) $(SRC)/mmap_fun.c
+
####################################################################
## OLD STUFF
diff -r d480ad380981 -r 5fedd998fc73 src/Mk-fbx86.mk
--- a/src/Mk-fbx86.mk Mon Jul 30 13:32:39 2012 +0300
+++ b/src/Mk-fbx86.mk Mon Jul 30 15:58:08 2012 +0300
@@ -27,7 +27,7 @@
-Wmissing-declarations -Wredundant-decls
# Source definitions
-CENVFLAGS = -DCENV_CPU_I386=1 -DCENV_SYS_FREEBSD=1 -DKLH10_DEV_LITES=1
+CENVFLAGS = -DCENV_CPU_I386=1 -DCENV_SYS_FREEBSD=1 -DKLH10_DEV_LITES=1 -DCENV_MMAP=1
# Any target with no customized rule here is simply passed on to the
# standard Makefile. If no target is specified, "usage" is passed on
diff -r d480ad380981 -r 5fedd998fc73 src/dprpxx.c
--- a/src/dprpxx.c Mon Jul 30 13:32:39 2012 +0300
+++ b/src/dprpxx.c Mon Jul 30 15:58:08 2012 +0300
@@ -60,6 +60,10 @@
# include <sys/ioctl.h>
#endif
+#if CENV_MMAP
+#include "mmap_fun.h"
+#endif
+
struct devdk {
struct dp_s d_dp; /* Point to shared memory area */
struct dprpxx_s *d_rp; /* Shorthand ptr to DPRPXX struct in area */
@@ -188,6 +192,11 @@
d->d_10mem = NULL;
if (d->d_rp->dprp_dma) {
/* Attempt to attach segment into our address space */
+#if CENV_MMAP
+ size_t memsiz;
+ char *ptr = (char *)mmap_attach_by_tp(d->d_rp->dprp_shmid,&memsiz);
+ d->d_10siz = memsiz / sizeof(w10_t);
+#else
char *ptr = (char *)shmat(d->d_rp->dprp_shmid, (void *)0, SHM_RND);
struct shmid_ds shmds;
@@ -208,7 +217,8 @@
/* Set size of 10 memory we have mapped */
d->d_10siz = shmds.shm_segsz / sizeof(w10_t);
}
-
+#endif
+
d->d_10mem = (w10_t *)ptr; /* Won, set up pointer! */
if (ptr && DBGFLG)
fprintf(stderr, "[dprpxx: Mapped 10 mem, %ld wds]",
diff -r d480ad380981 -r 5fedd998fc73 src/dpsup.c
--- a/src/dpsup.c Mon Jul 30 13:32:39 2012 +0300
+++ b/src/dpsup.c Mon Jul 30 15:58:08 2012 +0300
@@ -66,6 +66,11 @@
RCSID(dpsup_c,"$Id: dpsup.c,v 2.5 2003/02/23 18:16:05 klh Exp $")
#endif
+#if CENV_MMAP
+#include <sys/stat.h>
+#include "mmap_fun.h"
+#endif
+
static int dp_cxinit(struct dpc_s *, int, int, int, size_t, size_t);
\f
/* DP_INIT - Called from superior (KLH10) to initialize device subprocess
@@ -111,6 +116,17 @@
}
/* Create a shared mem seg. Set perms to owner-only RW. */
+#if CENV_MMAP
+ {
+ int thisid;
+ void *ptr;
+ ptr = mmap_new_segment(totsiz, &thisid);
+ if (!ptr)
+ return FALSE;
+ shmid = thisid;
+ dpc = (struct dpc_s *)ptr;
+ }
+#else
if ((shmid = shmget(IPC_PRIVATE, (u_int)totsiz, 0600)) == -1) {
fprintf(stderr, "[dp_init: shmget failed - %d]\r\n", errno);
return FALSE;
@@ -123,6 +139,7 @@
fprintf(stderr, "[dp_init: shmat failed - %d]\r\n", errno);
return FALSE;
}
+#endif
/* Won, init the shared DPC struct */
memset((char *)dpc, 0, totsiz);
@@ -133,8 +150,12 @@
if (!dp_cxinit(dpc, 1, outtyp, outarg, dpcsiz, outsiz)
|| !dp_cxinit(dpc, 0, intyp, inarg, dpcsiz+outsiz, insiz)) {
+#if CENV_MMAP
+ mmap_unmap_unlink(shmid, dpc);
+#else
shmdt((caddr_t)dpc); /* Detach attached segment */
shmctl(shmid, IPC_RMID, (struct shmid_ds *)NULL);
+#endif
fprintf(stderr, "[dp_init: xinit failed]\r\n");
return FALSE;
}
@@ -270,9 +291,13 @@
/* Try to kill shared mem segment */
if (dp->dp_type == DP_XT_MSIG) {
+#if CENV_MMAP
+ mmap_unmap_unlink(dp->dp_shmid, dp->dp_adr);
+#else
shmdt((caddr_t)(dp->dp_adr)); /* Detach attached segment */
shmctl(dp->dp_shmid, IPC_RMID, /* then try to flush it */
(struct shmid_ds *)NULL);
+#endif
dp->dp_adr = NULL;
dp->dp_shmid = 0;
dp->dp_type = 0;
@@ -374,12 +399,16 @@
}
/* Got shmid for segment from our parent, try attaching it! */
+#if CENV_MMAP
+ dpc = (struct dpc_s *)mmap_attach_by_tp(shmarg, 0);
+#else
dpc = (struct dpc_s *)shmat((int)shmarg, (void *)NULL, SHM_RND);
if (dpc == (struct dpc_s *)-1) {
fprintf(stderr, "[%s: Couldn't attach shmid 0x%lx]\r\n",
argv[0], shmarg);
return 0;
}
+#endif
/* Verify that we got a DP memory structure */
if (strncmp(dpc->dpc_magic, DPC_MAGIC, sizeof(dpc->dpc_magic)) != 0) {
diff -r d480ad380981 -r 5fedd998fc73 src/mmap_fun.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/mmap_fun.c Mon Jul 30 15:58:08 2012 +0300
@@ -0,0 +1,230 @@
+#if CENV_MMAP
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#include "mmap_fun.h"
+#include <string.h>
+
+#define MMAP_MAGIC 1270978931
+#define MMAP_MAGIC2 (MMAP_MAGIC + 0x01020304)
+
+#ifndef MMAP_DEBUG
+#define MMAP_DEBUG 0
+#endif
+
+struct mmap_info {
+ int magic;
+ size_t mmap_size;
+ int pagesize;
+ char fname[128];
+ char vinfo[1024];
+};
+
+struct mmap_info2 {
+ int magic;
+};
+
+char *
+tp_to_name(char *buf, int tp)
+
+{
+ int pid,lp;
+ pid = mmap_tp_to_pid(tp);
+ lp = mmap_tp_to_lp(tp);
+ sprintf(buf,"mmap/%05d.%05d",pid,lp);
+ return buf;
+}
+
+static int mmap_localpid = 0;
+
+int
+new_mmap_id()
+
+{
+ int mypid = getpid();
+ int thispid;
+
+ thispid = mmap_pid_lp_to_tp(mypid,mmap_localpid);
+ mmap_localpid++;
+ return thispid;
+}
+
+void *
+mmap_attach_by_tp(int thispid, size_t *memsizp)
+
+{
+ char namebuf[100];
+ char *mmap_fname;
+ int fd = -1;
+ struct stat stb;
+ void *rp = 0;
+ void *ptr;
+ int pagesize = getpagesize();
+ struct mmap_info *ip;
+ struct mmap_info2 *ip2;
+
+ mmap_fname = tp_to_name(namebuf,thispid);
+
+ MMAP_DEBUG && fprintf(stderr,"Pid:%d Attach: Opening file %s\n",getpid(),mmap_fname);
+ fd = open(mmap_fname,O_RDWR,0);
+ if (fd == -1) {
+ fprintf(stderr,"Pid:%d cannot open file %s\n",getpid(),mmap_fname);
+ goto err;
+ }
+ fstat(fd,&stb);
+ ptr = mmap(0,stb.st_size,PROT_READ|PROT_WRITE,MAP_SHARED|MAP_HASSEMAPHORE,fd,0);
+ MMAP_DEBUG && fprintf(stderr,"Pid:%d mmap returns %p\n",getpid(),ptr);
+ if (ptr == MAP_FAILED)
+ goto err;
+ if (minherit(ptr,stb.st_size,INHERIT_SHARE) != 0) {
+ fprintf(stderr,"Pid:%d minherit fails\n",getpid());
+ }
+ if (mlock(ptr,stb.st_size) != 0) {
+ fprintf(stderr,"Pid:%d mlock fails\n",getpid());
+ }
+ ip = ptr;
+ if (memsizp) {
+ *memsizp = ip->mmap_size - pagesize - pagesize;
+ }
+ if (ip->magic == MMAP_MAGIC) {
+ ip2 = ptr + ip->mmap_size - pagesize;
+ if (ip2->magic == MMAP_MAGIC2) {
+ MMAP_DEBUG && fprintf(stderr,"Pid:%d Mapped file %s %ld bytes, vinfo: %s\n",getpid(),mmap_fname,ip->mmap_size - pagesize,ip->vinfo);
+ } else {
+ fprintf(stderr,"Pid:%d Invalid magic2 %s\n",getpid(),mmap_fname);
+ goto err;
+ }
+ } else {
+ fprintf(stderr,"Pid:%d Invalid magic %s\n",getpid(),mmap_fname);
+ goto err;
+ }
+ rp = ptr + pagesize;
+ err:
+ if (fd != -1) {
+ close(fd);
+ fd = -1;
+ }
+ return rp;
+}
+
+void
+mmap_unmap_unlink(int thispid, void *ptr)
+
+{
+ char namebuf[100];
+ char *mmap_fname;
+ int fd = -1;
+ struct stat stb;
+ int pagesize = getpagesize();
+ struct mmap_info *ip;
+
+ ip = ptr - pagesize;
+
+ mmap_fname = tp_to_name(namebuf,thispid);
+
+#if 0
+ fd = open(mmap_fname,O_RDWR,0);
+ if (fd == -1)
+ goto err;
+ fstat(fd,&stb);
+ munmap(ptr, stb.st_size);
+#else
+ munmap(ip, ip->mmap_size);
+#endif
+ unlink(mmap_fname);
+
+ err:
+ if (fd != -1) {
+ close(fd);
+ fd = -1;
+ }
+}
+
+void*
+mmap_new_segment(size_t memsiz0, int *thisid_p)
+
+{
+ int thisid;
+ char namebuf[100];
+ char *mmap_fname;
+ void *rp = 0;
+ void *ptr;
+ int fd = -1;
+ int pagesize = getpagesize();
+ size_t memsiz;
+ struct mmap_info *ip;
+ struct mmap_info2 *ip2;
+
+ memsiz = pagesize + memsiz0 + pagesize;
+
+ thisid = new_mmap_id();
+
+ mmap_fname = tp_to_name(namebuf,thisid);
+
+ MMAP_DEBUG && fprintf(stderr,"Pid:%d Create: Opening file %s\n",getpid(),mmap_fname);
+ fd = open(mmap_fname,O_RDWR|O_CREAT|O_TRUNC,0666);
+
+ if (fd == -1) {
+ fprintf(stderr,"Pid:%d cannot open file %s\n",getpid(),mmap_fname);
+ goto err;
+ }
+
+ {
+ char b[16*1024];
+ int n;
+ size_t o;
+ memset(b,0,sizeof(b));
+
+ for(o = 0; o < memsiz; o += n) {
+ int n0;
+ n0 = memsiz - o;
+ if (n0 > sizeof(b))
+ n0 = sizeof(b);
+ n = write(fd,b,n0);
+ if (n != n0) {
+ goto err;
+ }
+ }
+ }
+
+#ifndef MAP_NOSYNC
+#define MAP_NOSYNC 0
+#endif
+ ptr = mmap(0,memsiz,PROT_READ|PROT_WRITE,MAP_SHARED|MAP_HASSEMAPHORE,fd,0);
+ MMAP_DEBUG && fprintf(stderr,"Pid:%d mmap returns %p\n",getpid(),ptr);
+ if (ptr == MAP_FAILED)
+ goto err;
+
+ if (minherit(ptr,memsiz,INHERIT_SHARE) != 0) {
+ fprintf(stderr,"Pid:%d minherit fails\n",getpid());
+ }
+ if (mlock(ptr,memsiz) != 0) {
+ fprintf(stderr,"Pid:%d mlock fails\n",getpid());
+ }
+
+ ip = ptr;
+ ip->mmap_size = memsiz;
+ ip->pagesize = pagesize;
+ strncpy(ip->fname,mmap_fname,sizeof(ip->fname));
+ sprintf(ip->vinfo,"\n.....verbose\nFilename: %s\nPagesize: %d\nMemsize: %ld\n",mmap_fname,pagesize,(long int)memsiz);
+ ip->magic = MMAP_MAGIC;
+ ip2 = ptr + memsiz - pagesize;
+ ip2->magic = MMAP_MAGIC2;
+ rp = ptr + pagesize;
+ if (thisid_p)
+ *thisid_p = thisid;
+ MMAP_DEBUG && fprintf(stderr,"Pid:%d Mapped file %s %ld bytes\n",getpid(),mmap_fname,ip->mmap_size - pagesize);
+
+ err:
+ if (fd != -1) {
+ close(fd);
+ fd = -1;
+ }
+
+ return rp;
+
+}
+#endif
diff -r d480ad380981 -r 5fedd998fc73 src/mmap_fun.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/mmap_fun.h Mon Jul 30 15:58:08 2012 +0300
@@ -0,0 +1,14 @@
+#ifndef __MMAP_FUN__
+#define __MMAP_FUN__ 1
+
+extern char *tp_to_name(char *buf, int tp);
+extern int new_mmap_id();
+extern void *mmap_attach_by_tp(int thispid, size_t *memsizp);
+extern void *mmap_new_segment(size_t memsiz, int *thisid_p);
+extern void mmap_unmap_unlink(int thispid, void *ptr);
+
+#define mmap_pid_lp_to_tp(pid,lp) (((pid) << 14) | ((lp) & 0x7fff))
+#define mmap_tp_to_pid(tp) (((tp) >> 14) & 0x3ffff)
+#define mmap_tp_to_lp(tp) ((tp) & 0x3fff)
+
+#endif
diff -r d480ad380981 -r 5fedd998fc73 src/osdsup.c
--- a/src/osdsup.c Mon Jul 30 13:32:39 2012 +0300
+++ b/src/osdsup.c Mon Jul 30 15:58:08 2012 +0300
@@ -44,7 +44,6 @@
*/
#include <stdio.h>
-#include <stdlib.h>
#include <string.h> /* For strrchr */
#include "klh10.h"
@@ -52,6 +51,10 @@
#include "osdsup.h"
#include "kn10ops.h"
+#if CENV_MMAP
+#include <sys/mman.h>
+#endif
+
#if CENV_SYS_UNIX
# include <sys/types.h>
# include <sys/stat.h>
@@ -1589,6 +1592,11 @@
return FALSE;
}
\f
+
+#if CENV_MMAP
+#include "mmap_fun.h"
+#endif
+
/* Memory Mapping facilities
** These use the SYSV IPC shared memory calls.
** It's becoming increasingly likely that most unices will have them.
@@ -1603,6 +1611,25 @@
char **aptr)
{
#if CENV_SYS_UNIX && KLH10_DEV_DP
+#if CENV_MMAP
+ int thisid;
+ void *ptr;
+ int r = FALSE;
+
+ *aptr = NULL;
+
+ ptr = mmap_new_segment(memsiz, &thisid);
+
+ if (!ptr)
+ goto err;
+
+ *amm = thisid;
+ *aptr = ptr;
+ r =TRUE;
+
+ err:
+ return r;
+#else
int shmid;
char *ptr;
@@ -1635,6 +1662,7 @@
*amm = shmid; /* Remember shared seg ID */
*aptr = ptr;
return TRUE;
+#endif
#else
errno = 0; /* No error, just not implemented */
return FALSE;
@@ -1651,10 +1679,14 @@
os_mmkill(osmm_t mm, char *ptr)
{
#if CENV_SYS_UNIX && KLH10_DEV_DP
+#if CENV_MMAP
+ mmap_unmap_unlink(mm, ptr);
+#else
shmdt((caddr_t)ptr); /* Detach attached segment */
shmctl(mm, IPC_RMID, /* then try to flush it */
(struct shmid_ds *)NULL);
#endif
+#endif
return TRUE;
}
reply other threads:[~2012-08-06 14:22 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox