|
| 1 | +#include "postgres.h" |
| 2 | +#include "fmgr.h" |
| 3 | +#include "miscadmin.h" |
| 4 | +#include "postmaster/postmaster.h" |
| 5 | +#include "postmaster/bgworker.h" |
| 6 | +#include "storage/s_lock.h" |
| 7 | +#include "storage/spin.h" |
| 8 | +#include "storage/pg_sema.h" |
| 9 | +#include "storage/shmem.h" |
| 10 | + |
| 11 | +#include "bgwpool.h" |
| 12 | + |
| 13 | +typedef struct |
| 14 | +{ |
| 15 | + BgwPoolConstructor constructor; |
| 16 | + int id; |
| 17 | +} BgwPoolExecutorCtx; |
| 18 | + |
| 19 | +size_t n_snapshots; |
| 20 | +size_t n_active; |
| 21 | + |
| 22 | +static void BgwPoolMainLoop(Datum arg) |
| 23 | +{ |
| 24 | + BgwPoolExecutorCtx* ctx = (BgwPoolExecutorCtx*)arg; |
| 25 | + int id = ctx->id; |
| 26 | + BgwPool* pool = ctx->constructor(); |
| 27 | + int size; |
| 28 | + void* work; |
| 29 | + |
| 30 | + BackgroundWorkerUnblockSignals(); |
| 31 | + BackgroundWorkerInitializeConnection(pool->dbname, NULL); |
| 32 | + |
| 33 | + elog(WARNING, "Start background worker %d", id); |
| 34 | + |
| 35 | + while(true) { |
| 36 | + PGSemaphoreLock(&pool->available); |
| 37 | + SpinLockAcquire(&pool->lock); |
| 38 | + size = *(int*)&pool->queue[pool->head]; |
| 39 | + Assert(size < pool->size); |
| 40 | + work = palloc(size); |
| 41 | + pool->active -= 1; |
| 42 | + if (pool->head + size + 4 > pool->size) { |
| 43 | + memcpy(work, pool->queue, size); |
| 44 | + pool->head = INTALIGN(size); |
| 45 | + } else { |
| 46 | + memcpy(work, &pool->queue[pool->head+4], size); |
| 47 | + pool->head += 4 + INTALIGN(size); |
| 48 | + } |
| 49 | + if (pool->size == pool->head) { |
| 50 | + pool->head = 0; |
| 51 | + } |
| 52 | + if (pool->producerBlocked) { |
| 53 | + pool->producerBlocked = false; |
| 54 | + PGSemaphoreUnlock(&pool->overflow); |
| 55 | + } |
| 56 | + SpinLockRelease(&pool->lock); |
| 57 | + pool->executor(id, work, size); |
| 58 | + pfree(work); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +void BgwPoolInit(BgwPool* pool, BgwPoolExecutor executor, char const* dbname, size_t queueSize) |
| 63 | +{ |
| 64 | + pool->queue = (char*)ShmemAlloc(queueSize); |
| 65 | + pool->executor = executor; |
| 66 | + PGSemaphoreCreate(&pool->available); |
| 67 | + PGSemaphoreCreate(&pool->overflow); |
| 68 | + PGSemaphoreReset(&pool->available); |
| 69 | + PGSemaphoreReset(&pool->overflow); |
| 70 | + SpinLockInit(&pool->lock); |
| 71 | + pool->producerBlocked = false; |
| 72 | + pool->head = 0; |
| 73 | + pool->tail = 0; |
| 74 | + pool->size = queueSize; |
| 75 | + pool->active = 0; |
| 76 | + strcpy(pool->dbname, dbname); |
| 77 | +} |
| 78 | + |
| 79 | +void BgwPoolStart(int nWorkers, BgwPoolConstructor constructor) |
| 80 | +{ |
| 81 | + int i; |
| 82 | + BackgroundWorker worker; |
| 83 | + |
| 84 | + MemSet(&worker, 0, sizeof(BackgroundWorker)); |
| 85 | + worker.bgw_flags = BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION; |
| 86 | + worker.bgw_start_time = BgWorkerStart_ConsistentState; |
| 87 | + worker.bgw_main = BgwPoolMainLoop; |
| 88 | + worker.bgw_restart_time = 10; /* Wait 10 seconds for restart before crash */ |
| 89 | + |
| 90 | + for (i = 0; i < nWorkers; i++) { |
| 91 | + BgwPoolExecutorCtx* ctx = (BgwPoolExecutorCtx*)malloc(sizeof(BgwPoolExecutorCtx)); |
| 92 | + snprintf(worker.bgw_name, BGW_MAXLEN, "bgw_pool_worker_%d", i+1); |
| 93 | + ctx->id = i; |
| 94 | + ctx->constructor = constructor; |
| 95 | + worker.bgw_main_arg = (Datum)ctx; |
| 96 | + RegisterBackgroundWorker(&worker); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +void BgwPoolExecute(BgwPool* pool, void* work, size_t size) |
| 101 | +{ |
| 102 | + Assert(size+4 <= pool->size); |
| 103 | + |
| 104 | + SpinLockAcquire(&pool->lock); |
| 105 | + while (true) { |
| 106 | + if ((pool->head <= pool->tail && pool->size - pool->tail < size + 4 && pool->head < size) |
| 107 | + || (pool->head > pool->tail && pool->head - pool->tail < size + 4)) |
| 108 | + { |
| 109 | + pool->producerBlocked = true; |
| 110 | + SpinLockRelease(&pool->lock); |
| 111 | + PGSemaphoreLock(&pool->overflow); |
| 112 | + SpinLockAcquire(&pool->lock); |
| 113 | + } else { |
| 114 | + pool->active += 1; |
| 115 | + n_snapshots += 1; |
| 116 | + n_active += pool->active; |
| 117 | + *(int*)&pool->queue[pool->tail] = size; |
| 118 | + if (pool->size - pool->tail >= size + 4) { |
| 119 | + memcpy(&pool->queue[pool->tail+4], work, size); |
| 120 | + pool->tail += 4 + INTALIGN(size); |
| 121 | + } else { |
| 122 | + memcpy(pool->queue, work, size); |
| 123 | + pool->tail = INTALIGN(size); |
| 124 | + } |
| 125 | + if (pool->tail == pool->size) { |
| 126 | + pool->tail = 0; |
| 127 | + } |
| 128 | + PGSemaphoreUnlock(&pool->available); |
| 129 | + break; |
| 130 | + } |
| 131 | + } |
| 132 | + SpinLockRelease(&pool->lock); |
| 133 | +} |
| 134 | + |
0 commit comments