Support packets in flight
[samplevnf.git] / VNFs / DPPD-PROX / stats_mempool.c
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #include <rte_mempool.h>
18 #include <rte_version.h>
19 #include <inttypes.h>
20
21 #include "prox_malloc.h"
22 #include "prox_port_cfg.h"
23 #include "stats_mempool.h"
24
25 struct stats_mempool_manager {
26         uint32_t n_mempools;
27         struct mempool_stats mempool_stats[0];
28 };
29
30 static struct stats_mempool_manager *smm;
31
32 struct mempool_stats *stats_get_mempool_stats(uint32_t i)
33 {
34         return &smm->mempool_stats[i];
35 }
36
37 int stats_get_n_mempools(void)
38 {
39         return smm->n_mempools;
40 }
41
42 static struct stats_mempool_manager *alloc_stats_mempool_manager(void)
43 {
44         const uint32_t socket_id = rte_lcore_to_socket_id(rte_lcore_id());
45         uint32_t n_max_mempools = sizeof(prox_port_cfg[0].pool)/sizeof(prox_port_cfg[0].pool[0]);
46         uint32_t n_mempools = 0;
47         size_t mem_size = sizeof(struct stats_mempool_manager);
48
49         for (uint8_t i = 0; i < PROX_MAX_PORTS; ++i) {
50                 if (!prox_port_cfg[i].active)
51                         continue;
52
53                 for (uint8_t j = 0; j < n_max_mempools; ++j) {
54                         if (prox_port_cfg[i].pool[j] && prox_port_cfg[i].pool_size[j]) {
55                                 mem_size += sizeof(struct mempool_stats);
56                         }
57                 }
58         }
59
60         return prox_zmalloc(mem_size, socket_id);
61 }
62
63 void stats_mempool_init(void)
64 {
65         uint32_t n_max_mempools = sizeof(prox_port_cfg[0].pool)/sizeof(prox_port_cfg[0].pool[0]);
66
67         smm = alloc_stats_mempool_manager();
68         for (uint8_t i = 0; i < PROX_MAX_PORTS; ++i) {
69                 if (!prox_port_cfg[i].active)
70                         continue;
71
72                 for (uint8_t j = 0; j < n_max_mempools; ++j) {
73                         if (prox_port_cfg[i].pool[j] && prox_port_cfg[i].pool_size[j]) {
74                                 struct mempool_stats *ms = &smm->mempool_stats[smm->n_mempools];
75
76                                 ms->pool = prox_port_cfg[i].pool[j];
77                                 ms->port = i;
78                                 ms->queue = j;
79                                 ms->size = prox_port_cfg[i].pool_size[j];
80                                 smm->n_mempools++;
81                         }
82                 }
83         }
84 }
85
86 void stats_mempool_update(void)
87 {
88         for (uint8_t mp_id = 0; mp_id < smm->n_mempools; ++mp_id) {
89                 /* Note: The function free_count returns the number of used entries. */
90 #if RTE_VERSION >= RTE_VERSION_NUM(17,5,0,0)
91                 smm->mempool_stats[mp_id].free = rte_mempool_avail_count(smm->mempool_stats[mp_id].pool);
92 #else
93                 smm->mempool_stats[mp_id].free = rte_mempool_count(smm->mempool_stats[mp_id].pool);
94 #endif
95         }
96 }