add FILE argument to debug functions
[dpdk.git] / lib / librte_ring / rte_ring.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 /*
35  * Derived from FreeBSD's bufring.c
36  *
37  **************************************************************************
38  *
39  * Copyright (c) 2007,2008 Kip Macy kmacy@freebsd.org
40  * All rights reserved.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions are met:
44  *
45  * 1. Redistributions of source code must retain the above copyright notice,
46  *    this list of conditions and the following disclaimer.
47  *
48  * 2. The name of Kip Macy nor the names of other
49  *    contributors may be used to endorse or promote products derived from
50  *    this software without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
53  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
56  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
57  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
58  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
59  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
60  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
61  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
62  * POSSIBILITY OF SUCH DAMAGE.
63  *
64  ***************************************************************************/
65
66 #include <stdio.h>
67 #include <stdarg.h>
68 #include <string.h>
69 #include <stdint.h>
70 #include <inttypes.h>
71 #include <errno.h>
72 #include <sys/queue.h>
73
74 #include <rte_common.h>
75 #include <rte_log.h>
76 #include <rte_memory.h>
77 #include <rte_memzone.h>
78 #include <rte_launch.h>
79 #include <rte_tailq.h>
80 #include <rte_eal.h>
81 #include <rte_eal_memconfig.h>
82 #include <rte_atomic.h>
83 #include <rte_per_lcore.h>
84 #include <rte_lcore.h>
85 #include <rte_branch_prediction.h>
86 #include <rte_errno.h>
87 #include <rte_string_fns.h>
88 #include <rte_spinlock.h>
89
90 #include "rte_ring.h"
91
92 TAILQ_HEAD(rte_ring_list, rte_ring);
93
94 /* true if x is a power of 2 */
95 #define POWEROF2(x) ((((x)-1) & (x)) == 0)
96
97 /* return the size of memory occupied by a ring */
98 ssize_t
99 rte_ring_get_memsize(unsigned count)
100 {
101         ssize_t sz;
102
103         /* count must be a power of 2 */
104         if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK )) {
105                 RTE_LOG(ERR, RING,
106                         "Requested size is invalid, must be power of 2, and "
107                         "do not exceed the size limit %u\n", RTE_RING_SZ_MASK);
108                 return -EINVAL;
109         }
110
111         sz = sizeof(struct rte_ring) + count * sizeof(void *);
112         sz = RTE_ALIGN(sz, CACHE_LINE_SIZE);
113         return sz;
114 }
115
116 int
117 rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
118         unsigned flags)
119 {
120         /* compilation-time checks */
121         RTE_BUILD_BUG_ON((sizeof(struct rte_ring) &
122                           CACHE_LINE_MASK) != 0);
123 #ifdef RTE_RING_SPLIT_PROD_CONS
124         RTE_BUILD_BUG_ON((offsetof(struct rte_ring, cons) &
125                           CACHE_LINE_MASK) != 0);
126 #endif
127         RTE_BUILD_BUG_ON((offsetof(struct rte_ring, prod) &
128                           CACHE_LINE_MASK) != 0);
129 #ifdef RTE_LIBRTE_RING_DEBUG
130         RTE_BUILD_BUG_ON((sizeof(struct rte_ring_debug_stats) &
131                           CACHE_LINE_MASK) != 0);
132         RTE_BUILD_BUG_ON((offsetof(struct rte_ring, stats) &
133                           CACHE_LINE_MASK) != 0);
134 #endif
135
136         /* init the ring structure */
137         memset(r, 0, sizeof(*r));
138         rte_snprintf(r->name, sizeof(r->name), "%s", name);
139         r->flags = flags;
140         r->prod.watermark = count;
141         r->prod.sp_enqueue = !!(flags & RING_F_SP_ENQ);
142         r->cons.sc_dequeue = !!(flags & RING_F_SC_DEQ);
143         r->prod.size = r->cons.size = count;
144         r->prod.mask = r->cons.mask = count-1;
145         r->prod.head = r->cons.head = 0;
146         r->prod.tail = r->cons.tail = 0;
147
148         return 0;
149 }
150
151 /* create the ring */
152 struct rte_ring *
153 rte_ring_create(const char *name, unsigned count, int socket_id,
154                 unsigned flags)
155 {
156         char mz_name[RTE_MEMZONE_NAMESIZE];
157         struct rte_ring *r;
158         const struct rte_memzone *mz;
159         ssize_t ring_size;
160         int mz_flags = 0;
161         struct rte_ring_list* ring_list = NULL;
162
163         /* check that we have an initialised tail queue */
164         if ((ring_list =
165              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_RING, rte_ring_list)) == NULL) {
166                 rte_errno = E_RTE_NO_TAILQ;
167                 return NULL;
168         }
169
170         ring_size = rte_ring_get_memsize(count);
171         if (ring_size < 0) {
172                 rte_errno = ring_size;
173                 return NULL;
174         }
175
176         rte_snprintf(mz_name, sizeof(mz_name), "%s%s", RTE_RING_MZ_PREFIX, name);
177
178         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
179
180         /* reserve a memory zone for this ring. If we can't get rte_config or
181          * we are secondary process, the memzone_reserve function will set
182          * rte_errno for us appropriately - hence no check in this this function */
183         mz = rte_memzone_reserve(mz_name, ring_size, socket_id, mz_flags);
184         if (mz != NULL) {
185                 r = mz->addr;
186                 /* no need to check return value here, we already checked the
187                  * arguments above */
188                 rte_ring_init(r, name, count, flags);
189                 TAILQ_INSERT_TAIL(ring_list, r, next);
190         } else {
191                 r = NULL;
192                 RTE_LOG(ERR, RING, "Cannot reserve memory\n");
193         }
194         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
195
196         return r;
197 }
198
199 /*
200  * change the high water mark. If *count* is 0, water marking is
201  * disabled
202  */
203 int
204 rte_ring_set_water_mark(struct rte_ring *r, unsigned count)
205 {
206         if (count >= r->prod.size)
207                 return -EINVAL;
208
209         /* if count is 0, disable the watermarking */
210         if (count == 0)
211                 count = r->prod.size;
212
213         r->prod.watermark = count;
214         return 0;
215 }
216
217 /* dump the status of the ring on the console */
218 void
219 rte_ring_dump(FILE *f, const struct rte_ring *r)
220 {
221 #ifdef RTE_LIBRTE_RING_DEBUG
222         struct rte_ring_debug_stats sum;
223         unsigned lcore_id;
224 #endif
225
226         fprintf(f, "ring <%s>@%p\n", r->name, r);
227         fprintf(f, "  flags=%x\n", r->flags);
228         fprintf(f, "  size=%"PRIu32"\n", r->prod.size);
229         fprintf(f, "  ct=%"PRIu32"\n", r->cons.tail);
230         fprintf(f, "  ch=%"PRIu32"\n", r->cons.head);
231         fprintf(f, "  pt=%"PRIu32"\n", r->prod.tail);
232         fprintf(f, "  ph=%"PRIu32"\n", r->prod.head);
233         fprintf(f, "  used=%u\n", rte_ring_count(r));
234         fprintf(f, "  avail=%u\n", rte_ring_free_count(r));
235         if (r->prod.watermark == r->prod.size)
236                 fprintf(f, "  watermark=0\n");
237         else
238                 fprintf(f, "  watermark=%"PRIu32"\n", r->prod.watermark);
239
240         /* sum and dump statistics */
241 #ifdef RTE_LIBRTE_RING_DEBUG
242         memset(&sum, 0, sizeof(sum));
243         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
244                 sum.enq_success_bulk += r->stats[lcore_id].enq_success_bulk;
245                 sum.enq_success_objs += r->stats[lcore_id].enq_success_objs;
246                 sum.enq_quota_bulk += r->stats[lcore_id].enq_quota_bulk;
247                 sum.enq_quota_objs += r->stats[lcore_id].enq_quota_objs;
248                 sum.enq_fail_bulk += r->stats[lcore_id].enq_fail_bulk;
249                 sum.enq_fail_objs += r->stats[lcore_id].enq_fail_objs;
250                 sum.deq_success_bulk += r->stats[lcore_id].deq_success_bulk;
251                 sum.deq_success_objs += r->stats[lcore_id].deq_success_objs;
252                 sum.deq_fail_bulk += r->stats[lcore_id].deq_fail_bulk;
253                 sum.deq_fail_objs += r->stats[lcore_id].deq_fail_objs;
254         }
255         fprintf(f, "  size=%"PRIu32"\n", r->prod.size);
256         fprintf(f, "  enq_success_bulk=%"PRIu64"\n", sum.enq_success_bulk);
257         fprintf(f, "  enq_success_objs=%"PRIu64"\n", sum.enq_success_objs);
258         fprintf(f, "  enq_quota_bulk=%"PRIu64"\n", sum.enq_quota_bulk);
259         fprintf(f, "  enq_quota_objs=%"PRIu64"\n", sum.enq_quota_objs);
260         fprintf(f, "  enq_fail_bulk=%"PRIu64"\n", sum.enq_fail_bulk);
261         fprintf(f, "  enq_fail_objs=%"PRIu64"\n", sum.enq_fail_objs);
262         fprintf(f, "  deq_success_bulk=%"PRIu64"\n", sum.deq_success_bulk);
263         fprintf(f, "  deq_success_objs=%"PRIu64"\n", sum.deq_success_objs);
264         fprintf(f, "  deq_fail_bulk=%"PRIu64"\n", sum.deq_fail_bulk);
265         fprintf(f, "  deq_fail_objs=%"PRIu64"\n", sum.deq_fail_objs);
266 #else
267         fprintf(f, "  no statistics available\n");
268 #endif
269 }
270
271 /* dump the status of all rings on the console */
272 void
273 rte_ring_list_dump(FILE *f)
274 {
275         const struct rte_ring *mp;
276         struct rte_ring_list *ring_list;
277
278         /* check that we have an initialised tail queue */
279         if ((ring_list = 
280              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_RING, rte_ring_list)) == NULL) {
281                 rte_errno = E_RTE_NO_TAILQ;
282                 return; 
283         }
284
285         rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
286
287         TAILQ_FOREACH(mp, ring_list, next) {
288                 rte_ring_dump(f, mp);
289         }
290
291         rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
292 }
293
294 /* search a ring from its name */
295 struct rte_ring *
296 rte_ring_lookup(const char *name)
297 {
298         struct rte_ring *r;
299         struct rte_ring_list *ring_list;
300
301         /* check that we have an initialized tail queue */
302         if ((ring_list = 
303              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_RING, rte_ring_list)) == NULL) {
304                 rte_errno = E_RTE_NO_TAILQ;
305                 return NULL;    
306         }
307
308         rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
309         
310         TAILQ_FOREACH(r, ring_list, next) {
311                 if (strncmp(name, r->name, RTE_RING_NAMESIZE) == 0)
312                         break;
313         }
314
315         rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
316
317         if (r == NULL)
318                 rte_errno = ENOENT;
319
320         return r;
321 }