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
|
/*
* 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.
*/
#ifndef EVENTLIB_INIT_H
#define EVENTLIB_INIT_H
/* All pointers targeted to shared memory are annotated with 'shmptr' */
#define shmptr /* empty, pure annotation */
#include "eventlib.h"
#include "utility.h"
#include "eventlib_tbuf.h"
#include "eventlib_flt.h"
#define ALIGN_64_DOWN(x) ((x) & ~((typeof(x))(sizeof(uint64_t) - 1)))
#define ALIGN_64_UP(x) ALIGN_64_DOWN((x) + ((uint32_t)sizeof(uint64_t) - 1))
#define ALIGN_64_CHECK(x) ((x) == ALIGN_64_DOWN(x))
/* Eventlib uses two shared memory blocks, one is read only for readers,
* and other is read-write for readers.
*
* These blocks can be physically accessed by different eventlib builds
* simultaneuosly. Thus special mesures have to be taken to ensure
* compatibility.
*
* - Blocks are separated into subblocks corresponding to subsystems. At this
* time, subsystems are "tracebuf" and "filtering". One more could be -
* alive management. Room for subsystems costs 8 bytes per subsystem per
* shmem block - thus should not be an issue to reserve space for 8 of them.
* - Each subblock holds some sort of version/compatibility information, that
* can be used in subsystem-specific way to find out if reader's
* implementation of this subsystem can understand writer's implementation
* of this subsystem. Details are subsystem-specific.
* - Thus reader can understand some of writer's subsystems, but maybe not
* all. Understanding 'tracebuf' subsystem is required.Not understanding
* other subsystem is possible but then reader will have only limited
* information.
*/
#define EVENTLIB_SUBSYS_TRACEBUF 0
#define EVENTLIB_SUBSYS_FILTERING 1
#define EVENTLIB_SUBSYS_MAX 2
#define EVENTLIB_TBUFS_MAX 6
#define EVENTLIB_MAGIC_W2R 0x52574c45 /* 'ELWR' in little endian */
#define EVENTLIB_MAGIC_R2W 0x57524c45 /* 'ELRW' in little endian */
struct eventlib_shared {
/* Different magic is used for two shared memory blocks */
uint32_t magic;
/* Number of trace buffers
* Below value is updated from writer context
*/
uint32_t num_buffers;
/* Locatons of subsystem's information. If subsystem is not presented
* in particular block, corresponding offset/size are kept zeroed.
*/
struct {
uint32_t offset;
uint32_t size;
} subsys[EVENTLIB_SUBSYS_MAX];
} __attribute__((__packed__));
struct eventlib_init {
/* writer, init time: offsets of currently unallocated space */
uint32_t w2r_offset, r2w_offset;
/* Local copy of subsystem information is used to protect against
* unexpected asynchronous changes (not expected under normal use).
*/
struct eventlib_shared w2r_copy;
struct eventlib_shared r2w_copy;
struct eventlib_tbuf_ctx tbuf[EVENTLIB_TBUFS_MAX];
struct eventlib_flt_ctx flt;
};
static inline void *subsys_shm_area(shmptr struct eventlib_shared *sh,
struct eventlib_shared *copy, int subsys)
{
if (sh == NULL)
return NULL;
return ((void *)((uintptr_t)sh + copy->subsys[subsys].offset));
}
#define subsys_w2r_area(ctx, subsys) \
subsys_shm_area(ctx->w2r_shm, &ctx->priv->w2r_copy, \
EVENTLIB_SUBSYS_ ## subsys)
#define subsys_r2w_area(ctx, subsys) \
subsys_shm_area(ctx->r2w_shm, &ctx->priv->r2w_copy, \
EVENTLIB_SUBSYS_ ## subsys)
static inline uint32_t subsys_shm_size(struct eventlib_shared *copy, int subsys)
{
return copy->subsys[subsys].size;
}
#define subsys_w2r_size(ctx, subsys) \
subsys_shm_size(&ctx->priv->w2r_copy, EVENTLIB_SUBSYS_ ## subsys)
#define subsys_r2w_size(ctx, subsys) \
subsys_shm_size(&ctx->priv->r2w_copy, EVENTLIB_SUBSYS_ ## subsys)
static inline int subsys_shm_carve(struct eventlib_shared *copy,
uint32_t *shm_size, uint32_t *shm_offset, int subsys, uint32_t size)
{
if (size > *shm_size - *shm_offset)
return -ENOSPC;
copy->subsys[subsys].offset = *shm_offset;
copy->subsys[subsys].size = size;
*shm_offset += ALIGN_64_UP(size);
return 0;
}
#define subsys_w2r_carve(ctx, subsys, size) \
subsys_shm_carve(&ctx->priv->w2r_copy, &ctx->w2r_shm_size, \
&ctx->priv->w2r_offset, EVENTLIB_SUBSYS_ ## subsys, size)
#define subsys_r2w_carve(ctx, subsys, size) \
subsys_shm_carve(&ctx->priv->r2w_copy, &ctx->r2w_shm_size, \
&ctx->priv->r2w_offset, EVENTLIB_SUBSYS_ ## subsys, size)
#endif
|