summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAnatoly Burakov <anatoly.burakov@intel.com>2018-05-31 10:51:01 +0100
committerThomas Monjalon <thomas@monjalon.net>2018-07-13 11:27:30 +0200
commit0b82bd7b24c95ddd77f8e0b93df8456e1a38d627 (patch)
treed388c426f9752c7bd4923e3f3ce914c3ef417cb8 /test
parent68b6092bd3c70e78fa30d8c3688adb4b66bf426e (diff)
downloaddpdk-0b82bd7b24c95ddd77f8e0b93df8456e1a38d627.zip
dpdk-0b82bd7b24c95ddd77f8e0b93df8456e1a38d627.tar.gz
dpdk-0b82bd7b24c95ddd77f8e0b93df8456e1a38d627.tar.xz
memzone: improve zero-length reserve
Currently, reserving zero-length memzones is done by looking at malloc statistics, and reserving biggest sized element found in those statistics. This has two issues. First, there is a race condition. The heap is unlocked between the time we check stats, and the time we reserve malloc element for memzone. This may lead to inability to reserve the memzone we wanted to reserve, because another allocation might have taken place and biggest sized element may no longer be available. Second, the size returned by malloc statistics does not include any alignment information, which is worked around by being conservative and subtracting alignment length from the final result. This leads to fragmentation and reserving memzones that could have been bigger but aren't. Fix all of this by using earlier-introduced operation to reserve biggest possible malloc element. This, however, comes with a trade-off, because we can only lock one heap at a time. So, if we check the first available heap and find *any* element at all, that element will be considered "the biggest", even though other heaps might have bigger elements. We cannot know what other heaps have before we try and allocate it, and it is not a good idea to lock all of the heaps at the same time, so, we will just document this limitation and encourage users to reserve memzones with socket id properly set. Also, fixup unit tests to account for the new behavior. Fixes: fafcc11985a2 ("mem: rework memzone to be allocated by malloc") Signed-off-by: Anatoly Burakov <anatoly.burakov@intel.com>
Diffstat (limited to 'test')
-rw-r--r--test/test/test_memzone.c165
1 files changed, 95 insertions, 70 deletions
diff --git a/test/test/test_memzone.c b/test/test/test_memzone.c
index efcf732..452d7cc 100644
--- a/test/test/test_memzone.c
+++ b/test/test/test_memzone.c
@@ -467,61 +467,69 @@ test_memzone_reserve_flags(void)
/* Find the heap with the greatest free block size */
static size_t
-find_max_block_free_size(const unsigned _align)
+find_max_block_free_size(unsigned int align, unsigned int socket_id)
{
struct rte_malloc_socket_stats stats;
- unsigned i, align = _align;
- size_t len = 0;
+ size_t len, overhead;
- for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
- rte_malloc_get_socket_stats(i, &stats);
- if (stats.greatest_free_size > len)
- len = stats.greatest_free_size;
- }
+ rte_malloc_get_socket_stats(socket_id, &stats);
+
+ len = stats.greatest_free_size;
+ overhead = MALLOC_ELEM_OVERHEAD;
+
+ if (len == 0)
+ return 0;
- if (align < RTE_CACHE_LINE_SIZE)
- align = RTE_CACHE_LINE_ROUNDUP(align+1);
+ align = RTE_CACHE_LINE_ROUNDUP(align);
+ overhead += align;
- if (len <= MALLOC_ELEM_OVERHEAD + align)
+ if (len < overhead)
return 0;
- return len - MALLOC_ELEM_OVERHEAD - align;
+ return len - overhead;
}
static int
test_memzone_reserve_max(void)
{
- const struct rte_memzone *mz;
- size_t maxlen;
+ unsigned int i;
- maxlen = find_max_block_free_size(0);
+ for (i = 0; i < rte_socket_count(); i++) {
+ const struct rte_memzone *mz;
+ size_t maxlen;
+ int socket;
- if (maxlen == 0) {
- printf("There is no space left!\n");
- return 0;
- }
+ socket = rte_socket_id_by_idx(i);
+ maxlen = find_max_block_free_size(0, socket);
- mz = rte_memzone_reserve(TEST_MEMZONE_NAME("max_zone"), 0,
- SOCKET_ID_ANY, 0);
- if (mz == NULL){
- printf("Failed to reserve a big chunk of memory - %s\n",
- rte_strerror(rte_errno));
- rte_dump_physmem_layout(stdout);
- rte_memzone_dump(stdout);
- return -1;
- }
+ if (maxlen == 0) {
+ printf("There is no space left!\n");
+ return 0;
+ }
- if (mz->len != maxlen) {
- printf("Memzone reserve with 0 size did not return bigest block\n");
- printf("Expected size = %zu, actual size = %zu\n", maxlen, mz->len);
- rte_dump_physmem_layout(stdout);
- rte_memzone_dump(stdout);
- return -1;
- }
+ mz = rte_memzone_reserve(TEST_MEMZONE_NAME("max_zone"), 0,
+ socket, 0);
+ if (mz == NULL) {
+ printf("Failed to reserve a big chunk of memory - %s\n",
+ rte_strerror(rte_errno));
+ rte_dump_physmem_layout(stdout);
+ rte_memzone_dump(stdout);
+ return -1;
+ }
- if (rte_memzone_free(mz)) {
- printf("Fail memzone free\n");
- return -1;
+ if (mz->len != maxlen) {
+ printf("Memzone reserve with 0 size did not return bigest block\n");
+ printf("Expected size = %zu, actual size = %zu\n",
+ maxlen, mz->len);
+ rte_dump_physmem_layout(stdout);
+ rte_memzone_dump(stdout);
+ return -1;
+ }
+
+ if (rte_memzone_free(mz)) {
+ printf("Fail memzone free\n");
+ return -1;
+ }
}
return 0;
@@ -530,45 +538,62 @@ test_memzone_reserve_max(void)
static int
test_memzone_reserve_max_aligned(void)
{
- const struct rte_memzone *mz;
- size_t maxlen = 0;
+ unsigned int i;
- /* random alignment */
- rte_srand((unsigned)rte_rdtsc());
- const unsigned align = 1 << ((rte_rand() % 8) + 5); /* from 128 up to 4k alignment */
+ for (i = 0; i < rte_socket_count(); i++) {
+ const struct rte_memzone *mz;
+ size_t maxlen, minlen = 0;
+ int socket;
- maxlen = find_max_block_free_size(align);
+ socket = rte_socket_id_by_idx(i);
- if (maxlen == 0) {
- printf("There is no space left for biggest %u-aligned memzone!\n", align);
- return 0;
- }
+ /* random alignment */
+ rte_srand((unsigned int)rte_rdtsc());
+ const unsigned int align = 1 << ((rte_rand() % 8) + 5); /* from 128 up to 4k alignment */
- mz = rte_memzone_reserve_aligned(TEST_MEMZONE_NAME("max_zone_aligned"),
- 0, SOCKET_ID_ANY, 0, align);
- if (mz == NULL){
- printf("Failed to reserve a big chunk of memory - %s\n",
- rte_strerror(rte_errno));
- rte_dump_physmem_layout(stdout);
- rte_memzone_dump(stdout);
- return -1;
- }
+ /* memzone size may be between size and size - align */
+ minlen = find_max_block_free_size(align, socket);
+ maxlen = find_max_block_free_size(0, socket);
- if (mz->len != maxlen) {
- printf("Memzone reserve with 0 size and alignment %u did not return"
- " bigest block\n", align);
- printf("Expected size = %zu, actual size = %zu\n",
- maxlen, mz->len);
- rte_dump_physmem_layout(stdout);
- rte_memzone_dump(stdout);
- return -1;
- }
+ if (minlen == 0 || maxlen == 0) {
+ printf("There is no space left for biggest %u-aligned memzone!\n",
+ align);
+ return 0;
+ }
- if (rte_memzone_free(mz)) {
- printf("Fail memzone free\n");
- return -1;
- }
+ mz = rte_memzone_reserve_aligned(
+ TEST_MEMZONE_NAME("max_zone_aligned"),
+ 0, socket, 0, align);
+ if (mz == NULL) {
+ printf("Failed to reserve a big chunk of memory - %s\n",
+ rte_strerror(rte_errno));
+ rte_dump_physmem_layout(stdout);
+ rte_memzone_dump(stdout);
+ return -1;
+ }
+ if (mz->addr != RTE_PTR_ALIGN(mz->addr, align)) {
+ printf("Memzone reserve with 0 size and alignment %u did not return aligned block\n",
+ align);
+ rte_dump_physmem_layout(stdout);
+ rte_memzone_dump(stdout);
+ return -1;
+ }
+ if (mz->len < minlen || mz->len > maxlen) {
+ printf("Memzone reserve with 0 size and alignment %u did not return"
+ " bigest block\n", align);
+ printf("Expected size = %zu-%zu, actual size = %zu\n",
+ minlen, maxlen, mz->len);
+ rte_dump_physmem_layout(stdout);
+ rte_memzone_dump(stdout);
+ return -1;
+ }
+
+ if (rte_memzone_free(mz)) {
+ printf("Fail memzone free\n");
+ return -1;
+ }
+ }
return 0;
}