2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1996-2001
5
* Sleepycat Software. All rights reserved.
10
static const char revid[] = "$Id: mp_region.c,v 11.33 2001/06/13 01:42:58 bostic Exp $";
13
#ifndef NO_SYSTEM_INCLUDES
14
#include <sys/types.h>
23
static int __mpool_init __P((DB_ENV *, DB_MPOOL *, int, int));
24
#ifdef MUTEX_SYSTEM_RESOURCES
25
static size_t __mpool_region_maint __P((REGINFO *));
30
* Internal version of memp_open: only called from DB_ENV->open.
32
* PUBLIC: int __memp_open __P((DB_ENV *));
41
roff_t reg_size, *regids;
43
int htab_buckets, ret;
45
/* Figure out how big each cache region is. */
46
reg_size = (dbenv->mp_gbytes / dbenv->mp_ncache) * GIGABYTE;
47
reg_size += ((dbenv->mp_gbytes %
48
dbenv->mp_ncache) * GIGABYTE) / dbenv->mp_ncache;
49
reg_size += dbenv->mp_bytes / dbenv->mp_ncache;
52
* Figure out how many hash buckets each region will have. Assume we
53
* want to keep the hash chains with under 10 pages on each chain. We
54
* don't know the pagesize in advance, and it may differ for different
55
* files. Use a pagesize of 1K for the calculation -- we walk these
56
* chains a lot, they must be kept short.
58
htab_buckets = __db_tablesize((reg_size / (1 * 1024)) / 10);
60
/* Create and initialize the DB_MPOOL structure. */
61
if ((ret = __os_calloc(dbenv, 1, sizeof(*dbmp), &dbmp)) != 0)
63
LIST_INIT(&dbmp->dbregq);
64
TAILQ_INIT(&dbmp->dbmfq);
67
/* Join/create the first mpool region. */
68
memset(®info, 0, sizeof(REGINFO));
69
reginfo.type = REGION_TYPE_MPOOL;
70
reginfo.id = INVALID_REGION_ID;
71
reginfo.mode = dbenv->db_mode;
72
reginfo.flags = REGION_JOIN_OK;
73
if (F_ISSET(dbenv, DB_ENV_CREATE))
74
F_SET(®info, REGION_CREATE_OK);
75
if ((ret = __db_r_attach(dbenv, ®info, reg_size)) != 0)
79
* If we created the region, initialize it. Create or join any
82
if (F_ISSET(®info, REGION_CREATE)) {
84
* We define how many regions there are going to be, allocate
85
* the REGINFO structures and create them. Make sure we don't
86
* clear the wrong entries on error.
88
dbmp->nreg = dbenv->mp_ncache;
89
if ((ret = __os_calloc(dbenv,
90
dbmp->nreg, sizeof(REGINFO), &dbmp->reginfo)) != 0)
92
/* Make sure we don't clear the wrong entries on error. */
93
for (i = 0; i < dbmp->nreg; ++i)
94
dbmp->reginfo[i].id = INVALID_REGION_ID;
95
dbmp->reginfo[0] = reginfo;
97
/* Initialize the first region. */
98
if ((ret = __mpool_init(dbenv, dbmp, 0, htab_buckets)) != 0)
102
* Create/initialize remaining regions and copy their IDs into
105
mp = R_ADDR(dbmp->reginfo, dbmp->reginfo[0].rp->primary);
106
regids = R_ADDR(dbmp->reginfo, mp->regids);
107
for (i = 1; i < dbmp->nreg; ++i) {
108
dbmp->reginfo[i].type = REGION_TYPE_MPOOL;
109
dbmp->reginfo[i].id = INVALID_REGION_ID;
110
dbmp->reginfo[i].mode = dbenv->db_mode;
111
dbmp->reginfo[i].flags = REGION_CREATE_OK;
112
if ((ret = __db_r_attach(
113
dbenv, &dbmp->reginfo[i], reg_size)) != 0)
116
__mpool_init(dbenv, dbmp, i, htab_buckets)) != 0)
118
R_UNLOCK(dbenv, &dbmp->reginfo[i]);
120
regids[i] = dbmp->reginfo[i].id;
124
* Determine how many regions there are going to be, allocate
125
* the REGINFO structures and fill in local copies of that
128
mp = R_ADDR(®info, reginfo.rp->primary);
129
dbmp->nreg = mp->nreg;
130
if ((ret = __os_calloc(dbenv,
131
dbmp->nreg, sizeof(REGINFO), &dbmp->reginfo)) != 0)
133
/* Make sure we don't clear the wrong entries on error. */
134
for (i = 0; i < dbmp->nreg; ++i)
135
dbmp->reginfo[i].id = INVALID_REGION_ID;
136
dbmp->reginfo[0] = reginfo;
138
/* Join remaining regions. */
139
regids = R_ADDR(dbmp->reginfo, mp->regids);
140
for (i = 1; i < dbmp->nreg; ++i) {
141
dbmp->reginfo[i].type = REGION_TYPE_MPOOL;
142
dbmp->reginfo[i].id = regids[i];
143
dbmp->reginfo[i].mode = 0;
144
dbmp->reginfo[i].flags = REGION_JOIN_OK;
145
if ((ret = __db_r_attach(
146
dbenv, &dbmp->reginfo[i], 0)) != 0)
148
R_UNLOCK(dbenv, &dbmp->reginfo[i]);
152
/* Set the local addresses for the regions. */
153
for (i = 0; i < dbmp->nreg; ++i)
154
dbmp->reginfo[i].primary =
155
R_ADDR(&dbmp->reginfo[i], dbmp->reginfo[i].rp->primary);
157
/* If the region is threaded, allocate a mutex to lock the handles. */
158
if (F_ISSET(dbenv, DB_ENV_THREAD)) {
159
if ((ret = __db_mutex_alloc(
160
dbenv, dbmp->reginfo, 1, &dbmp->mutexp)) != 0) {
163
if ((ret = __db_shmutex_init(dbenv, dbmp->mutexp, 0,
164
MUTEX_THREAD, dbmp->reginfo,
165
(REGMAINT *)R_ADDR(dbmp->reginfo, mp->maint_off))) != 0)
169
R_UNLOCK(dbenv, dbmp->reginfo);
171
dbenv->mp_handle = dbmp;
174
err: if (dbmp->reginfo != NULL && dbmp->reginfo[0].addr != NULL) {
175
if (F_ISSET(dbmp->reginfo, REGION_CREATE))
176
ret = __db_panic(dbenv, ret);
178
R_UNLOCK(dbenv, dbmp->reginfo);
180
for (i = 0; i < dbmp->nreg; ++i)
181
if (dbmp->reginfo[i].id != INVALID_REGION_ID)
183
dbenv, &dbmp->reginfo[i], 0);
184
__os_free(dbenv, dbmp->reginfo,
185
dbmp->nreg * sizeof(*dbmp->reginfo));
187
if (dbmp->mutexp != NULL)
188
__db_mutex_free(dbenv, dbmp->reginfo, dbmp->mutexp);
189
__os_free(dbenv, dbmp, sizeof(*dbmp));
195
* Initialize a MPOOL structure in shared memory.
198
__mpool_init(dbenv, dbmp, reginfo_off, htab_buckets)
201
int reginfo_off, htab_buckets;
206
#ifdef MUTEX_SYSTEM_RESOURCES
214
reginfo = &dbmp->reginfo[reginfo_off];
215
if ((ret = __db_shalloc(reginfo->addr,
216
sizeof(MPOOL), MUTEX_ALIGN, ®info->primary)) != 0)
218
reginfo->rp->primary = R_OFFSET(reginfo, reginfo->primary);
219
mp = reginfo->primary;
220
memset(mp, 0, sizeof(*mp));
222
#ifdef MUTEX_SYSTEM_RESOURCES
223
maint_size = __mpool_region_maint(reginfo);
224
/* Allocate room for the maintenance info and initialize it. */
225
if ((ret = __db_shalloc(reginfo->addr,
226
sizeof(REGMAINT) + maint_size, 0, &p)) != 0)
228
__db_maintinit(reginfo, p, maint_size);
229
mp->maint_off = R_OFFSET(reginfo, p);
232
if (reginfo_off == 0) {
233
SH_TAILQ_INIT(&mp->mpfq);
235
if ((ret = __db_shmutex_init(dbenv, &mp->sync_mutex,
236
R_OFFSET(dbmp->reginfo, &mp->sync_mutex) +
237
DB_FCNTL_OFF_MPOOL, 0, dbmp->reginfo,
238
(REGMAINT *)R_ADDR(dbmp->reginfo, mp->maint_off))) != 0)
244
mp->nreg = dbmp->nreg;
245
if ((ret = __db_shalloc(dbmp->reginfo[0].addr,
246
dbmp->nreg * sizeof(int), 0, &p)) != 0)
248
mp->regids = R_OFFSET(dbmp->reginfo, p);
251
SH_TAILQ_INIT(&mp->bhq);
253
/* Allocate hash table space and initialize it. */
254
if ((ret = __db_shalloc(reginfo->addr,
255
htab_buckets * sizeof(DB_HASHTAB), 0, &htab)) != 0)
257
__db_hashinit(htab, htab_buckets);
258
mp->htab = R_OFFSET(reginfo, htab);
259
mp->htab_buckets = htab_buckets;
262
* Only the environment creator knows the total cache size, fill in
263
* those statistics now.
265
mp->stat.st_gbytes = dbenv->mp_gbytes;
266
mp->stat.st_bytes = dbenv->mp_bytes;
270
mem_err:__db_err(dbenv, "Unable to allocate memory for mpool region");
271
err: if (reginfo->primary != NULL)
272
__db_shalloc_free(reginfo->addr, reginfo->primary);
278
* Internal version of memp_close: only called from DB_ENV->close.
280
* PUBLIC: int __memp_close __P((DB_ENV *));
293
dbmp = dbenv->mp_handle;
295
/* Discard DB_MPREGs. */
296
while ((mpreg = LIST_FIRST(&dbmp->dbregq)) != NULL) {
297
LIST_REMOVE(mpreg, q);
298
__os_free(dbenv, mpreg, sizeof(DB_MPREG));
301
/* Discard DB_MPOOLFILEs. */
302
while ((dbmfp = TAILQ_FIRST(&dbmp->dbmfq)) != NULL)
303
if ((t_ret = __memp_fclose(dbmfp, 1)) != 0 && ret == 0)
306
/* Discard the thread mutex. */
307
if (dbmp->mutexp != NULL)
308
__db_mutex_free(dbenv, dbmp->reginfo, dbmp->mutexp);
310
/* Detach from the region(s). */
311
for (i = 0; i < dbmp->nreg; ++i)
312
if ((t_ret = __db_r_detach(
313
dbenv, &dbmp->reginfo[i], 0)) != 0 && ret == 0)
316
__os_free(dbenv, dbmp->reginfo, dbmp->nreg * sizeof(*dbmp->reginfo));
317
__os_free(dbenv, dbmp, sizeof(*dbmp));
319
dbenv->mp_handle = NULL;
323
#ifdef MUTEX_SYSTEM_RESOURCES
325
* __mpool_region_maint --
326
* Return the amount of space needed for region maintenance info.
330
__mpool_region_maint(infop)
337
* For mutex maintenance we need one mutex per possible page.
338
* Compute the maximum number of pages this cache can have.
339
* Also add in an mpool mutex and mutexes for all dbenv and db
342
numlocks = ((infop->rp->size / DB_MIN_PGSIZE) + 1);
343
numlocks += DB_MAX_HANDLES;
344
s = sizeof(roff_t) * numlocks;
350
* __mpool_region_destroy
351
* Destroy any region maintenance info.
353
* PUBLIC: void __mpool_region_destroy __P((DB_ENV *, REGINFO *));
356
__mpool_region_destroy(dbenv, infop)
360
__db_shlocks_destroy(infop, (REGMAINT *)R_ADDR(infop,
361
((MPOOL *)R_ADDR(infop, infop->rp->primary))->maint_off));
363
COMPQUIET(dbenv, NULL);
364
COMPQUIET(infop, NULL);