PostgreSQL Source Code  git master
injection_points.c File Reference
#include "postgres.h"
#include "fmgr.h"
#include "miscadmin.h"
#include "storage/condition_variable.h"
#include "storage/dsm_registry.h"
#include "storage/ipc.h"
#include "storage/lwlock.h"
#include "storage/shmem.h"
#include "utils/builtins.h"
#include "utils/injection_point.h"
#include "utils/wait_event.h"
Include dependency graph for injection_points.c:

Go to the source code of this file.

Data Structures

struct  InjectionPointCondition
 
struct  InjectionPointSharedState
 

Macros

#define INJ_MAX_WAIT   8
 
#define INJ_NAME_MAXLEN   64
 
#define INJ_MAX_CONDITION   4
 

Typedefs

typedef struct InjectionPointCondition InjectionPointCondition
 
typedef struct InjectionPointSharedState InjectionPointSharedState
 

Functions

PGDLLEXPORT void injection_error (const char *name)
 
PGDLLEXPORT void injection_notice (const char *name)
 
PGDLLEXPORT void injection_wait (const char *name)
 
static void injection_point_init_state (void *ptr)
 
static void injection_init_shmem (void)
 
static bool injection_point_allowed (const char *name)
 
static void injection_points_cleanup (int code, Datum arg)
 
 PG_FUNCTION_INFO_V1 (injection_points_attach)
 
