summaryrefslogtreecommitdiff
path: root/contrib/parallel_dummy/parallel_dummy.c
blob: f92030e0663a256c03ee79dedb5a58332adcee62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*--------------------------------------------------------------------------
 *
 * parallel_dummy.c
 *		Test harness code for parallel mode code.
 *
 * Copyright (C) 2013-2014, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
 *		contrib/parallel_dummy/parallel_dummy.c
 *
 * -------------------------------------------------------------------------
 */

#include "postgres.h"

#include "access/heapam.h"
#include "access/parallel.h"
#include "access/xact.h"
#include "fmgr.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
#include "storage/spin.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/snapmgr.h"
#include "utils/tqual.h"

PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(parallel_sleep);
PG_FUNCTION_INFO_V1(parallel_count);

#define		PARALLEL_DUMMY_KEY			1

typedef struct
{
	int32		sleep_time;
} ParallelSleepInfo;

typedef struct
{
	int32		relid;
	slock_t		mutex;
	BlockNumber	lastblock;
	BlockNumber	currentblock;
	BlockNumber prefetchblock;
	int64		ntuples;
} ParallelCountInfo;

void		_PG_init(void);
void		sleep_worker_main(dsm_segment *seg, shm_toc *toc);
void		count_worker_main(dsm_segment *seg, shm_toc *toc);

static void count_helper(Relation rel, ParallelCountInfo *info);

int prefetch_distance;
int prefetch_increment;

void
_PG_init()
{
	DefineCustomIntVariable("parallel_dummy.prefetch_distance",
          "Sets the prefetch distance in blocks.",
		  NULL, &prefetch_distance,
		  0, 0, INT_MAX, PGC_USERSET, 0, NULL, NULL, NULL);
	DefineCustomIntVariable("parallel_dummy.prefetch_increment",
          "Sets the prefetch increment in blocks.",
		  NULL, &prefetch_increment,
		  8, 1, INT_MAX, PGC_USERSET, 0, NULL, NULL, NULL);
}


Datum
parallel_sleep(PG_FUNCTION_ARGS)
{
	int32		sleep_time = PG_GETARG_INT32(0);
	int32		nworkers = PG_GETARG_INT32(1);
	bool		already_in_parallel_mode = IsInParallelMode();
	ParallelContext *pcxt;
	ParallelSleepInfo *info;

	if (nworkers < 0)
		ereport(ERROR,
				(errmsg("number of parallel workers must be non-negative")));

	if (!already_in_parallel_mode)
		EnterParallelMode();

	pcxt = CreateParallelContextForExtension("parallel_dummy",
											 "sleep_worker_main",
											 nworkers);
	shm_toc_estimate_chunk(&pcxt->estimator, sizeof(ParallelSleepInfo));
	shm_toc_estimate_keys(&pcxt->estimator, 1);
	InitializeParallelDSM(pcxt);
	info = shm_toc_allocate(pcxt->toc, sizeof(ParallelSleepInfo));
	info->sleep_time = sleep_time;
	shm_toc_insert(pcxt->toc, PARALLEL_DUMMY_KEY, info);
	LaunchParallelWorkers(pcxt);

	/* here's where we do the "real work" ... */
	DirectFunctionCall1(pg_sleep, Float8GetDatum((float8) sleep_time));

	WaitForParallelWorkersToFinish(pcxt);
	DestroyParallelContext(pcxt);

	if (!already_in_parallel_mode)
		ExitParallelMode();

	PG_RETURN_VOID();
}

Datum
parallel_count(PG_FUNCTION_ARGS)
{
	Oid			relid = PG_GETARG_OID(0);
	int32		nworkers = PG_GETARG_INT32(1);
	bool		already_in_parallel_mode = IsInParallelMode();
	ParallelContext *pcxt;
	ParallelCountInfo *info;
	Relation	rel;
	int64		result;

	if (nworkers < 0)
		ereport(ERROR,
				(errmsg("number of parallel workers must be non-negative")));

	rel = relation_open(relid, AccessShareLock);

	if (!already_in_parallel_mode)
		EnterParallelMode();

	pcxt = CreateParallelContextForExtension("parallel_dummy",
											 "count_worker_main",
											 nworkers);
	shm_toc_estimate_chunk(&pcxt->estimator, sizeof(ParallelCountInfo));
	shm_toc_estimate_keys(&pcxt->estimator, 1);
	InitializeParallelDSM(pcxt);
	info = shm_toc_allocate(pcxt->toc, sizeof(ParallelCountInfo));
	info->relid = relid;
	SpinLockInit(&info->mutex);
	info->lastblock = RelationGetNumberOfBlocks(rel);
	info->currentblock = 0;
	info->prefetchblock = 0;
	info->ntuples = 0;
	shm_toc_insert(pcxt->toc, PARALLEL_DUMMY_KEY, info);
	LaunchParallelWorkers(pcxt);

	/* here's where we do the "real work" ... */
	count_helper(rel, info);

	WaitForParallelWorkersToFinish(pcxt);

	result = info->ntuples;

	DestroyParallelContext(pcxt);

	relation_close(rel, AccessShareLock);

	if (!already_in_parallel_mode)
		ExitParallelMode();

	PG_RETURN_INT64(result);
}

