libbladeRF: Utilize all available TX buffers

This fixes issue #438, in which the bladerf_sync_tx() implementation
was limited by the number of transfers, and not taking advantage
of the available TX buffers (beyond N=<num_transfers>).

This is addressed by allowing the bladerf_sync_tx() context to defer
buffer submission to the sync TX callback handler when it is unable to
submit filled buffers.

Once assigned the responsibility of submitting deferred (filled)
buffers, the sync TX callback will continue to do so until
no filled buffers are available. At this point, the callback
relinquishes its submission duty back to the bladerf_sync_tx() context.
This commit is contained in:
Jon Szymaniak 2015-11-20 11:46:22 -05:00 • committed by Jon Szymaniak
commit 6b197881f5
3 changed files with 123 additions and 50 deletions

View file

@ -71,6 +71,11 @@ int sync_init(struct bladerf *dev,
return BLADERF_ERR_INVAL;
}
if (module != BLADERF_MODULE_TX && module != BLADERF_MODULE_RX) {
log_debug("Invalid bladerf_module value encountered: %d", module);
status = BLADERF_ERR_INVAL;
}
switch (format) {
case BLADERF_FORMAT_SC16_Q11:
case BLADERF_FORMAT_SC16_Q11_META:
@ -88,25 +93,23 @@ int sync_init(struct bladerf *dev,
}
/* Deallocate any existing sync handle for this module */
switch (module) {
case BLADERF_MODULE_TX:
case BLADERF_MODULE_RX:
sync_deinit(dev->sync[module]);
sync = dev->sync[module] =
(struct bladerf_sync *) calloc(1, sizeof(struct bladerf_sync));
sync_deinit(dev->sync[module]);
if (dev->sync[module] == NULL) {
status = BLADERF_ERR_MEM;
}
break;
sync = dev->sync[module] =
(struct bladerf_sync *) calloc(1, sizeof(struct bladerf_sync));
default:
log_debug("Invalid bladerf_module value encountered: %d", module);
status = BLADERF_ERR_INVAL;
if (dev->sync[module] == NULL) {
return BLADERF_ERR_MEM;
}
if (status != 0) {
return status;
switch (module) {
case BLADERF_MODULE_TX:
sync->buf_mgmt.submitter = SYNC_TX_SUBMITTER_FN;
break;
case BLADERF_MODULE_RX:
sync->buf_mgmt.submitter = SYNC_TX_SUBMITTER_INVALID;
break;
}
sync->dev = dev;
@ -165,7 +168,7 @@ int sync_init(struct bladerf *dev,
case BLADERF_MODULE_TX:
sync->buf_mgmt.prod_i = 0;
sync->buf_mgmt.cons_i = 0;
sync->buf_mgmt.cons_i = BUFFER_MGMT_INVALID_INDEX;
sync->buf_mgmt.partial_off = 0;
for (i = 0; i < num_buffers; i++) {
@ -611,37 +614,64 @@ int sync_rx(struct bladerf *dev, void *samples, unsigned num_samples,
/* Assumes buffer lock is held */
static int advance_tx_buffer(struct bladerf_sync *s, struct buffer_mgmt *b)
{
int status;
int status = 0;
const unsigned int idx = b->prod_i;
log_verbose("%s: Marking buf[%u] full\n", __FUNCTION__, b->prod_i);
b->status[b->prod_i] = SYNC_BUFFER_IN_FLIGHT;
/* Mark buffer full */
log_verbose("%s: Marking buf[%u] full\n", __FUNCTION__, idx);
b->status[idx] = SYNC_BUFFER_FULL;
/* This call may block and it results in a per-stream lock being held, so
* the buffer lock must be dropped.
*
* A callback may occur in the meantime, but this will not touch the status
* for this this buffer, or the producer index.
*/
MUTEX_UNLOCK(&b->lock);
status = async_submit_stream_buffer(s->worker->stream,
b->buffers[b->prod_i],
s->stream_config.timeout_ms,
false);
MUTEX_LOCK(&b->lock);
if (b->submitter == SYNC_TX_SUBMITTER_FN) {
/* This call may block and it results in a per-stream lock being held, so
* the buffer lock must be dropped.
*
* A callback may occur in the meantime, but this will not touch the status
* for this this buffer, or the producer index.
*/
MUTEX_UNLOCK(&b->lock);
status = async_submit_stream_buffer(s->worker->stream,
b->buffers[idx],
s->stream_config.timeout_ms,
true);
MUTEX_LOCK(&b->lock);
if (status == 0) {
b->prod_i = (b->prod_i + 1) % b->num_buffers;
if (status == 0) {
log_verbose("%s: buf[%u] submitted. Marking in-flight.\n",
__FUNCTION__, idx);
/* Go handle the next buffer, if we have one available. Otherwise,
* check up on the worker's state and restart it if needed. */
if (b->status[b->prod_i] == SYNC_BUFFER_EMPTY) {
s->state = SYNC_STATE_BUFFER_READY;
b->status[idx] = SYNC_BUFFER_IN_FLIGHT;
} else if (status == BLADERF_ERR_WOULD_BLOCK) {
log_verbose("%s: Deferring buf[%u] submission to worker callback.\n",
__FUNCTION__, idx);
/* Assign callback the duty of submitting deferred buffers,
* and use buffer_mgmt.cons_i to denote which it should submit
* (i.e., consume). */
b->submitter = SYNC_TX_SUBMITTER_CALLBACK;
b->cons_i = idx;
/* This is expected and we are handling it. Don't propogate this
* status back up */
status = 0;
} else {
s->state = SYNC_STATE_CHECK_WORKER;
}
log_debug("%s: Failed to submit buf[%u].\n", __FUNCTION__, idx);
return status;
}
}
/* Advance "producer" insertion index. */
b->prod_i = (idx + 1) % b->num_buffers;
/* Determine our next state based upon the state of the next buffer we
* want to use. */
if (b->status[b->prod_i] == SYNC_BUFFER_EMPTY) {
/* Buffer is empty and ready for use */
s->state = SYNC_STATE_BUFFER_READY;
} else {
log_debug("%s: Failed to advance buffer: %s\n",
__FUNCTION__, bladerf_strerror(status));
/* We'll have to wait on this buffer to become ready. First, we'll
* verify that the worker is running. */
s->state = SYNC_STATE_CHECK_WORKER;
}
return status;
@ -776,6 +806,10 @@ int sync_tx(struct bladerf *dev, void *samples, unsigned int num_samples,
* buffers. Therefore the RESET_BUF_MGMT state is
* skipped here. */
s->state = SYNC_STATE_START_WORKER;
} else {
/* Worker is running - continue onto checking for and
* potentially waiting for an available buffer */
s->state = SYNC_STATE_WAIT_FOR_BUFFER;
}
}
break;

View file

@ -52,6 +52,19 @@ typedef enum {
SYNC_META_STATE_SAMPLES, /**< Process samples */
} sync_meta_state;
typedef enum {
/** Invalid selection */
SYNC_TX_SUBMITTER_INVALID = -1,
/** sync_tx() is repsonsible for submitting buffers for async transfer */
SYNC_TX_SUBMITTER_FN,
/** The TX worker callbacks should be returning buffers for submission */
SYNC_TX_SUBMITTER_CALLBACK
} sync_tx_submitter;
#define BUFFER_MGMT_INVALID_INDEX (UINT_MAX)
struct buffer_mgmt {
sync_buffer_status *status;
@ -67,6 +80,10 @@ struct buffer_mgmt {
* resubmission */
unsigned int resubmit_count;
/* Applicable to TX only. Denotes which context is responsible for
* submitting full buffers to the underlying async system */
sync_tx_submitter submitter;
MUTEX lock;
pthread_cond_t buf_ready; /**< Buffer produced by RX callback, or

View file

@ -125,6 +125,8 @@ static void *tx_callback(struct bladerf *dev,
struct sync_worker *w = s->worker;
struct buffer_mgmt *b = &s->buf_mgmt;
void *ret = BLADERF_STREAM_NO_DATA;
/* Check if the caller has requested us to shut down. We'll keep the
* SHUTDOWN bit set through our transition into the IDLE state so we
* can act on it there. */
@ -138,25 +140,45 @@ static void *tx_callback(struct bladerf *dev,
return NULL;
}
/* Mark the last transfer as being completed. Note that the first
* callbacks we get have samples=NULL */
/* The initial set of callbacks will do not provide us with any
* completed sample buffers */
if (samples != NULL) {
MUTEX_LOCK(&b->lock);
/* Mark the completed buffer as being empty */
completed_idx = sync_buf2idx(b, samples);
assert(b->status[completed_idx] == SYNC_BUFFER_IN_FLIGHT);
b->status[completed_idx] = SYNC_BUFFER_EMPTY;
pthread_cond_signal(&b->buf_ready);
/* If the callback is assigned to be the submitter, there are
* buffers pending submission */
if (b->submitter == SYNC_TX_SUBMITTER_CALLBACK) {
assert(b->cons_i != BUFFER_MGMT_INVALID_INDEX);
if (b->status[b->cons_i] == SYNC_BUFFER_FULL) {
/* This buffer is ready to ship out ("consume") */
log_verbose("%s: Submitting deferred buf[%u]\n",
__FUNCTION__, b->cons_i);
ret = b->buffers[b->cons_i];
b->status[b->cons_i] = SYNC_BUFFER_IN_FLIGHT;
b->cons_i = (b->cons_i + 1) % b->num_buffers;
} else {
log_verbose("%s: No deferred buffer available. "
"Assigning submitter=FN\n", __FUNCTION__);
b->submitter = SYNC_TX_SUBMITTER_FN;
b->cons_i = BUFFER_MGMT_INVALID_INDEX;
}
}
MUTEX_UNLOCK(&b->lock);
log_verbose("%s worker: Buffer %u emptied.\r\n",
MODULE_STR(s), completed_idx);
}
return BLADERF_STREAM_NO_DATA;
return ret;
}
int sync_worker_init(struct bladerf_sync *s)