PostgreSQL Source Code git master
Loading...
Searching...
No Matches
parallel.h File Reference
#include <limits.h>
#include "pg_backup_archiver.h"
Include dependency graph for parallel.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ParallelState
 

Macros

#define PG_MAX_JOBS   INT_MAX
 

Typedefs

typedef void(* ParallelCompletionPtr) (ArchiveHandle *AH, TocEntry *te, int status, void *callback_data)
 
typedef struct ParallelSlot ParallelSlot
 
typedef struct ParallelState ParallelState
 

Enumerations

enum  WFW_WaitOption { WFW_NO_WAIT , WFW_GOT_STATUS , WFW_ONE_IDLE , WFW_ALL_IDLE }
 

Functions

void init_parallel_dump_utils (void)
 
bool IsEveryWorkerIdle (ParallelState *pstate)
 
void WaitForWorkers (ArchiveHandle *AH, ParallelState *pstate, WFW_WaitOption mode)
 
ParallelStateParallelBackupStart (ArchiveHandle *AH)
 
void DispatchJobForTocEntry (ArchiveHandle *AH, ParallelState *pstate, TocEntry *te, T_Action act, ParallelCompletionPtr callback, void *callback_data)
 
void ParallelBackupEnd (ArchiveHandle *AH, ParallelState *pstate)
 
void set_archive_cancel_info (ArchiveHandle *AH, PGconn *conn)
 

Macro Definition Documentation

◆ PG_MAX_JOBS

#define PG_MAX_JOBS   INT_MAX

Definition at line 48 of file parallel.h.

Typedef Documentation

◆ ParallelCompletionPtr

typedef void(* ParallelCompletionPtr) (ArchiveHandle *AH, TocEntry *te, int status, void *callback_data)

Definition at line 24 of file parallel.h.

◆ ParallelSlot

Definition at line 52 of file parallel.h.

◆ ParallelState

Enumeration Type Documentation

◆ WFW_WaitOption

Enumerator
WFW_NO_WAIT 
WFW_GOT_STATUS 
WFW_ONE_IDLE 
WFW_ALL_IDLE 

Definition at line 30 of file parallel.h.

31{
WFW_WaitOption
Definition parallel.h:31
@ WFW_ALL_IDLE
Definition parallel.h:35
@ WFW_GOT_STATUS
Definition parallel.h:33
@ WFW_NO_WAIT
Definition parallel.h:32
@ WFW_ONE_IDLE
Definition parallel.h:34

Function Documentation

◆ DispatchJobForTocEntry()

void DispatchJobForTocEntry ( ArchiveHandle AH,
ParallelState pstate,
TocEntry te,
T_Action  act,
ParallelCompletionPtr  callback,
void callback_data 
)
extern

Definition at line 1221 of file parallel.c.

1227{
1228 int worker;
1229 char buf[256];
1230
1231 /* Get a worker, waiting if none are idle */
1232 while ((worker = GetIdleWorker(pstate)) == NO_SLOT)
1233 WaitForWorkers(AH, pstate, WFW_ONE_IDLE);
1234
1235 /* Construct and send command string */
1236 buildWorkerCommand(AH, te, act, buf, sizeof(buf));
1237
1238 sendMessageToWorker(pstate, worker, buf);
1239
1240 /* Remember worker is busy, and which TocEntry it's working on */
1241 pstate->parallelSlot[worker].workerStatus = WRKR_WORKING;
1242 pstate->parallelSlot[worker].callback = callback;
1243 pstate->parallelSlot[worker].callback_data = callback_data;
1244 pstate->te[worker] = te;
1245}
void WaitForWorkers(ArchiveHandle *AH, ParallelState *pstate, WFW_WaitOption mode)
Definition parallel.c:1467
@ WRKR_WORKING
Definition parallel.c:81
static int GetIdleWorker(ParallelState *pstate)
Definition parallel.c:1252
static void buildWorkerCommand(ArchiveHandle *AH, TocEntry *te, T_Action act, char *buf, int buflen)
Definition parallel.c:1124
#define NO_SLOT
Definition parallel.c:74
static void sendMessageToWorker(ParallelState *pstate, int worker, const char *str)
Definition parallel.c:1660
static char buf[DEFAULT_XLOG_SEG_SIZE]
static int fb(int x)
ParallelCompletionPtr callback
Definition parallel.c:100
void * callback_data
Definition parallel.c:101
T_WorkerStatus workerStatus
Definition parallel.c:97
TocEntry ** te
Definition parallel.h:59
ParallelSlot * parallelSlot
Definition parallel.h:60
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)