void
sleep_worker_main(dsm_segment *seg, shm_toc *toc)
{
	ParallelSleepInfo *info;

	info = shm_toc_lookup(toc, PARALLEL_DUMMY_KEY);
	Assert(info != NULL);

	/* here's where we do the "real work" ... */
	DirectFunctionCall1(pg_sleep, Float8GetDatum((float8) info->sleep_time));
}

void
count_worker_main(dsm_segment *seg, shm_toc *toc)
{
	ParallelCountInfo *info;
	Relation	rel;

	info = shm_toc_lookup(toc, PARALLEL_DUMMY_KEY);
	Assert(info != NULL);

	rel = relation_open(info->relid, AccessShareLock);
	count_helper(rel, info);
	relation_close(rel, AccessShareLock);
}

static void
count_helper(Relation rel, ParallelCountInfo *info)
{
	int64		ntuples = 0;
	int64		mytuples = 0;
	Oid			relid = info->relid;
	Snapshot	snapshot = GetActiveSnapshot();

	for (;;)
	{
		BlockNumber blkno;
		Buffer		buffer;
		Page		page;
		int			lines;
		OffsetNumber	lineoff;
		ItemId		lpp;
		bool		all_visible;
		bool		done = false;
#ifdef USE_PREFETCH
		BlockNumber prefetch_blkno = InvalidBlockNumber;
		uint32		prefetch_count = 0;
#endif

		CHECK_FOR_INTERRUPTS();

		SpinLockAcquire(&info->mutex);
		if (info->currentblock >= info->lastblock)
			done = true;
		else
		{
#ifdef USE_PREFETCH
			BlockNumber	max_prefetch;

			max_prefetch = info->lastblock - info->prefetchblock;
			if (max_prefetch > 0 &&
				info->prefetchblock - info->currentblock < prefetch_distance)
			{
				prefetch_blkno = info->prefetchblock;
				prefetch_count = Min(prefetch_increment, max_prefetch);
				info->prefetchblock += prefetch_count;
			}
#endif
			blkno = info->currentblock++;
		}
		info->ntuples += ntuples;
		SpinLockRelease(&info->mutex);

		mytuples += ntuples;
		if (done)
			break;

#ifdef USE_PREFETCH
		while (prefetch_count > 0)
		{
			PrefetchBuffer(rel, MAIN_FORKNUM, prefetch_blkno);
			++prefetch_blkno;
			--prefetch_count;
		}
#endif

		buffer = ReadBuffer(rel, blkno);
		LockBuffer(buffer, BUFFER_LOCK_SHARE);
		page = BufferGetPage(buffer);
		lines = PageGetMaxOffsetNumber(page);
		ntuples = 0;

		all_visible = PageIsAllVisible(page) && !snapshot->takenDuringRecovery;

		for (lineoff = FirstOffsetNumber, lpp = PageGetItemId(page, lineoff);
			 lineoff <= lines;
			 lineoff++, lpp++)
		{
			HeapTupleData	loctup;

			if (!ItemIdIsNormal(lpp))
				continue;
			if (all_visible)
			{
				++ntuples;
				continue;
			}

			loctup.t_tableOid = relid;
			loctup.t_data = (HeapTupleHeader) PageGetItem(page, lpp);
			loctup.t_len = ItemIdGetLength(lpp);

			if (HeapTupleSatisfiesVisibility(&loctup, snapshot, buffer))
				++ntuples;
		}

		UnlockReleaseBuffer(buffer);
	}

	elog(NOTICE, "PID %d counted " INT64_FORMAT " tuples", MyProcPid, mytuples);
}