PostgreSQL Source Code  git master
test.c File Reference
#include "postgres.h"
#include "fmgr.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "varatt.h"
#include "test_shm_mq.h"
Include dependency graph for test.c:

Go to the source code of this file.

Functions

 PG_FUNCTION_INFO_V1 (test_shm_mq)
 
 PG_FUNCTION_INFO_V1 (test_shm_mq_pipelined)
 
static void verify_message (Size origlen, char *origdata, Size newlen, char *newdata)
 
Datum test_shm_mq (PG_FUNCTION_ARGS)
 
Datum test_shm_mq_pipelined (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 

Function Documentation

◆ PG_FUNCTION_INFO_V1() [1/2]

PG_FUNCTION_INFO_V1 ( test_shm_mq  )

◆ PG_FUNCTION_INFO_V1() [2/2]

PG_FUNCTION_INFO_V1 ( test_shm_mq_pipelined  )

◆ test_shm_mq()

Datum test_shm_mq ( PG_FUNCTION_ARGS  )

Definition at line 40 of file test.c.

41 {
42  int64 queue_size = PG_GETARG_INT64(0);
43  text *message = PG_GETARG_TEXT_PP(1);
44  char *message_contents = VARDATA_ANY(message);
45  int message_size = VARSIZE_ANY_EXHDR(message);
46  int32 loop_count = PG_GETARG_INT32(2);
47  int32 nworkers = PG_GETARG_INT32(3);
48  dsm_segment *seg;
49  shm_mq_handle *outqh;
50  shm_mq_handle *inqh;
52  Size len;
53  void *data;
54 
55  /* A negative loopcount is nonsensical. */
56  if (loop_count < 0)
57  ereport(ERROR,
58  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
59  errmsg("repeat count size must be an integer value greater than or equal to zero")));
60 
61  /*
62  * Since this test sends data using the blocking interfaces, it cannot
63  * send data to itself. Therefore, a minimum of 1 worker is required. Of
64  * course, a negative worker count is nonsensical.
65  */
66  if (nworkers <= 0)
67  ereport(ERROR,
68  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
69  errmsg("number of workers must be an integer value greater than zero")));
70 
71  /* Set up dynamic shared memory segment and background workers. */
72  test_shm_mq_setup(queue_size, nworkers, &seg, &outqh, &inqh);
73 
74  /* Send the initial message. */
75  res = shm_mq_send(outqh, message_size, message_contents, false, true);
76  if (res != SHM_MQ_SUCCESS)
77  ereport(ERROR,
78  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
79  errmsg("could not send message")));
80 
81  /*
82  * Receive a message and send it back out again. Do this a number of
83  * times equal to the loop count.
84  */
85  for (;;)
86  {
87  /* Receive a message. */
88  res = shm_mq_receive(inqh, &len, &data, false);
89  if (res != SHM_MQ_SUCCESS)
90  ereport(ERROR,
91  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
92  errmsg("could not receive message")));
93 
94  /* If this is supposed to be the last iteration, stop here. */
95  if (--loop_count <= 0)
96  break;
97 
98  /* Send it back out. */
99  res = shm_mq_send(outqh, len, data, false, true);
100  if (res != SHM_MQ_SUCCESS)
101  ereport(ERROR,
102  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
103  errmsg("could not send message")));
104  }
105 
106  /*
107  * Finally, check that we got back the same message from the last
108  * iteration that we originally sent.
109  */
110  verify_message(message_size, message_contents, len, data);
111 
112  /* Clean up. */
113  dsm_detach(seg);
114 
115  PG_RETURN_VOID();
116 }
signed int int32
Definition: c.h:483
size_t Size
Definition: c.h:594
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:776
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
const void size_t len
const void * data
void test_shm_mq_setup(int64 queue_size, int32 nworkers, dsm_segment **segp, shm_mq_handle **output, shm_mq_handle **input)
Definition: setup.c:48
shm_mq_result shm_mq_receive(shm_mq_handle *mqh, Size *nbytesp, void **datap, bool nowait)
Definition: shm_mq.c:573
shm_mq_result shm_mq_send(shm_mq_handle *mqh, Size nbytes, const void *data, bool nowait, bool force_flush)
Definition: shm_mq.c:330
shm_mq_result
Definition: shm_mq.h:37
@ SHM_MQ_SUCCESS
Definition: shm_mq.h:38
Definition: c.h:676
static void verify_message(Size origlen, char *origdata, Size newlen, char *newdata)
Definition: test.c:251
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317

References data, dsm_detach(), ereport, errcode(), errmsg(), ERROR, len, PG_GETARG_INT32, PG_GETARG_INT64, PG_GETARG_TEXT_PP, PG_RETURN_VOID, res, shm_mq_receive(), shm_mq_send(), SHM_MQ_SUCCESS, test_shm_mq_setup(), VARDATA_ANY, VARSIZE_ANY_EXHDR, and verify_message().

◆ test_shm_mq_pipelined()

Datum test_shm_mq_pipelined ( PG_FUNCTION_ARGS  )

Definition at line 129 of file test.c.