References buf, buildWorkerCommand(), ParallelSlot::callback, callback(), ParallelSlot::callback_data, fb(), GetIdleWorker(), NO_SLOT, ParallelState::parallelSlot, sendMessageToWorker(), ParallelState::te, WaitForWorkers(), WFW_ONE_IDLE, ParallelSlot::workerStatus, and WRKR_WORKING.

Referenced by restore_toc_entries_parallel(), and WriteDataChunks().

◆ init_parallel_dump_utils()

void init_parallel_dump_utils ( void  )
extern

Definition at line 238 of file parallel.c.

239{
240#ifdef WIN32
242 {
244 int err;
245
246 /* Prepare for threaded operation */
249
250 /* Initialize socket access */
251 err = WSAStartup(MAKEWORD(2, 2), &wsaData);
252 if (err != 0)
253 pg_fatal("%s() failed: error code %d", "WSAStartup", err);
254
255 parallel_init_done = true;
256 }
257#endif
258}
void err(int eval, const char *fmt,...)
Definition err.c:43
#define pg_fatal(...)

References err(), fb(), and pg_fatal.

Referenced by main().

◆ IsEveryWorkerIdle()

bool IsEveryWorkerIdle ( ParallelState pstate)
extern

Definition at line 1284 of file parallel.c.

1285{
1286 int i;
1287
1288 for (i = 0; i < pstate->numWorkers; i++)
1289 {
1290 if (pstate->parallelSlot[i].workerStatus != WRKR_IDLE)
1291 return false;
1292 }
1293 return true;
1294}
@ WRKR_IDLE
Definition parallel.c:80
int i
Definition isn.c:77

References i, ParallelState::numWorkers, ParallelState::parallelSlot, ParallelSlot::workerStatus, and WRKR_IDLE.

Referenced by ParallelBackupEnd(), restore_toc_entries_parallel(), and WaitForWorkers().

◆ ParallelBackupEnd()

void ParallelBackupEnd ( ArchiveHandle AH,
ParallelState pstate 
)
extern

Definition at line 1075 of file parallel.c.

1076{
1077 int i;
1078
1079 /* No work if non-parallel */
1080 if (pstate->numWorkers == 1)
1081 return;
1082
1083 /* There should not be any unfinished jobs */
1084 Assert(IsEveryWorkerIdle(pstate));
1085
1086 /* Close the sockets so that the workers know they can exit */
1087 for (i = 0; i < pstate->numWorkers; i++)
1088 {
1091 }
1092
1093 /* Wait for them to exit */
1095
1096 /*
1097 * Unlink pstate from shutdown_info, so the exit handler will not try to
1098 * use it; and likewise unlink from signal_info.
1099 */
1102
1103 /* Release state (mere neatnik-ism, since we're about to terminate) */
1104 free(pstate->te);
1105 free(pstate->parallelSlot);
1106 free(pstate);
1107}
static void set_cancel_pstate(ParallelState *pstate)
Definition parallel.c:805
static ShutdownInformation shutdown_info
Definition parallel.c:154
bool IsEveryWorkerIdle(ParallelState *pstate)
Definition parallel.c:1284
static void WaitForTerminatingWorkers(ParallelState *pstate)
Definition parallel.c:462
#define Assert(condition)
Definition c.h:885
#define closesocket
Definition port.h:397
#define free(a)
ParallelState * pstate
Definition parallel.c:150

