Blame libringbuffer/backend_internal.h

Packit c04fcb
#ifndef _LTTNG_RING_BUFFER_BACKEND_INTERNAL_H
Packit c04fcb
#define _LTTNG_RING_BUFFER_BACKEND_INTERNAL_H
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * libringbuffer/backend_internal.h
Packit c04fcb
 *
Packit c04fcb
 * Ring buffer backend (internal helpers).
Packit c04fcb
 *
Packit c04fcb
 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Packit c04fcb
 *
Packit c04fcb
 * This library is free software; you can redistribute it and/or
Packit c04fcb
 * modify it under the terms of the GNU Lesser General Public
Packit c04fcb
 * License as published by the Free Software Foundation; only
Packit c04fcb
 * version 2.1 of the License.
Packit c04fcb
 *
Packit c04fcb
 * This library is distributed in the hope that it will be useful,
Packit c04fcb
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit c04fcb
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Packit c04fcb
 * Lesser General Public License for more details.
Packit c04fcb
 *
Packit c04fcb
 * You should have received a copy of the GNU Lesser General Public
Packit c04fcb
 * License along with this library; if not, write to the Free Software
Packit c04fcb
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Packit c04fcb
 */
Packit c04fcb
Packit c04fcb
#include <unistd.h>
Packit c04fcb
#include <urcu/compiler.h>
Packit c04fcb
Packit c04fcb
#include <lttng/ringbuffer-config.h>
Packit c04fcb
#include "backend_types.h"
Packit c04fcb
#include "frontend_types.h"
Packit c04fcb
#include "shm.h"
Packit c04fcb
Packit c04fcb
/* Ring buffer backend API presented to the frontend */
Packit c04fcb
Packit c04fcb
/* Ring buffer and channel backend create/free */
Packit c04fcb
Packit c04fcb
int lib_ring_buffer_backend_create(struct lttng_ust_lib_ring_buffer_backend *bufb,
Packit c04fcb
				   struct channel_backend *chan, int cpu,
Packit c04fcb
				   struct lttng_ust_shm_handle *handle,
Packit c04fcb
				   struct shm_object *shmobj);
Packit c04fcb
void channel_backend_unregister_notifiers(struct channel_backend *chanb);
Packit c04fcb
void lib_ring_buffer_backend_free(struct lttng_ust_lib_ring_buffer_backend *bufb);
Packit c04fcb
int channel_backend_init(struct channel_backend *chanb,
Packit c04fcb
			 const char *name,
Packit c04fcb
			 const struct lttng_ust_lib_ring_buffer_config *config,
Packit c04fcb
			 size_t subbuf_size,
Packit c04fcb
			 size_t num_subbuf, struct lttng_ust_shm_handle *handle,
Packit c04fcb
			 const int *stream_fds);
Packit c04fcb
void channel_backend_free(struct channel_backend *chanb,
Packit c04fcb
			  struct lttng_ust_shm_handle *handle);
Packit c04fcb
Packit c04fcb
void lib_ring_buffer_backend_reset(struct lttng_ust_lib_ring_buffer_backend *bufb,
Packit c04fcb
				   struct lttng_ust_shm_handle *handle);
Packit c04fcb
void channel_backend_reset(struct channel_backend *chanb);
Packit c04fcb
Packit c04fcb
int lib_ring_buffer_backend_init(void);
Packit c04fcb
void lib_ring_buffer_backend_exit(void);
Packit c04fcb
Packit c04fcb
extern void _lib_ring_buffer_write(struct lttng_ust_lib_ring_buffer_backend *bufb,
Packit c04fcb
				   size_t offset, const void *src, size_t len,
Packit c04fcb
				   ssize_t pagecpy);
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Subbuffer ID bits for overwrite mode. Need to fit within a single word to be
Packit c04fcb
 * exchanged atomically.
Packit c04fcb
 *
Packit c04fcb
 * Top half word, except lowest bit, belongs to "offset", which is used to keep
Packit c04fcb
 * to count the produced buffers.  For overwrite mode, this provides the
Packit c04fcb
 * consumer with the capacity to read subbuffers in order, handling the
