parallel_workers.c

text/plain

Filename: parallel_workers.c
Type: text/plain
Part: 0
Message: Re: simple patch for discussion
#include <stdio.h>
#include <limits.h>
#include <math.h>

#define Max(x, y)				((x) > (y) ? (x) : (y))

/* GUC */
double parallel_worker_scaling_logarithm = 3.0;

int masters_algoriothm(unsigned int heap_pages)
{
	int heap_parallel_workers = 1;
	int heap_parallel_threshold = 128;
	while (heap_pages >= (unsigned int) (heap_parallel_threshold * 3))
	{
		heap_parallel_workers++;
		heap_parallel_threshold *= 3;
		if (heap_parallel_threshold > INT_MAX / 3)
			break;		/* avoid overflow */
	}


	return heap_parallel_workers;
}

int workers_via_log(unsigned int heap_pages)
{
	int heap_parallel_threshold = 128;
	double log10scale;
	int		parallel_workers;
	
	if (heap_pages <= heap_parallel_threshold)
		return 1;

	log10scale = log(parallel_worker_scaling_logarithm);

	parallel_workers = (int) ceil(log(heap_pages - heap_parallel_threshold) / log10scale) - floor(log(heap_parallel_threshold) / log10scale);
	return Max(parallel_workers, 1);
}


int main(void)
{
	for (unsigned long long pages = 128; pages <= 0xffffffff; pages *= 1.1)
	{
		printf("Table Size = %llu MB pages = %u old workers = %d, new workers = %d\n",
			   ((unsigned long long) pages) * 8192 / 1024 / 1024, pages, masters_algoriothm(pages), workers_via_log(pages));
	}
	return 0;
}