2004-08-13 03:31:15 +04:00
|
|
|
/*
|
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _OMPI_CIRCULAR_BUFFER_FIFO
|
|
|
|
#define _OMPI_CIRCULAR_BUFFER_FIFO
|
|
|
|
|
2004-08-13 23:49:13 +04:00
|
|
|
#include "include/constants.h"
|
2004-08-13 03:31:15 +04:00
|
|
|
#include "include/sys/cache.h"
|
2004-08-19 03:24:27 +04:00
|
|
|
#include "include/sys/atomic.h"
|
2004-08-13 23:49:13 +04:00
|
|
|
#include "mca/mpool/mpool.h"
|
2004-08-17 03:06:33 +04:00
|
|
|
#include "util/pow2.h"
|
2004-08-13 03:31:15 +04:00
|
|
|
|
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* This defines a set of functions to create, and manipulate a FIFO
|
|
|
|
* set up in a circular buffer. FIFO elements are assumed to be
|
|
|
|
* pointers. Pointers are written to the head, and read from the
|
2004-08-18 01:49:01 +04:00
|
|
|
* tail. For thread safety, a spin lock is provided in the
|
|
|
|
* ompi_cb_fifo_ctl_t structure, but it's use must be managed by
|
|
|
|
* the calling routines - this is not by these set of routines.
|
2004-08-13 03:31:15 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* error code */
|
|
|
|
#define OMPI_CB_ERROR -1
|
|
|
|
#define OMPI_CB_FREE (void *) 0
|
|
|
|
#define OMPI_CB_RESERVED (void *) 1
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Structure used to keep track of the fifo status
|
|
|
|
*/
|
|
|
|
struct ompi_cb_fifo_ctl_t {
|
|
|
|
/* spin-lock for access control */
|
2004-08-25 00:17:44 +04:00
|
|
|
ompi_lock_t lock;
|
2004-08-13 03:31:15 +04:00
|
|
|
|
|
|
|
/* current queue index */
|
|
|
|
volatile unsigned int fifo_index;
|
|
|
|
|
|
|
|
/* number of entries that have been used, but not invalidated. used
|
|
|
|
* for lazy resource reclamation */
|
|
|
|
volatile unsigned int num_to_clear;
|
|
|
|
|
|
|
|
};
|
|
|
|
typedef struct ompi_cb_fifo_ctl_t ompi_cb_fifo_ctl_t;
|
|
|
|
|
|
|
|
/* default settings */
|
|
|
|
|
|
|
|
/* data structure used to describe the fifo */
|
|
|
|
struct ompi_cb_fifo_t {
|
|
|
|
|
|
|
|
/* size of fifo */
|
|
|
|
int size;
|
|
|
|
|
2004-08-13 23:49:13 +04:00
|
|
|
/* frequency of lazy free */
|
|
|
|
int lazy_free_frequency;
|
|
|
|
|
2004-08-25 00:17:44 +04:00
|
|
|
/* fifo memory locality index */
|
|
|
|
int fifo_memory_locality_index;
|
|
|
|
|
|
|
|
/* head memory locality index */
|
|
|
|
int head_memory_locality_index;
|
|
|
|
|
|
|
|
/* tail memory locality index */
|
|
|
|
int tail_memory_locality_index;
|
|
|
|
|
2004-08-13 03:31:15 +04:00
|
|
|
/* head of queue - where next entry will be written */
|
|
|
|
ompi_cb_fifo_ctl_t *head;
|
|
|
|
|
|
|
|
/* tail of queue - next element to read */
|
|
|
|
ompi_cb_fifo_ctl_t *tail;
|
|
|
|
|
|
|
|
/* mask - to handle wrap around */
|
|
|
|
unsigned int mask;
|
|
|
|
|
|
|
|
/* circular buffer array */
|
2004-08-25 00:17:44 +04:00
|
|
|
volatile void **queue;
|
2004-08-13 03:31:15 +04:00
|
|
|
|
2004-08-13 23:49:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct ompi_cb_fifo_t ompi_cb_fifo_t;
|
2004-08-13 03:31:15 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize a fifo
|
|
|
|
*
|
|
|
|
* @param size_of_fifo Length of fifo array (IN)
|
|
|
|
*
|
|
|
|
* @param fifo_memory_locality_index Locality index to apply to
|
|
|
|
* the fifo array. Not currently
|
|
|
|
* in use (IN)
|
|
|
|
*
|
|
|
|
* @param tail_memory_locality_index Locality index to apply to the
|
|
|
|
* head control structure. Not
|
|
|
|
* currently in use (IN)
|
|
|
|
*
|
|
|
|
* @param tail_memory_locality_index Locality index to apply to the
|
|
|
|
* tail control structure. Not
|
|
|
|
* currently in use (IN)
|
|
|
|
*
|
|
|
|
* @param fifo Pointer to data structure defining this fifo (IN)
|
|
|
|
*
|
|
|
|
* @param memory_allocator Pointer to the memory allocator to use
|
|
|
|
* to allocate memory for this fifo. (IN)
|
|
|
|
*
|
|
|
|
* @returncode Error code
|
|
|
|
*
|
|
|
|
*/
|
2004-08-13 23:49:13 +04:00
|
|
|
static inline int ompi_cb_fifo_init(int size_of_fifo, int lazy_free_freq,
|
|
|
|
int fifo_memory_locality_index, int head_memory_locality_index,
|
|
|
|
int tail_memory_locality_index, ompi_cb_fifo_t *fifo,
|
|
|
|
mca_mpool_base_module_t *memory_allocator)
|
2004-08-13 03:31:15 +04:00
|
|
|
{
|
|
|
|
|
2004-08-17 03:06:33 +04:00
|
|
|
int errorCode = OMPI_SUCCESS,i;
|
|
|
|
size_t len_to_allocate;
|
2004-08-13 03:31:15 +04:00
|
|
|
|
|
|
|
/* verify that size is power of 2, and greatter that 0 - if not,
|
|
|
|
* round up */
|
|
|
|
if ( 0 >= size_of_fifo) {
|
|
|
|
return OMPI_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set fifo size */
|
2004-08-17 03:06:33 +04:00
|
|
|
fifo->size = ompi_round_up_to_nearest_pow2(size_of_fifo);
|
2004-08-13 23:49:13 +04:00
|
|
|
|
|
|
|
/* set lazy free frequence */
|
|
|
|
if( ( 0 >= lazy_free_freq ) ||
|
|
|
|
( lazy_free_freq > fifo->size) ) {
|
|
|
|
return OMPI_ERROR;
|
|
|
|
}
|
|
|
|
fifo->lazy_free_frequency=lazy_free_freq;
|
2004-08-13 03:31:15 +04:00
|
|
|
|
|
|
|
/* this will be used to mask off the higher order bits,
|
|
|
|
* and use the & operator for the wrap-around */
|
2004-08-17 03:37:07 +04:00
|
|
|
fifo->mask = (fifo->size - 1);
|
2004-08-13 03:31:15 +04:00
|
|
|
|
|
|
|
/* allocate fifo array */
|
2004-08-17 03:37:07 +04:00
|
|
|
len_to_allocate = sizeof(void *) * fifo->size;
|
2004-08-13 03:31:15 +04:00
|
|
|
fifo->queue=memory_allocator->mpool_alloc(len_to_allocate,CACHE_LINE_SIZE);
|
|
|
|
if ( NULL == fifo->queue) {
|
2004-08-13 23:49:13 +04:00
|
|
|
return OMPI_ERR_OUT_OF_RESOURCE;
|
2004-08-13 03:31:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize the queue entries */
|
2004-08-17 03:37:07 +04:00
|
|
|
for (i = 0; i < fifo->size; i++) {
|
2004-08-13 23:49:13 +04:00
|
|
|
fifo->queue[i] = OMPI_CB_FREE;
|
2004-08-13 03:31:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate head control structure */
|
|
|
|
len_to_allocate = sizeof(ompi_cb_fifo_ctl_t);
|
|
|
|
fifo->head=memory_allocator->mpool_alloc(len_to_allocate,CACHE_LINE_SIZE);
|
|
|
|
if ( NULL == fifo->head) {
|
2004-08-13 23:49:13 +04:00
|
|
|
return OMPI_ERR_OUT_OF_RESOURCE;
|
2004-08-13 03:31:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize the head structure */
|
2004-08-25 00:17:44 +04:00
|
|
|
ompi_atomic_unlock(&(fifo->head->lock));
|
2004-08-13 23:49:13 +04:00
|
|
|
fifo->head->fifo_index=0;
|
|
|
|
fifo->head->num_to_clear=0;
|
2004-08-13 03:31:15 +04:00
|
|
|
|
|
|
|
/* allocate tail control structure */
|
|
|
|
len_to_allocate = sizeof(ompi_cb_fifo_ctl_t);
|
|
|
|
fifo->tail=memory_allocator->mpool_alloc(len_to_allocate,CACHE_LINE_SIZE);
|
|
|
|
if ( NULL == fifo->tail) {
|
2004-08-13 23:49:13 +04:00
|
|
|
return OMPI_ERR_OUT_OF_RESOURCE;
|
2004-08-13 03:31:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize the head structure */
|
2004-08-25 00:17:44 +04:00
|
|
|
ompi_atomic_unlock(&(fifo->tail->lock));
|
2004-08-13 23:49:13 +04:00
|
|
|
fifo->tail->fifo_index=0;
|
|
|
|
fifo->tail->num_to_clear=0;
|
2004-08-13 03:31:15 +04:00
|
|
|
|
2004-08-25 00:17:44 +04:00
|
|
|
/* set memory locality indecies */
|
|
|
|
fifo->fifo_memory_locality_index=fifo_memory_locality_index;
|
|
|
|
fifo->head_memory_locality_index=head_memory_locality_index;
|
|
|
|
fifo->tail_memory_locality_index=tail_memory_locality_index;
|
|
|
|
|
2004-08-13 03:31:15 +04:00
|
|
|
/* return */
|
|
|
|
return errorCode;
|
|
|
|
}
|
|
|
|
|
2004-08-17 03:06:33 +04:00
|
|
|
/**
|
|
|
|
* function to cleanup the fifo
|
|
|
|
*
|
|
|
|
* @param fifo Pointer to data structure defining this fifo (IN)
|
|
|
|
*
|
|
|
|
* @param memory_allocator Pointer to the memory allocator to use
|
|
|
|
* to allocate memory for this fifo. (IN)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static inline int ompi_cb_fifo_free( ompi_cb_fifo_t *fifo,
|
|
|
|
mca_mpool_base_module_t *memory_allocator)
|
|
|
|
{
|
|
|
|
|
|
|
|
int errorCode = OMPI_SUCCESS;
|
|
|
|
|
|
|
|
/* make sure null fifo is not passed in */
|
|
|
|
if ( NULL == fifo) {
|
|
|
|
return OMPI_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free fifo array */
|
|
|
|
if( NULL != fifo->queue){
|
|
|
|
memory_allocator->mpool_free(fifo->queue);
|
2004-08-25 00:17:44 +04:00
|
|
|
fifo->queue=NULL;
|
2004-08-17 03:06:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* free head control structure */
|
|
|
|
if( NULL != fifo->head) {
|
2004-08-25 00:17:44 +04:00
|
|
|
memory_allocator->mpool_free(fifo->head);
|
|
|
|
fifo->head=NULL;
|
|
|
|
|
2004-08-17 03:06:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* free tail control structure */
|
|
|
|
if( NULL != fifo->tail) {
|
2004-08-25 00:17:44 +04:00
|
|
|
memory_allocator->mpool_free(fifo->tail);
|
|
|
|
fifo->tail=NULL;
|
2004-08-17 03:06:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* return */
|
|
|
|
return errorCode;
|
|
|
|
}
|
|
|
|
|
2004-08-13 03:31:15 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Write pointer to the specified slot
|
|
|
|
*
|
|
|
|
* @param slot Slot index (IN)
|
|
|
|
*
|
|
|
|
* @param data Pointer value to write in specified slot (IN)
|
|
|
|
*
|
|
|
|
* @param fifo Pointer to data structure defining this fifo (IN)
|
|
|
|
*
|
|
|
|
* @returncode Slot index to which data is written
|
|
|
|
*
|
|
|
|
*/
|
2004-08-13 23:49:13 +04:00
|
|
|
static inline int ompi_cb_fifo_write_to_slot(int slot, void* data, ompi_cb_fifo_t *fifo) {
|
2004-08-13 03:31:15 +04:00
|
|
|
int wrote_to_slot = OMPI_CB_ERROR;
|
|
|
|
/* make sure that this slot is already reserved */
|
|
|
|
if (fifo->queue[slot] == OMPI_CB_RESERVED ) {
|
|
|
|
fifo->queue[slot] = data;
|
|
|
|
return slot;
|
|
|
|
} else {
|
|
|
|
return wrote_to_slot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to write pointer to the head of the queue
|
|
|
|
*
|
|
|
|
* @param data Pointer value to write in specified slot (IN)
|
|
|
|
*
|
|
|
|
* @param fifo Pointer to data structure defining this fifo (IN)
|
|
|
|
*
|
|
|
|
* @returncode Slot index to which data is written
|
|
|
|
*
|
|
|
|
*/
|
2004-08-13 23:49:13 +04:00
|
|
|
static inline int ompi_cb_fifo_write_to_head(void *data, ompi_cb_fifo_t *fifo) {
|
2004-08-13 03:31:15 +04:00
|
|
|
int slot = OMPI_CB_ERROR, index;
|
|
|
|
index = fifo->head->fifo_index;
|
|
|
|
/* make sure the head is pointing at a free element - avoid wrap
|
|
|
|
* around */
|
2004-08-13 23:49:13 +04:00
|
|
|
if (fifo->queue[index] == OMPI_CB_FREE) {
|
2004-08-13 03:31:15 +04:00
|
|
|
slot = index;
|
|
|
|
fifo->queue[slot] = data;
|
|
|
|
(fifo->head->fifo_index)++;
|
|
|
|
(fifo->head->fifo_index) &= fifo->mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return */
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reserve slot in the fifo array
|
|
|
|
*
|
|
|
|
* @param fifo Pointer to data structure defining this fifo (IN)
|
|
|
|
*
|
|
|
|
* @returncode Slot index to which data is written
|
|
|
|
*
|
|
|
|
* @returncode OMPI_CB_ERROR failed to allocate index
|
|
|
|
*
|
|
|
|
*/
|
2004-08-13 23:49:13 +04:00
|
|
|
static inline int ompi_cb_fifo_get_slot(ompi_cb_fifo_t *fifo) {
|
2004-08-13 03:31:15 +04:00
|
|
|
int return_value = OMPI_CB_ERROR,index;
|
|
|
|
|
|
|
|
index = fifo->head->fifo_index;
|
|
|
|
/* try and reserve slot */
|
|
|
|
if (fifo->queue[index] == OMPI_CB_FREE) {
|
|
|
|
fifo->queue[index] = OMPI_CB_RESERVED;
|
|
|
|
return_value = index;
|
|
|
|
(fifo->head->fifo_index)++;
|
|
|
|
(fifo->head->fifo_index) &= fifo->mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return */
|
|
|
|
return return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to read pointer from the tail of the queue
|
|
|
|
*
|
2004-08-25 00:17:44 +04:00
|
|
|
* @param data Pointer to where data was be written (OUT)
|
2004-08-13 03:31:15 +04:00
|
|
|
*
|
|
|
|
* @param fifo Pointer to data structure defining this fifo (IN)
|
|
|
|
*
|
2004-08-19 02:53:04 +04:00
|
|
|
* @param flush_entries_read force the lazy free to happen (IN)
|
|
|
|
*
|
2004-08-25 00:17:44 +04:00
|
|
|
* @param queue_empty checks to see if the fifo is empty, but only if
|
|
|
|
* flush_entries_read is set (OUT)
|
|
|
|
*
|
2004-08-13 03:31:15 +04:00
|
|
|
* @returncode Slot index to which data is written
|
|
|
|
*
|
|
|
|
*/
|
2004-08-19 02:53:04 +04:00
|
|
|
static inline void *ompi_cb_fifo_read_from_tail(ompi_cb_fifo_t *fifo,
|
2004-08-25 00:17:44 +04:00
|
|
|
bool flush_entries_read, bool *queue_empty)
|
2004-08-13 03:31:15 +04:00
|
|
|
{
|
2004-08-18 01:49:01 +04:00
|
|
|
int index = 0,clearIndex, i;
|
|
|
|
void *read_from_tail = (void *)OMPI_CB_ERROR;
|
2004-08-25 00:17:44 +04:00
|
|
|
*queue_empty=false;
|
2004-08-13 03:31:15 +04:00
|
|
|
|
|
|
|
/* check to see that the data is valid */
|
|
|
|
if ((fifo->queue[fifo->tail->fifo_index] == OMPI_CB_FREE) ||
|
|
|
|
(fifo->queue[fifo->tail->fifo_index] == OMPI_CB_RESERVED))
|
|
|
|
{
|
2004-08-18 01:49:01 +04:00
|
|
|
return (void *)OMPI_CB_ERROR;
|
2004-08-13 03:31:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set return data */
|
|
|
|
index = fifo->tail->fifo_index;
|
2004-08-18 01:49:01 +04:00
|
|
|
read_from_tail = (void *)fifo->queue[index];
|
2004-08-13 23:49:13 +04:00
|
|
|
fifo->tail->num_to_clear++;
|
2004-08-13 03:31:15 +04:00
|
|
|
|
|
|
|
/* check to see if time to do a lazy free of queue slots */
|
2004-08-19 02:53:04 +04:00
|
|
|
if ( (fifo->tail->num_to_clear == fifo->lazy_free_frequency) ||
|
|
|
|
flush_entries_read ) {
|
2004-08-25 00:17:44 +04:00
|
|
|
clearIndex = index - fifo->tail->num_to_clear + 1;
|
2004-08-13 03:31:15 +04:00
|
|
|
clearIndex &= fifo->mask;
|
|
|
|
|
2004-08-25 00:17:44 +04:00
|
|
|
for (i = 0; i < fifo->tail->num_to_clear; i++) {
|
2004-08-13 23:49:13 +04:00
|
|
|
fifo->queue[clearIndex] = OMPI_CB_FREE;
|
2004-08-13 03:31:15 +04:00
|
|
|
clearIndex++;
|
2004-08-13 23:49:13 +04:00
|
|
|
clearIndex &= fifo->mask;
|
2004-08-13 03:31:15 +04:00
|
|
|
}
|
2004-08-25 00:17:44 +04:00
|
|
|
fifo->tail->num_to_clear = 0;
|
|
|
|
|
|
|
|
/* check to see if queue is empty */
|
|
|
|
if( flush_entries_read &&
|
|
|
|
(fifo->tail->fifo_index == fifo->head->fifo_index) ) {
|
|
|
|
*queue_empty=true;
|
|
|
|
}
|
2004-08-13 03:31:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* increment counter for later lazy free */
|
|
|
|
(fifo->tail->fifo_index)++;
|
|
|
|
(fifo->tail->fifo_index) &= fifo->mask;
|
|
|
|
|
|
|
|
/* return */
|
|
|
|
return read_from_tail;
|
|
|
|
}
|
2004-08-17 03:37:07 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the fifo size
|
|
|
|
*
|
|
|
|
* @param fifo Pointer to data structure defining this fifo (IN)
|
|
|
|
*
|
|
|
|
* @returncode fifo size
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static inline int ompi_cb_fifo_size(ompi_cb_fifo_t *fifo) {
|
|
|
|
|
|
|
|
return fifo->size;
|
|
|
|
}
|
|
|
|
|
2004-08-13 03:31:15 +04:00
|
|
|
#endif /* !_OMPI_CIRCULAR_BUFFER_FIFO */
|