| 1 |
#include "xframe_queue.h" |
| 2 |
#include "xbus-core.h" |
| 3 |
#include "dahdi_debug.h" |
| 4 |
|
| 5 |
extern int debug; |
| 6 |
|
| 7 |
static xframe_t *transport_alloc_xframe(xbus_t *xbus, gfp_t gfp_flags); |
| 8 |
static void transport_free_xframe(xbus_t *xbus, xframe_t *xframe); |
| 9 |
|
| 10 |
void xframe_queue_init(struct xframe_queue *q, |
| 11 |
unsigned int steady_state_count, unsigned int max_count, |
| 12 |
const char *name, void *priv) |
| 13 |
{ |
| 14 |
memset(q, 0, sizeof(*q)); |
| 15 |
spin_lock_init(&q->lock); |
| 16 |
INIT_LIST_HEAD(&q->head); |
| 17 |
q->max_count = XFRAME_QUEUE_MARGIN + max_count; |
| 18 |
q->steady_state_count = XFRAME_QUEUE_MARGIN + steady_state_count; |
| 19 |
q->name = name; |
| 20 |
q->priv = priv; |
| 21 |
} |
| 22 |
EXPORT_SYMBOL(xframe_queue_init); |
| 23 |
|
| 24 |
void xframe_queue_clearstats(struct xframe_queue *q) |
| 25 |
{ |
| 26 |
q->worst_count = 0; |
| 27 |
//q->overflows = 0; /* Never clear overflows */ |
| 28 |
q->worst_lag_usec = 0L; |
| 29 |
} |
| 30 |
EXPORT_SYMBOL(xframe_queue_clearstats); |
| 31 |
|
| 32 |
static void __xframe_dump_queue(struct xframe_queue *q) |
| 33 |
{ |
| 34 |
xframe_t *xframe; |
| 35 |
int i = 0; |
| 36 |
char prefix[30]; |
| 37 |
struct timeval now; |
| 38 |
|
| 39 |
do_gettimeofday(&now); |
| 40 |
printk(KERN_DEBUG "%s: dump queue '%s' (first packet in each frame)\n", |
| 41 |
THIS_MODULE->name, q->name); |
| 42 |
list_for_each_entry_reverse(xframe, &q->head, frame_list) { |
| 43 |
xpacket_t *pack = (xpacket_t *)&xframe->packets[0]; |
| 44 |
long usec = usec_diff(&now, &xframe->tv_queued); |
| 45 |
snprintf(prefix, ARRAY_SIZE(prefix), " %3d> %5ld.%03ld msec", |
| 46 |
i++, usec / 1000, usec % 1000); |
| 47 |
dump_packet(prefix, pack, 1); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
static bool __xframe_enqueue(struct xframe_queue *q, xframe_t *xframe) |
| 52 |
{ |
| 53 |
int ret = 1; |
| 54 |
static int overflow_cnt; |
| 55 |
|
| 56 |
if (unlikely(q->disabled)) { |
| 57 |
ret = 0; |
| 58 |
goto out; |
| 59 |
} |
| 60 |
if (q->count >= q->max_count) { |
| 61 |
q->overflows++; |
| 62 |
if ((overflow_cnt++ % 1000) < 5) { |
| 63 |
NOTICE("Overflow of %-15s: counts %3d, %3d, %3d " |
| 64 |
"worst %3d, overflows %3d " |
| 65 |
"worst_lag %02ld.%ld ms\n", |
| 66 |
q->name, q->steady_state_count, q->count, |
| 67 |
q->max_count, q->worst_count, q->overflows, |
| 68 |
q->worst_lag_usec / 1000, |
| 69 |
q->worst_lag_usec % 1000); |
| 70 |
__xframe_dump_queue(q); |
| 71 |
} |
| 72 |
ret = 0; |
| 73 |
goto out; |
| 74 |
} |
| 75 |
if (++q->count > q->worst_count) |
| 76 |
q->worst_count = q->count; |
| 77 |
list_add_tail(&xframe->frame_list, &q->head); |
| 78 |
do_gettimeofday(&xframe->tv_queued); |
| 79 |
out: |
| 80 |
return ret; |
| 81 |
} |
| 82 |
|
| 83 |
bool xframe_enqueue(struct xframe_queue *q, xframe_t *xframe) |
| 84 |
{ |
| 85 |
unsigned long flags; |
| 86 |
int ret; |
| 87 |
|
| 88 |
spin_lock_irqsave(&q->lock, flags); |
| 89 |
ret = __xframe_enqueue(q, xframe); |
| 90 |
spin_unlock_irqrestore(&q->lock, flags); |
| 91 |
return ret; |
| 92 |
} |
| 93 |
EXPORT_SYMBOL(xframe_enqueue); |
| 94 |
|
| 95 |
static xframe_t *__xframe_dequeue(struct xframe_queue *q) |
| 96 |
{ |
| 97 |
xframe_t *frm = NULL; |
| 98 |
struct list_head *h; |
| 99 |
struct timeval now; |
| 100 |
unsigned long usec_lag; |
| 101 |
|
| 102 |
if (list_empty(&q->head)) |
| 103 |
goto out; |
| 104 |
h = q->head.next; |
| 105 |
list_del_init(h); |
| 106 |
--q->count; |
| 107 |
frm = list_entry(h, xframe_t, frame_list); |
| 108 |
do_gettimeofday(&now); |
| 109 |
usec_lag = |
| 110 |
(now.tv_sec - frm->tv_queued.tv_sec) * 1000 * 1000 + (now.tv_usec - |
| 111 |
frm-> |
| 112 |
tv_queued. |
| 113 |
tv_usec); |
| 114 |
if (q->worst_lag_usec < usec_lag) |
| 115 |
q->worst_lag_usec = usec_lag; |
| 116 |
out: |
| 117 |
return frm; |
| 118 |
} |
| 119 |
|
| 120 |
xframe_t *xframe_dequeue(struct xframe_queue *q) |
| 121 |
{ |
| 122 |
unsigned long flags; |
| 123 |
xframe_t *frm; |
| 124 |
|
| 125 |
spin_lock_irqsave(&q->lock, flags); |
| 126 |
frm = __xframe_dequeue(q); |
| 127 |
spin_unlock_irqrestore(&q->lock, flags); |
| 128 |
return frm; |
| 129 |
} |
| 130 |
EXPORT_SYMBOL(xframe_dequeue); |
| 131 |
|
| 132 |
void xframe_queue_disable(struct xframe_queue *q, bool disabled) |
| 133 |
{ |
| 134 |
q->disabled = disabled; |
| 135 |
} |
| 136 |
EXPORT_SYMBOL(xframe_queue_disable); |
| 137 |
|
| 138 |
void xframe_queue_clear(struct xframe_queue *q) |
| 139 |
{ |
| 140 |
xframe_t *xframe; |
| 141 |
xbus_t *xbus = q->priv; |
| 142 |
int i = 0; |
| 143 |
|
| 144 |
xframe_queue_disable(q, 1); |
| 145 |
while ((xframe = xframe_dequeue(q)) != NULL) { |
| 146 |
transport_free_xframe(xbus, xframe); |
| 147 |
i++; |
| 148 |
} |
| 149 |
XBUS_DBG(DEVICES, xbus, "%s: finished queue clear (%d items)\n", |
| 150 |
q->name, i); |
| 151 |
} |
| 152 |
EXPORT_SYMBOL(xframe_queue_clear); |
| 153 |
|
| 154 |
uint xframe_queue_count(struct xframe_queue *q) |
| 155 |
{ |
| 156 |
return q->count; |
| 157 |
} |
| 158 |
EXPORT_SYMBOL(xframe_queue_count); |
| 159 |
|
| 160 |
/*------------------------- Frame Alloc/Dealloc --------------------*/ |
| 161 |
|
| 162 |
static xframe_t *transport_alloc_xframe(xbus_t *xbus, gfp_t gfp_flags) |
| 163 |
{ |
| 164 |
struct xbus_ops *ops; |
| 165 |
xframe_t *xframe; |
| 166 |
unsigned long flags; |
| 167 |
|
| 168 |
BUG_ON(!xbus); |
| 169 |
ops = transportops_get(xbus); |
| 170 |
if (unlikely(!ops)) { |
| 171 |
XBUS_ERR(xbus, "Missing transport\n"); |
| 172 |
return NULL; |
| 173 |
} |
| 174 |
spin_lock_irqsave(&xbus->transport.lock, flags); |
| 175 |
#if 0 |
| 176 |
XBUS_INFO(xbus, "%s (transport_refcount=%d)\n", |
| 177 |
__func__, atomic_read(&xbus->transport.transport_refcount)); |
| 178 |
#endif |
| 179 |
xframe = ops->alloc_xframe(xbus, gfp_flags); |
| 180 |
if (!xframe) { |
| 181 |
static int rate_limit; |
| 182 |
|
| 183 |
if ((rate_limit++ % 3001) == 0) |
| 184 |
XBUS_ERR(xbus, |
| 185 |
"Failed xframe allocation from transport (%d)\n", |
| 186 |
rate_limit); |
| 187 |
transportops_put(xbus); |
| 188 |
/* fall through */ |
| 189 |
} |
| 190 |
spin_unlock_irqrestore(&xbus->transport.lock, flags); |
| 191 |
return xframe; |
| 192 |
} |
| 193 |
|
| 194 |
static void transport_free_xframe(xbus_t *xbus, xframe_t *xframe) |
| 195 |
{ |
| 196 |
struct xbus_ops *ops; |
| 197 |
unsigned long flags; |
| 198 |
|
| 199 |
BUG_ON(!xbus); |
| 200 |
ops = xbus->transport.ops; |
| 201 |
BUG_ON(!ops); |
| 202 |
spin_lock_irqsave(&xbus->transport.lock, flags); |
| 203 |
#if 0 |
| 204 |
XBUS_INFO(xbus, "%s (transport_refcount=%d)\n", |
| 205 |
__func__, atomic_read(&xbus->transport.transport_refcount)); |
| 206 |
#endif |
| 207 |
ops->free_xframe(xbus, xframe); |
| 208 |
transportops_put(xbus); |
| 209 |
spin_unlock_irqrestore(&xbus->transport.lock, flags); |
| 210 |
} |
| 211 |
|
| 212 |
static bool xframe_queue_adjust(struct xframe_queue *q) |
| 213 |
{ |
| 214 |
xbus_t *xbus; |
| 215 |
xframe_t *xframe; |
| 216 |
int delta; |
| 217 |
unsigned long flags; |
| 218 |
int ret = 0; |
| 219 |
|
| 220 |
BUG_ON(!q); |
| 221 |
xbus = q->priv; |
| 222 |
BUG_ON(!xbus); |
| 223 |
spin_lock_irqsave(&q->lock, flags); |
| 224 |
delta = q->count - q->steady_state_count; |
| 225 |
if (delta < -XFRAME_QUEUE_MARGIN) { |
| 226 |
/* Increase pool by one frame */ |
| 227 |
//XBUS_INFO(xbus, "%s(%d): Allocate one\n", q->name, delta); |
| 228 |
xframe = transport_alloc_xframe(xbus, GFP_ATOMIC); |
| 229 |
if (!xframe) { |
| 230 |
static int rate_limit; |
| 231 |
|
| 232 |
if ((rate_limit++ % 3001) == 0) |
| 233 |
XBUS_ERR(xbus, "%s: failed frame allocation\n", |
| 234 |
q->name); |
| 235 |
goto out; |
| 236 |
} |
| 237 |
if (!__xframe_enqueue(q, xframe)) { |
| 238 |
static int rate_limit; |
| 239 |
|
| 240 |
if ((rate_limit++ % 3001) == 0) |
| 241 |
XBUS_ERR(xbus, "%s: failed enqueueing frame\n", |
| 242 |
q->name); |
| 243 |
transport_free_xframe(xbus, xframe); |
| 244 |
goto out; |
| 245 |
} |
| 246 |
} else if (delta > XFRAME_QUEUE_MARGIN) { |
| 247 |
/* Decrease pool by one frame */ |
| 248 |
//XBUS_INFO(xbus, "%s(%d): Free one\n", q->name, delta); |
| 249 |
xframe = __xframe_dequeue(q); |
| 250 |
if (!xframe) { |
| 251 |
static int rate_limit; |
| 252 |
|
| 253 |
if ((rate_limit++ % 3001) == 0) |
| 254 |
XBUS_ERR(xbus, "%s: failed dequeueing frame\n", |
| 255 |
q->name); |
| 256 |
goto out; |
| 257 |
} |
| 258 |
transport_free_xframe(xbus, xframe); |
| 259 |
} |
| 260 |
ret = 1; |
| 261 |
out: |
| 262 |
spin_unlock_irqrestore(&q->lock, flags); |
| 263 |
return ret; |
| 264 |
} |
| 265 |
|
| 266 |
xframe_t *get_xframe(struct xframe_queue *q) |
| 267 |
{ |
| 268 |
xframe_t *xframe; |
| 269 |
xbus_t *xbus; |
| 270 |
|
| 271 |
BUG_ON(!q); |
| 272 |
xbus = (xbus_t *)q->priv; |
| 273 |
BUG_ON(!xbus); |
| 274 |
xframe_queue_adjust(q); |
| 275 |
xframe = xframe_dequeue(q); |
| 276 |
if (!xframe) { |
| 277 |
static int rate_limit; |
| 278 |
|
| 279 |
if ((rate_limit++ % 3001) == 0) |
| 280 |
XBUS_ERR(xbus, "%s STILL EMPTY (%d)\n", q->name, |
| 281 |
rate_limit); |
| 282 |
return NULL; |
| 283 |
} |
| 284 |
BUG_ON(xframe->xframe_magic != XFRAME_MAGIC); |
| 285 |
atomic_set(&xframe->frame_len, 0); |
| 286 |
xframe->first_free = xframe->packets; |
| 287 |
do_gettimeofday(&xframe->tv_created); |
| 288 |
/* |
| 289 |
* If later parts bother to correctly initialize their |
| 290 |
* headers, there is no need to memset() the whole data. |
| 291 |
* |
| 292 |
* ticket:403 |
| 293 |
* |
| 294 |
* memset(xframe->packets, 0, xframe->frame_maxlen); |
| 295 |
*/ |
| 296 |
//XBUS_INFO(xbus, "%s\n", __func__); |
| 297 |
return xframe; |
| 298 |
} |
| 299 |
EXPORT_SYMBOL(get_xframe); |
| 300 |
|
| 301 |
void put_xframe(struct xframe_queue *q, xframe_t *xframe) |
| 302 |
{ |
| 303 |
xbus_t *xbus; |
| 304 |
|
| 305 |
BUG_ON(!q); |
| 306 |
xbus = (xbus_t *)q->priv; |
| 307 |
BUG_ON(!xbus); |
| 308 |
//XBUS_INFO(xbus, "%s\n", __func__); |
| 309 |
BUG_ON(!TRANSPORT_EXIST(xbus)); |
| 310 |
if (unlikely(!xframe_enqueue(q, xframe))) { |
| 311 |
XBUS_ERR(xbus, "Failed returning xframe to %s\n", q->name); |
| 312 |
transport_free_xframe(xbus, xframe); |
| 313 |
return; |
| 314 |
} |
| 315 |
xframe_queue_adjust(q); |
| 316 |
} |
| 317 |
EXPORT_SYMBOL(put_xframe); |