mp_method.c 4.22 KB
Newer Older
unknown's avatar
unknown committed
1 2 3
/*-
 * See the file LICENSE for redistribution information.
 *
unknown's avatar
unknown committed
4
 * Copyright (c) 1996-2002
unknown's avatar
unknown committed
5 6 7 8 9
 *	Sleepycat Software.  All rights reserved.
 */
#include "db_config.h"

#ifndef lint
unknown's avatar
unknown committed
10
static const char revid[] = "$Id: mp_method.c,v 11.29 2002/03/27 04:32:27 bostic Exp $";
unknown's avatar
unknown committed
11 12 13 14 15
#endif /* not lint */

#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>

unknown's avatar
unknown committed
16 17 18
#ifdef HAVE_RPC
#include <rpc/rpc.h>
#endif
unknown's avatar
unknown committed
19 20 21
#endif

#include "db_int.h"
unknown's avatar
unknown committed
22 23
#include "dbinc/db_shash.h"
#include "dbinc/mp.h"
unknown's avatar
unknown committed
24 25

#ifdef HAVE_RPC
unknown's avatar
unknown committed
26 27
#include "dbinc_auto/db_server.h"
#include "dbinc_auto/rpc_client_ext.h"
unknown's avatar
unknown committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#endif

static int __memp_set_cachesize __P((DB_ENV *, u_int32_t, u_int32_t, int));
static int __memp_set_mp_mmapsize __P((DB_ENV *, size_t));

/*
 * __memp_dbenv_create --
 *	Mpool specific creation of the DB_ENV structure.
 *
 * PUBLIC: void __memp_dbenv_create __P((DB_ENV *));
 */
void
__memp_dbenv_create(dbenv)
	DB_ENV *dbenv;
{
	/*
unknown's avatar
unknown committed
44 45 46 47 48
	 * !!!
	 * Our caller has not yet had the opportunity to reset the panic
	 * state or turn off mutex locking, and so we can neither check
	 * the panic state or acquire a mutex in the DB_ENV create path.
	 *
unknown's avatar
unknown committed
49 50 51 52
	 * We default to 32 8K pages.  We don't default to a flat 256K, because
	 * some systems require significantly more memory to hold 32 pages than
	 * others.  For example, HP-UX with POSIX pthreads needs 88 bytes for
	 * a POSIX pthread mutex and almost 200 bytes per buffer header, while
unknown's avatar
unknown committed
53 54
	 * Solaris needs 24 and 52 bytes for the same structures.  The minimum
	 * number of hash buckets is 37.  These contain a mutex also.
unknown's avatar
unknown committed
55
	 */
unknown's avatar
unknown committed
56 57
	dbenv->mp_bytes =
	    32 * ((8 * 1024) + sizeof(BH)) + 37 * sizeof(DB_MPOOL_HASH);
unknown's avatar
unknown committed
58 59
	dbenv->mp_ncache = 1;

unknown's avatar
unknown committed
60
#ifdef HAVE_RPC
unknown's avatar
unknown committed
61 62 63
	if (F_ISSET(dbenv, DB_ENV_RPCCLIENT)) {
		dbenv->set_cachesize = __dbcl_env_cachesize;
		dbenv->set_mp_mmapsize = __dbcl_set_mp_mmapsize;
unknown's avatar
unknown committed
64 65 66 67 68 69 70 71
		dbenv->memp_dump_region = NULL;
		dbenv->memp_fcreate = __dbcl_memp_fcreate;
		dbenv->memp_nameop = NULL;
		dbenv->memp_register = __dbcl_memp_register;
		dbenv->memp_stat = __dbcl_memp_stat;
		dbenv->memp_sync = __dbcl_memp_sync;
		dbenv->memp_trickle = __dbcl_memp_trickle;
	} else
unknown's avatar
unknown committed
72
#endif
unknown's avatar
unknown committed
73 74 75 76 77 78 79 80 81 82 83
	{
		dbenv->set_cachesize = __memp_set_cachesize;
		dbenv->set_mp_mmapsize = __memp_set_mp_mmapsize;
		dbenv->memp_dump_region = __memp_dump_region;
		dbenv->memp_fcreate = __memp_fcreate;
		dbenv->memp_nameop = __memp_nameop;
		dbenv->memp_register = __memp_register;
		dbenv->memp_stat = __memp_stat;
		dbenv->memp_sync = __memp_sync;
		dbenv->memp_trickle = __memp_trickle;
	}
unknown's avatar
unknown committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97
}

/*
 * __memp_set_cachesize --
 *	Initialize the cache size.
 */
static int
__memp_set_cachesize(dbenv, gbytes, bytes, ncache)
	DB_ENV *dbenv;
	u_int32_t gbytes, bytes;
	int ncache;
{
	ENV_ILLEGAL_AFTER_OPEN(dbenv, "set_cachesize");

unknown's avatar
unknown committed
98 99 100
	/* Normalize the values. */
	if (ncache == 0)
		ncache = 1;
unknown's avatar
unknown committed
101 102

	/*
unknown's avatar
unknown committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
	 * You can only store 4GB-1 in an unsigned 32-bit value, so correct for
	 * applications that specify 4GB cache sizes -- we know what they meant.
	 */
	if (gbytes / ncache == 4 && bytes == 0) {
		--gbytes;
		bytes = GIGABYTE - 1;
	} else {
		gbytes += bytes / GIGABYTE;
		bytes %= GIGABYTE;
	}

	/* Avoid too-large cache sizes, they result in a region size of zero. */
	if (gbytes / ncache > 4 || (gbytes / ncache == 4 && bytes != 0)) {
		__db_err(dbenv, "individual cache size too large");
		return (EINVAL);
	}

	/*
	 * If the application requested less than 500Mb, increase the cachesize
	 * by 25% and factor in the size of the hash buckets to account for our
	 * overhead.  (I'm guessing caches over 500Mb are specifically sized,
	 * that is, it's a large server and the application actually knows how
	 * much memory is available.  We only document the 25% overhead number,
	 * not the hash buckets, but I don't see a reason to confuse the issue,
	 * it shouldn't matter to an application.)
unknown's avatar
unknown committed
128 129 130
	 *
	 * There is a minimum cache size, regardless.
	 */
unknown's avatar
unknown committed
131 132 133 134 135
	if (gbytes == 0) {
		if (bytes < 500 * MEGABYTE)
			bytes += (bytes / 4) + 37 * sizeof(DB_MPOOL_HASH);
		if (bytes / ncache < DB_CACHESIZE_MIN)
			bytes = ncache * DB_CACHESIZE_MIN;
unknown's avatar
unknown committed
136 137
	}

unknown's avatar
unknown committed
138 139 140 141
	dbenv->mp_gbytes = gbytes;
	dbenv->mp_bytes = bytes;
	dbenv->mp_ncache = ncache;

unknown's avatar
unknown committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
	return (0);
}

/*
 * __memp_set_mp_mmapsize --
 *	Set the maximum mapped file size.
 */
static int
__memp_set_mp_mmapsize(dbenv, mp_mmapsize )
	DB_ENV *dbenv;
	size_t mp_mmapsize;
{
	dbenv->mp_mmapsize = mp_mmapsize;
	return (0);
}