veil_shmem.c File Reference


Detailed Description

     Author:       Marc Munro
     Copyright (c) 2005, 2006 Marc Munro
     License:      BSD
 $Id: veil_shmem.c,v 1.7 2007/07/31 22:18:27 bloodnok Exp $

Functions for dealing with veil shared memory.

This provides dynamic memory allocation, like malloc, from chunks of shared memory allocated from the Postgres shared memory pool. In order to be able to reset and reload shared memory structures while other backends continue to use the existing structures, a shared memory reset creates a new context, or switches to an existing one that is no longer in use. No more than two separate contexts will be created.

Each context of veil shared memory is associated with a shared hash, which is used to store veil's shared variables. A specially named variable, VEIL_SHMEMCTL appears only in context0 and contains a reference to chunk0, and the ShmemCtl structure. From this structure we can identify the current context, the initial chunks for each active context, and whether a context switch is in progress.

A context switch takes place in 3 steps:

To access shared variable "x" in a new session, the following steps are taken:

Note that We use a dynamicall allocated LWLock, VeilLWLock to protect our shared control structures.

Definition in file veil_shmem.c.

#include "postgres.h"
#include "utils/hsearch.h"
#include "storage/pg_shmem.h"
#include "storage/shmem.h"
#include "storage/lwlock.h"
#include "access/xact.h"
#include "miscadmin.h"
#include "veil_version.h"
#include "veil_shmem.h"
#include "veil_funcs.h"

Include dependency graph for veil_shmem.c:

Go to the source code of this file.

Defines

#define OTHER_CONTEXT(x)   (x ? 0: 1)
 Return the index of the other context from the one supplied.

Functions

void _PG_init ()
 Veil's startup function.
static HTAB * create_shared_hash (const char *hashname)
 Create/attach to the shared hash identified by hashname.
static HTAB * get_hash0 ()
 Return reference to the HTAB for the shared hash associated with context 0.
static HTAB * get_hash1 ()
 Return reference to the HTAB for the shared hash associated with context 1.
static MemContextget_shmem_context (char *name, size_t size, bool *p_found)
 Allocate or attach to, a new chunk of shared memory for a named memory context.
static void shmalloc_init ()
 Attach to, creating and initialising as necessary, the shared memory control structure.
static int get_cur_context_id ()
 Return the id (index) of the current context for this session.
static MemContextget_cur_context ()
 Return pointer to shared memory allocated for the current context.
void * do_vl_shmalloc (MemContext *context, size_t size)
 Dynamically allocate a piece of shared memory from the current context, doing no locking.
void * vl_shmalloc (size_t size)
 Dynamically allocate a piece of shared memory from the current context.
void vl_free (void *mem)
 Free a piece of shared memory within the current context.
HTAB * vl_get_shared_hash ()
 Return the shared hash for the current context.
static void clear_hash (HTAB *hash)
 Reset one of the shared hashes.
bool vl_prepare_context_switch ()
 Prepare for a switch to the alternate context.
bool vl_complete_context_switch ()
 Complete the context switch started by vl_prepare_context_switch().
void vl_force_context_switch ()
 In desparation, if we are unable to complete a context switch, we should use this function.

Variables

static ShmemCtlshared_meminfo = ((void *) 0)
 shared_meminfo provides access to the ShmemCtl structure allocated in context 0.
static bool prepared_for_switch = ((bool) 0)
 Whether the current backend is in the process of switching contexts.
static LWLockId VeilLWLock = AddinShmemInitLock
 The LWLock that Veil will use for managing concurrent access to shared memory.
static LWLockId InitialLWLock = AddinShmemInitLock
 The LWLock to be used while initially setting up shared memory and allocating a veil database-specific LWLock.
static bool using_registered_shmem = ((bool) 0)
 Whether Veil has registered for addin shared memory or not.


Define Documentation

#define OTHER_CONTEXT (  )     (x ? 0: 1)

Return the index of the other context from the one supplied.

Parameters:
x the context for which we want the other one.
Returns:
the opposite context to that of x.

Definition at line 101 of file veil_shmem.c.


Function Documentation

void _PG_init ( void   ) 

Veil's startup function.

This should be run when the Veil shared library is loaded by postgres.

If shared_preload_libraries is not defined, Veil may still be run but it will steal shared memory from postgres, potentially exhausting it.

This function is not defined if VEIL_TRIAL is set. This allows a veil_trail shared object to be created separately from the normal veil shared object, which may be used without defining shared_preload_libraries.

Definition at line 125 of file veil_shmem.c.

References veil_config_init(), veil_dbs_in_cluster(), and veil_shmem_context_size().

Here is the call graph for this function:

static HTAB* create_shared_hash ( const char *  hashname  )  [static]

Create/attach to the shared hash identified by hashname.

Return a pointer to an HTAB that references the shared hash. All locking is handled by the caller.

Parameters:
hashname 
Returns:
Pointer to HTAB referencing the shared hash.

Definition at line 150 of file veil_shmem.c.

