00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #include "postgres.h"
00056 #include "utils/hsearch.h"
00057 #include "storage/pg_shmem.h"
00058 #include "storage/shmem.h"
00059 #include "storage/lwlock.h"
00060 #include "access/xact.h"
00061 #include "miscadmin.h"
00062 #include "veil_version.h"
00063 #include "veil_shmem.h"
00064 #include "veil_funcs.h"
00065
00066 #if PG_VERSION_GE(8, 2)
00067
00068
00069
00070
00071
00072 static ShmemCtl *shared_meminfo = NULL;
00073
00074
00075
00076
00077
00078
00079 static bool prepared_for_switch = false;
00080
00081
00082
00083
00084
00085
00086 static LWLockId VeilLWLock = AddinShmemInitLock;
00087
00088
00089
00090
00091
00092 static LWLockId InitialLWLock = AddinShmemInitLock;
00093
00094
00095
00096
00097
00098
00099
00100
00101 #define OTHER_CONTEXT(x) (x ? 0: 1)
00102
00103
00104
00105
00106
00107
00108 static bool using_registered_shmem = false;
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 #ifndef VEIL_TRIAL
00124 void
00125 _PG_init()
00126 {
00127 int veil_dbs = veil_dbs_in_cluster();
00128
00129
00130 veil_config_init();
00131
00132
00133 RequestAddinShmemSpace(2 * veil_shmem_context_size() * veil_dbs);
00134
00135
00136 RequestAddinLWLocks(veil_dbs);
00137 }
00138 #endif
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149 static HTAB *
00150 create_shared_hash(const char *hashname)
00151 {
00152 HASHCTL hashctl;
00153 HTAB *result;
00154 char *db_hashname;
00155 int hash_elems = veil_shared_hash_elems();
00156
00157
00158
00159 db_hashname = (char *) vl_malloc(HASH_KEYLEN);
00160 (void) snprintf(db_hashname, HASH_KEYLEN - 1, "%s_%u",
00161 hashname, MyDatabaseId);
00162 hashctl.keysize = HASH_KEYLEN;
00163 hashctl.entrysize = sizeof(VarEntry);
00164
00165 result = ShmemInitHash(db_hashname, hash_elems,
00166 hash_elems, &hashctl, HASH_ELEM);
00167 pfree(db_hashname);
00168 return result;
00169 }
00170
00171
00172
00173
00174
00175
00176
00177 static HTAB *
00178 get_hash0()
00179 {
00180 static HTAB *hash0 = NULL;
00181
00182 if (!hash0) {
00183 hash0 = create_shared_hash("VEIL_SHARED1");
00184 }
00185 return hash0;
00186 }
00187
00188
00189
00190
00191
00192
00193
00194 static HTAB *
00195 get_hash1()
00196 {
00197 static HTAB *hash1 = NULL;
00198
00199 if (!hash1) {
00200 hash1 = create_shared_hash("VEIL_SHARED2");
00201 }
00202
00203 return hash1;
00204 }
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216 static MemContext *
00217 get_shmem_context(char *name,
00218 size_t size,
00219 bool *p_found)
00220 {
00221 int i;
00222 MemContext *context;
00223 char *uniqname = (char *) vl_malloc(strlen(name) + 16);
00224 int max_dbs = veil_dbs_in_cluster();
00225
00226 for (i = 0; i < max_dbs; i++) {
00227 (void) sprintf(uniqname, "%s_%d", name, i);
00228 context = ShmemInitStruct(uniqname, size, p_found);;
00229 if (!context) {
00230 ereport(ERROR,
00231 (errcode(ERRCODE_INTERNAL_ERROR),
00232 errmsg("veil: cannot allocate shared memory(1)")));
00233 }
00234
00235 if (*p_found) {
00236
00237 if (context->db_id == MyDatabaseId) {
00238
00239
00240 return context;
00241 }
00242 }
00243 else {
00244
00245 context->db_id = MyDatabaseId;
00246 context->next = sizeof(MemContext);
00247 context->limit = size;
00248 context->lwlock = VeilLWLock;
00249 return context;
00250 }
00251 }
00252
00253
00254
00255
00256
00257 for (i = 0; i < max_dbs; i++) {
00258 (void) sprintf(uniqname, "%s_%d", name, i);
00259 context = ShmemInitStruct(uniqname, size, p_found);;
00260
00261 if (!context) {
00262 ereport(ERROR,
00263 (errcode(ERRCODE_INTERNAL_ERROR),
00264 errmsg("veil: cannot allocate shared memory(2)")));
00265 }
00266
00267 if (*p_found) {
00268
00269 if (!vl_db_exists(context->db_id)) {
00270
00271 context->db_id = MyDatabaseId;
00272 context->next = sizeof(MemContext);
00273 context->limit = size;
00274
00275 *p_found = false;
00276 return context;
00277 }
00278 }
00279 else {
00280
00281
00282
00283 context->db_id = MyDatabaseId;
00284 context->next = sizeof(MemContext);
00285 context->limit = size;
00286 return context;
00287 }
00288 }
00289 ereport(ERROR,
00290 (errcode(ERRCODE_INTERNAL_ERROR),
00291 errmsg("veil: no more shared memory contexts allowed")));
00292 }
00293
00294
00295 static void shmalloc_init();
00296
00297
00298
00299
00300
00301
00302 static int
00303 get_cur_context_id()
00304 {
00305 static initialised = false;
00306 int context;
00307
00308 if (!initialised) {
00309 shmalloc_init();
00310 initialised = true;
00311 }
00312
00313 context = shared_meminfo->current_context;
00314 if (prepared_for_switch) {
00315 context = OTHER_CONTEXT(context);
00316 }
00317 else {
00318
00319
00320 if (TransactionIdPrecedes(GetCurrentTransactionId(),
00321 shared_meminfo->xid[context]))
00322 {
00323 context = OTHER_CONTEXT(context);
00324 }
00325 }
00326
00327 return context;
00328 }
00329
00330
00331
00332
00333
00334
00335 static MemContext *
00336 get_cur_context()
00337 {
00338 int context;
00339 context = get_cur_context_id();
00340 return shared_meminfo->context[context];
00341 }
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352 void *
00353 do_vl_shmalloc(MemContext *context,
00354 size_t size)
00355 {
00356 void *result;
00357 size_t amount = (size_t) MAXALIGN(size);
00358
00359 if ((amount + context->next) <= context->limit) {
00360 result = context + context->next;
00361 context->next += amount;
00362 }
00363 else {
00364 ereport(ERROR,
00365 (ERROR,
00366 (errcode(ERRCODE_INTERNAL_ERROR),
00367 errmsg("veil: out of shared memory"))));
00368 }
00369 return result;
00370 }
00371
00372
00373
00374
00375
00376
00377
00378
00379 void *
00380 vl_shmalloc(size_t size)
00381 {
00382 MemContext *context;
00383 void *result;
00384
00385 context = get_cur_context();
00386
00387 LWLockAcquire(VeilLWLock, LW_EXCLUSIVE);
00388 result = do_vl_shmalloc(context, size);
00389 LWLockRelease(VeilLWLock);
00390
00391 return result;
00392 }
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402 void
00403 vl_free(void *mem)
00404 {
00405 return;
00406 }
00407
00408
00409
00410
00411
00412
00413 static void
00414 shmalloc_init()
00415 {
00416 if (!shared_meminfo) {
00417 VarEntry *var;
00418 MemContext *context0;
00419 MemContext *context1;
00420 bool found = false;
00421 size_t allocated;
00422 HTAB *hash0;
00423 HTAB *hash1;
00424 size_t size;
00425
00426 size = veil_shmem_context_size();
00427
00428 LWLockAcquire(InitialLWLock, LW_EXCLUSIVE);
00429 context0 = get_shmem_context("VEIL_SHMEM0", size, &found);
00430
00431 if (found) {
00432 shared_meminfo = context0->memctl;
00433 VeilLWLock = shared_meminfo->veil_lwlock;
00434
00435
00436
00437 LWLockAcquire(VeilLWLock, LW_EXCLUSIVE);
00438 LWLockRelease(InitialLWLock);
00439 LWLockRelease(VeilLWLock);
00440 }
00441 else {
00442
00443
00444
00445
00446
00447
00448 shared_meminfo = do_vl_shmalloc(context0, sizeof(ShmemCtl));
00449
00450 if (context0->lwlock != InitialLWLock) {
00451
00452
00453 VeilLWLock = context0->lwlock;
00454 }
00455 else {
00456
00457 VeilLWLock = LWLockAssign();
00458 }
00459
00460
00461
00462 context0->lwlock = VeilLWLock;
00463 shared_meminfo->veil_lwlock = VeilLWLock;
00464
00465
00466 LWLockAcquire(VeilLWLock, LW_EXCLUSIVE);
00467 LWLockRelease(InitialLWLock);
00468
00469
00470
00471
00472 context1 = get_shmem_context("VEIL_SHMEM1", size, &found);
00473
00474
00475 context0->memctl = shared_meminfo;
00476 context1->memctl = shared_meminfo;
00477
00478
00479 shared_meminfo->type = OBJ_SHMEMCTL;
00480 shared_meminfo->current_context = 0;
00481 shared_meminfo->total_allocated[0] = size;
00482 shared_meminfo->total_allocated[1] = size;
00483 shared_meminfo->switching = false;
00484 shared_meminfo->context[0] = context0;
00485 shared_meminfo->context[1] = context1;
00486 shared_meminfo->xid[0] = GetCurrentTransactionId();
00487 shared_meminfo->xid[1] = shared_meminfo->xid[0];
00488 shared_meminfo->initialised = true;
00489
00490
00491 hash0 = get_hash0();
00492 hash1 = get_hash1();
00493
00494
00495 var = (VarEntry *) hash_search(hash0, (void *) "VEIL_SHMEMCTL",
00496 HASH_ENTER, &found);
00497
00498
00499 var->obj = (Object *) shared_meminfo;
00500 var->shared = true;
00501
00502 var = (VarEntry *) hash_search(hash0, (void *) "VEIL_SHMEMCTL",
00503 HASH_ENTER, &found);
00504
00505
00506 LWLockRelease(VeilLWLock);
00507 }
00508 }
00509 }
00510
00511
00512
00513
00514
00515
00516 HTAB *
00517 vl_get_shared_hash()
00518 {
00519 int context;
00520 HTAB *hash;
00521 static initialised = false;
00522
00523 if (!initialised) {
00524 (void) get_cur_context();
00525 initialised = true;
00526 }
00527
00528 context = get_cur_context_id();
00529
00530 if (context == 0) {
00531 hash = get_hash0();
00532 }
00533 else {
00534 hash = get_hash1();
00535 }
00536
00537 return hash;
00538 }
00539
00540
00541
00542
00543
00544
00545
00546 static void
00547 clear_hash(HTAB *hash)
00548 {
00549 static HASH_SEQ_STATUS status;
00550 VarEntry *var;
00551
00552 hash_seq_init(&status, hash);
00553 while (var = hash_seq_search(&status)) {
00554 if (strncmp("VEIL_SHMEMCTL", var->key, strlen("VEIL_SHMEMCTL")) != 0) {
00555 (void) hash_search(hash, var->key, HASH_REMOVE, NULL);
00556 }
00557 }
00558 }
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568 bool
00569 vl_prepare_context_switch()
00570 {
00571 int context_curidx;
00572 int context_newidx;
00573 HTAB *hash0 = get_hash0();
00574 HTAB *hash1 = get_hash1();
00575 TransactionId oldest_xid;
00576 MemContext *context;
00577
00578 (void) get_cur_context();
00579
00580 LWLockAcquire(VeilLWLock, LW_EXCLUSIVE);
00581
00582 if (shared_meminfo->switching) {
00583
00584 LWLockRelease(VeilLWLock);
00585 return false;
00586 }
00587
00588 shared_meminfo->switching = true;
00589
00590
00591
00592
00593 context_curidx = shared_meminfo->current_context;
00594 context_newidx = OTHER_CONTEXT(context_curidx);
00595
00596
00597
00598
00599 oldest_xid = GetOldestXmin(false);
00600 if (TransactionIdPrecedes(oldest_xid,
00601 shared_meminfo->xid[context_curidx]))
00602 {
00603
00604
00605
00606
00607 shared_meminfo->switching = false;
00608 LWLockRelease(VeilLWLock);
00609 return false;
00610 }
00611 else {
00612
00613
00614
00615 context = shared_meminfo->context[context_newidx];
00616 context->next = sizeof(MemContext);
00617
00618
00619
00620 if (context_newidx == 0) {
00621 context->next += sizeof(ShmemCtl);
00622 clear_hash(hash0);
00623 }
00624 else {
00625 clear_hash(hash1);
00626 }
00627 }
00628
00629 LWLockRelease(VeilLWLock);
00630 prepared_for_switch = true;
00631 return true;
00632 }
00633
00634
00635
00636
00637
00638
00639
00640 bool
00641 vl_complete_context_switch()
00642 {
00643 int context_curidx;
00644 int context_newidx;
00645
00646 if (!prepared_for_switch) {
00647 ereport(ERROR,
00648 (errcode(ERRCODE_INTERNAL_ERROR),
00649 errmsg("failed to complete context switch"),
00650 errdetail("Not prepared for switch - "
00651 "invalid state for operation")));
00652 }
00653
00654 LWLockAcquire(VeilLWLock, LW_EXCLUSIVE);
00655 context_curidx = shared_meminfo->current_context;
00656 context_newidx = OTHER_CONTEXT(context_curidx);
00657
00658 if (!shared_meminfo->switching) {
00659
00660 LWLockRelease(VeilLWLock);
00661
00662 ereport(ERROR,
00663 (errcode(ERRCODE_INTERNAL_ERROR),
00664 errmsg("failed to complete context switch"),
00665 errdetail("Session does not have switching set to true- "
00666 "invalid state for operation")));
00667 }
00668
00669 shared_meminfo->switching = false;
00670 shared_meminfo->current_context = context_newidx;
00671 shared_meminfo->xid[context_newidx] = GetCurrentTransactionId();
00672 LWLockRelease(VeilLWLock);
00673 prepared_for_switch = false;
00674 return true;
00675 }
00676
00677
00678
00679
00680
00681 void
00682 vl_force_context_switch()
00683 {
00684 int context_curidx;
00685 int context_newidx;
00686 MemContext *context;
00687 HTAB *hash0 = get_hash0();
00688 HTAB *hash1 = get_hash1();
00689
00690 (void) get_cur_context();
00691
00692 LWLockAcquire(VeilLWLock, LW_EXCLUSIVE);
00693
00694 context_curidx = shared_meminfo->current_context;
00695 context_newidx = OTHER_CONTEXT(context_curidx);
00696
00697
00698
00699 context = shared_meminfo->context[context_newidx];
00700 context->next = sizeof(MemContext);
00701
00702
00703
00704 if (context_newidx == 0) {
00705 context->next += sizeof(ShmemCtl);
00706 clear_hash(hash0);
00707 }
00708 else {
00709 clear_hash(hash1);
00710 }
00711
00712 shared_meminfo->switching = false;
00713 shared_meminfo->current_context = context_newidx;
00714 shared_meminfo->xid[context_newidx] = GetCurrentTransactionId();
00715 shared_meminfo->xid[0] = GetCurrentTransactionId();
00716 LWLockRelease(VeilLWLock);
00717 prepared_for_switch = false;
00718 }
00719
00720 #else
00721 #include "veil_shmem_pre82.inc"
00722
00723 #endif