130 {
131  int64 queue_size = PG_GETARG_INT64(0);
132  text *message = PG_GETARG_TEXT_PP(1);
133  char *message_contents = VARDATA_ANY(message);
134  int message_size = VARSIZE_ANY_EXHDR(message);
135  int32 loop_count = PG_GETARG_INT32(2);
136  int32 nworkers = PG_GETARG_INT32(3);
137  bool verify = PG_GETARG_BOOL(4);
138  int32 send_count = 0;
139  int32 receive_count = 0;
140  dsm_segment *seg;
141  shm_mq_handle *outqh;
142  shm_mq_handle *inqh;
144  Size len;
145  void *data;
146 
147  /* A negative loopcount is nonsensical. */
148  if (loop_count < 0)
149  ereport(ERROR,
150  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
151  errmsg("repeat count size must be an integer value greater than or equal to zero")));
152 
153  /*
154  * Using the nonblocking interfaces, we can even send data to ourselves,
155  * so the minimum number of workers for this test is zero.
156  */
157  if (nworkers < 0)
158  ereport(ERROR,
159  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
160  errmsg("number of workers must be an integer value greater than or equal to zero")));
161 
162  /* Set up dynamic shared memory segment and background workers. */
163  test_shm_mq_setup(queue_size, nworkers, &seg, &outqh, &inqh);
164 
165  /* Main loop. */
166  for (;;)
167  {
168  bool wait = true;
169 
170  /*
171  * If we haven't yet sent the message the requisite number of times,
172  * try again to send it now. Note that when shm_mq_send() returns
173  * SHM_MQ_WOULD_BLOCK, the next call to that function must pass the
174  * same message size and contents; that's not an issue here because
175  * we're sending the same message every time.
176  */
177  if (send_count < loop_count)
178  {
179  res = shm_mq_send(outqh, message_size, message_contents, true,
180  true);
181  if (res == SHM_MQ_SUCCESS)
182  {
183  ++send_count;
184  wait = false;
185  }
186  else if (res == SHM_MQ_DETACHED)
187  ereport(ERROR,
188  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
189  errmsg("could not send message")));
190  }
191 
192  /*
193  * If we haven't yet received the message the requisite number of
194  * times, try to receive it again now.
195  */
196  if (receive_count < loop_count)
197  {
198  res = shm_mq_receive(inqh, &len, &data, true);
199  if (res == SHM_MQ_SUCCESS)
200  {
201  ++receive_count;
202  /* Verifying every time is slow, so it's optional. */
203  if (verify)
204  verify_message(message_size, message_contents, len, data);
205  wait = false;
206  }
207  else if (res == SHM_MQ_DETACHED)
208  ereport(ERROR,
209  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
210  errmsg("could not receive message")));
211  }
212  else
213  {
214  /*
215  * Otherwise, we've received the message enough times. This
216  * shouldn't happen unless we've also sent it enough times.
217  */
218  if (send_count != receive_count)
219  ereport(ERROR,
220  (errcode(ERRCODE_INTERNAL_ERROR),
221  errmsg("message sent %d times, but received %d times",
222  send_count, receive_count)));
223  break;
224  }
225 
226  if (wait)
227  {
228  /*
229  * If we made no progress, wait for one of the other processes to
230  * which we are connected to set our latch, indicating that they
231  * have read or written data and therefore there may now be work
232  * for us to do.
233  */
238  }
239  }
240 
241  /* Clean up. */
242  dsm_detach(seg);
243 
244  PG_RETURN_VOID();
245 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
struct Latch * MyLatch
Definition: globals.c:58
void ResetLatch(Latch *latch)
Definition: latch.c:697
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:490
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:130
#define WL_LATCH_SET
Definition: latch.h:125
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
@ SHM_MQ_DETACHED
Definition: shm_mq.h:40
@ WAIT_EVENT_EXTENSION
Definition: wait_event.h:58

References CHECK_FOR_INTERRUPTS, data, dsm_detach(), ereport, errcode(), errmsg(), ERROR, len, MyLatch, PG_GETARG_BOOL, PG_GETARG_INT32, PG_GETARG_INT64, PG_GETARG_TEXT_PP, PG_RETURN_VOID, res, ResetLatch(), SHM_MQ_DETACHED, shm_mq_receive(), shm_mq_send(), SHM_MQ_SUCCESS, test_shm_mq_setup(), VARDATA_ANY, VARSIZE_ANY_EXHDR, verify_message(), WAIT_EVENT_EXTENSION, WaitLatch(), WL_EXIT_ON_PM_DEATH, and WL_LATCH_SET.

◆ verify_message()

static void verify_message ( Size  origlen,
char *  origdata,
Size  newlen,
char *  newdata 
)
static

Definition at line 251 of file test.c.

252 {
253  Size i;
254 
255  if (origlen != newlen)
256  ereport(ERROR,
257  (errmsg("message corrupted"),
258  errdetail("The original message was %zu bytes but the final message is %zu bytes.",
259  origlen, newlen)));
260 
261  for (i = 0; i < origlen; ++i)
262  if (origdata[i] != newdata[i])
263  ereport(ERROR,
264  (errmsg("message corrupted"),
265  errdetail("The new and original messages differ at byte %zu of %zu.", i, origlen)));
266 }
int errdetail(const char *fmt,...)
Definition: elog.c:1202
int i
Definition: isn.c:73

References ereport, errdetail(), errmsg(), ERROR, and i.

Referenced by test_shm_mq(), and test_shm_mq_pipelined().

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 23 of file test.c.