References HASH_KEYLEN, veil_shared_hash_elems(), and vl_malloc().

Referenced by get_hash0(), and get_hash1().

Here is the call graph for this function:

static HTAB* get_hash0 (  )  [static]

Return reference to the HTAB for the shared hash associated with context 0.

Returns:
Pointer to HTAB referencing shared hash for context 0.

Definition at line 178 of file veil_shmem.c.

References create_shared_hash().

Here is the call graph for this function:

static HTAB* get_hash1 (  )  [static]

Return reference to the HTAB for the shared hash associated with context 1.

Returns:
Pointer to HTAB referencing shared hash for context 1.

Definition at line 195 of file veil_shmem.c.

References create_shared_hash().

Here is the call graph for this function:

static MemContext* get_shmem_context ( char *  name,
size_t  size,
bool *  p_found 
) [static]

Allocate or attach to, a new chunk of shared memory for a named memory context.

Parameters:
name The name
p_found Pointer to boolean that will identify whether this chunk has already been initialised.
Returns:
Pointer to chunk of shared memory.

Definition at line 217 of file veil_shmem.c.

References MemContext::db_id, MemContext::limit, MemContext::lwlock, MemContext::next, veil_dbs_in_cluster(), VeilLWLock, vl_db_exists(), and vl_malloc().

Here is the call graph for this function:

static void shmalloc_init (  )  [static]

Attach to, creating and initialising as necessary, the shared memory control structure.

Record this for the session in shared_meminfo.

Definition at line 419 of file veil_shmem.c.

Referenced by get_cur_context_id().

static int get_cur_context_id (  )  [static]

Return the id (index) of the current context for this session.

Returns:
The current context id

Definition at line 306 of file veil_shmem.c.

References shmalloc_init().

Here is the call graph for this function:

static MemContext* get_cur_context (  )  [static]

Return pointer to shared memory allocated for the current context.

Returns:
The current context.

Definition at line 339 of file veil_shmem.c.

Referenced by vl_get_shared_hash().

void* do_vl_shmalloc ( MemContext context,
size_t  size 
)

Dynamically allocate a piece of shared memory from the current context, doing no locking.

Parameters:
context The context in which we are operating
size The size of the requested piece of memory.
Returns:
Pointer to dynamically allocated memory.

Definition at line 356 of file veil_shmem.c.

References MemContext::next.

void* vl_shmalloc ( size_t  size  ) 

Dynamically allocate a piece of shared memory from the current context.

Parameters:
size The size of the requested piece of memory.
Returns:
Pointer to dynamically allocated memory.

Definition at line 385 of file veil_shmem.c.

Referenced by vl_NewBitmap(), vl_NewInt4(), and vl_NewRange().

void vl_free ( void *  mem  ) 

Free a piece of shared memory within the current context.

Currently this does nothing as implementation of freeing of shared memory has been deferred.

Parameters:
mem Pointer to the memory to be freed.

Definition at line 408 of file veil_shmem.c.

References shared_meminfo.

Referenced by vl_NewBitmap().

HTAB* vl_get_shared_hash ( void   ) 

Return the shared hash for the current context.

Returns:
Pointer to the HTAB for the current context's shared hash.

Definition at line 522 of file veil_shmem.c.

References get_cur_context().

Referenced by ensure_init(), and vl_lookup_shared_variable().

Here is the call graph for this function:

static void clear_hash ( HTAB *  hash  )  [static]

Reset one of the shared hashes.

This is one of the final steps in a context switch.

Returns:
hash The shared hash that is to be reset.

Definition at line 552 of file veil_shmem.c.

bool vl_prepare_context_switch ( void   ) 

Prepare for a switch to the alternate context.

Switching will only be allowed if there are no transactions that may still be using the context to which we are switching, and there is no other process attempting the switch.

Returns:
true if the switch preparation was successful.

Definition at line 574 of file veil_shmem.c.

bool vl_complete_context_switch ( void   ) 

Complete the context switch started by vl_prepare_context_switch().

Raise an ERROR if the context switch cannot be completed.

Returns:
true if the context switch is successfully completed.

Definition at line 646 of file veil_shmem.c.


Variable Documentation

bool prepared_for_switch = ((bool) 0) [static]

Whether the current backend is in the process of switching contexts.

If so, it will be setting up the non-current context in readiness for making it available to all other backends.

Definition at line 79 of file veil_shmem.c.

LWLockId VeilLWLock = AddinShmemInitLock [static]

The LWLock that Veil will use for managing concurrent access to shared memory.

It is initialised to a lock id that is distinct from any tha twill be dynamically allocated.

Definition at line 86 of file veil_shmem.c.

Referenced by get_shmem_context().

bool using_registered_shmem = ((bool) 0) [static]

Whether Veil has registered for addin shared memory or not.

If not, Veil will steal shared memory from postgres with the subsequent risk of shared memory exhaustion.

Definition at line 108 of file veil_shmem.c.


Generated on Tue Jul 31 15:35:57 2007 for Veil by  doxygen 1.5.2