Datum injection_points_attach (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (injection_points_run)
 
Datum injection_points_run (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (injection_points_wakeup)
 
Datum injection_points_wakeup (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (injection_points_set_local)
 
Datum injection_points_set_local (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (injection_points_detach)
 
Datum injection_points_detach (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 
static InjectionPointSharedStateinj_state = NULL
 
static bool injection_point_local = false
 

Macro Definition Documentation

◆ INJ_MAX_CONDITION

#define INJ_MAX_CONDITION   4

Definition at line 36 of file injection_points.c.

◆ INJ_MAX_WAIT

#define INJ_MAX_WAIT   8

Definition at line 34 of file injection_points.c.

◆ INJ_NAME_MAXLEN

#define INJ_NAME_MAXLEN   64

Definition at line 35 of file injection_points.c.

Typedef Documentation

◆ InjectionPointCondition

◆ InjectionPointSharedState

Function Documentation

◆ injection_error()

void injection_error ( const char *  name)

Definition at line 187 of file injection_points.c.

188 {
190  return;
191 
192  elog(ERROR, "error triggered for injection point %s", name);
193 }
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
static bool injection_point_allowed(const char *name)
const char * name

References elog, ERROR, injection_point_allowed(), and name.

◆ injection_init_shmem()

static void injection_init_shmem ( void  )
static

Definition at line 102 of file injection_points.c.

103 {
104  bool found;
105 
106  if (inj_state != NULL)
107  return;
108 
109  inj_state = GetNamedDSMSegment("injection_points",
112  &found);
113 }
void * GetNamedDSMSegment(const char *name, size_t size, void(*init_callback)(void *ptr), bool *found)
Definition: dsm_registry.c:131
static void injection_point_init_state(void *ptr)
static InjectionPointSharedState * inj_state

References GetNamedDSMSegment(), inj_state, and injection_point_init_state().

Referenced by injection_point_allowed(), injection_points_detach(), injection_points_set_local(), injection_points_wakeup(), and injection_wait().

◆ injection_notice()

void injection_notice ( const char *  name)

Definition at line 196 of file injection_points.c.

197 {
199  return;
200 
201  elog(NOTICE, "notice triggered for injection point %s", name);
202 }
#define NOTICE
Definition: elog.h:35

References elog, injection_point_allowed(), name, and NOTICE.

◆ injection_point_allowed()

static bool injection_point_allowed ( const char *  name)
static

Definition at line 123 of file injection_points.c.

124 {
125  bool result = true;
126 
127  if (inj_state == NULL)
129 
131 
132  for (int i = 0; i < INJ_MAX_CONDITION; i++)
133  {
135 
136  if (strcmp(condition->name, name) == 0)
137  {
138  /*
139  * Check if this injection point is allowed to run in this
140  * process.
141  */
142  if (MyProcPid != condition->pid)
143  {
144  result = false;
145  break;
146  }
147  }
148  }
149 
151 
152  return result;
153 }
int MyProcPid
Definition: globals.c:45
static void injection_init_shmem(void)
#define INJ_MAX_CONDITION
int i
Definition: isn.c:73
#define SpinLockRelease(lock)
Definition: spin.h:64
#define SpinLockAcquire(lock)
Definition: spin.h:62
char name[INJ_NAME_MAXLEN]
InjectionPointCondition conditions[INJ_MAX_CONDITION]

References InjectionPointSharedState::conditions, i, INJ_MAX_CONDITION, inj_state, injection_init_shmem(), InjectionPointSharedState::lock, MyProcPid, name, InjectionPointCondition::name, InjectionPointCondition::pid, SpinLockAcquire, and SpinLockRelease.

Referenced by injection_error(), injection_notice(), and injection_wait().

◆ injection_point_init_state()

static void injection_point_init_state ( void *  ptr)
static

Definition at line 87 of file injection_points.c.

88 {
90 
91  SpinLockInit(&state->lock);
92  memset(state->wait_counts, 0, sizeof(state->wait_counts));
93  memset(state->name, 0, sizeof(state->name));
94  memset(state->conditions, 0, sizeof(state->conditions));
95  ConditionVariableInit(&state->wait_point);
96 }
void ConditionVariableInit(ConditionVariable *cv)
#define SpinLockInit(lock)
Definition: spin.h:60
Definition: regguts.h:323

References ConditionVariableInit(), and SpinLockInit.

Referenced by injection_init_shmem().

◆ injection_points_attach()

Datum injection_points_attach ( PG_FUNCTION_ARGS  )

Definition at line 272 of file injection_points.c.

273 {
276  char *function;
277 
278  if (strcmp(action, "error") == 0)
279  function = "injection_error";
280  else if (strcmp(action, "notice") == 0)
281  function = "injection_notice";
282  else if (strcmp(action, "wait") == 0)
283  function = "injection_wait";
284  else
285  elog(ERROR, "incorrect action \"%s\" for injection point creation", action);
286 
287  InjectionPointAttach(name, "injection_points", function);
288 
290  {
291  int index = -1;
292 
293  /*
294  * Register runtime condition to link this injection point to the
295  * current process.
296  */
298  for (int i = 0; i < INJ_MAX_CONDITION; i++)
299  {
301 
302  if (condition->name[0] == '\0')
303  {
304  index = i;
305  strlcpy(condition->name, name, INJ_NAME_MAXLEN);
306  condition->pid = MyProcPid;
307  break;
308  }
309  }
311 
312  if (index < 0)
313  elog(FATAL,
314  "could not find free slot for condition of injection point %s",
315  name);
316  }
317 
318  PG_RETURN_VOID();
319 }
#define FATAL
Definition: elog.h:41
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
void InjectionPointAttach(const char *name, const char *library, const char *function)
static bool injection_point_local
#define INJ_NAME_MAXLEN
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
Definition: type.h:95
char * text_to_cstring(const text *t)
Definition: varlena.c:217

References generate_unaccent_rules::action, InjectionPointSharedState::conditions, elog, ERROR, FATAL, i, INJ_MAX_CONDITION, INJ_NAME_MAXLEN, inj_state, injection_point_local, InjectionPointAttach(), InjectionPointSharedState::lock, MyProcPid, name, InjectionPointCondition::name, PG_GETARG_TEXT_PP, PG_RETURN_VOID, InjectionPointCondition::pid, SpinLockAcquire, SpinLockRelease, strlcpy(), and text_to_cstring().

◆ injection_points_cleanup()

static void injection_points_cleanup ( int  code,
Datum  arg 
)
static

Definition at line 160 of file injection_points.c.

161 {
162  /* Leave if nothing is tracked locally */
164  return;
165 
167  for (int i = 0; i < INJ_MAX_CONDITION; i++)
168  {
170 
171  if (condition->name[0] == '\0')
172  continue;
173 
174  if (condition->pid != MyProcPid)
175  continue;
176 
177  /* Detach the injection point and unregister condition */
178  InjectionPointDetach(condition->name);
179  condition->name[0] = '\0';
180  condition->pid = 0;
181  }
183 }
void InjectionPointDetach(const char *name)

References InjectionPointSharedState::conditions, i, INJ_MAX_CONDITION, inj_state, injection_point_local, InjectionPointDetach(), InjectionPointSharedState::lock, MyProcPid, InjectionPointCondition::name, InjectionPointCondition::pid, SpinLockAcquire, and SpinLockRelease.

Referenced by injection_points_set_local().

◆ injection_points_detach()

Datum injection_points_detach ( PG_FUNCTION_ARGS  )

Definition at line 402 of file injection_points.c.

403 {
405 
407 
408  if (inj_state == NULL)
410 
411  /* Clean up any conditions associated to this injection point */
413  for (int i = 0; i < INJ_MAX_CONDITION; i++)
414  {
416 
417  if (strcmp(condition->name, name) == 0)
418  {
419  condition->pid = 0;
420  condition->name[0] = '\0';
421  }
422  }
424 
425  PG_RETURN_VOID();
426 }

References InjectionPointSharedState::conditions, i, INJ_MAX_CONDITION, inj_state, injection_init_shmem(), InjectionPointDetach(), InjectionPointSharedState::lock, name, InjectionPointCondition::name, PG_GETARG_TEXT_PP, PG_RETURN_VOID, InjectionPointCondition::pid, SpinLockAcquire, SpinLockRelease, and text_to_cstring().

◆ injection_points_run()

Datum injection_points_run ( PG_FUNCTION_ARGS  )

Definition at line 326 of file injection_points.c.

327 {
329 
331 
332  PG_RETURN_VOID();
333 }
#define INJECTION_POINT(name)

References INJECTION_POINT, name, PG_GETARG_TEXT_PP, PG_RETURN_VOID, and text_to_cstring().

◆ injection_points_set_local()

Datum injection_points_set_local ( PG_FUNCTION_ARGS  )

Definition at line 380 of file injection_points.c.

381 {
382  /* Enable flag to add a runtime condition based on this process ID */
383  injection_point_local = true;
384 
385  if (inj_state == NULL)
387 
388  /*
389  * Register a before_shmem_exit callback to remove any injection points
390  * linked to this process.
391  */
393 
394  PG_RETURN_VOID();
395 }
static void injection_points_cleanup(int code, Datum arg)
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:337
uintptr_t Datum
Definition: postgres.h:64

References before_shmem_exit(), inj_state, injection_init_shmem(), injection_point_local, injection_points_cleanup(), and PG_RETURN_VOID.

◆ injection_points_wakeup()

Datum injection_points_wakeup ( PG_FUNCTION_ARGS  )

Definition at line 340 of file injection_points.c.

341 {
343  int index = -1;
344 
345  if (inj_state == NULL)
347 
348  /* First bump the wait counter for the injection point to wake up */
350  for (int i = 0; i < INJ_MAX_WAIT; i++)
351  {
352  if (strcmp(name, inj_state->name[i]) == 0)
353  {
354  index = i;
355  break;
356  }
357  }
358  if (index < 0)
359  {
361  elog(ERROR, "could not find injection point %s to wake up", name);
362  }
365 
366  /* And broadcast the change to the waiters */
368  PG_RETURN_VOID();
369 }
void ConditionVariableBroadcast(ConditionVariable *cv)
#define INJ_MAX_WAIT
uint32 wait_counts[INJ_MAX_WAIT]
char name[INJ_MAX_WAIT][INJ_NAME_MAXLEN]
ConditionVariable wait_point

References ConditionVariableBroadcast(), elog, ERROR, i, INJ_MAX_WAIT, inj_state, injection_init_shmem(), InjectionPointSharedState::lock, name, InjectionPointSharedState::name, PG_GETARG_TEXT_PP, PG_RETURN_VOID, SpinLockAcquire, SpinLockRelease, text_to_cstring(), InjectionPointSharedState::wait_counts, and InjectionPointSharedState::wait_point.

◆ injection_wait()

void injection_wait ( const char *  name)

Definition at line 206 of file injection_points.c.

207 {
208  uint32 old_wait_counts = 0;
209  int index = -1;
210  uint32 injection_wait_event = 0;
211 
212  if (inj_state == NULL)
214 
216  return;
217 
218  /*
219  * Use the injection point name for this custom wait event. Note that
220  * this custom wait event name is not released, but we don't care much for
221  * testing as this should be short-lived.
222  */
223  injection_wait_event = WaitEventExtensionNew(name);
224 
225  /*
226  * Find a free slot to wait for, and register this injection point's name.
227  */
229  for (int i = 0; i < INJ_MAX_WAIT; i++)
230  {
231  if (inj_state->name[i][0] == '\0')
232  {
233  index = i;
235  old_wait_counts = inj_state->wait_counts[i];
236  break;
237  }
238  }
240 
241  if (index < 0)
242  elog(ERROR, "could not find free slot for wait of injection point %s ",
243  name);
244 
245  /* And sleep.. */
247  for (;;)
248  {
249  uint32 new_wait_counts;
250 
252  new_wait_counts = inj_state->wait_counts[index];
254 
255  if (old_wait_counts != new_wait_counts)
256  break;
257  ConditionVariableSleep(&inj_state->wait_point, injection_wait_event);
258  }
260 
261  /* Remove this injection point from the waiters. */
263  inj_state->name[index][0] = '\0';
265 }
unsigned int uint32
Definition: c.h:506
bool ConditionVariableCancelSleep(void)
void ConditionVariablePrepareToSleep(ConditionVariable *cv)
void ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info)
uint32 WaitEventExtensionNew(const char *wait_event_name)
Definition: wait_event.c:162

References ConditionVariableCancelSleep(), ConditionVariablePrepareToSleep(), ConditionVariableSleep(), elog, ERROR, i, INJ_MAX_WAIT, INJ_NAME_MAXLEN, inj_state, injection_init_shmem(), injection_point_allowed(), InjectionPointSharedState::lock, name, InjectionPointSharedState::name, SpinLockAcquire, SpinLockRelease, strlcpy(), InjectionPointSharedState::wait_counts, InjectionPointSharedState::wait_point, and WaitEventExtensionNew().

◆ PG_FUNCTION_INFO_V1() [1/5]

PG_FUNCTION_INFO_V1 ( injection_points_attach  )

◆ PG_FUNCTION_INFO_V1() [2/5]

PG_FUNCTION_INFO_V1 ( injection_points_detach  )

◆ PG_FUNCTION_INFO_V1() [3/5]

PG_FUNCTION_INFO_V1 ( injection_points_run  )

◆ PG_FUNCTION_INFO_V1() [4/5]

PG_FUNCTION_INFO_V1 ( injection_points_set_local  )

◆ PG_FUNCTION_INFO_V1() [5/5]

PG_FUNCTION_INFO_V1 ( injection_points_wakeup  )

Variable Documentation

◆ inj_state

◆ injection_point_local

bool injection_point_local = false
static

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 31 of file injection_points.c.