FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
refcount.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /*
17  * vlib provides lock-free counters but those
18  * - Have 16bits per-CPU counter, which may overflow.
19  * - Would only increment.
20  *
21  * This is very similar to vlib counters, but may be used to count reference.
22  * Such a counter includes an arbitrary number of counters. Each counter
23  * is identified by its index. This is used to aggregate per-cpu memory.
24  *
25  * Warning:
26  * This reference counter is lock-free but is not race-condition free.
27  * The counting result is approximate and another mechanism needs to be used
28  * in order to ensure that an object may be freed.
29  *
30  */
31 
32 #include <vnet/vnet.h>
33 #include <vppinfra/lock.h>
34 
35 /*
36  * Reference counting
37  * A specific reference counter is used. The design is quite
38  * similar to vlib counters but:
39  * - It is possible to decrease the value
40  * - Summing will not zero the per-thread counters
41  * - Only the thread can reallocate its own counters vector (to avoid concurrency issues)
42 */
43 typedef struct {
48 
49 typedef struct {
52 
55 {
56  clib_spinlock_lock (&counter_lock);
57 }
58 
61 {
62  clib_spinlock_unlock (&counter_lock);
63 }
64 
65 void __vlib_refcount_resize(vlib_refcount_per_cpu_t *per_cpu, u32 size);
66 
69 {
70  vlib_refcount_per_cpu_t *per_cpu = &r->per_cpu[thread_index];
71  if (PREDICT_FALSE(counter_index >= vec_len(per_cpu->counters)))
72  __vlib_refcount_resize(per_cpu, clib_max(counter_index + 16,(vec_len(per_cpu->counters)) * 2));
73 
74  per_cpu->counters[counter_index] += v;
75 }
76 
78 
81 {
84  r->per_cpu = 0;
85  vec_validate (r->per_cpu, tm->n_vlib_mains - 1);
86 
88  {
89  clib_spinlock_init (&r->per_cpu[thread_index].counter_lock);
90  }
91 }
92 
93 
clib_spinlock_init
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:65
thread_index
u32 thread_index
Definition: nat44_ei_hairpinning.c:492
clib_max
#define clib_max(x, y)
Definition: clib.h:335
CLIB_CACHE_LINE_ALIGN_MARK
#define CLIB_CACHE_LINE_ALIGN_MARK(mark)
Definition: cache.h:60
counter_index
static u32 counter_index(vlib_main_t *vm, vlib_error_t e)
Definition: drop.c:67
vlib_refcount_init
static_always_inline void vlib_refcount_init(vlib_refcount_t *r)
Definition: refcount.h:80
vlib_refcount_lock
static_always_inline void vlib_refcount_lock(clib_spinlock_t counter_lock)
Definition: refcount.h:54
r
vnet_hw_if_output_node_runtime_t * r
Definition: interface_output.c:1071
i32
signed int i32
Definition: types.h:77
vlib_thread_main_t::n_vlib_mains
u32 n_vlib_mains
Definition: threads.h:283
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
vlib_refcount_per_cpu_t
Definition: refcount.h:43
lock.h
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
vlib_refcount_unlock
static_always_inline void vlib_refcount_unlock(clib_spinlock_t counter_lock)
Definition: refcount.h:60
clib_spinlock_lock
static_always_inline void clib_spinlock_lock(clib_spinlock_t *p)
Definition: lock.h:82
static_always_inline
#define static_always_inline
Definition: clib.h:112
clib_spinlock_s
Definition: lock.h:51
vlib_refcount_per_cpu_t::counter_lock
clib_spinlock_t counter_lock
Definition: refcount.h:45
vlib_refcount_t::per_cpu
vlib_refcount_per_cpu_t * per_cpu
Definition: refcount.h:50
vec_validate
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment)
Definition: vec.h:523
size
u32 size
Definition: vhost_user.h:125
index
u32 index
Definition: flow_types.api:221
u64
unsigned long u64
Definition: types.h:89
u32
unsigned int u32
Definition: types.h:88
vlib_thread_main_t
Definition: threads.h:264
clib_spinlock_unlock
static_always_inline void clib_spinlock_unlock(clib_spinlock_t *p)
Definition: lock.h:121
vlib_refcount_get
u64 vlib_refcount_get(vlib_refcount_t *r, u32 index)
Definition: refcount.c:31
vlib_refcount_t
Definition: refcount.h:49
vnet.h
vlib_get_thread_main
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:56
vlib_refcount_add
static_always_inline void vlib_refcount_add(vlib_refcount_t *r, u32 thread_index, u32 counter_index, i32 v)
Definition: refcount.h:68
vlib_refcount_per_cpu_t::counters
u32 * counters
Definition: refcount.h:44