References Assert, closesocket, fb(), free, i, IsEveryWorkerIdle(), ParallelState::numWorkers, ParallelState::parallelSlot, ParallelSlot::pipeRead, ParallelSlot::pipeWrite, ShutdownInformation::pstate, set_cancel_pstate(), shutdown_info, ParallelState::te, and WaitForTerminatingWorkers().

Referenced by _CloseArchive(), and RestoreArchive().

◆ ParallelBackupStart()

ParallelState * ParallelBackupStart ( ArchiveHandle AH)
extern

Definition at line 913 of file parallel.c.

914{
915 ParallelState *pstate;
916 int i;
917
918 Assert(AH->public.numWorkers > 0);
919
921
922 pstate->numWorkers = AH->public.numWorkers;
923 pstate->te = NULL;
924 pstate->parallelSlot = NULL;
925
926 if (AH->public.numWorkers == 1)
927 return pstate;
928
929 /* Create status arrays, being sure to initialize all fields to 0 */
930 pstate->te =
932 pstate->parallelSlot =
934
935#ifdef WIN32
936 /* Make fmtId() and fmtQualifiedId() use thread-local storage */
938#endif
939
940 /*
941 * Set the pstate in shutdown_info, to tell the exit handler that it must
942 * clean up workers as well as the main database connection. But we don't
943 * set this in signal_info yet, because we don't want child processes to
944 * inherit non-NULL signal_info.pstate.
945 */
946 shutdown_info.pstate = pstate;
947
948 /*
949 * Temporarily disable query cancellation on the leader connection. This
950 * ensures that child processes won't inherit valid AH->connCancel
951 * settings and thus won't try to issue cancels against the leader's
952 * connection. No harm is done if we fail while it's disabled, because
953 * the leader connection is idle at this point anyway.
954 */
956
957 /* Ensure stdio state is quiesced before forking */
958 fflush(NULL);
959
960 /* Create desired number of workers */
961 for (i = 0; i < pstate->numWorkers; i++)
962 {
963#ifdef WIN32
964 WorkerInfo *wi;
965 uintptr_t handle;
966#else
967 pid_t pid;
968#endif
969 ParallelSlot *slot = &(pstate->parallelSlot[i]);
970 int pipeMW[2],
971 pipeWM[2];
972
973 /* Create communication pipes for this worker */
974 if (pgpipe(pipeMW) < 0 || pgpipe(pipeWM) < 0)
975 pg_fatal("could not create communication channels: %m");
976
977 /* leader's ends of the pipes */
978 slot->pipeRead = pipeWM[PIPE_READ];
979 slot->pipeWrite = pipeMW[PIPE_WRITE];
980 /* child's ends of the pipes */
983
984#ifdef WIN32
985 /* Create transient structure to pass args to worker function */
987
988 wi->AH = AH;
989 wi->slot = slot;
990
991 handle = _beginthreadex(NULL, 0, (void *) &init_spawned_worker_win32,
992 wi, 0, &(slot->threadId));
993 slot->hThread = handle;
994 slot->workerStatus = WRKR_IDLE;
995#else /* !WIN32 */
996 pid = fork();
997 if (pid == 0)
998 {
999 /* we are the worker */
1000 int j;
1001
1002 /* this is needed for GetMyPSlot() */
1003 slot->pid = getpid();
1004
1005 /* instruct signal handler that we're in a worker now */
1006 signal_info.am_worker = true;
1007
1008 /* close read end of Worker -> Leader */
1010 /* close write end of Leader -> Worker */
1012
1013 /*
1014 * Close all inherited fds for communication of the leader with
1015 * previously-forked workers.
1016 */
1017 for (j = 0; j < i; j++)
1018 {
1021 }
1022
1023 /* Run the worker ... */
1024 RunWorker(AH, slot);
1025
1026 /* We can just exit(0) when done */
1027 exit(0);
1028 }
1029 else if (pid < 0)
1030 {
1031 /* fork failed */
1032 pg_fatal("could not create worker process: %m");
1033 }
1034
1035 /* In Leader after successful fork */
1036 slot->pid = pid;
1037 slot->workerStatus = WRKR_IDLE;
1038
1039 /* close read end of Leader -> Worker */
1041 /* close write end of Worker -> Leader */
1043#endif /* WIN32 */
1044 }
1045
1046 /*
1047 * Having forked off the workers, disable SIGPIPE so that leader isn't
1048 * killed if it tries to send a command to a dead worker. We don't want
1049 * the workers to inherit this setting, though.
1050 */
1051#ifndef WIN32
1053#endif
1054
1055 /*
1056 * Re-establish query cancellation on the leader connection.
1057 */
1059
1060 /*
1061 * Tell the cancel signal handler to forward signals to worker processes,
1062 * too. (As with query cancel, we did not need this earlier because the
1063 * workers have not yet been given anything to do; if we die before this
1064 * point, any already-started workers will see EOF and quit promptly.)
1065 */
1066 set_cancel_pstate(pstate);
1067
1068 return pstate;
1069}
#define pgpipe(a)
Definition parallel.c:139
#define PIPE_READ
Definition parallel.c:71
static void RunWorker(ArchiveHandle *AH, ParallelSlot *slot)
Definition parallel.c:845
#define PIPE_WRITE
Definition parallel.c:72
static volatile DumpSignalInformation signal_info
Definition parallel.c:175
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition parallel.c:746
#define pg_malloc_object(type)
Definition fe_memutils.h:50
#define pg_malloc0_array(type, count)
Definition fe_memutils.h:57
int j
Definition isn.c:78
#define pqsignal
Definition port.h:547
PQExpBuffer(* getLocalPQExpBuffer)(void)
int numWorkers
Definition pg_backup.h:240
int pipeRevRead
Definition parallel.c:107
int pipeRevWrite
Definition parallel.c:108
#define SIGPIPE
Definition win32_port.h:163

