2012-12-27 08:26:30 +08:00
|
|
|
/*
|
|
|
|
* parallel.c
|
|
|
|
*
|
|
|
|
* multi-process support
|
|
|
|
*
|
2014-01-08 05:05:30 +08:00
|
|
|
* Copyright (c) 2010-2014, PostgreSQL Global Development Group
|
2012-12-27 08:26:30 +08:00
|
|
|
* contrib/pg_upgrade/parallel.c
|
|
|
|
*/
|
|
|
|
|
Create libpgcommon, and move pg_malloc et al to it
libpgcommon is a new static library to allow sharing code among the
various frontend programs and backend; this lets us eliminate duplicate
implementations of common routines. We avoid libpgport, because that's
intended as a place for porting issues; per discussion, it seems better
to keep them separate.
The first use case, and the only implemented by this patch, is pg_malloc
and friends, which many frontend programs were already using.
At the same time, we can use this to provide palloc emulation functions
for the frontend; this way, some palloc-using files in the backend can
also be used by the frontend cleanly. To do this, we change palloc() in
the backend to be a function instead of a macro on top of
MemoryContextAlloc(). This was previously believed to cause loss of
performance, but this implementation has been tweaked by Tom and Andres
so that on modern compilers it provides a slight improvement over the
previous one.
This lets us clean up some places that were already with
localized hacks.
Most of the pg_malloc/palloc changes in this patch were authored by
Andres Freund. Zoltán Böszörményi also independently provided a form of
that. libpgcommon infrastructure was authored by Álvaro.
2013-02-12 21:33:40 +08:00
|
|
|
#include "postgres_fe.h"
|
2012-12-27 08:26:30 +08:00
|
|
|
|
|
|
|
#include "pg_upgrade.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
2013-05-30 04:58:43 +08:00
|
|
|
static int parallel_jobs;
|
2012-12-27 08:26:30 +08:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
/*
|
|
|
|
* Array holding all active threads. There can't be any gaps/zeros so
|
|
|
|
* it can be passed to WaitForMultipleObjects(). We use two arrays
|
|
|
|
* so the thread_handles array can be passed to WaitForMultipleObjects().
|
|
|
|
*/
|
2013-05-30 04:58:43 +08:00
|
|
|
HANDLE *thread_handles;
|
2012-12-27 08:26:30 +08:00
|
|
|
|
2013-05-30 04:58:43 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
2013-07-25 01:15:47 +08:00
|
|
|
char *log_file;
|
|
|
|
char *opt_log_file;
|
|
|
|
char *cmd;
|
2013-01-09 21:57:47 +08:00
|
|
|
} exec_thread_arg;
|
2012-12-27 08:26:30 +08:00
|
|
|
|
2013-05-30 04:58:43 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
DbInfoArr *old_db_arr;
|
|
|
|
DbInfoArr *new_db_arr;
|
2013-07-25 01:15:47 +08:00
|
|
|
char *old_pgdata;
|
|
|
|
char *new_pgdata;
|
|
|
|
char *old_tablespace;
|
2013-01-09 21:57:47 +08:00
|
|
|
} transfer_thread_arg;
|
|
|
|
|
|
|
|
exec_thread_arg **exec_thread_args;
|
|
|
|
transfer_thread_arg **transfer_thread_args;
|
|
|
|
|
|
|
|
/* track current thread_args struct so reap_child() can be used for all cases */
|
2013-05-30 04:58:43 +08:00
|
|
|
void **cur_thread_args;
|
2012-12-27 08:26:30 +08:00
|
|
|
|
2013-05-30 04:58:43 +08:00
|
|
|
DWORD win32_exec_prog(exec_thread_arg *args);
|
|
|
|
DWORD win32_transfer_all_new_dbs(transfer_thread_arg *args);
|
2012-12-27 08:26:30 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* parallel_exec_prog
|
|
|
|
*
|
|
|
|
* This has the same API as exec_prog, except it does parallel execution,
|
|
|
|
* and therefore must throw errors and doesn't return an error status.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
parallel_exec_prog(const char *log_file, const char *opt_log_file,
|
|
|
|
const char *fmt,...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char cmd[MAX_STRING];
|
2013-05-30 04:58:43 +08:00
|
|
|
|
2012-12-27 08:26:30 +08:00
|
|
|
#ifndef WIN32
|
|
|
|
pid_t child;
|
|
|
|
#else
|
|
|
|
HANDLE child;
|
2013-05-30 04:58:43 +08:00
|
|
|
exec_thread_arg *new_arg;
|
2012-12-27 08:26:30 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
vsnprintf(cmd, sizeof(cmd), fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (user_opts.jobs <= 1)
|
|
|
|
/* throw_error must be true to allow jobs */
|
|
|
|
exec_prog(log_file, opt_log_file, true, "%s", cmd);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* parallel */
|
2013-01-09 21:57:47 +08:00
|
|
|
#ifdef WIN32
|
2013-07-25 10:01:14 +08:00
|
|
|
if (thread_handles == NULL)
|
|
|
|
thread_handles = pg_malloc(user_opts.jobs * sizeof(HANDLE));
|
|
|
|
|
|
|
|
if (exec_thread_args == NULL)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
exec_thread_args = pg_malloc(user_opts.jobs * sizeof(exec_thread_arg *));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For safety and performance, we keep the args allocated during
|
|
|
|
* the entire life of the process, and we don't free the args in a
|
|
|
|
* thread different from the one that allocated it.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < user_opts.jobs; i++)
|
|
|
|
exec_thread_args[i] = pg_malloc0(sizeof(exec_thread_arg));
|
|
|
|
}
|
|
|
|
|
2013-05-30 04:58:43 +08:00
|
|
|
cur_thread_args = (void **) exec_thread_args;
|
|
|
|
#endif
|
2012-12-27 08:26:30 +08:00
|
|
|
/* harvest any dead children */
|
|
|
|
while (reap_child(false) == true)
|
|
|
|
;
|
|
|
|
|
|
|
|
/* must we wait for a dead child? */
|
|
|
|
if (parallel_jobs >= user_opts.jobs)
|
|
|
|
reap_child(true);
|
2013-05-30 04:58:43 +08:00
|
|
|
|
2012-12-27 08:26:30 +08:00
|
|
|
/* set this before we start the job */
|
|
|
|
parallel_jobs++;
|
2013-05-30 04:58:43 +08:00
|
|
|
|
2012-12-27 08:26:30 +08:00
|
|
|
/* Ensure stdio state is quiesced before forking */
|
|
|
|
fflush(NULL);
|
|
|
|
|
|
|
|
#ifndef WIN32
|
|
|
|
child = fork();
|
|
|
|
if (child == 0)
|
|
|
|
/* use _exit to skip atexit() functions */
|
|
|
|
_exit(!exec_prog(log_file, opt_log_file, true, "%s", cmd));
|
|
|
|
else if (child < 0)
|
|
|
|
/* fork failed */
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("could not create worker process: %s\n", strerror(errno));
|
2012-12-27 08:26:30 +08:00
|
|
|
#else
|
2013-07-30 21:23:31 +08:00
|
|
|
/* empty array element are always at the end */
|
2013-05-30 04:58:43 +08:00
|
|
|
new_arg = exec_thread_args[parallel_jobs - 1];
|
2012-12-27 08:26:30 +08:00
|
|
|
|
|
|
|
/* Can only pass one pointer into the function, so use a struct */
|
2013-07-25 01:15:47 +08:00
|
|
|
if (new_arg->log_file)
|
|
|
|
pg_free(new_arg->log_file);
|
|
|
|
new_arg->log_file = pg_strdup(log_file);
|
|
|
|
if (new_arg->opt_log_file)
|
|
|
|
pg_free(new_arg->opt_log_file);
|
|
|
|
new_arg->opt_log_file = opt_log_file ? pg_strdup(opt_log_file) : NULL;
|
|
|
|
if (new_arg->cmd)
|
|
|
|
pg_free(new_arg->cmd);
|
|
|
|
new_arg->cmd = pg_strdup(cmd);
|
2012-12-27 08:26:30 +08:00
|
|
|
|
|
|
|
child = (HANDLE) _beginthreadex(NULL, 0, (void *) win32_exec_prog,
|
2013-05-30 04:58:43 +08:00
|
|
|
new_arg, 0, NULL);
|
2012-12-27 08:26:30 +08:00
|
|
|
if (child == 0)
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("could not create worker thread: %s\n", strerror(errno));
|
2012-12-27 08:26:30 +08:00
|
|
|
|
2013-05-30 04:58:43 +08:00
|
|
|
thread_handles[parallel_jobs - 1] = child;
|
2012-12-27 08:26:30 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
DWORD
|
2013-01-09 21:57:47 +08:00
|
|
|
win32_exec_prog(exec_thread_arg *args)
|
2012-12-27 08:26:30 +08:00
|
|
|
{
|
2013-05-30 04:58:43 +08:00
|
|
|
int ret;
|
2012-12-27 08:26:30 +08:00
|
|
|
|
|
|
|
ret = !exec_prog(args->log_file, args->opt_log_file, true, "%s", args->cmd);
|
|
|
|
|
|
|
|
/* terminates thread */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2013-01-09 21:57:47 +08:00
|
|
|
/*
|
|
|
|
* parallel_transfer_all_new_dbs
|
|
|
|
*
|
|
|
|
* This has the same API as transfer_all_new_dbs, except it does parallel execution
|
|
|
|
* by transfering multiple tablespaces in parallel
|
|
|
|
*/
|
2013-05-30 04:58:43 +08:00
|
|
|
void
|
|
|
|
parallel_transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
|
|
|
|
char *old_pgdata, char *new_pgdata,
|
|
|
|
char *old_tablespace)
|
2013-01-09 21:57:47 +08:00
|
|
|
{
|
|
|
|
#ifndef WIN32
|
|
|
|
pid_t child;
|
|
|
|
#else
|
|
|
|
HANDLE child;
|
2013-05-30 04:58:43 +08:00
|
|
|
transfer_thread_arg *new_arg;
|
2013-01-09 21:57:47 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (user_opts.jobs <= 1)
|
|
|
|
/* throw_error must be true to allow jobs */
|
|
|
|
transfer_all_new_dbs(old_db_arr, new_db_arr, old_pgdata, new_pgdata, NULL);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* parallel */
|
|
|
|
#ifdef WIN32
|
2013-07-25 10:01:14 +08:00
|
|
|
if (thread_handles == NULL)
|
|
|
|
thread_handles = pg_malloc(user_opts.jobs * sizeof(HANDLE));
|
|
|
|
|
|
|
|
if (transfer_thread_args == NULL)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
transfer_thread_args = pg_malloc(user_opts.jobs * sizeof(transfer_thread_arg *));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For safety and performance, we keep the args allocated during
|
|
|
|
* the entire life of the process, and we don't free the args in a
|
|
|
|
* thread different from the one that allocated it.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < user_opts.jobs; i++)
|
|
|
|
transfer_thread_args[i] = pg_malloc0(sizeof(transfer_thread_arg));
|
|
|
|
}
|
|
|
|
|
2013-05-30 04:58:43 +08:00
|
|
|
cur_thread_args = (void **) transfer_thread_args;
|
2013-01-09 21:57:47 +08:00
|
|
|
#endif
|
|
|
|
/* harvest any dead children */
|
|
|
|
while (reap_child(false) == true)
|
|
|
|
;
|
|
|
|
|
|
|
|
/* must we wait for a dead child? */
|
|
|
|
if (parallel_jobs >= user_opts.jobs)
|
|
|
|
reap_child(true);
|
2013-05-30 04:58:43 +08:00
|
|
|
|
2013-01-09 21:57:47 +08:00
|
|
|
/* set this before we start the job */
|
|
|
|
parallel_jobs++;
|
2013-05-30 04:58:43 +08:00
|
|
|
|
2013-01-09 21:57:47 +08:00
|
|
|
/* Ensure stdio state is quiesced before forking */
|
|
|
|
fflush(NULL);
|
|
|
|
|
|
|
|
#ifndef WIN32
|
|
|
|
child = fork();
|
|
|
|
if (child == 0)
|
|
|
|
{
|
|
|
|
transfer_all_new_dbs(old_db_arr, new_db_arr, old_pgdata, new_pgdata,
|
|
|
|
old_tablespace);
|
|
|
|
/* if we take another exit path, it will be non-zero */
|
|
|
|
/* use _exit to skip atexit() functions */
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
else if (child < 0)
|
|
|
|
/* fork failed */
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("could not create worker process: %s\n", strerror(errno));
|
2013-01-09 21:57:47 +08:00
|
|
|
#else
|
2013-07-30 21:23:31 +08:00
|
|
|
/* empty array element are always at the end */
|
2013-05-30 04:58:43 +08:00
|
|
|
new_arg = transfer_thread_args[parallel_jobs - 1];
|
2013-01-09 21:57:47 +08:00
|
|
|
|
|
|
|
/* Can only pass one pointer into the function, so use a struct */
|
|
|
|
new_arg->old_db_arr = old_db_arr;
|
|
|
|
new_arg->new_db_arr = new_db_arr;
|
2013-07-25 01:15:47 +08:00
|
|
|
if (new_arg->old_pgdata)
|
|
|
|
pg_free(new_arg->old_pgdata);
|
|
|
|
new_arg->old_pgdata = pg_strdup(old_pgdata);
|
|
|
|
if (new_arg->new_pgdata)
|
|
|
|
pg_free(new_arg->new_pgdata);
|
|
|
|
new_arg->new_pgdata = pg_strdup(new_pgdata);
|
|
|
|
if (new_arg->old_tablespace)
|
|
|
|
pg_free(new_arg->old_tablespace);
|
|
|
|
new_arg->old_tablespace = old_tablespace ? pg_strdup(old_tablespace) : NULL;
|
2013-01-09 21:57:47 +08:00
|
|
|
|
2013-07-24 22:00:37 +08:00
|
|
|
child = (HANDLE) _beginthreadex(NULL, 0, (void *) win32_transfer_all_new_dbs,
|
2013-05-30 04:58:43 +08:00
|
|
|
new_arg, 0, NULL);
|
2013-01-09 21:57:47 +08:00
|
|
|
if (child == 0)
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("could not create worker thread: %s\n", strerror(errno));
|
2013-01-09 21:57:47 +08:00
|
|
|
|
2013-05-30 04:58:43 +08:00
|
|
|
thread_handles[parallel_jobs - 1] = child;
|
2013-01-09 21:57:47 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
DWORD
|
|
|
|
win32_transfer_all_new_dbs(transfer_thread_arg *args)
|
|
|
|
{
|
|
|
|
transfer_all_new_dbs(args->old_db_arr, args->new_db_arr, args->old_pgdata,
|
|
|
|
args->new_pgdata, args->old_tablespace);
|
|
|
|
|
|
|
|
/* terminates thread */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2012-12-27 08:26:30 +08:00
|
|
|
/*
|
|
|
|
* collect status from a completed worker child
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
reap_child(bool wait_for_child)
|
|
|
|
{
|
|
|
|
#ifndef WIN32
|
2013-05-30 04:58:43 +08:00
|
|
|
int work_status;
|
|
|
|
int ret;
|
2012-12-27 08:26:30 +08:00
|
|
|
#else
|
2013-05-30 04:58:43 +08:00
|
|
|
int thread_num;
|
|
|
|
DWORD res;
|
2012-12-27 08:26:30 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (user_opts.jobs <= 1 || parallel_jobs == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
#ifndef WIN32
|
|
|
|
ret = waitpid(-1, &work_status, wait_for_child ? 0 : WNOHANG);
|
|
|
|
|
|
|
|
/* no children or, for WNOHANG, no dead children */
|
|
|
|
if (ret <= 0 || !WIFEXITED(work_status))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (WEXITSTATUS(work_status) != 0)
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("child worker exited abnormally: %s\n", strerror(errno));
|
2012-12-27 08:26:30 +08:00
|
|
|
#else
|
|
|
|
/* wait for one to finish */
|
|
|
|
thread_num = WaitForMultipleObjects(parallel_jobs, thread_handles,
|
2013-05-30 04:58:43 +08:00
|
|
|
false, wait_for_child ? INFINITE : 0);
|
2012-12-27 08:26:30 +08:00
|
|
|
|
|
|
|
if (thread_num == WAIT_TIMEOUT || thread_num == WAIT_FAILED)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* compute thread index in active_threads */
|
|
|
|
thread_num -= WAIT_OBJECT_0;
|
2013-05-30 04:58:43 +08:00
|
|
|
|
2012-12-27 08:26:30 +08:00
|
|
|
/* get the result */
|
|
|
|
GetExitCodeThread(thread_handles[thread_num], &res);
|
|
|
|
if (res != 0)
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("child worker exited abnormally: %s\n", strerror(errno));
|
2012-12-27 08:26:30 +08:00
|
|
|
|
|
|
|
/* dispose of handle to stop leaks */
|
|
|
|
CloseHandle(thread_handles[thread_num]);
|
|
|
|
|
2013-05-30 04:58:43 +08:00
|
|
|
/* Move last slot into dead child's position */
|
2012-12-27 08:26:30 +08:00
|
|
|
if (thread_num != parallel_jobs - 1)
|
|
|
|
{
|
2013-05-30 04:58:43 +08:00
|
|
|
void *tmp_args;
|
|
|
|
|
2012-12-27 08:26:30 +08:00
|
|
|
thread_handles[thread_num] = thread_handles[parallel_jobs - 1];
|
|
|
|
|
|
|
|
/*
|
2013-07-30 21:23:31 +08:00
|
|
|
* Move last active thead arg struct into the now-dead slot,
|
|
|
|
* and the now-dead slot to the end for reuse by the next thread.
|
|
|
|
* Though the thread struct is in use by another thread, we can
|
|
|
|
* safely swap the struct pointers within the array.
|
2012-12-27 08:26:30 +08:00
|
|
|
*/
|
2013-01-09 21:57:47 +08:00
|
|
|
tmp_args = cur_thread_args[thread_num];
|
|
|
|
cur_thread_args[thread_num] = cur_thread_args[parallel_jobs - 1];
|
|
|
|
cur_thread_args[parallel_jobs - 1] = tmp_args;
|
2012-12-27 08:26:30 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* do this after job has been removed */
|
|
|
|
parallel_jobs--;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|