fix shm objects wait fd and allocation error handling
[lttng-ust.git] / libringbuffer / backend.h
CommitLineData
852c2936
MD
1#ifndef _LINUX_RING_BUFFER_BACKEND_H
2#define _LINUX_RING_BUFFER_BACKEND_H
3
4/*
5 * linux/ringbuffer/backend.h
6 *
7 * Copyright (C) 2008-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
9 * Ring buffer backend (API).
10 *
11 * Dual LGPL v2.1/GPL v2 license.
12 *
13 * Credits to Steven Rostedt for proposing to use an extra-subbuffer owned by
14 * the reader in flight recorder mode.
15 */
16
14641deb
MD
17#include <unistd.h>
18
19#include "ust/core.h"
20
852c2936 21/* Internal helpers */
4931a13e
MD
22#include "backend_internal.h"
23#include "frontend_internal.h"
852c2936
MD
24
25/* Ring buffer backend API */
26
27/* Ring buffer backend access (read/write) */
28
29extern size_t lib_ring_buffer_read(struct lib_ring_buffer_backend *bufb,
1d498196
MD
30 size_t offset, void *dest, size_t len,
31 struct shm_handle *handle);
852c2936 32
852c2936 33extern int lib_ring_buffer_read_cstr(struct lib_ring_buffer_backend *bufb,
1d498196
MD
34 size_t offset, void *dest, size_t len,
35 struct shm_handle *handle);
852c2936 36
852c2936
MD
37/*
38 * Return the address where a given offset is located.
39 * Should be used to get the current subbuffer header pointer. Given we know
40 * it's never on a page boundary, it's safe to write directly to this address,
41 * as long as the write is never bigger than a page size.
42 */
43extern void *
44lib_ring_buffer_offset_address(struct lib_ring_buffer_backend *bufb,
1d498196
MD
45 size_t offset,
46 struct shm_handle *handle);
852c2936
MD
47extern void *
48lib_ring_buffer_read_offset_address(struct lib_ring_buffer_backend *bufb,
1d498196
MD
49 size_t offset,
50 struct shm_handle *handle);
852c2936
MD
51
52/**
53 * lib_ring_buffer_write - write data to a buffer backend
54 * @config : ring buffer instance configuration
55 * @ctx: ring buffer context. (input arguments only)
56 * @src : source pointer to copy from
57 * @len : length of data to copy
58 *
59 * This function copies "len" bytes of data from a source pointer to a buffer
60 * backend, at the current context offset. This is more or less a buffer
61 * backend-specific memcpy() operation. Calls the slow path (_ring_buffer_write)
62 * if copy is crossing a page boundary.
63 */
64static inline
65void lib_ring_buffer_write(const struct lib_ring_buffer_config *config,
66 struct lib_ring_buffer_ctx *ctx,
67 const void *src, size_t len)
68{
69 struct lib_ring_buffer_backend *bufb = &ctx->buf->backend;
70 struct channel_backend *chanb = &ctx->chan->backend;
1d498196 71 struct shm_handle *handle = ctx->handle;
a6352fd4 72 size_t sbidx;
852c2936 73 size_t offset = ctx->buf_offset;
1d498196 74 struct lib_ring_buffer_backend_pages_shmp *rpages;
852c2936
MD
75 unsigned long sb_bindex, id;
76
77 offset &= chanb->buf_size - 1;
78 sbidx = offset >> chanb->subbuf_size_order;
4746ae29 79 id = shmp_index(handle, bufb->buf_wsb, sbidx)->id;
852c2936 80 sb_bindex = subbuffer_id_get_index(config, id);
4746ae29 81 rpages = shmp_index(handle, bufb->array, sb_bindex);
852c2936
MD
82 CHAN_WARN_ON(ctx->chan,
83 config->mode == RING_BUFFER_OVERWRITE
84 && subbuffer_id_is_noref(config, id));
a6352fd4
MD
85 /*
86 * Underlying layer should never ask for writes across
87 * subbuffers.
88 */
89 CHAN_WARN_ON(chanb, offset >= chanb->buf_size);
90 lib_ring_buffer_do_copy(config,
4746ae29 91 shmp_index(handle, shmp(handle, rpages->shmp)->p, offset & ~(chanb->subbuf_size - 1)),
a6352fd4 92 src, len);
852c2936
MD
93 ctx->buf_offset += len;
94}
95
96/*
97 * This accessor counts the number of unread records in a buffer.
98 * It only provides a consistent value if no reads not writes are performed
99 * concurrently.
100 */
101static inline
102unsigned long lib_ring_buffer_get_records_unread(
103 const struct lib_ring_buffer_config *config,
1d498196
MD
104 struct lib_ring_buffer *buf,
105 struct shm_handle *handle)
852c2936
MD
106{
107 struct lib_ring_buffer_backend *bufb = &buf->backend;
1d498196 108 struct lib_ring_buffer_backend_pages_shmp *pages;
852c2936
MD
109 unsigned long records_unread = 0, sb_bindex, id;
110 unsigned int i;
111
1d498196 112 for (i = 0; i < shmp(handle, bufb->chan)->backend.num_subbuf; i++) {
4746ae29 113 id = shmp_index(handle, bufb->buf_wsb, i)->id;
852c2936 114 sb_bindex = subbuffer_id_get_index(config, id);
4746ae29 115 pages = shmp_index(handle, bufb->array, sb_bindex);
1d498196 116 records_unread += v_read(config, &shmp(handle, pages->shmp)->records_unread);
852c2936
MD
117 }
118 if (config->mode == RING_BUFFER_OVERWRITE) {
119 id = bufb->buf_rsb.id;
120 sb_bindex = subbuffer_id_get_index(config, id);
4746ae29 121 pages = shmp_index(handle, bufb->array, sb_bindex);
1d498196 122 records_unread += v_read(config, &shmp(handle, pages->shmp)->records_unread);
852c2936
MD
123 }
124 return records_unread;
125}
126
852c2936 127#endif /* _LINUX_RING_BUFFER_BACKEND_H */
This page took 0.028938 seconds and 4 git commands to generate.