References DumpSignalInformation::am_worker, Assert, closesocket, _archiveHandle::connection, fb(), getLocalPQExpBuffer, i, j, ParallelState::numWorkers, Archive::numWorkers, ParallelState::parallelSlot, pg_fatal, pg_malloc0_array, pg_malloc_object, pgpipe, ParallelSlot::pid, PIPE_READ, PIPE_WRITE, ParallelSlot::pipeRead, ParallelSlot::pipeRevRead, ParallelSlot::pipeRevWrite, ParallelSlot::pipeWrite, pqsignal, ShutdownInformation::pstate, _archiveHandle::public, RunWorker(), set_archive_cancel_info(), set_cancel_pstate(), shutdown_info, signal_info, SIGPIPE, ParallelState::te, ParallelSlot::workerStatus, and WRKR_IDLE.

Referenced by _CloseArchive(), and RestoreArchive().

◆ set_archive_cancel_info()

void set_archive_cancel_info ( ArchiveHandle AH,
PGconn conn 
)
extern

Definition at line 746 of file parallel.c.

747{
749
750 /*
751 * Activate the interrupt handler if we didn't yet in this process. On
752 * Windows, this also initializes signal_info_lock; therefore it's
753 * important that this happen at least once before we fork off any
754 * threads.
755 */
757
758 /*
759 * On Unix, we assume that storing a pointer value is atomic with respect
760 * to any possible signal interrupt. On Windows, use a critical section.
761 */
762
763#ifdef WIN32
765#endif
766
767 /* Free the old one if we have one */
769 /* be sure interrupt handler doesn't use pointer while freeing */
770 AH->connCancel = NULL;
771
772 if (oldConnCancel != NULL)
774
775 /* Set the new one if specified */
776 if (conn)
778
779 /*
780 * On Unix, there's only ever one active ArchiveHandle per process, so we
781 * can just set signal_info.myAH unconditionally. On Windows, do that
782 * only in the main thread; worker threads have to make sure their
783 * ArchiveHandle appears in the pstate data, which is dealt with in
784 * RunWorker().
785 */
786#ifndef WIN32
787 signal_info.myAH = AH;
788#else
790 signal_info.myAH = AH;
791#endif
792
793#ifdef WIN32
795#endif
796}
static void set_cancel_handler(void)
Definition parallel.c:624
PGcancel * PQgetCancel(PGconn *conn)
Definition fe-cancel.c:368
void PQfreeCancel(PGcancel *cancel)
Definition fe-cancel.c:502
PGconn * conn
Definition streamutil.c:52
ArchiveHandle * myAH
Definition parallel.c:167
PGcancel *volatile connCancel

