-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfifo.c
197 lines (168 loc) · 4.32 KB
/
fifo.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
/*
* B-Queue -- An efficient and practical queueing for fast core-to-core
* communication
*
* Copyright (C) 2011 Junchang Wang <[email protected]>
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "fifo.h"
#include <sched.h>
#if defined(FIFO_DEBUG)
#include <assert.h>
#endif
inline uint64_t read_tsc()
{
uint64_t time;
uint32_t msw , lsw;
__asm__ __volatile__("rdtsc\n\t"
"movl %%edx, %0\n\t"
"movl %%eax, %1\n\t"
: "=r" (msw), "=r"(lsw)
:
: "%edx" , "%eax");
time = ((uint64_t) msw << 32) | lsw;
return time;
}
inline void wait_ticks(uint64_t ticks)
{
uint64_t current_time;
uint64_t time = read_tsc();
time += ticks;
do {
current_time = read_tsc();
} while (current_time < time);
}
static ELEMENT_TYPE ELEMENT_ZERO = 0x0UL;
/*************************************************/
/********** Queue Functions **********************/
/*************************************************/
void queue_init(struct queue_t *q)
{
memset(q, 0, sizeof(struct queue_t));
#if defined(CONS_BATCH)
q->batch_history = CONS_BATCH_SIZE;
#endif
}
#if defined(PROD_BATCH) || defined(CONS_BATCH)
inline int leqthan(volatile ELEMENT_TYPE point, volatile ELEMENT_TYPE batch_point)
{
return (point == batch_point);
}
#endif
#if defined(PROD_BATCH)
int enqueue(struct queue_t * q, ELEMENT_TYPE value)
{
uint32_t tmp_head;
if( q->head == q->batch_head ) {
tmp_head = q->head + PROD_BATCH_SIZE;
if ( tmp_head >= QUEUE_SIZE )
tmp_head = 0;
if ( q->data[tmp_head] ) {
wait_ticks(CONGESTION_PENALTY);
return BUFFER_FULL;
}
q->batch_head = tmp_head;
}
q->data[q->head] = value;
q->head ++;
if ( q->head >= QUEUE_SIZE ) {
q->head = 0;
}
return SUCCESS;
}
#else
int enqueue(struct queue_t * q, ELEMENT_TYPE value)
{
if ( q->data[q->head] )
return BUFFER_FULL;
q->data[q->head] = value;
q->head ++;
if ( q->head >= QUEUE_SIZE ) {
q->head = 0;
}
return SUCCESS;
}
#endif
#if defined(CONS_BATCH)
static inline int backtracking(struct queue_t * q)
{
uint32_t tmp_tail;
tmp_tail = q->tail + CONS_BATCH_SIZE;
if ( tmp_tail >= QUEUE_SIZE ) {
tmp_tail = 0;
#if defined(ADAPTIVE)
if (q->batch_history < CONS_BATCH_SIZE) {
q->batch_history =
(CONS_BATCH_SIZE < (q->batch_history + BATCH_INCREAMENT))?
CONS_BATCH_SIZE : (q->batch_history + BATCH_INCREAMENT);
}
#endif
}
#if defined(BACKTRACKING)
unsigned long batch_size = q->batch_history;
while (!(q->data[tmp_tail])) {
wait_ticks(CONGESTION_PENALTY);
batch_size = batch_size >> 1;
if( batch_size >= 0 ) {
tmp_tail = q->tail + batch_size;
if (tmp_tail >= QUEUE_SIZE)
tmp_tail = 0;
}
else
return -1;
}
#if defined(ADAPTIVE)
q->batch_history = batch_size;
#endif
#else
if ( !q->data[tmp_tail] ) {
wait_ticks(CONGESTION_PENALTY);
return -1;
}
#endif /* end BACKTRACKING */
if ( tmp_tail == q->tail ) {
tmp_tail = (tmp_tail + 1) >= QUEUE_SIZE ?
0 : tmp_tail + 1;
}
q->batch_tail = tmp_tail;
return 0;
}
int dequeue(struct queue_t * q, ELEMENT_TYPE * value)
{
if( q->tail == q->batch_tail ) {
if ( backtracking(q) != 0 )
return BUFFER_EMPTY;
}
*value = q->data[q->tail];
q->data[q->tail] = ELEMENT_ZERO;
q->tail ++;
if ( q->tail >= QUEUE_SIZE )
q->tail = 0;
return SUCCESS;
}
#else
int dequeue(struct queue_t * q, ELEMENT_TYPE * value)
{
if ( !q->data[q->tail] )
return BUFFER_EMPTY;
*value = q->data[q->tail];
q->data[q->tail] = ELEMENT_ZERO;
q->tail ++;
if ( q->tail >= QUEUE_SIZE )
q->tail = 0;
return SUCCESS;
}
#endif