2 // Copyright (c) 2010-2017 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <rte_mempool.h>
18 #include <rte_version.h>
21 #include "prox_malloc.h"
22 #include "prox_port_cfg.h"
23 #include "stats_mempool.h"
25 struct stats_mempool_manager {
27 struct mempool_stats mempool_stats[0];
30 static struct stats_mempool_manager *smm;
32 struct mempool_stats *stats_get_mempool_stats(uint32_t i)
34 return &smm->mempool_stats[i];
37 int stats_get_n_mempools(void)
39 return smm->n_mempools;
42 static struct stats_mempool_manager *alloc_stats_mempool_manager(void)
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);
49 for (uint8_t i = 0; i < PROX_MAX_PORTS; ++i) {
50 if (!prox_port_cfg[i].active)
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);
60 return prox_zmalloc(mem_size, socket_id);
63 void stats_mempool_init(void)
65 uint32_t n_max_mempools = sizeof(prox_port_cfg[0].pool)/sizeof(prox_port_cfg[0].pool[0]);
67 smm = alloc_stats_mempool_manager();
68 for (uint8_t i = 0; i < PROX_MAX_PORTS; ++i) {
69 if (!prox_port_cfg[i].active)
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];
76 ms->pool = prox_port_cfg[i].pool[j];
79 ms->size = prox_port_cfg[i].pool_size[j];
86 void stats_mempool_update(void)
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);
93 smm->mempool_stats[mp_id].free = rte_mempool_count(smm->mempool_stats[mp_id].pool);