[master] ba54815 Revert "Drop long broken umem code"

Nils Goroll nils.goroll at uplex.de
Thu Sep 28 10:31:06 UTC 2017


commit ba54815bc5d5d3a6e8a5df81eb463ec899a5643f
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Tue Sep 26 15:03:34 2017 +0200

    Revert "Drop long broken umem code"
    
    This reverts commit 766dee06e1eba0309453a1c6e1115975b46eed8b.
    
    Conflicts:
    	bin/varnishd/mgt/mgt_main.c

diff --git a/bin/varnishd/Makefile.am b/bin/varnishd/Makefile.am
index 1d60d91..228c83d 100644
--- a/bin/varnishd/Makefile.am
+++ b/bin/varnishd/Makefile.am
@@ -101,6 +101,7 @@ varnishd_SOURCES = \
 	storage/storage_persistent_silo.c \
 	storage/storage_persistent_subr.c \
 	storage/storage_simple.c \
+	storage/storage_umem.c \
 	waiter/mgt_waiter.c \
 	waiter/cache_waiter.c \
 	waiter/cache_waiter_epoll.c \
@@ -161,7 +162,7 @@ varnishd_LDADD = \
 	@SAN_LDFLAGS@ \
 	@JEMALLOC_LDADD@ \
 	@PCRE_LIBS@ \
-	${DL_LIBS} ${PTHREAD_LIBS} ${NET_LIBS} ${RT_LIBS} ${LIBM}
+	${DL_LIBS} ${PTHREAD_LIBS} ${NET_LIBS} ${RT_LIBS} ${LIBM} ${UMEM_LIBS}
 
 noinst_PROGRAMS = vhp_gen_hufdec
 vhp_gen_hufdec_SOURCES = hpack/vhp_gen_hufdec.c
diff --git a/bin/varnishd/Makefile.phk b/bin/varnishd/Makefile.phk
index f018d00..6882faa 100644
--- a/bin/varnishd/Makefile.phk
+++ b/bin/varnishd/Makefile.phk
@@ -89,6 +89,7 @@ PROG_SRC += storage/storage_persistent.c
 PROG_SRC += storage/storage_persistent_silo.c
 PROG_SRC += storage/storage_persistent_subr.c
 PROG_SRC += storage/storage_simple.c
+PROG_SRC += storage/storage_umem.c
 
 PROG_SRC += waiter/cache_waiter.c
 PROG_SRC += waiter/cache_waiter_epoll.c
diff --git a/bin/varnishd/mgt/mgt_main.c b/bin/varnishd/mgt/mgt_main.c
index 0bc438a..dd340db 100644
--- a/bin/varnishd/mgt/mgt_main.c
+++ b/bin/varnishd/mgt/mgt_main.c
@@ -126,6 +126,9 @@ usage(void)
 
 	printf(FMT, "-s [name=]kind[,options]", "Storage specification");
 	printf(FMT, "", "Can be specified multiple times.");
+#ifdef HAVE_LIBUMEM
+	printf(FMT, "", "  -s umem");
+#endif
 	printf(FMT, "", "  -s malloc");
 	printf(FMT, "", "  -s file");
 
diff --git a/bin/varnishd/storage/mgt_stevedore.c b/bin/varnishd/storage/mgt_stevedore.c
index c3e161d..ae2428f 100644
--- a/bin/varnishd/storage/mgt_stevedore.c
+++ b/bin/varnishd/storage/mgt_stevedore.c
@@ -122,6 +122,9 @@ static const struct choice STV_choice[] = {
 	{ "malloc",			&sma_stevedore },
 	{ "deprecated_persistent",	&smp_stevedore },
 	{ "persistent",			&smp_fake_stevedore },
+#if defined(HAVE_LIBUMEM) && 0
+	{ "umem",			&smu_stevedore },
+#endif
 	{ NULL,		NULL }
 };
 
