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 1205 of file parallel.c.

1211{
1212 int worker;
1213 char buf[256];
1214
1215 /* Get a worker, waiting if none are idle */
1216 while ((worker = GetIdleWorker(pstate)) == NO_SLOT)
1217 WaitForWorkers(AH, pstate, WFW_ONE_IDLE);
1218
1219 /* Construct and send command string */
1220 buildWorkerCommand(AH, te, act, buf, sizeof(buf));
1221
1222 sendMessageToWorker(pstate, worker, buf);
1223
1224 /* Remember worker is busy, and which TocEntry it's working on */
1225 pstate->parallelSlot[worker].workerStatus = WRKR_WORKING;
1226 pstate->parallelSlot[worker].callback = callback;
1227 pstate->parallelSlot[worker].callback_data = callback_data;
1228 pstate->te[worker] = te;
1229}
void WaitForWorkers(ArchiveHandle *AH, ParallelState *pstate, WFW_WaitOption mode)
Definition parallel.c:1451
@ WRKR_WORKING
Definition parallel.c:81
static int GetIdleWorker(ParallelState *pstate)
Definition parallel.c:1236
static void buildWorkerCommand(ArchiveHandle *AH, TocEntry *te, T_Action act, char *buf, int buflen)
Definition parallel.c:1108
#define NO_SLOT
Definition parallel.c:74
static void sendMessageToWorker(ParallelState *pstate, int worker, const char *str)
Definition parallel.c:1644
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 1268 of file parallel.c.

1269{
1270 int i;
1271
1272 for (i = 0; i < pstate->numWorkers; i++)
1273 {
1274 if (pstate->parallelSlot[i].workerStatus != WRKR_IDLE)
1275 return false;
1276 }
1277 return true;
1278}
@ 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 1059 of file parallel.c.

1060{
1061 int i;
1062
1063 /* No work if non-parallel */
1064 if (pstate->numWorkers == 1)
1065 return;
1066
1067 /* There should not be any unfinished jobs */
1068 Assert(IsEveryWorkerIdle(pstate));
1069
1070 /* Close the sockets so that the workers know they can exit */
1071 for (i = 0; i < pstate->numWorkers; i++)
1072 {
1075 }
1076
1077 /* Wait for them to exit */
1079
1080 /*
1081 * Unlink pstate from shutdown_info, so the exit handler will not try to
1082 * use it; and likewise unlink from signal_info.
1083 */
1086
1087 /* Release state (mere neatnik-ism, since we're about to terminate) */
1088 free(pstate->te);
1089 free(pstate->parallelSlot);
1090 free(pstate);
1091}
static void set_cancel_pstate(ParallelState *pstate)
Definition parallel.c:787
static ShutdownInformation shutdown_info
Definition parallel.c:154
bool IsEveryWorkerIdle(ParallelState *pstate)
Definition parallel.c:1268
static void WaitForTerminatingWorkers(ParallelState *pstate)
Definition parallel.c:448
#define Assert(condition)
Definition c.h:1002
#define closesocket
Definition port.h:398
#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 895 of file parallel.c.

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

729{
731
732 /*
733 * Activate the interrupt handler if we didn't yet in this process. On
734 * Windows, this also initializes signal_info_lock; therefore it's
735 * important that this happen at least once before we fork off any
736 * threads.
737 */
739
740 /*
741 * On Unix, we assume that storing a pointer value is atomic with respect
742 * to any possible signal interrupt. On Windows, use a critical section.
743 */
744
745#ifdef WIN32
747#endif
748
749 /* Free the old one if we have one */
751 /* be sure interrupt handler doesn't use pointer while freeing */
752 AH->connCancel = NULL;
753
754 if (oldConnCancel != NULL)
756
757 /* Set the new one if specified */
758 if (conn)
760
761 /*
762 * On Unix, there's only ever one active ArchiveHandle per process, so we
763 * can just set signal_info.myAH unconditionally. On Windows, do that
764 * only in the main thread; worker threads have to make sure their
765 * ArchiveHandle appears in the pstate data, which is dealt with in
766 * RunWorker().
767 */
768#ifndef WIN32
769 signal_info.myAH = AH;
770#else
772 signal_info.myAH = AH;
773#endif
774
775#ifdef WIN32
777#endif
778}
static void set_cancel_handler(void)
Definition parallel.c:611
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 1451 of file parallel.c.

1452{
1453 bool do_wait = false;
1454
1455 /*
1456 * In GOT_STATUS mode, always block waiting for a message, since we can't
1457 * return till we get something. In other modes, we don't block the first
1458 * time through the loop.
1459 */
1460 if (mode == WFW_GOT_STATUS)
1461 {
1462 /* Assert that caller knows what it's doing */
1463 Assert(!IsEveryWorkerIdle(pstate));
1464 do_wait = true;
1465 }
1466
1467 for (;;)
1468 {
1469 /*
1470 * Check for status messages, even if we don't need to block. We do
1471 * not try very hard to reap all available messages, though, since
1472 * there's unlikely to be more than one.
1473 */
1474 if (ListenToWorkers(AH, pstate, do_wait))
1475 {
1476 /*
1477 * If we got a message, we are done by definition for GOT_STATUS
1478 * mode, and we can also be certain that there's at least one idle
1479 * worker. So we're done in all but ALL_IDLE mode.
1480 */
1481 if (mode != WFW_ALL_IDLE)
1482 return;
1483 }
1484
1485 /* Check whether we must wait for new status messages */
1486 switch (mode)
1487 {
1488 case WFW_NO_WAIT:
1489 return; /* never wait */
1490 case WFW_GOT_STATUS:
1491 Assert(false); /* can't get here, because we waited */
1492 break;
1493 case WFW_ONE_IDLE:
1494 if (GetIdleWorker(pstate) != NO_SLOT)
1495 return;
1496 break;
1497 case WFW_ALL_IDLE:
1498 if (IsEveryWorkerIdle(pstate))
1499 return;
1500 break;
1501 }
1502
1503 /* Loop back, and this time wait for something to happen */
1504 do_wait = true;
1505 }
1506}
static bool ListenToWorkers(ArchiveHandle *AH, ParallelState *pstate, bool do_wait)
Definition parallel.c:1398
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().