Packit c04fcb
 * situation where producers would write up to 2^15 buffers (or 2^31 for 64-bit
Packit c04fcb
 * systems) concurrently with a single execution of get_subbuf (between offset
Packit c04fcb
 * sampling and subbuffer ID exchange).
Packit c04fcb
 */
Packit c04fcb
Packit c04fcb
#define HALF_ULONG_BITS		(CAA_BITS_PER_LONG >> 1)
Packit c04fcb
Packit c04fcb
#define SB_ID_OFFSET_SHIFT	(HALF_ULONG_BITS + 1)
Packit c04fcb
#define SB_ID_OFFSET_COUNT	(1UL << SB_ID_OFFSET_SHIFT)
Packit c04fcb
#define SB_ID_OFFSET_MASK	(~(SB_ID_OFFSET_COUNT - 1))
Packit c04fcb
/*
Packit c04fcb
 * Lowest bit of top word half belongs to noref. Used only for overwrite mode.
Packit c04fcb
 */
Packit c04fcb
#define SB_ID_NOREF_SHIFT	(SB_ID_OFFSET_SHIFT - 1)
Packit c04fcb
#define SB_ID_NOREF_COUNT	(1UL << SB_ID_NOREF_SHIFT)
Packit c04fcb
#define SB_ID_NOREF_MASK	SB_ID_NOREF_COUNT
Packit c04fcb
/*
Packit c04fcb
 * In overwrite mode: lowest half of word is used for index.
Packit c04fcb
 * Limit of 2^16 subbuffers per buffer on 32-bit, 2^32 on 64-bit.
Packit c04fcb
 * In producer-consumer mode: whole word used for index.
Packit c04fcb
 */
Packit c04fcb
#define SB_ID_INDEX_SHIFT	0
Packit c04fcb
#define SB_ID_INDEX_COUNT	(1UL << SB_ID_INDEX_SHIFT)
Packit c04fcb
#define SB_ID_INDEX_MASK	(SB_ID_NOREF_COUNT - 1)
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Construct the subbuffer id from offset, index and noref. Use only the index
Packit c04fcb
 * for producer-consumer mode (offset and noref are only used in overwrite
Packit c04fcb
 * mode).
Packit c04fcb
 */
Packit c04fcb
static inline
Packit c04fcb
unsigned long subbuffer_id(const struct lttng_ust_lib_ring_buffer_config *config,
Packit c04fcb
			   unsigned long offset, unsigned long noref,
Packit c04fcb
			   unsigned long index)
