PostgreSQL Source Code  git master
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

typedef struct ParallelSlot ParallelSlot

Definition at line 24 of file parallel.h.

◆ ParallelState

typedef struct ParallelState 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 
)

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:79
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:72
static void sendMessageToWorker(ParallelState *pstate, int worker, const char *str)
Definition: parallel.c:1644
static char * buf
Definition: pg_test_fsync.c:67
ParallelCompletionPtr callback
Definition: parallel.c:98
void * callback_data
Definition: parallel.c:99
T_WorkerStatus workerStatus
Definition: parallel.c:95
TocEntry ** te
Definition: parallel.h:59
ParallelSlot * parallelSlot
Definition: parallel.h:60
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:46

References buf, buildWorkerCommand(), ParallelSlot::callback, callback(), ParallelSlot::callback_data, 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  )

Definition at line 236 of file parallel.c.

237 {
238 #ifdef WIN32
239  if (!parallel_init_done)
240  {
241  WSADATA wsaData;
242  int err;
243 
244  /* Prepare for threaded operation */
245  tls_index = TlsAlloc();
246  mainThreadId = GetCurrentThreadId();
247 
248  /* Initialize socket access */
249  err = WSAStartup(MAKEWORD(2, 2), &wsaData);
250  if (err != 0)
251  pg_fatal("%s() failed: error code %d", "WSAStartup", err);
252 
253  parallel_init_done = true;
254  }
255 #endif
256 }
#define pg_fatal(...)

References pg_fatal.

Referenced by main().

◆ IsEveryWorkerIdle()

bool IsEveryWorkerIdle ( ParallelState pstate)

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:78
int i
Definition: isn.c:73
int numWorkers
Definition: parallel.h:57

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 
)

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  {
1073  closesocket(pstate->parallelSlot[i].pipeRead);
1074  closesocket(pstate->parallelSlot[i].pipeWrite);
1075  }
1076 
1077  /* Wait for them to exit */
1078  WaitForTerminatingWorkers(pstate);
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  */
1084  shutdown_info.pstate = NULL;
1085  set_cancel_pstate(NULL);
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:789
static ShutdownInformation shutdown_info
Definition: parallel.c:152
bool IsEveryWorkerIdle(ParallelState *pstate)
Definition: parallel.c:1268
static void WaitForTerminatingWorkers(ParallelState *pstate)
Definition: parallel.c:446
#define free(a)
Definition: header.h:65
Assert(fmt[strlen(fmt) - 1] !='\n')
#define closesocket
Definition: port.h:349
int pipeRead
Definition: parallel.c:103
int pipeWrite
Definition: parallel.c:104
ParallelState * pstate
Definition: parallel.c:148

References Assert(), closesocket, 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)

Definition at line 897 of file parallel.c.

