blob: a7b525d67beec21cd1494f59a9859733189889f7 [file] [log] [blame]
Pierre Pfister041eacc2016-08-04 16:13:09 +01001/*
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
Hongjun Nibf4be572017-11-13 20:34:06 +080016#include <vnet/util/refcount.h>
Pierre Pfister041eacc2016-08-04 16:13:09 +010017
18void __vlib_refcount_resize(vlib_refcount_per_cpu_t *per_cpu, u32 size)
19{
20 u32 *new_counter = 0, *old_counter;
21 vec_validate(new_counter, size);
Hongjun Nibf4be572017-11-13 20:34:06 +080022 vlib_refcount_lock(per_cpu->counter_lock);
23 memcpy(new_counter, per_cpu->counters, vec_len(per_cpu->counters)*4);
Pierre Pfister041eacc2016-08-04 16:13:09 +010024 old_counter = per_cpu->counters;
25 per_cpu->counters = new_counter;
Hongjun Nibf4be572017-11-13 20:34:06 +080026 vlib_refcount_unlock(per_cpu->counter_lock);
Pierre Pfister041eacc2016-08-04 16:13:09 +010027 CLIB_MEMORY_BARRIER();
Pierre Pfister041eacc2016-08-04 16:13:09 +010028 vec_free(old_counter);
29}
30
31u64 vlib_refcount_get(vlib_refcount_t *r, u32 index)
32{
33 u64 count = 0;
34 vlib_thread_main_t *tm = vlib_get_thread_main ();
Damjan Marion586afd72017-04-05 19:18:20 +020035 u32 thread_index;
36 for (thread_index = 0; thread_index < tm->n_vlib_mains; thread_index++) {
Hongjun Nibf4be572017-11-13 20:34:06 +080037 vlib_refcount_lock(r->per_cpu[thread_index].counter_lock);
38 if (index < vec_len(r->per_cpu[thread_index].counters))
39 {
40 count += r->per_cpu[thread_index].counters[index];
41 }
42 vlib_refcount_unlock(r->per_cpu[thread_index].counter_lock);
Pierre Pfister041eacc2016-08-04 16:13:09 +010043 }
44 return count;
45}
46