Packit c04fcb
{
Packit c04fcb
	if (config->mode == RING_BUFFER_OVERWRITE)
Packit c04fcb
		return (offset << SB_ID_OFFSET_SHIFT)
Packit c04fcb
		       | (noref << SB_ID_NOREF_SHIFT)
Packit c04fcb
		       | index;
Packit c04fcb
	else
Packit c04fcb
		return index;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Compare offset with the offset contained within id. Return 1 if the offset
Packit c04fcb
 * bits are identical, else 0.
Packit c04fcb
 */
Packit c04fcb
static inline
Packit c04fcb
int subbuffer_id_compare_offset(const struct lttng_ust_lib_ring_buffer_config *config,
Packit c04fcb
				unsigned long id, unsigned long offset)
Packit c04fcb
{
Packit c04fcb
	return (id & SB_ID_OFFSET_MASK) == (offset << SB_ID_OFFSET_SHIFT);
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
static inline
Packit c04fcb
unsigned long subbuffer_id_get_index(const struct lttng_ust_lib_ring_buffer_config *config,
Packit c04fcb
				     unsigned long id)
Packit c04fcb
{
Packit c04fcb
	if (config->mode == RING_BUFFER_OVERWRITE)
Packit c04fcb
		return id & SB_ID_INDEX_MASK;
Packit c04fcb
	else
Packit c04fcb
		return id;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
static inline
Packit c04fcb
unsigned long subbuffer_id_is_noref(const struct lttng_ust_lib_ring_buffer_config *config,
Packit c04fcb
				    unsigned long id)
Packit c04fcb
{
Packit c04fcb
	if (config->mode == RING_BUFFER_OVERWRITE)
Packit c04fcb
		return !!(id & SB_ID_NOREF_MASK);
Packit c04fcb
	else
Packit c04fcb
		return 1;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Only used by reader on subbuffer ID it has exclusive access to. No volatile
Packit c04fcb
 * needed.
Packit c04fcb
 */
Packit c04fcb
static inline
Packit c04fcb
void subbuffer_id_set_noref(const struct lttng_ust_lib_ring_buffer_config *config,
Packit c04fcb
			    unsigned long *id)
Packit c04fcb
{
Packit c04fcb
	if (config->mode == RING_BUFFER_OVERWRITE)
Packit c04fcb
		*id |= SB_ID_NOREF_MASK;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
static inline
Packit c04fcb
void subbuffer_id_set_noref_offset(const struct lttng_ust_lib_ring_buffer_config *config,
Packit c04fcb
				   unsigned long *id, unsigned long offset)
Packit c04fcb
{
Packit c04fcb
	unsigned long tmp;
Packit c04fcb
Packit c04fcb
	if (config->mode == RING_BUFFER_OVERWRITE) {
Packit c04fcb
		tmp = *id;
Packit c04fcb
		tmp &= ~SB_ID_OFFSET_MASK;
Packit c04fcb
		tmp |= offset << SB_ID_OFFSET_SHIFT;
Packit c04fcb
		tmp |= SB_ID_NOREF_MASK;
Packit c04fcb
		/* Volatile store, read concurrently by readers. */
Packit c04fcb
		CMM_ACCESS_ONCE(*id) = tmp;
Packit c04fcb
	}
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
/* No volatile access, since already used locally */
Packit c04fcb
static inline
Packit c04fcb
void subbuffer_id_clear_noref(const struct lttng_ust_lib_ring_buffer_config *config,
Packit c04fcb
			      unsigned long *id)
Packit c04fcb
{
Packit c04fcb
	if (config->mode == RING_BUFFER_OVERWRITE)
Packit c04fcb
		*id &= ~SB_ID_NOREF_MASK;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * For overwrite mode, cap the number of subbuffers per buffer to:
Packit c04fcb
 * 2^16 on 32-bit architectures
Packit c04fcb
 * 2^32 on 64-bit architectures
Packit c04fcb
 * This is required to fit in the index part of the ID. Return 0 on success,
Packit c04fcb
 * -EPERM on failure.
Packit c04fcb
 */
Packit c04fcb
static inline
Packit c04fcb
int subbuffer_id_check_index(const struct lttng_ust_lib_ring_buffer_config *config,
Packit c04fcb
			     unsigned long num_subbuf)
Packit c04fcb
{
Packit c04fcb
	if (config->mode == RING_BUFFER_OVERWRITE)
Packit c04fcb
		return (num_subbuf > (1UL << HALF_ULONG_BITS)) ? -EPERM : 0;
Packit c04fcb
	else
Packit c04fcb
		return 0;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
static inline
Packit c04fcb
void subbuffer_count_record(const struct lttng_ust_lib_ring_buffer_config *config,
Packit c04fcb
			    struct lttng_ust_lib_ring_buffer_backend *bufb,
Packit c04fcb
			    unsigned long idx, struct lttng_ust_shm_handle *handle)
Packit c04fcb
{
Packit c04fcb
	unsigned long sb_bindex;
Packit c04fcb
Packit c04fcb
	sb_bindex = subbuffer_id_get_index(config, shmp_index(handle, bufb->buf_wsb, idx)->id);
Packit c04fcb
	v_inc(config, &shmp(handle, shmp_index(handle, bufb->array, sb_bindex)->shmp)->records_commit);
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Reader has exclusive subbuffer access for record consumption. No need to
Packit c04fcb
 * perform the decrement atomically.
Packit c04fcb
 */
Packit c04fcb
static inline
Packit c04fcb
void subbuffer_consume_record(const struct lttng_ust_lib_ring_buffer_config *config,
Packit c04fcb
			      struct lttng_ust_lib_ring_buffer_backend *bufb,
Packit c04fcb
			      struct lttng_ust_shm_handle *handle)
Packit c04fcb
{
Packit c04fcb
	unsigned long sb_bindex;
Packit c04fcb
Packit c04fcb
	sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
Packit c04fcb
	CHAN_WARN_ON(shmp(handle, bufb->chan),
Packit c04fcb
		     !v_read(config, &shmp(handle, shmp_index(handle, bufb->array, sb_bindex)->shmp)->records_unread));
Packit c04fcb
	/* Non-atomic decrement protected by exclusive subbuffer access */
Packit c04fcb
	_v_dec(config, &shmp(handle, shmp_index(handle, bufb->array, sb_bindex)->shmp)->records_unread);
Packit c04fcb
	v_inc(config, &bufb->records_read);
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
static inline
Packit c04fcb
unsigned long subbuffer_get_records_count(
Packit c04fcb
				const struct lttng_ust_lib_ring_buffer_config *config,
Packit c04fcb
				struct lttng_ust_lib_ring_buffer_backend *bufb,
Packit c04fcb
				unsigned long idx,
Packit c04fcb
				struct lttng_ust_shm_handle *handle)
Packit c04fcb
{
Packit c04fcb
	unsigned long sb_bindex;
Packit c04fcb
Packit c04fcb
	sb_bindex = subbuffer_id_get_index(config, shmp_index(handle, bufb->buf_wsb, idx)->id);
Packit c04fcb
	return v_read(config, &shmp(handle, shmp_index(handle, bufb->array, sb_bindex)->shmp)->records_commit);
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Must be executed at subbuffer delivery when the writer has _exclusive_
Packit c04fcb
 * subbuffer access. See lib_ring_buffer_check_deliver() for details.
Packit c04fcb
 * lib_ring_buffer_get_records_count() must be called to get the records
Packit c04fcb
 * count before this function, because it resets the records_commit
Packit c04fcb
 * count.
Packit c04fcb
 */
Packit c04fcb
static inline
Packit c04fcb
unsigned long subbuffer_count_records_overrun(
Packit c04fcb
				const struct lttng_ust_lib_ring_buffer_config *config,
Packit c04fcb
				struct lttng_ust_lib_ring_buffer_backend *bufb,
Packit c04fcb
				unsigned long idx,
Packit c04fcb
				struct lttng_ust_shm_handle *handle)
Packit c04fcb
{
Packit c04fcb
	struct lttng_ust_lib_ring_buffer_backend_pages_shmp *pages;
Packit c04fcb
	unsigned long overruns, sb_bindex;
Packit c04fcb
Packit c04fcb
	sb_bindex = subbuffer_id_get_index(config, shmp_index(handle, bufb->buf_wsb, idx)->id);
Packit c04fcb
	pages = shmp_index(handle, bufb->array, sb_bindex);
Packit c04fcb
	overruns = v_read(config, &shmp(handle, pages->shmp)->records_unread);
Packit c04fcb
	v_set(config, &shmp(handle, pages->shmp)->records_unread,
Packit c04fcb
	      v_read(config, &shmp(handle, pages->shmp)->records_commit));
Packit c04fcb
	v_set(config, &shmp(handle, pages->shmp)->records_commit, 0);
Packit c04fcb
Packit c04fcb
	return overruns;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
static inline
Packit c04fcb
void subbuffer_set_data_size(const struct lttng_ust_lib_ring_buffer_config *config,
Packit c04fcb
			     struct lttng_ust_lib_ring_buffer_backend *bufb,
Packit c04fcb
			     unsigned long idx,
Packit c04fcb
			     unsigned long data_size,
Packit c04fcb
			     struct lttng_ust_shm_handle *handle)
Packit c04fcb
{
Packit c04fcb
	struct lttng_ust_lib_ring_buffer_backend_pages_shmp *pages;
Packit c04fcb
	unsigned long sb_bindex;
Packit c04fcb
Packit c04fcb
	sb_bindex = subbuffer_id_get_index(config, shmp_index(handle, bufb->buf_wsb, idx)->id);
Packit c04fcb
	pages = shmp_index(handle, bufb->array, sb_bindex);
Packit c04fcb
	shmp(handle, pages->shmp)->data_size = data_size;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
static inline
Packit c04fcb
unsigned long subbuffer_get_read_data_size(
Packit c04fcb
				const struct lttng_ust_lib_ring_buffer_config *config,
Packit c04fcb
				struct lttng_ust_lib_ring_buffer_backend *bufb,
Packit c04fcb
				struct lttng_ust_shm_handle *handle)
Packit c04fcb
{
Packit c04fcb
	struct lttng_ust_lib_ring_buffer_backend_pages_shmp *pages;
Packit c04fcb
	unsigned long sb_bindex;
Packit c04fcb
Packit c04fcb
	sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
Packit c04fcb
	pages = shmp_index(handle, bufb->array, sb_bindex);
Packit c04fcb
	return shmp(handle, pages->shmp)->data_size;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
static inline
Packit c04fcb
unsigned long subbuffer_get_data_size(
Packit c04fcb
				const struct lttng_ust_lib_ring_buffer_config *config,
Packit c04fcb
				struct lttng_ust_lib_ring_buffer_backend *bufb,
Packit c04fcb
				unsigned long idx,
Packit c04fcb
				struct lttng_ust_shm_handle *handle)
Packit c04fcb
{
Packit c04fcb
	struct lttng_ust_lib_ring_buffer_backend_pages_shmp *pages;
Packit c04fcb
	unsigned long sb_bindex;
Packit c04fcb
Packit c04fcb
	sb_bindex = subbuffer_id_get_index(config, shmp_index(handle, bufb->buf_wsb, idx)->id);
Packit c04fcb
	pages = shmp_index(handle, bufb->array, sb_bindex);
Packit c04fcb
	return shmp(handle, pages->shmp)->data_size;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
static inline
Packit c04fcb
void subbuffer_inc_packet_count(const struct lttng_ust_lib_ring_buffer_config *config,
Packit c04fcb
		struct lttng_ust_lib_ring_buffer_backend *bufb,
Packit c04fcb
		unsigned long idx, struct lttng_ust_shm_handle *handle)
Packit c04fcb
{
Packit c04fcb
	shmp_index(handle, bufb->buf_cnt, idx)->seq_cnt++;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
/**
Packit c04fcb
 * lib_ring_buffer_clear_noref - Clear the noref subbuffer flag, called by
Packit c04fcb
 *                               writer.
Packit c04fcb
 */
Packit c04fcb
static inline
Packit c04fcb
void lib_ring_buffer_clear_noref(const struct lttng_ust_lib_ring_buffer_config *config,
Packit c04fcb
				 struct lttng_ust_lib_ring_buffer_backend *bufb,
Packit c04fcb
				 unsigned long idx,
Packit c04fcb
				 struct lttng_ust_shm_handle *handle)
Packit c04fcb
{
Packit c04fcb
	unsigned long id, new_id;
Packit c04fcb
Packit c04fcb
	if (config->mode != RING_BUFFER_OVERWRITE)
Packit c04fcb
		return;
Packit c04fcb
Packit c04fcb
	/*
Packit c04fcb
	 * Performing a volatile access to read the sb_pages, because we want to
Packit c04fcb
	 * read a coherent version of the pointer and the associated noref flag.
Packit c04fcb
	 */
Packit c04fcb
	id = CMM_ACCESS_ONCE(shmp_index(handle, bufb->buf_wsb, idx)->id);
Packit c04fcb
	for (;;) {
Packit c04fcb
		/* This check is called on the fast path for each record. */
Packit c04fcb
		if (caa_likely(!subbuffer_id_is_noref(config, id))) {
Packit c04fcb
			/*
Packit c04fcb
			 * Store after load dependency ordering the writes to
Packit c04fcb
			 * the subbuffer after load and test of the noref flag
Packit c04fcb
			 * matches the memory barrier implied by the cmpxchg()
Packit c04fcb
			 * in update_read_sb_index().
Packit c04fcb
			 */
Packit c04fcb
			return;	/* Already writing to this buffer */
Packit c04fcb
		}
Packit c04fcb
		new_id = id;
Packit c04fcb
		subbuffer_id_clear_noref(config, &new_id);
Packit c04fcb
		new_id = uatomic_cmpxchg(&shmp_index(handle, bufb->buf_wsb, idx)->id, id, new_id);
Packit c04fcb
		if (caa_likely(new_id == id))
Packit c04fcb
			break;
Packit c04fcb
		id = new_id;
Packit c04fcb
	}
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
/**
Packit c04fcb
 * lib_ring_buffer_set_noref_offset - Set the noref subbuffer flag and offset,
Packit c04fcb
 *                                    called by writer.
Packit c04fcb
 */
Packit c04fcb
static inline
Packit c04fcb
void lib_ring_buffer_set_noref_offset(const struct lttng_ust_lib_ring_buffer_config *config,
Packit c04fcb
				      struct lttng_ust_lib_ring_buffer_backend *bufb,
Packit c04fcb
				      unsigned long idx, unsigned long offset,
Packit c04fcb
				      struct lttng_ust_shm_handle *handle)
Packit c04fcb
{
Packit c04fcb
	if (config->mode != RING_BUFFER_OVERWRITE)
Packit c04fcb
		return;
Packit c04fcb
Packit c04fcb
	/*
Packit c04fcb
	 * Because ring_buffer_set_noref() is only called by a single thread
Packit c04fcb
	 * (the one which updated the cc_sb value), there are no concurrent
Packit c04fcb
	 * updates to take care of: other writers have not updated cc_sb, so
Packit c04fcb
	 * they cannot set the noref flag, and concurrent readers cannot modify
Packit c04fcb
	 * the pointer because the noref flag is not set yet.
Packit c04fcb
	 * The smp_wmb() in ring_buffer_commit() takes care of ordering writes
Packit c04fcb
	 * to the subbuffer before this set noref operation.
Packit c04fcb
	 * subbuffer_set_noref() uses a volatile store to deal with concurrent
Packit c04fcb
	 * readers of the noref flag.
Packit c04fcb
	 */
Packit c04fcb
	CHAN_WARN_ON(shmp(handle, bufb->chan),
Packit c04fcb
		     subbuffer_id_is_noref(config, shmp_index(handle, bufb->buf_wsb, idx)->id));
Packit c04fcb
	/*
Packit c04fcb
	 * Memory barrier that ensures counter stores are ordered before set
Packit c04fcb
	 * noref and offset.
Packit c04fcb
	 */
Packit c04fcb
	cmm_smp_mb();
Packit c04fcb
	subbuffer_id_set_noref_offset(config, &shmp_index(handle, bufb->buf_wsb, idx)->id, offset);
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
/**
Packit c04fcb
 * update_read_sb_index - Read-side subbuffer index update.
Packit c04fcb
 */
Packit c04fcb
static inline
Packit c04fcb
int update_read_sb_index(const struct lttng_ust_lib_ring_buffer_config *config,
Packit c04fcb
			 struct lttng_ust_lib_ring_buffer_backend *bufb,
Packit c04fcb
			 struct channel_backend *chanb,
Packit c04fcb
			 unsigned long consumed_idx,
Packit c04fcb
			 unsigned long consumed_count,
Packit c04fcb
			 struct lttng_ust_shm_handle *handle)
Packit c04fcb
{
Packit c04fcb
	unsigned long old_id, new_id;
Packit c04fcb
Packit c04fcb
	if (config->mode == RING_BUFFER_OVERWRITE) {
Packit c04fcb
		/*
Packit c04fcb
		 * Exchange the target writer subbuffer with our own unused
Packit c04fcb
		 * subbuffer. No need to use CMM_ACCESS_ONCE() here to read the
Packit c04fcb
		 * old_wpage, because the value read will be confirmed by the
Packit c04fcb
		 * following cmpxchg().
Packit c04fcb
		 */
Packit c04fcb
		old_id = shmp_index(handle, bufb->buf_wsb, consumed_idx)->id;
Packit c04fcb
		if (caa_unlikely(!subbuffer_id_is_noref(config, old_id)))
Packit c04fcb
			return -EAGAIN;
Packit c04fcb
		/*
Packit c04fcb
		 * Make sure the offset count we are expecting matches the one
Packit c04fcb
		 * indicated by the writer.
Packit c04fcb
		 */
Packit c04fcb
		if (caa_unlikely(!subbuffer_id_compare_offset(config, old_id,
Packit c04fcb
							  consumed_count)))
Packit c04fcb
			return -EAGAIN;
Packit c04fcb
		CHAN_WARN_ON(shmp(handle, bufb->chan),
Packit c04fcb
			     !subbuffer_id_is_noref(config, bufb->buf_rsb.id));
Packit c04fcb
		subbuffer_id_set_noref_offset(config, &bufb->buf_rsb.id,
Packit c04fcb
					      consumed_count);
Packit c04fcb
		new_id = uatomic_cmpxchg(&shmp_index(handle, bufb->buf_wsb, consumed_idx)->id, old_id,
Packit c04fcb
				 bufb->buf_rsb.id);
Packit c04fcb
		if (caa_unlikely(old_id != new_id))
Packit c04fcb
			return -EAGAIN;
Packit c04fcb
		bufb->buf_rsb.id = new_id;
Packit c04fcb
	} else {
Packit c04fcb
		/* No page exchange, use the writer page directly */
Packit c04fcb
		bufb->buf_rsb.id = shmp_index(handle, bufb->buf_wsb, consumed_idx)->id;
Packit c04fcb
	}
Packit c04fcb
	return 0;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
#ifndef inline_memcpy
Packit c04fcb
#define inline_memcpy(dest, src, n)	memcpy(dest, src, n)
Packit c04fcb
#endif
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Use the architecture-specific memcpy implementation for constant-sized
Packit c04fcb
 * inputs, but rely on an inline memcpy for length statically unknown.
Packit c04fcb
 * The function call to memcpy is just way too expensive for a fast path.
Packit c04fcb
 */
Packit c04fcb
#define lib_ring_buffer_do_copy(config, dest, src, len)		\
Packit c04fcb
do {								\
Packit c04fcb
	size_t __len = (len);					\
Packit c04fcb
	if (__builtin_constant_p(len))				\
Packit c04fcb
		memcpy(dest, src, __len);			\
Packit c04fcb
	else							\
Packit c04fcb
		inline_memcpy(dest, src, __len);		\
Packit c04fcb
} while (0)
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * write len bytes to dest with c
Packit c04fcb
 */
Packit c04fcb
static inline
Packit c04fcb
void lib_ring_buffer_do_memset(char *dest, int c, unsigned long len)
Packit c04fcb
{
Packit c04fcb
	unsigned long i;
Packit c04fcb
Packit c04fcb
	for (i = 0; i < len; i++)
Packit c04fcb
		dest[i] = c;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
/* arch-agnostic implementation */
Packit c04fcb
Packit c04fcb
static inline int lttng_ust_fls(unsigned int x)
Packit c04fcb
{
Packit c04fcb
	int r = 32;
Packit c04fcb
Packit c04fcb
	if (!x)
Packit c04fcb
		return 0;
Packit c04fcb
	if (!(x & 0xFFFF0000U)) {
Packit c04fcb
		x <<= 16;
Packit c04fcb
		r -= 16;
Packit c04fcb
	}
Packit c04fcb
	if (!(x & 0xFF000000U)) {
Packit c04fcb
		x <<= 8;
Packit c04fcb
		r -= 8;
Packit c04fcb
	}
Packit c04fcb
	if (!(x & 0xF0000000U)) {
Packit c04fcb
		x <<= 4;
Packit c04fcb
		r -= 4;
Packit c04fcb
	}
Packit c04fcb
	if (!(x & 0xC0000000U)) {
Packit c04fcb
		x <<= 2;
Packit c04fcb
		r -= 2;
Packit c04fcb
	}
Packit c04fcb
	if (!(x & 0x80000000U)) {
Packit c04fcb
		/* No need to bit shift on last operation */
Packit c04fcb
		r -= 1;
Packit c04fcb
	}
Packit c04fcb
	return r;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
static inline int get_count_order(unsigned int count)
Packit c04fcb
{
Packit c04fcb
	int order;
Packit c04fcb
Packit c04fcb
	order = lttng_ust_fls(count) - 1;
Packit c04fcb
	if (count & (count - 1))
Packit c04fcb
		order++;
Packit c04fcb
	return order;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
#endif /* _LTTNG_RING_BUFFER_BACKEND_INTERNAL_H */