From 602c9ca33a4ee58e53cc43c51f081b0e13c7232b Mon Sep 17 00:00:00 2001 From: Intel Date: Tue, 23 Jul 2013 00:00:00 +0200 Subject: [PATCH] sched: bitmap is now dynamically allocated Signed-off-by: Intel --- config/defconfig_i686-default-linuxapp-gcc | 1 - config/defconfig_i686-default-linuxapp-icc | 1 - config/defconfig_x86_64-default-linuxapp-gcc | 1 - config/defconfig_x86_64-default-linuxapp-icc | 1 - lib/librte_sched/rte_bitmap.h | 115 ++++++++++++++----- lib/librte_sched/rte_sched.c | 23 ++-- 6 files changed, 100 insertions(+), 42 deletions(-) diff --git a/config/defconfig_i686-default-linuxapp-gcc b/config/defconfig_i686-default-linuxapp-gcc index cf10dba9fe..f8219297fc 100644 --- a/config/defconfig_i686-default-linuxapp-gcc +++ b/config/defconfig_i686-default-linuxapp-gcc @@ -248,7 +248,6 @@ CONFIG_RTE_SCHED_RED=n CONFIG_RTE_SCHED_COLLECT_STATS=n CONFIG_RTE_SCHED_SUBPORT_TC_OV=n CONFIG_RTE_SCHED_PORT_N_GRINDERS=8 -CONFIG_RTE_BITMAP_ARRAY1_SIZE=16 # # Compile librte_kni diff --git a/config/defconfig_i686-default-linuxapp-icc b/config/defconfig_i686-default-linuxapp-icc index 5139a23c03..ad01e06727 100644 --- a/config/defconfig_i686-default-linuxapp-icc +++ b/config/defconfig_i686-default-linuxapp-icc @@ -249,7 +249,6 @@ CONFIG_RTE_SCHED_RED=n CONFIG_RTE_SCHED_COLLECT_STATS=n CONFIG_RTE_SCHED_SUBPORT_TC_OV=n CONFIG_RTE_SCHED_PORT_N_GRINDERS=8 -CONFIG_RTE_BITMAP_ARRAY1_SIZE=16 # # Compile librte_kni diff --git a/config/defconfig_x86_64-default-linuxapp-gcc b/config/defconfig_x86_64-default-linuxapp-gcc index 7b143dda3c..88de741c58 100644 --- a/config/defconfig_x86_64-default-linuxapp-gcc +++ b/config/defconfig_x86_64-default-linuxapp-gcc @@ -249,7 +249,6 @@ CONFIG_RTE_SCHED_RED=n CONFIG_RTE_SCHED_COLLECT_STATS=n CONFIG_RTE_SCHED_SUBPORT_TC_OV=n CONFIG_RTE_SCHED_PORT_N_GRINDERS=8 -CONFIG_RTE_BITMAP_ARRAY1_SIZE=16 # # Compile the test application diff --git a/config/defconfig_x86_64-default-linuxapp-icc b/config/defconfig_x86_64-default-linuxapp-icc index b92c3dfc0b..1a0eab0c38 100644 --- a/config/defconfig_x86_64-default-linuxapp-icc +++ b/config/defconfig_x86_64-default-linuxapp-icc @@ -249,7 +249,6 @@ CONFIG_RTE_SCHED_RED=n CONFIG_RTE_SCHED_COLLECT_STATS=n CONFIG_RTE_SCHED_SUBPORT_TC_OV=n CONFIG_RTE_SCHED_PORT_N_GRINDERS=8 -CONFIG_RTE_BITMAP_ARRAY1_SIZE=16 # # Compile librte_kni diff --git a/lib/librte_sched/rte_bitmap.h b/lib/librte_sched/rte_bitmap.h index c52db32a78..f5c610bc70 100644 --- a/lib/librte_sched/rte_bitmap.h +++ b/lib/librte_sched/rte_bitmap.h @@ -65,6 +65,7 @@ extern "C" { * ***/ +#include #include #include #include @@ -77,11 +78,6 @@ extern "C" { #include #endif -/** Number of elements in array1. Each element in array1 is a 64-bit slab. */ -#ifndef RTE_BITMAP_ARRAY1_SIZE -#define RTE_BITMAP_ARRAY1_SIZE 16 -#endif - /* Slab */ #define RTE_BITMAP_SLAB_BIT_SIZE 64 #define RTE_BITMAP_SLAB_BIT_SIZE_LOG2 6 @@ -98,7 +94,8 @@ extern "C" { /** Bitmap data structure */ struct rte_bitmap { - uint64_t array1[RTE_BITMAP_ARRAY1_SIZE]; /**< Bitmap array1 */ + /* Context for array1 and array2 */ + uint64_t *array1; /**< Bitmap array1 */ uint64_t *array2; /**< Bitmap array2 */ uint32_t array1_size; /**< Number of 64-bit slabs in array1 that are actually used */ uint32_t array2_size; /**< Number of 64-bit slabs in array2 */ @@ -108,12 +105,15 @@ struct rte_bitmap { uint32_t offset1; /**< Bitmap scan: Offset of current bit within current array1 slab */ uint32_t index2; /**< Bitmap scan: Index of current array2 slab */ uint32_t go2; /**< Bitmap scan: Go/stop condition for current array2 cache line */ -} __rte_cache_aligned; + + /* Storage space for array1 and array2 */ + uint8_t memory[0]; +}; static inline void __rte_bitmap_index1_inc(struct rte_bitmap *bmp) { - bmp->index1 = (bmp->index1 + 1) & (RTE_BITMAP_ARRAY1_SIZE - 1); + bmp->index1 = (bmp->index1 + 1) & (bmp->array1_size - 1); } static inline uint64_t @@ -165,10 +165,42 @@ rte_bsf64(uint64_t slab, uint32_t *pos) #endif +static inline uint32_t +__rte_bitmap_get_memory_footprint(uint32_t n_bits, + uint32_t *array1_byte_offset, uint32_t *array1_slabs, + uint32_t *array2_byte_offset, uint32_t *array2_slabs) +{ + uint32_t n_slabs_context, n_slabs_array1, n_cache_lines_context_and_array1; + uint32_t n_cache_lines_array2; + uint32_t n_bytes_total; + + n_cache_lines_array2 = (n_bits + RTE_BITMAP_CL_BIT_SIZE - 1) / RTE_BITMAP_CL_BIT_SIZE; + n_slabs_array1 = (n_cache_lines_array2 + RTE_BITMAP_SLAB_BIT_SIZE - 1) / RTE_BITMAP_SLAB_BIT_SIZE; + n_slabs_array1 = rte_align32pow2(n_slabs_array1); + n_slabs_context = (sizeof(struct rte_bitmap) + (RTE_BITMAP_SLAB_BIT_SIZE / 8) - 1) / (RTE_BITMAP_SLAB_BIT_SIZE / 8); + n_cache_lines_context_and_array1 = (n_slabs_context + n_slabs_array1 + RTE_BITMAP_CL_SLAB_SIZE - 1) / RTE_BITMAP_CL_SLAB_SIZE; + n_bytes_total = (n_cache_lines_context_and_array1 + n_cache_lines_array2) * CACHE_LINE_SIZE; + + if (array1_byte_offset) { + *array1_byte_offset = n_slabs_context * (RTE_BITMAP_SLAB_BIT_SIZE / 8); + } + if (array1_slabs) { + *array1_slabs = n_slabs_array1; + } + if (array2_byte_offset) { + *array2_byte_offset = n_cache_lines_context_and_array1 * CACHE_LINE_SIZE; + } + if (array2_slabs) { + *array2_slabs = n_cache_lines_array2 * RTE_BITMAP_CL_SLAB_SIZE; + } + + return n_bytes_total; +} + static inline void __rte_bitmap_scan_init(struct rte_bitmap *bmp) { - bmp->index1 = RTE_BITMAP_ARRAY1_SIZE - 1; + bmp->index1 = bmp->array1_size - 1; bmp->offset1 = RTE_BITMAP_SLAB_BIT_SIZE - 1; __rte_bitmap_index2_set(bmp); bmp->index2 += RTE_BITMAP_CL_SLAB_SIZE; @@ -176,6 +208,24 @@ __rte_bitmap_scan_init(struct rte_bitmap *bmp) bmp->go2 = 0; } +/** + * Bitmap memory footprint calculation + * + * @param n_bits + * Number of bits in the bitmap + * @return + * Bitmap memory footprint measured in bytes on success, 0 on error + */ +static inline uint32_t +rte_bitmap_get_memory_footprint(uint32_t n_bits) { + /* Check input arguments */ + if (n_bits == 0) { + return 0; + } + + return __rte_bitmap_get_memory_footprint(n_bits, NULL, NULL, NULL, NULL); +} + /** * Bitmap initialization * @@ -188,32 +238,41 @@ __rte_bitmap_scan_init(struct rte_bitmap *bmp) * @return * 0 upon success, error code otherwise */ -static inline int -rte_bitmap_init(struct rte_bitmap *bmp, uint8_t *array2, uint32_t n_bits) +static inline struct rte_bitmap * +rte_bitmap_init(uint32_t n_bits, uint8_t *mem, uint32_t mem_size) { - uint32_t array1_size, array2_size; + struct rte_bitmap *bmp; + uint32_t array1_byte_offset, array1_slabs, array2_byte_offset, array2_slabs; + uint32_t size; /* Check input arguments */ - if ((bmp == NULL) || - (array2 == NULL) || (((uintptr_t) array2) & CACHE_LINE_MASK) || - (n_bits == 0) || (n_bits & RTE_BITMAP_CL_BIT_MASK)){ - return -1; + if (n_bits == 0) { + return NULL; } - - array2_size = n_bits / RTE_BITMAP_SLAB_BIT_SIZE; - array1_size = ((n_bits / RTE_BITMAP_CL_BIT_SIZE) + (RTE_BITMAP_SLAB_BIT_SIZE - 1)) / RTE_BITMAP_SLAB_BIT_SIZE; - if (array1_size > RTE_BITMAP_ARRAY1_SIZE){ - return -1; + + if ((mem == NULL) || (((uintptr_t) mem) & CACHE_LINE_MASK)) { + return NULL; + } + + size = __rte_bitmap_get_memory_footprint(n_bits, + &array1_byte_offset, &array1_slabs, + &array2_byte_offset, &array2_slabs); + if (size < mem_size) { + return NULL; } /* Setup bitmap */ - memset(bmp, 0, sizeof(struct rte_bitmap)); - bmp->array2 = (uint64_t *) array2; - bmp->array1_size = array1_size; - bmp->array2_size = array2_size; + memset(mem, 0, size); + bmp = (struct rte_bitmap *) mem; + + bmp->array1 = (uint64_t *) &mem[array1_byte_offset]; + bmp->array1_size = array1_slabs; + bmp->array2 = (uint64_t *) &mem[array2_byte_offset]; + bmp->array2_size = array2_slabs; + __rte_bitmap_scan_init(bmp); - return 0; + return bmp; } /** @@ -244,7 +303,7 @@ rte_bitmap_free(struct rte_bitmap *bmp) static inline void rte_bitmap_reset(struct rte_bitmap *bmp) { - memset(bmp->array1, 0, sizeof(bmp->array1)); + memset(bmp->array1, 0, bmp->array1_size * sizeof(uint64_t)); memset(bmp->array2, 0, bmp->array2_size * sizeof(uint64_t)); __rte_bitmap_scan_init(bmp); } @@ -419,7 +478,7 @@ __rte_bitmap_scan_search(struct rte_bitmap *bmp) bmp->offset1 = 0; /* Look for another array1 slab */ - for (i = 0; i < RTE_BITMAP_ARRAY1_SIZE; i ++, __rte_bitmap_index1_inc(bmp)) { + for (i = 0; i < bmp->array1_size; i ++, __rte_bitmap_index1_inc(bmp)) { value1 = bmp->array1[bmp->index1]; if (rte_bsf64(value1, &bmp->offset1)) { diff --git a/lib/librte_sched/rte_sched.c b/lib/librte_sched/rte_sched.c index daa1a0d9e3..a75b4b9c81 100644 --- a/lib/librte_sched/rte_sched.c +++ b/lib/librte_sched/rte_sched.c @@ -249,7 +249,7 @@ struct rte_sched_port { uint32_t pipe_exhaustion; /* Bitmap */ - struct rte_bitmap bmp; + struct rte_bitmap *bmp; uint32_t grinder_base_bmp_pos[RTE_SCHED_PORT_N_GRINDERS] __rte_aligned_16; /* Grinders */ @@ -408,7 +408,7 @@ rte_sched_port_get_array_base(struct rte_sched_port_params *params, enum rte_sch uint32_t size_queue = n_queues_per_port * sizeof(struct rte_sched_queue); uint32_t size_queue_extra = n_queues_per_port * sizeof(struct rte_sched_queue_extra); uint32_t size_pipe_profiles = RTE_SCHED_PIPE_PROFILES_PER_PORT * sizeof(struct rte_sched_pipe_profile); - uint32_t size_bmp_array = n_queues_per_port / 8; + uint32_t size_bmp_array = rte_bitmap_get_memory_footprint(n_queues_per_port); uint32_t size_per_pipe_queue_array, size_queue_array; uint32_t base, i; @@ -606,7 +606,7 @@ rte_sched_port_config(struct rte_sched_port_params *params) { struct rte_sched_port *port = NULL; const struct rte_memzone *mz = NULL; - uint32_t mem_size, i; + uint32_t mem_size, bmp_mem_size, n_queues_per_port, i; /* Check user parameters. Determine the amount of memory to allocate */ mem_size = rte_sched_port_get_memory_footprint(params); @@ -686,7 +686,10 @@ rte_sched_port_config(struct rte_sched_port_params *params) rte_sched_port_config_pipe_profile_table(port, params); /* Bitmap */ - if (rte_bitmap_init(&port->bmp, port->bmp_array, rte_sched_port_queues_per_port(port)) != 0) { + n_queues_per_port = rte_sched_port_queues_per_port(port); + bmp_mem_size = rte_bitmap_get_memory_footprint(n_queues_per_port); + port->bmp = rte_bitmap_init(n_queues_per_port, port->bmp_array, bmp_mem_size); + if (port->bmp == NULL) { RTE_LOG(INFO, SCHED, "Bitmap init error\n"); return NULL; } @@ -704,7 +707,7 @@ rte_sched_port_free(struct rte_sched_port *port) if (port == NULL){ return; } - rte_bitmap_free(&port->bmp); + rte_bitmap_free(port->bmp); return; } @@ -1106,7 +1109,7 @@ debug_pipe_is_empty(struct rte_sched_port *port, uint32_t pindex) for (i = 0; i < 16; i ++){ uint32_t queue_empty = rte_sched_port_queue_is_empty(port, qindex + i); - uint32_t bmp_bit_clear = (rte_bitmap_get(&port->bmp, qindex + i) == 0); + uint32_t bmp_bit_clear = (rte_bitmap_get(port->bmp, qindex + i) == 0); if (queue_empty != bmp_bit_clear){ rte_panic("Queue status mismatch for queue %u of pipe %u\n", i, pindex); @@ -1182,7 +1185,7 @@ rte_sched_port_enqueue_qwa_prefetch0(struct rte_sched_port *port, uint32_t qinde q_qw = qbase + (q->qw & (qsize - 1)); rte_prefetch0(q_qw); - rte_bitmap_prefetch0(&port->bmp, qindex); + rte_bitmap_prefetch0(port->bmp, qindex); } static inline int @@ -1211,7 +1214,7 @@ rte_sched_port_enqueue_qwa(struct rte_sched_port *port, uint32_t qindex, struct q->qw ++; /* Activate queue in the port bitmap */ - rte_bitmap_set(&port->bmp, qindex); + rte_bitmap_set(port->bmp, qindex); /* Statistics */ #ifdef RTE_SCHED_COLLECT_STATS @@ -1638,7 +1641,7 @@ grinder_schedule(struct rte_sched_port *port, uint32_t pos) if (queue->qr == queue->qw) { uint32_t qindex = grinder->qindex[grinder->qpos]; - rte_bitmap_clear(&port->bmp, qindex); + rte_bitmap_clear(port->bmp, qindex); grinder->qmask &= ~(1 << grinder->qpos); grinder->wrr_mask[grinder->qpos] = 0; rte_sched_port_set_queue_empty_timestamp(port, qindex); @@ -1804,7 +1807,7 @@ grinder_next_pipe(struct rte_sched_port *port, uint32_t pos) uint32_t bmp_pos = 0; /* Get another non-empty pipe group */ - if (unlikely(rte_bitmap_scan(&port->bmp, &bmp_pos, &bmp_slab) <= 0)) { + if (unlikely(rte_bitmap_scan(port->bmp, &bmp_pos, &bmp_slab) <= 0)) { return 0; } -- 2.20.1