1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
/*
* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <linux/string.h>
#include "eventlib.h"
#include "tracebuf.h"
#include "eventlib_init.h"
#include "eventlib_tbuf.h"
/* Reads sequences from shared memory can be attempted at most two times */
#define PULL_COUNT_MAX 2
#define INIT_COUNT_MAX 2
/* Valid sequence IDs are always in the below range (inclusive) */
#define SEQUENCE_ID_MIN (0ULL)
#define SEQUENCE_ID_MAX (~0ULL)
/* Bit 0 of compat word shows if writer has initialized filtering.
* Other bits in compat word must be zero in this version
*/
#define TBUF_COMPAT(ctx) ((ctx->flags & EVENTLIB_FLAG_INIT_FILTERING) ? 1 : 0)
#define TBUF_CHECK_COMPAT(compat) (((compat) & ~1u) == 0)
static int tbuf_writer_init(struct eventlib_ctx *ctx)
{
uint8_t *area;
shmptr struct eventlib_tbuf_w2r *w2r;
uint32_t size;
int ret = 0;
uint32_t idx;
ret = subsys_w2r_carve(ctx, TRACEBUF,
ctx->w2r_shm_size - ctx->priv->w2r_offset);
if (ret != 0)
return ret;
if (ctx->num_buffers == 0 || ctx->num_buffers > EVENTLIB_TBUFS_MAX)
return -EIO;
area = subsys_w2r_area(ctx, TRACEBUF);
size = subsys_w2r_size(ctx, TRACEBUF);
size /= ctx->num_buffers;
size = ALIGN_64_DOWN(size);
if (size < sizeof(struct eventlib_tbuf_w2r))
return -ENOSPC;
for (idx = 0; idx < ctx->num_buffers; idx++) {
w2r = (struct eventlib_tbuf_w2r *)(area + (size * idx));
w2r->compat = TBUF_COMPAT(ctx);
ret = tracebuf_init(&ctx->priv->tbuf[idx].tbuf_ctx,
&w2r->tbuf,
size - (uint32_t)sizeof(struct eventlib_tbuf_w2r));
if (ret != 0)
break;
}
return ret;
}
static int tbuf_reader_init(struct eventlib_ctx *ctx)
{
uint8_t *area;
shmptr struct eventlib_tbuf_w2r *w2r;
uint32_t size;
uint32_t idx;
int ret = 0;
if (ctx->priv->w2r_copy.num_buffers == 0 ||
ctx->priv->w2r_copy.num_buffers > EVENTLIB_TBUFS_MAX)
return -EIO;
area = subsys_w2r_area(ctx, TRACEBUF);
size = subsys_w2r_size(ctx, TRACEBUF);
size /= ctx->priv->w2r_copy.num_buffers;
size = ALIGN_64_DOWN(size);
if (size < sizeof(struct eventlib_tbuf_w2r))
return -EIO;
for (idx = 0; idx < ctx->priv->w2r_copy.num_buffers; idx++) {
w2r = (struct eventlib_tbuf_w2r *)(area + (size * idx));
if (!TBUF_CHECK_COMPAT(w2r->compat))
return -EPROTONOSUPPORT;
ret = tracebuf_bind(&ctx->priv->tbuf[idx].tbuf_ctx,
&w2r->tbuf,
size - (uint32_t)sizeof(struct eventlib_tbuf_w2r));
if (ret != 0)
break;
}
return ret;
}
int tbuf_init(struct eventlib_ctx *ctx)
{
int ret;
switch (ctx->direction) {
case EVENTLIB_DIRECTION_WRITER:
ret = tbuf_writer_init(ctx);
break;
case EVENTLIB_DIRECTION_READER:
ret = tbuf_reader_init(ctx);
break;
default:
ret = -EINVAL;
break;
}
return ret;
}
void eventlib_write(struct eventlib_ctx *ctx, uint32_t idx,
event_type_t type, event_timestamp_t ts, void *data, uint32_t size)
{
struct tracehdr hdr;
if (ctx->direction != EVENTLIB_DIRECTION_WRITER)
return;
if (idx >= ctx->num_buffers)
return;
hdr.params = ts;
hdr.reserved = type;
tracebuf_push(&ctx->priv->tbuf[idx].tbuf_ctx, &hdr, data, size);
}
static int tbuf_pull_single(struct eventlib_tbuf_ctx *tbuf,
struct pullstate *state, uint64_t *seqid, struct record *rec,
void *payload, uint32_t *paylen)
{
struct tracehdr hdr;
unsigned int i;
uint32_t length;
int ret = -EPROTO;
for (i = 0; i < PULL_COUNT_MAX; i++) {
length = *paylen;
ret = tracebuf_pull(&tbuf->tbuf_ctx, state,
&hdr, payload, &length);
if (ret != -EAGAIN)
break;
}
if (ret != 0)
return ret;
/* We need to use a generic copy here because the destination address
* may not be aligned. For example, the address may be an odd value.
*/
memcpy(&rec->size, &length, sizeof(uint32_t));
memcpy(&rec->type, &hdr.reserved, sizeof(uint32_t));
memcpy(&rec->ts, &hdr.params, sizeof(uint64_t));
*seqid = hdr.seqid;
*paylen = length;
return 0;
}
static int tbuf_pull_multiple(struct eventlib_tbuf_ctx *tbuf, void *buffer,
uint32_t *size, uint64_t *min, uint64_t *max)
{
uint64_t seqid = 0ULL;
struct pullstate state;
uintptr_t current;
uint32_t length;
uint32_t avail;
int ret;
*min = SEQUENCE_ID_MAX;
*max = SEQUENCE_ID_MIN;
pull_init(&tbuf->tbuf_ctx, &state);
current = (uintptr_t)buffer;
avail = *size;
while (avail >= sizeof(struct record)) {
length = avail - (uint32_t)sizeof(struct record);
ret = tbuf_pull_single(tbuf, &state, &seqid,
(struct record *)current,
(void *)(current + sizeof(struct record)),
&length);
/* Check if all events have been processed. This happens
* when we've consumed all available event data and we did
* not encounter any duplicates.
*/
if (ret == -ENOBUFS)
break;
/* Check if there was any other type of error. We may have
* been interrupted, or maybe we detected data corruption.
* Just report the error back to the caller.
*/
if (ret != 0)
return ret;
/* Check if this is a duplicate event. If so, there's no
* need to advance any further as all subsequent events
* will have occurred earlier than this event.
*/
if (seqid <= tbuf->seqid_ack)
break;
if (seqid > *max)
*max = seqid;
if (seqid < *min)
*min = seqid;
/* Check if it's safe to advance to the next event position.
* This should not fail under all expecteed scenarios if the
* underlying subsystem is behaving properly.
*/
if (avail < (uint32_t)sizeof(struct record) + length)
return -EIO;
avail -= (uint32_t)sizeof(struct record) + length;
current += (uint32_t)sizeof(struct record) + length;
}
*size = *size - avail;
return 0;
}
static int eventlib_read_from_tbuf(struct eventlib_tbuf_ctx *tbuf, void *buffer,
uint32_t *size, uint64_t *lost)
{
int ret = -EPROTO;
unsigned int i;
uint64_t min;
uint64_t max;
if (lost)
*lost = 0;
for (i = 0; i < INIT_COUNT_MAX; i++) {
ret = tbuf_pull_multiple(tbuf, buffer, size, &min, &max);
if (ret != -EINTR)
break;
}
if (ret != 0 || *size == 0)
return ret;
if (lost) {
/* Check if we have any newly detected lost events to report.
* These lost events are seq IDs in range (seqid_ack, min).
*/
if (min > tbuf->seqid_ack + 1)
*lost = min - tbuf->seqid_ack - 1;
else
*lost = 0;
}
tbuf->seqid_ack = max;
return 0;
}
int eventlib_read(struct eventlib_ctx *ctx, void *buffer, uint32_t *size,
uint64_t *lost)
{
int ret = -EPROTO;
unsigned int idx;
uint64_t accum_lost;
uint8_t *copy_buffer;
uint32_t copy_size;
uint32_t accum_empty;
if (lost)
*lost = 0;
accum_empty = *size;
copy_buffer = (uint8_t *)(buffer);
if (ctx->direction != EVENTLIB_DIRECTION_READER)
return -EPROTO;
for (idx = 0; idx < ctx->priv->w2r_copy.num_buffers; idx++) {
accum_lost = 0;
copy_size = accum_empty;
ret = eventlib_read_from_tbuf(&ctx->priv->tbuf[idx],
copy_buffer, ©_size, &accum_lost);
if (ret != 0)
break;
/* Update empty slots */
accum_empty -= copy_size;
/* Advanced to next free slot */
copy_buffer = copy_buffer + copy_size;
if (lost)
*lost += accum_lost;
}
/* Number of bytes written */
*size -= accum_empty;
return ret;
}
|