898 {
899  ParallelState *pstate;
900  int i;
901 
902  Assert(AH->public.numWorkers > 0);
903 
904  pstate = (ParallelState *) pg_malloc(sizeof(ParallelState));
905 
906  pstate->numWorkers = AH->public.numWorkers;
907  pstate->te = NULL;
908  pstate->parallelSlot = NULL;
909 
910  if (AH->public.numWorkers == 1)
911  return pstate;
912 
913  /* Create status arrays, being sure to initialize all fields to 0 */
914  pstate->te = (TocEntry **)
915  pg_malloc0(pstate->numWorkers * sizeof(TocEntry *));
916  pstate->parallelSlot = (ParallelSlot *)
917  pg_malloc0(pstate->numWorkers * sizeof(ParallelSlot));
918 
919 #ifdef WIN32
920  /* Make fmtId() and fmtQualifiedId() use thread-local storage */
921  getLocalPQExpBuffer = getThreadLocalPQExpBuffer;
922 #endif
923 
924  /*
925  * Set the pstate in shutdown_info, to tell the exit handler that it must
926  * clean up workers as well as the main database connection. But we don't
927  * set this in signal_info yet, because we don't want child processes to
928  * inherit non-NULL signal_info.pstate.
929  */
930  shutdown_info.pstate = pstate;
931 
932  /*
933  * Temporarily disable query cancellation on the leader connection. This
934  * ensures that child processes won't inherit valid AH->connCancel
935  * settings and thus won't try to issue cancels against the leader's
936  * connection. No harm is done if we fail while it's disabled, because
937  * the leader connection is idle at this point anyway.
938  */
939  set_archive_cancel_info(AH, NULL);
940 
941  /* Ensure stdio state is quiesced before forking */
942  fflush(NULL);
943 
944  /* Create desired number of workers */
945  for (i = 0; i < pstate->numWorkers; i++)
946  {
947 #ifdef WIN32
948  WorkerInfo *wi;
949  uintptr_t handle;
950 #else
951  pid_t pid;
952 #endif
953  ParallelSlot *slot = &(pstate->parallelSlot[i]);
954  int pipeMW[2],
955  pipeWM[2];
956 
957  /* Create communication pipes for this worker */
958  if (pgpipe(pipeMW) < 0 || pgpipe(pipeWM) < 0)
959  pg_fatal("could not create communication channels: %m");
960 
961  /* leader's ends of the pipes */
962  slot->pipeRead = pipeWM[PIPE_READ];
963  slot->pipeWrite = pipeMW[PIPE_WRITE];
964  /* child's ends of the pipes */
965  slot->pipeRevRead = pipeMW[PIPE_READ];
966  slot->pipeRevWrite = pipeWM[PIPE_WRITE];
967 
968 #ifdef WIN32
969  /* Create transient structure to pass args to worker function */
970  wi = (WorkerInfo *) pg_malloc(sizeof(WorkerInfo));
971 
972  wi->AH = AH;
973  wi->slot = slot;
974 
975  handle = _beginthreadex(NULL, 0, (void *) &init_spawned_worker_win32,
976  wi, 0, &(slot->threadId));
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 */
993  closesocket(pipeWM[PIPE_READ]);
994  /* close write end of Leader -> Worker */
995  closesocket(pipeMW[PIPE_WRITE]);
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  {
1003  closesocket(pstate->parallelSlot[j].pipeRead);
1004  closesocket(pstate->parallelSlot[j].pipeWrite);
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 */
1024  closesocket(pipeMW[PIPE_READ]);
1025  /* close write end of Worker -> Leader */
1026  closesocket(pipeWM[PIPE_WRITE]);
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:137
#define PIPE_READ
Definition: parallel.c:69
static void RunWorker(ArchiveHandle *AH, ParallelSlot *slot)
Definition: parallel.c:829
#define PIPE_WRITE
Definition: parallel.c:70
static volatile DumpSignalInformation signal_info
Definition: parallel.c:173
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition: parallel.c:730
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
int j
Definition: isn.c:74
static void const char fflush(stdout)
exit(1)
pqsigfunc pqsignal(int signo, pqsigfunc func)
PQExpBuffer(* getLocalPQExpBuffer)(void)
Definition: string_utils.c:27
int numWorkers
Definition: pg_backup.h:222
int pipeRevRead
Definition: parallel.c:105
int pipeRevWrite
Definition: parallel.c:106
pid_t pid
Definition: parallel.c:113
#define SIGPIPE
Definition: win32_port.h:181
#define SIG_IGN
Definition: win32_port.h:173

References DumpSignalInformation::am_worker, Assert(), closesocket, _archiveHandle::connection, exit(), fflush(), getLocalPQExpBuffer, i, j, ParallelState::numWorkers, Archive::numWorkers, ParallelState::parallelSlot, pg_fatal, pg_malloc(), pg_malloc0(), 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, SIG_IGN, 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 
)

Definition at line 730 of file parallel.c.

731 {
732  PGcancel *oldConnCancel;
733 
734  /*
735  * Activate the interrupt handler if we didn't yet in this process. On
736  * Windows, this also initializes signal_info_lock; therefore it's
737  * important that this happen at least once before we fork off any
738  * threads.
739  */
741 
742  /*
743  * On Unix, we assume that storing a pointer value is atomic with respect
744  * to any possible signal interrupt. On Windows, use a critical section.
745  */
746 
747 #ifdef WIN32
748  EnterCriticalSection(&signal_info_lock);
749 #endif
750 
751  /* Free the old one if we have one */
752  oldConnCancel = AH->connCancel;
753  /* be sure interrupt handler doesn't use pointer while freeing */
754  AH->connCancel = NULL;
755 
756  if (oldConnCancel != NULL)
757  PQfreeCancel(oldConnCancel);
758 
759  /* Set the new one if specified */
760  if (conn)
761  AH->connCancel = PQgetCancel(conn);
762 
763  /*
764  * On Unix, there's only ever one active ArchiveHandle per process, so we
765  * can just set signal_info.myAH unconditionally. On Windows, do that
766  * only in the main thread; worker threads have to make sure their
767  * ArchiveHandle appears in the pstate data, which is dealt with in
768  * RunWorker().
769  */
770 #ifndef WIN32
771  signal_info.myAH = AH;
772 #else
773  if (mainThreadId == GetCurrentThreadId())
774  signal_info.myAH = AH;
775 #endif
776 
777 #ifdef WIN32
778  LeaveCriticalSection(&signal_info_lock);
779 #endif
780 }
static void setup_cancel_handler(void)
Definition: parallel.c:608
PGcancel * PQgetCancel(PGconn *conn)
Definition: fe-connect.c:4235
void PQfreeCancel(PGcancel *cancel)
Definition: fe-connect.c:4303
PGconn * conn
Definition: streamutil.c:54
ArchiveHandle * myAH
Definition: parallel.c:165
PGcancel *volatile connCancel

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

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

◆ WaitForWorkers()

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

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
Definition: pg_checksums.c:65
static bool do_wait
Definition: pg_ctl.c:74

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().