diff --git a/bin/varnishd/storage/storage_umem.c b/bin/varnishd/storage/storage_umem.c
new file mode 100644
index 0000000..edc798f
--- /dev/null
+++ b/bin/varnishd/storage/storage_umem.c
@@ -0,0 +1,170 @@
+/*-
+ * Copyright (c) 2006 Verdens Gang AS
+ * Copyright (c) 2006-2011 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * Storage method based on umem_alloc(3MALLOC)
+ */
+
+//lint -e{766}
+#include "config.h"
+
+#if defined(HAVE_LIBUMEM) && 0
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <umem.h>
+
+#include "cache/cache.h"
+#include "cache/cache_obj.h"
+#include "storage/storage.h"
+#include "storage/storage_simple.h"
+
+static size_t			smu_max = SIZE_MAX;
+static MTX			smu_mtx;
+
+struct smu {
+	struct storage		s;
+	size_t			sz;
+};
+
+static struct storage *
+smu_alloc(const struct stevedore *st, size_t size)
+{
+	struct smu *smu;
+
+	Lck_Lock(&smu_mtx);
+	VSC_C_main->sma_nreq++;
+	if (VSC_C_main->sma_nbytes + size > smu_max)
+		size = 0;
+	else {
+		VSC_C_main->sma_nobj++;
+		VSC_C_main->sma_nbytes += size;
+		VSC_C_main->sma_balloc += size;
+	}
+	Lck_Unlock(&smu_mtx);
+
+	if (size == 0)
+		return (NULL);
+
+	smu = umem_zalloc(sizeof *smu, UMEM_DEFAULT);
+	if (smu == NULL)
+		return (NULL);
+	smu->sz = size;
+	smu->s.priv = smu;
+	smu->s.ptr = umem_alloc(size, UMEM_DEFAULT);
+	XXXAN(smu->s.ptr);
+	smu->s.len = 0;
+	smu->s.space = size;
+	smu->s.fd = -1;
+	smu->s.stevedore = st;
+	smu->s.magic = STORAGE_MAGIC;
+	return (&smu->s);
+}
+
+static void
+smu_free(struct storage *s)
+{
+	struct smu *smu;
+
+	CHECK_OBJ_NOTNULL(s, STORAGE_MAGIC);
+	smu = s->priv;
+	assert(smu->sz == smu->s.space);
+	Lck_Lock(&smu_mtx);
+	VSC_C_main->sma_nobj--;
+	VSC_C_main->sma_nbytes -= smu->sz;
+	VSC_C_main->sma_bfree += smu->sz;
+	Lck_Unlock(&smu_mtx);
+	umem_free(smu->s.ptr, smu->s.space);
+	umem_free(smu, sizeof *smu);
+}
+
+static void
+smu_trim(const struct storage *s, size_t size)
+{
+	struct smu *smu;
+	void *p;
+
+	CHECK_OBJ_NOTNULL(s, STORAGE_MAGIC);
+	smu = s->priv;
+	assert(smu->sz == smu->s.space);
+	if ((p = umem_alloc(size, UMEM_DEFAULT)) != NULL) {
+		memcpy(p, smu->s.ptr, size);
+		umem_free(smu->s.ptr, smu->s.space);
+		Lck_Lock(&smu_mtx);
+		VSC_C_main->sma_nbytes -= (smu->sz - size);
+		VSC_C_main->sma_bfree += smu->sz - size;
+		smu->sz = size;
+		Lck_Unlock(&smu_mtx);
+		smu->s.ptr = p;
+		smu->s.space = size;
+	}
+}
+
+static void
+smu_init(struct stevedore *parent, int ac, char * const *av)
+{
+	const char *e;
+	uintmax_t u;
+
+	(void)parent;
+
+	AZ(av[ac]);
+	if (ac > 1)
+		ARGV_ERR("(-sumem) too many arguments\n");
+
+	if (ac == 0 || *av[0] == '\0')
+		 return;
+
+	e = VNUM_2bytes(av[0], &u, 0);
+	if (e != NULL)
+		ARGV_ERR("(-sumem) size \"%s\": %s\n", av[0], e);
+	if ((u != (uintmax_t)(size_t)u))
+		ARGV_ERR("(-sumem) size \"%s\": too big\n", av[0]);
+	smu_max = u;
+}
+
+static void
+smu_open(const struct stevedore *st)
+{
+	(void)st;
+	AZ(pthread_mutex_init(&smu_mtx, NULL));
+}
+
+const struct stevedore smu_stevedore = {
+	.magic		=	STEVEDORE_MAGIC,
+	.name		=	"umem",
+	.init		=	smu_init,
+	.open		=	smu_open,
+	.alloc		=	smu_alloc,
+	.free		=	smu_free,
+	.trim		=	smu_trim,
+	.allocobj	=	SML_allocobj,
+	.panic		=	SML_panic,
+	.methods	=	&SML_methods,
+};
+
+#endif /* HAVE_UMEM_H */
diff --git a/config.phk b/config.phk
index b54257a..4111c35 100644
--- a/config.phk
+++ b/config.phk
@@ -71,7 +71,8 @@ if true ; then
 		sys/filio.h \
 		sys/mount.h \
 		sys/statvfs.h \
-		sys/vfs.h
+		sys/vfs.h \
+		umem.h
 	do
 		if [ -f /usr/include/$i ] ; then
 			n=`echo $i | tr '[a-z/.]' '[A-Z__]'`
@@ -295,6 +296,7 @@ exit 0
 # SO_SNDTIMEO_WORKS +
 # VCC_CC +
 # HAVE_EPOLL_CTL -
+# HAVE_LIBUMEM -
 # USE_PCRE_JIT -
 # _FILE_OFFSET_BITS -
 #
diff --git a/configure.ac b/configure.ac
index fa43c1e..5e20c0a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -87,6 +87,9 @@ if test "x$ax_cv_curses" != xyes; then
    AC_MSG_ERROR([requires an X/Open-compatible Curses library])
 fi
 
+# Userland slab allocator from Solaris, ported to other systems
+AC_CHECK_HEADERS([umem.h], [_VARNISH_CHECK_LIB(umem, umem_alloc)])
+
 # XXX: This _may_ be for OS/X
 AC_CHECK_LIBM
 AC_SUBST(LIBM)


More information about the varnish-commit mailing list