-
Notifications
You must be signed in to change notification settings - Fork 2
/
shmq.c
executable file
·277 lines (240 loc) · 6.73 KB
/
shmq.c
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
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <sys/mman.h>
#include <unistd.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <libtaomee/log.h>
#include <libtaomee/conf_parser/config.h>
#include "bindconf.h"
#include "shmq.h"
#include "daemon.h"
#include "util.h"
inline struct shm_block *
head_mb (const struct shm_queue *q)
{
return (struct shm_block *) ((char *) q->addr + q->addr->head);
}
inline struct shm_block *
tail_mb (const struct shm_queue *q)
{
return (struct shm_block *) ((char *) q->addr + q->addr->tail);
}
static int
do_shmq_create (struct shm_queue *q)
{
q->addr = (shm_head_t *) mmap (NULL, q->length, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANON, -1, 0);
if (q->addr == MAP_FAILED)
ERROR_RETURN (("mmap failed, %s", strerror (errno)), -1);
q->addr->head = sizeof (shm_head_t);
q->addr->tail = sizeof (shm_head_t);
atomic_set (&(q->addr->blk_cnt), 0);
pipe_create (q->pipe_handles);
return 0;
}
static int
align_queue_tail (struct shm_queue *q)
{
struct shm_block *pad;
/*
*addr->head < addr->tail condition always is ok at here
*/
if (likely (q->addr->head >= q->addr->tail))
return 0;
pad = tail_mb (q);
if (q->length - q->addr->tail < sizeof (shm_block_t)
|| pad->type == PAD_BLOCK)
q->addr->tail = sizeof (shm_head_t);
return 0;
}
static int
align_queue_head (struct shm_queue *q, const struct shm_block *mb)
{
int tail_pos = q->addr->tail;
int head_pos = q->addr->head;
struct shm_block *pad;
int surplus = q->length - head_pos;
if (unlikely (surplus < mb->length))
{
//queue is full
if (unlikely (tail_pos == sizeof (shm_head_t)))
ERROR_RETURN (("shm_queue is full,head=%d,tail=%d,mb_len=%d",
head_pos, tail_pos, mb->length), -1);
//bug
else if (unlikely (q->addr->tail > head_pos))
{
// should be impossible
ERROR_LOG("shm_queue bug, head=%d, tail=%d, mb_len=%d, total_len=%u",
head_pos, tail_pos, mb->length, q->length);
q->addr->tail = sizeof (shm_head_t);
q->addr->head = sizeof (shm_head_t);
//no pad mb
}
else if (unlikely (surplus < sizeof (shm_block_t)))
{
q->addr->head = sizeof (shm_head_t);
//pad mb
}
else
{
pad = head_mb (q);
pad->type = PAD_BLOCK;
pad->length = surplus;
pad->id = 0;
q->addr->head = sizeof (shm_head_t);
}
}
return 0;
}
int shmq_pop(struct shm_queue* q, struct shm_block** mb)
{
//queue is empty
if (q->addr->tail == q->addr->head) {
return -1;
}
align_queue_tail(q);
//queue is empty
if (q->addr->tail == q->addr->head) {
return -1;
}
shm_block_t* cur_mb = tail_mb(q);
int head_pos = q->addr->head;
#ifdef DEBUG
//tail block overflow
if (cur_mb->length < sizeof(struct shm_block) ||
(q->addr->tail < head_pos && q->addr->tail + cur_mb->length > head_pos)
|| (q->addr->tail > head_pos
&& q->addr->tail + cur_mb->length > q->length)) {
ERROR_LOG("shm_queue tail=%d,head=%d,shm_block length=%d", q->addr->tail, head_pos, cur_mb->length);
exit(-1);
}
#endif
if (cur_mb->length > page_size)
ERROR_RETURN(("too large packet, len=%d", cur_mb->length), -1);
*mb = cur_mb;
// atomic_dec (&q->addr->blk_cnt);
q->addr->tail += cur_mb->length;
/*
TRACE_LOG("pop queue: q=%p length=%d tail=%d head=%d id=%u count=%u fd=%d",
q, cur_mb->length, q->addr->tail, q->addr->head, (*mb)->id,
(atomic_dec(&q->addr->blk_cnt), atomic_read(&q->addr->blk_cnt)), cur_mb->fd);
*/
return 0;
}
int shmq_push(shm_queue_t* q, shm_block_t* mb, const void* data)
{
char* next_mb;
assert(mb->length >= sizeof(shm_block_t));
if (mb->length > page_size) {
ERROR_LOG("too large packet, len=%d", mb->length);
return -1;
}
if (align_queue_head(q, mb) == -1) {
return -1;
}
int cnt;
for (cnt = 0; cnt != 10; ++cnt) {
//queue is full, (page_size): prevent overwriting the buffer which shmq_pop refers to
if ( unlikely((q->addr->tail > q->addr->head)
&& (q->addr->tail < q->addr->head + mb->length + page_size)) ) {
ALERT_LOG("queue [%p] is full, wait 5 microsecs: [cnt=%d]", q, cnt);
usleep(5);
} else {
break;
}
}
if (unlikely(cnt == 10)) {
ALERT_LOG("queue [%p] is full.", q);
return -1;
}
next_mb = (char*)head_mb(q);
memcpy(next_mb, mb, sizeof (shm_block_t));
if (likely(mb->length > sizeof (shm_block_t)))
memcpy(next_mb + sizeof (shm_block_t), data, mb->length - sizeof (shm_block_t));
q->addr->head += mb->length;
write(q->pipe_handles[1], q, 1);
/*
TRACE_LOG("push queue: queue=%p,length=%d,tail=%d,head=%d,id=%u,count=%d,fd=%d",
q, mb->length, q->addr->tail, q->addr->head, mb->id,
(atomic_inc(&q->addr->blk_cnt), atomic_read(&q->addr->blk_cnt)), mb->fd);
*/
return 0;
}
int shmq_create(bind_config_elem_t* p)
{
int err;
p->sendq.length = config_get_intval("shmq_length", 1 << 26);
p->recvq.length = p->sendq.length;
err = do_shmq_create(&(p->sendq)) | do_shmq_create(&(p->recvq));
BOOT_LOG (err, "Create shared memory queue: %dMB", p->recvq.length / 1024 / 512);
}
/**
* close_shmq_pipe - close needless pipe fd
* @bc - bind config
* @idx - indicates which pipe fd to close for parent process, or upperbound for child process
* @is_child - indicates child or parent, 0 parent, otherwise child
*
*/
void close_shmq_pipe(bind_config_t* bc, int idx, int is_child)
{
if (is_child) {
int i = 0;
// close fds inherited from parent process
for ( ; i != idx; ++i ) {
close(bc->configs[i].recvq.pipe_handles[1]);
close(bc->configs[i].sendq.pipe_handles[0]);
}
} else {
close(bc->configs[idx].recvq.pipe_handles[0]);
close(bc->configs[idx].sendq.pipe_handles[1]);
}
}
void do_destroy_shmq(bind_config_elem_t* bc_elem)
{
struct shm_queue* q = &(bc_elem->sendq);
// close send queue
if ( q->addr ) {
munmap(q->addr, q->length);
q->addr = 0;
}
// close receive queue
q = &(bc_elem->recvq);
if ( q->addr ) {
munmap(q->addr, q->length);
q->addr = 0;
}
}
void shmq_destroy(const bind_config_elem_t* exclu_bc_elem, int max_shmq_num)
{
bind_config_t* bc = get_bind_conf();
int i = 0;
for ( ; i != max_shmq_num; ++i ) {
if (&(bc->configs[i]) != exclu_bc_elem) {
do_destroy_shmq(&(bc->configs[i]));
}
}
}
/*
char *shmblk_dump (const struct shm_block* mb)
{
static char dump[1024];
if (mb == NULL) {
sprintf (dump, "shm_block == NULL");
} else {
snprintf (dump, sizeof (dump) - 1, "id=%llu, type=%d, length=%u, \
sk.localip=%u, sk.localport=%u, sk.peerip=%u, sk.peerport=%u \
sk.recvtm =%u, sk.sendtm=%u, sk.timeout=%d, sk.flag=%u",
mb->id, mb->type, mb->length,
mb->skinfo.local_ip, mb->skinfo.local_port, mb->skinfo.remote_ip, mb->skinfo.remote_port,
mb->skinfo.recvtm, mb->skinfo.sendtm, mb->skinfo.timeout, mb->skinfo.type);
}
return dump;
}
*/