|
Packit |
c04fcb |
#ifndef _LTTNG_RING_BUFFER_BACKEND_H
|
|
Packit |
c04fcb |
#define _LTTNG_RING_BUFFER_BACKEND_H
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
/*
|
|
Packit |
c04fcb |
* libringbuffer/backend.h
|
|
Packit |
c04fcb |
*
|
|
Packit |
c04fcb |
* Ring buffer backend (API).
|
|
Packit |
c04fcb |
*
|
|
Packit |
c04fcb |
* Copyright (C) 2011-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 |
* Credits to Steven Rostedt for proposing to use an extra-subbuffer owned by
|
|
Packit |
c04fcb |
* the reader in flight recorder mode.
|
|
Packit |
c04fcb |
*/
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
#include <unistd.h>
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
/* Internal helpers */
|
|
Packit |
c04fcb |
#include "backend_internal.h"
|
|
Packit |
c04fcb |
#include "frontend_internal.h"
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
/* Ring buffer backend API */
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
/* Ring buffer backend access (read/write) */
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
extern size_t lib_ring_buffer_read(struct lttng_ust_lib_ring_buffer_backend *bufb,
|
|
Packit |
c04fcb |
size_t offset, void *dest, size_t len,
|
|
Packit |
c04fcb |
struct lttng_ust_shm_handle *handle);
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
extern int lib_ring_buffer_read_cstr(struct lttng_ust_lib_ring_buffer_backend *bufb,
|
|
Packit |
c04fcb |
size_t offset, void *dest, size_t len,
|
|
Packit |
c04fcb |
struct lttng_ust_shm_handle *handle);
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
/*
|
|
Packit |
c04fcb |
* Return the address where a given offset is located.
|
|
Packit |
c04fcb |
* Should be used to get the current subbuffer header pointer. Given we know
|
|
Packit |
c04fcb |
* it's never on a page boundary, it's safe to write directly to this address,
|
|
Packit |
c04fcb |
* as long as the write is never bigger than a page size.
|
|
Packit |
c04fcb |
*/
|
|
Packit |
c04fcb |
extern void *
|
|
Packit |
c04fcb |
lib_ring_buffer_offset_address(struct lttng_ust_lib_ring_buffer_backend *bufb,
|
|
Packit |
c04fcb |
size_t offset,
|
|
Packit |
c04fcb |
struct lttng_ust_shm_handle *handle);
|
|
Packit |
c04fcb |
extern void *
|
|
Packit |
c04fcb |
lib_ring_buffer_read_offset_address(struct lttng_ust_lib_ring_buffer_backend *bufb,
|
|
Packit |
c04fcb |
size_t offset,
|
|
Packit |
c04fcb |
struct lttng_ust_shm_handle *handle);
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
/**
|
|
Packit |
c04fcb |
* lib_ring_buffer_write - write data to a buffer backend
|
|
Packit |
c04fcb |
* @config : ring buffer instance configuration
|
|
Packit |
c04fcb |
* @ctx: ring buffer context. (input arguments only)
|
|
Packit |
c04fcb |
* @src : source pointer to copy from
|
|
Packit |
c04fcb |
* @len : length of data to copy
|
|
Packit |
c04fcb |
*
|
|
Packit |
c04fcb |
* This function copies "len" bytes of data from a source pointer to a buffer
|
|
Packit |
c04fcb |
* backend, at the current context offset. This is more or less a buffer
|
|
Packit |
c04fcb |
* backend-specific memcpy() operation. Calls the slow path (_ring_buffer_write)
|
|
Packit |
c04fcb |
* if copy is crossing a page boundary.
|
|
Packit |
c04fcb |
*/
|
|
Packit |
c04fcb |
static inline
|
|
Packit |
c04fcb |
void lib_ring_buffer_write(const struct lttng_ust_lib_ring_buffer_config *config,
|
|
Packit |
c04fcb |
struct lttng_ust_lib_ring_buffer_ctx *ctx,
|
|
Packit |
c04fcb |
const void *src, size_t len)
|
|
Packit |
c04fcb |
{
|
|
Packit |
c04fcb |
struct lttng_ust_lib_ring_buffer_backend *bufb = &ctx->buf->backend;
|
|
Packit |
c04fcb |
struct channel_backend *chanb = &ctx->chan->backend;
|
|
Packit |
c04fcb |
struct lttng_ust_shm_handle *handle = ctx->handle;
|
|
Packit |
c04fcb |
size_t sbidx;
|
|
Packit |
c04fcb |
size_t offset = ctx->buf_offset;
|
|
Packit |
c04fcb |
struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
|
|
Packit |
c04fcb |
unsigned long sb_bindex, id;
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
if (caa_unlikely(!len))
|
|
Packit |
c04fcb |
return;
|
|
Packit |
c04fcb |
offset &= chanb->buf_size - 1;
|
|
Packit |
c04fcb |
sbidx = offset >> chanb->subbuf_size_order;
|
|
Packit |
c04fcb |
id = shmp_index(handle, bufb->buf_wsb, sbidx)->id;
|
|
Packit |
c04fcb |
sb_bindex = subbuffer_id_get_index(config, id);
|
|
Packit |
c04fcb |
rpages = shmp_index(handle, bufb->array, sb_bindex);
|
|
Packit |
c04fcb |
CHAN_WARN_ON(ctx->chan,
|
|
Packit |
c04fcb |
config->mode == RING_BUFFER_OVERWRITE
|
|
Packit |
c04fcb |
&& subbuffer_id_is_noref(config, id));
|
|
Packit |
c04fcb |
/*
|
|
Packit |
c04fcb |
* Underlying layer should never ask for writes across
|
|
Packit |
c04fcb |
* subbuffers.
|
|
Packit |
c04fcb |
*/
|
|
Packit |
c04fcb |
CHAN_WARN_ON(chanb, offset >= chanb->buf_size);
|
|
Packit |
c04fcb |
lib_ring_buffer_do_copy(config,
|
|
Packit |
c04fcb |
shmp_index(handle, shmp(handle, rpages->shmp)->p, offset & (chanb->subbuf_size - 1)),
|
|
Packit |
c04fcb |
src, len);
|
|
Packit |
c04fcb |
ctx->buf_offset += len;
|
|
Packit |
c04fcb |
}
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
/*
|
|
Packit |
c04fcb |
* Copy up to @len string bytes from @src to @dest. Stop whenever a NULL
|
|
Packit |
c04fcb |
* terminating character is found in @src. Returns the number of bytes
|
|
Packit |
c04fcb |
* copied. Does *not* terminate @dest with NULL terminating character.
|
|
Packit |
c04fcb |
*/
|
|
Packit |
c04fcb |
static inline
|
|
Packit |
c04fcb |
size_t lib_ring_buffer_do_strcpy(const struct lttng_ust_lib_ring_buffer_config *config,
|
|
Packit |
c04fcb |
char *dest, const char *src, size_t len)
|
|
Packit |
c04fcb |
{
|
|
Packit |
c04fcb |
size_t count;
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
for (count = 0; count < len; count++) {
|
|
Packit |
c04fcb |
char c;
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
/*
|
|
Packit |
c04fcb |
* Only read source character once, in case it is
|
|
Packit |
c04fcb |
* modified concurrently.
|
|
Packit |
c04fcb |
*/
|
|
Packit |
c04fcb |
c = CMM_LOAD_SHARED(src[count]);
|
|
Packit |
c04fcb |
if (!c)
|
|
Packit |
c04fcb |
break;
|
|
Packit |
c04fcb |
lib_ring_buffer_do_copy(config, &dest[count], &c, 1);
|
|
Packit |
c04fcb |
}
|
|
Packit |
c04fcb |
return count;
|
|
Packit |
c04fcb |
}
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
/**
|
|
Packit |
c04fcb |
* lib_ring_buffer_strcpy - write string data to a buffer backend
|
|
Packit |
c04fcb |
* @config : ring buffer instance configuration
|
|
Packit |
c04fcb |
* @ctx: ring buffer context. (input arguments only)
|
|
Packit |
c04fcb |
* @src : source pointer to copy from
|
|
Packit |
c04fcb |
* @len : length of data to copy
|
|
Packit |
c04fcb |
* @pad : character to use for padding
|
|
Packit |
c04fcb |
*
|
|
Packit |
c04fcb |
* This function copies @len - 1 bytes of string data from a source
|
|
Packit |
c04fcb |
* pointer to a buffer backend, followed by a terminating '\0'
|
|
Packit |
c04fcb |
* character, at the current context offset. This is more or less a
|
|
Packit |
c04fcb |
* buffer backend-specific strncpy() operation. If a terminating '\0'
|
|
Packit |
c04fcb |
* character is found in @src before @len - 1 characters are copied, pad
|
|
Packit |
c04fcb |
* the buffer with @pad characters (e.g. '#').
|
|
Packit |
c04fcb |
*/
|
|
Packit |
c04fcb |
static inline
|
|
Packit |
c04fcb |
void lib_ring_buffer_strcpy(const struct lttng_ust_lib_ring_buffer_config *config,
|
|
Packit |
c04fcb |
struct lttng_ust_lib_ring_buffer_ctx *ctx,
|
|
Packit |
c04fcb |
const char *src, size_t len, int pad)
|
|
Packit |
c04fcb |
{
|
|
Packit |
c04fcb |
struct lttng_ust_lib_ring_buffer_backend *bufb = &ctx->buf->backend;
|
|
Packit |
c04fcb |
struct channel_backend *chanb = &ctx->chan->backend;
|
|
Packit |
c04fcb |
struct lttng_ust_shm_handle *handle = ctx->handle;
|
|
Packit |
c04fcb |
size_t sbidx, count;
|
|
Packit |
c04fcb |
size_t offset = ctx->buf_offset;
|
|
Packit |
c04fcb |
struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
|
|
Packit |
c04fcb |
unsigned long sb_bindex, id;
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
if (caa_unlikely(!len))
|
|
Packit |
c04fcb |
return;
|
|
Packit |
c04fcb |
offset &= chanb->buf_size - 1;
|
|
Packit |
c04fcb |
sbidx = offset >> chanb->subbuf_size_order;
|
|
Packit |
c04fcb |
id = shmp_index(handle, bufb->buf_wsb, sbidx)->id;
|
|
Packit |
c04fcb |
sb_bindex = subbuffer_id_get_index(config, id);
|
|
Packit |
c04fcb |
rpages = shmp_index(handle, bufb->array, sb_bindex);
|
|
Packit |
c04fcb |
CHAN_WARN_ON(ctx->chan,
|
|
Packit |
c04fcb |
config->mode == RING_BUFFER_OVERWRITE
|
|
Packit |
c04fcb |
&& subbuffer_id_is_noref(config, id));
|
|
Packit |
c04fcb |
/*
|
|
Packit |
c04fcb |
* Underlying layer should never ask for writes across
|
|
Packit |
c04fcb |
* subbuffers.
|
|
Packit |
c04fcb |
*/
|
|
Packit |
c04fcb |
CHAN_WARN_ON(chanb, offset >= chanb->buf_size);
|
|
Packit |
c04fcb |
count = lib_ring_buffer_do_strcpy(config,
|
|
Packit |
c04fcb |
shmp_index(handle, shmp(handle, rpages->shmp)->p,
|
|
Packit |
c04fcb |
offset & (chanb->subbuf_size - 1)),
|
|
Packit |
c04fcb |
src, len - 1);
|
|
Packit |
c04fcb |
offset += count;
|
|
Packit |
c04fcb |
/* Padding */
|
|
Packit |
c04fcb |
if (caa_unlikely(count < len - 1)) {
|
|
Packit |
c04fcb |
size_t pad_len = len - 1 - count;
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
lib_ring_buffer_do_memset(shmp_index(handle, shmp(handle, rpages->shmp)->p,
|
|
Packit |
c04fcb |
offset & (chanb->subbuf_size - 1)),
|
|
Packit |
c04fcb |
pad, pad_len);
|
|
Packit |
c04fcb |
offset += pad_len;
|
|
Packit |
c04fcb |
}
|
|
Packit |
c04fcb |
/* Final '\0' */
|
|
Packit |
c04fcb |
lib_ring_buffer_do_memset(shmp_index(handle, shmp(handle, rpages->shmp)->p,
|
|
Packit |
c04fcb |
offset & (chanb->subbuf_size - 1)),
|
|
Packit |
c04fcb |
'\0', 1);
|
|
Packit |
c04fcb |
ctx->buf_offset += len;
|
|
Packit |
c04fcb |
}
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
/*
|
|
Packit |
c04fcb |
* This accessor counts the number of unread records in a buffer.
|
|
Packit |
c04fcb |
* It only provides a consistent value if no reads not writes are performed
|
|
Packit |
c04fcb |
* concurrently.
|
|
Packit |
c04fcb |
*/
|
|
Packit |
c04fcb |
static inline
|
|
Packit |
c04fcb |
unsigned long lib_ring_buffer_get_records_unread(
|
|
Packit |
c04fcb |
const struct lttng_ust_lib_ring_buffer_config *config,
|
|
Packit |
c04fcb |
struct lttng_ust_lib_ring_buffer *buf,
|
|
Packit |
c04fcb |
struct lttng_ust_shm_handle *handle)
|
|
Packit |
c04fcb |
{
|
|
Packit |
c04fcb |
struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend;
|
|
Packit |
c04fcb |
struct lttng_ust_lib_ring_buffer_backend_pages_shmp *pages;
|
|
Packit |
c04fcb |
unsigned long records_unread = 0, sb_bindex, id;
|
|
Packit |
c04fcb |
unsigned int i;
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
for (i = 0; i < shmp(handle, bufb->chan)->backend.num_subbuf; i++) {
|
|
Packit |
c04fcb |
id = shmp_index(handle, bufb->buf_wsb, i)->id;
|
|
Packit |
c04fcb |
sb_bindex = subbuffer_id_get_index(config, id);
|
|
Packit |
c04fcb |
pages = shmp_index(handle, bufb->array, sb_bindex);
|
|
Packit |
c04fcb |
records_unread += v_read(config, &shmp(handle, pages->shmp)->records_unread);
|
|
Packit |
c04fcb |
}
|
|
Packit |
c04fcb |
if (config->mode == RING_BUFFER_OVERWRITE) {
|
|
Packit |
c04fcb |
id = bufb->buf_rsb.id;
|
|
Packit |
c04fcb |
sb_bindex = subbuffer_id_get_index(config, id);
|
|
Packit |
c04fcb |
pages = shmp_index(handle, bufb->array, sb_bindex);
|
|
Packit |
c04fcb |
records_unread += v_read(config, &shmp(handle, pages->shmp)->records_unread);
|
|
Packit |
c04fcb |
}
|
|
Packit |
c04fcb |
return records_unread;
|
|
Packit |
c04fcb |
}
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
#endif /* _LTTNG_RING_BUFFER_BACKEND_H */
|