License text standardization, add missing licenses
[lttng-ust.git] / libringbuffer / backend.h
CommitLineData
e92f3e28
MD
1#ifndef _LTTNG_RING_BUFFER_BACKEND_H
2#define _LTTNG_RING_BUFFER_BACKEND_H
852c2936
MD
3
4/*
e92f3e28 5 * libringbuffer/backend.h
852c2936
MD
6 *
7 * Ring buffer backend (API).
8 *
e92f3e28
MD
9 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; only
14 * version 2.1 of the License.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
852c2936
MD
24 *
25 * Credits to Steven Rostedt for proposing to use an extra-subbuffer owned by
26 * the reader in flight recorder mode.
27 */
28
14641deb
MD
29#include <unistd.h>
30
852c2936 31/* Internal helpers */
4931a13e
MD
32#include "backend_internal.h"
33#include "frontend_internal.h"
852c2936
MD
34
35/* Ring buffer backend API */
36
37/* Ring buffer backend access (read/write) */
38
4cfec15c 39extern size_t lib_ring_buffer_read(struct lttng_ust_lib_ring_buffer_backend *bufb,
1d498196 40 size_t offset, void *dest, size_t len,
38fae1d3 41 struct lttng_ust_shm_handle *handle);
852c2936 42
4cfec15c 43extern int lib_ring_buffer_read_cstr(struct lttng_ust_lib_ring_buffer_backend *bufb,
1d498196 44 size_t offset, void *dest, size_t len,
38fae1d3 45 struct lttng_ust_shm_handle *handle);
852c2936 46
852c2936
MD
47/*
48 * Return the address where a given offset is located.
49 * Should be used to get the current subbuffer header pointer. Given we know
50 * it's never on a page boundary, it's safe to write directly to this address,
51 * as long as the write is never bigger than a page size.
52 */
53extern void *
4cfec15c 54lib_ring_buffer_offset_address(struct lttng_ust_lib_ring_buffer_backend *bufb,
1d498196 55 size_t offset,
38fae1d3 56 struct lttng_ust_shm_handle *handle);
852c2936 57extern void *
4cfec15c 58lib_ring_buffer_read_offset_address(struct lttng_ust_lib_ring_buffer_backend *bufb,
1d498196 59 size_t offset,
38fae1d3 60 struct lttng_ust_shm_handle *handle);
852c2936
MD
61
62/**
63 * lib_ring_buffer_write - write data to a buffer backend
64 * @config : ring buffer instance configuration
65 * @ctx: ring buffer context. (input arguments only)
66 * @src : source pointer to copy from
67 * @len : length of data to copy
68 *
69 * This function copies "len" bytes of data from a source pointer to a buffer
70 * backend, at the current context offset. This is more or less a buffer
71 * backend-specific memcpy() operation. Calls the slow path (_ring_buffer_write)
72 * if copy is crossing a page boundary.
73 */
74static inline
4cfec15c
MD
75void lib_ring_buffer_write(const struct lttng_ust_lib_ring_buffer_config *config,
76 struct lttng_ust_lib_ring_buffer_ctx *ctx,
852c2936
MD
77 const void *src, size_t len)
78{
4cfec15c 79 struct lttng_ust_lib_ring_buffer_backend *bufb = &ctx->buf->backend;
852c2936 80 struct channel_backend *chanb = &ctx->chan->backend;
38fae1d3 81 struct lttng_ust_shm_handle *handle = ctx->handle;
a6352fd4 82 size_t sbidx;
852c2936 83 size_t offset = ctx->buf_offset;
4cfec15c 84 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
852c2936
MD
85 unsigned long sb_bindex, id;
86
87 offset &= chanb->buf_size - 1;
88 sbidx = offset >> chanb->subbuf_size_order;
4746ae29 89 id = shmp_index(handle, bufb->buf_wsb, sbidx)->id;
852c2936 90 sb_bindex = subbuffer_id_get_index(config, id);
4746ae29 91 rpages = shmp_index(handle, bufb->array, sb_bindex);
852c2936
MD
92 CHAN_WARN_ON(ctx->chan,
93 config->mode == RING_BUFFER_OVERWRITE
94 && subbuffer_id_is_noref(config, id));
a6352fd4
MD
95 /*
96 * Underlying layer should never ask for writes across
97 * subbuffers.
98 */
99 CHAN_WARN_ON(chanb, offset >= chanb->buf_size);
100 lib_ring_buffer_do_copy(config,
aead2025 101 shmp_index(handle, shmp(handle, rpages->shmp)->p, offset & (chanb->subbuf_size - 1)),
a6352fd4 102 src, len);
852c2936
MD
103 ctx->buf_offset += len;
104}
105
106/*
107 * This accessor counts the number of unread records in a buffer.
108 * It only provides a consistent value if no reads not writes are performed
109 * concurrently.
110 */
111static inline
112unsigned long lib_ring_buffer_get_records_unread(
4cfec15c
MD
113 const struct lttng_ust_lib_ring_buffer_config *config,
114 struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 115 struct lttng_ust_shm_handle *handle)
852c2936 116{
4cfec15c
MD
117 struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend;
118 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *pages;
852c2936
MD
119 unsigned long records_unread = 0, sb_bindex, id;
120 unsigned int i;
121
1d498196 122 for (i = 0; i < shmp(handle, bufb->chan)->backend.num_subbuf; i++) {
4746ae29 123 id = shmp_index(handle, bufb->buf_wsb, i)->id;
852c2936 124 sb_bindex = subbuffer_id_get_index(config, id);
4746ae29 125 pages = shmp_index(handle, bufb->array, sb_bindex);
1d498196 126 records_unread += v_read(config, &shmp(handle, pages->shmp)->records_unread);
852c2936
MD
127 }
128 if (config->mode == RING_BUFFER_OVERWRITE) {
129 id = bufb->buf_rsb.id;
130 sb_bindex = subbuffer_id_get_index(config, id);
4746ae29 131 pages = shmp_index(handle, bufb->array, sb_bindex);
1d498196 132 records_unread += v_read(config, &shmp(handle, pages->shmp)->records_unread);
852c2936
MD
133 }
134 return records_unread;
135}
136
e92f3e28 137#endif /* _LTTNG_RING_BUFFER_BACKEND_H */
This page took 0.02944 seconds and 4 git commands to generate.