References conn, _archiveHandle::connCancel, fb(), DumpSignalInformation::myAH, PQfreeCancel(), PQgetCancel(), set_cancel_handler(), and signal_info.

Referenced by ConnectDatabaseAhx(), DisconnectDatabase(), and ParallelBackupStart().

◆ WaitForWorkers()

void WaitForWorkers ( ArchiveHandle AH,
ParallelState pstate,
WFW_WaitOption  mode 
)
extern

Definition at line 1467 of file parallel.c.

1468{
1469 bool do_wait = false;
1470
1471 /*
1472 * In GOT_STATUS mode, always block waiting for a message, since we can't
1473 * return till we get something. In other modes, we don't block the first
1474 * time through the loop.
1475 */
1476 if (mode == WFW_GOT_STATUS)
1477 {
1478 /* Assert that caller knows what it's doing */
1479 Assert(!IsEveryWorkerIdle(pstate));
1480 do_wait = true;
1481 }
1482
1483 for (;;)
1484 {
1485 /*
1486 * Check for status messages, even if we don't need to block. We do
1487 * not try very hard to reap all available messages, though, since
1488 * there's unlikely to be more than one.
1489 */
1490 if (ListenToWorkers(AH, pstate, do_wait))
1491 {
1492 /*
1493 * If we got a message, we are done by definition for GOT_STATUS
1494 * mode, and we can also be certain that there's at least one idle
1495 * worker. So we're done in all but ALL_IDLE mode.
1496 */
1497 if (mode != WFW_ALL_IDLE)
1498 return;
1499 }
1500
1501 /* Check whether we must wait for new status messages */
1502 switch (mode)
1503 {
1504 case WFW_NO_WAIT:
1505 return; /* never wait */
1506 case WFW_GOT_STATUS:
1507 Assert(false); /* can't get here, because we waited */
1508 break;
1509 case WFW_ONE_IDLE:
1510 if (GetIdleWorker(pstate) != NO_SLOT)
1511 return;
1512 break;
1513 case WFW_ALL_IDLE:
1514 if (IsEveryWorkerIdle(pstate))
1515 return;
1516 break;
1517 }
1518
1519 /* Loop back, and this time wait for something to happen */
1520 do_wait = true;
1521 }
1522}
static bool ListenToWorkers(ArchiveHandle *AH, ParallelState *pstate, bool do_wait)
Definition parallel.c:1414
static PgChecksumMode mode
static bool do_wait
Definition pg_ctl.c:76

References Assert, do_wait, GetIdleWorker(), IsEveryWorkerIdle(), ListenToWorkers(), mode, NO_SLOT, WFW_ALL_IDLE, WFW_GOT_STATUS, WFW_NO_WAIT, and WFW_ONE_IDLE.

Referenced by DispatchJobForTocEntry(), restore_toc_entries_parallel(), and WriteDataChunks().