DPDK patches and discussions
 help / color / mirror / Atom feed
From: Gowrishankar <gowrishankar.m@linux.vnet.ibm.com>
To: Sergio Gonzalez Monroy <sergio.gonzalez.monroy@intel.com>
Cc: Anatoly Burakov <anatoly.burakov@intel.com>,
	dev@dpdk.org, Thomas Monjalon <thomas@monjalon.net>,
	Gowrishankar Muthukrishnan <gowrishankar.m@linux.vnet.ibm.com>,
	stable@dpdk.org
Subject: [dpdk-dev] [PATCH 2/2] eal/malloc: fix heap index to correctly insert memseg
Date: Thu,  3 May 2018 15:41:36 +0530	[thread overview]
Message-ID: <d6a102edb265b3c93494aef1f7cf095449e8abe1.1525341819.git.gowrishankar.m@linux.vnet.ibm.com> (raw)
In-Reply-To: <cover.1525341819.git.gowrishankar.m@linux.vnet.ibm.com>
In-Reply-To: <cover.1525341819.git.gowrishankar.m@linux.vnet.ibm.com>

From: Gowrishankar Muthukrishnan <gowrishankar.m@linux.vnet.ibm.com>

When there are multiple memsegs created and adding new memseg would
cause bigger heap size, its index in free_head list should be based
on new size of heap. Currently, only the size of elem is accounted
as in malloc_elem_free_list_insert. As heap total size gets bigger,
list of those memsegs should be at the right index, so that
malloc_heap_alloc would find suitable element for the requested
memory size by applications.

Fixes: b0489e7bca ("malloc: fix linear complexity")
Cc: stable@dpdk.org

Signed-off-by: Gowrishankar Muthukrishnan <gowrishankar.m@linux.vnet.ibm.com>

---
Eg. Below heap is in one numa socket, for the size of 1G (i.e 64*16MB).
All corresponding malloc_elem are always added in heap index 8,
as their size is always 16MB (and due to which, index is also 8 always).

        free_head = {{lh_first = 0x0}, {lh_first = 0x0}, {lh_first = 0x0}, {
          lh_first = 0x0}, {lh_first = 0x0}, {lh_first = 0x0}, {lh_first = 0x0}, {
          lh_first = 0x0}, {lh_first = 0x7efd3f000000}, {lh_first = 0x0}, {lh_first = 0x0}, {
          lh_first = 0x0}, {lh_first = 0x0}},
        alloc_count = 6, total_size = 1073733632},

Ideally, this list of memsegs should ideally be at slot 12, as they grow heap for 1G.
---
 lib/librte_eal/common/malloc_heap.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/lib/librte_eal/common/malloc_heap.c b/lib/librte_eal/common/malloc_heap.c
index 1cacf7f..f686e5e 100644
--- a/lib/librte_eal/common/malloc_heap.c
+++ b/lib/librte_eal/common/malloc_heap.c
@@ -105,10 +105,32 @@
 			ms->len - MALLOC_ELEM_OVERHEAD);
 	end_elem = RTE_PTR_ALIGN_FLOOR(end_elem, RTE_CACHE_LINE_SIZE);
 	const size_t elem_size = (uintptr_t)end_elem - (uintptr_t)start_elem;
+	size_t cur_idx, new_idx, heap_size;
 
 	malloc_elem_init(start_elem, heap, ms, elem_size);
 	malloc_elem_mkend(end_elem, start_elem);
+
+	/* Compare heap index based on its current size as well as
+	 * its new size with memseg added. If new size needs new index
+	 * move its free_head to the new slot.
+	 */
+	cur_idx = malloc_elem_free_list_index(heap->total_size);
+	heap_size = heap->total_size + elem_size;
+	new_idx = malloc_elem_free_list_index(heap_size);
+	if (cur_idx != new_idx) {
+		heap->free_head[new_idx] = heap->free_head[cur_idx];
+		memset(&heap->free_head[cur_idx],
+			0, sizeof(heap->free_head[cur_idx]));
+	}
+
+	/* malloc_elem_free_list_insert calculates index based on
+	 * elem->size, hence we set elem->size as new heap size,
+	 * while inserting this elem. After that, we reset elem->size
+	 * to its original value. A minor hack though!.
+	 */
+	start_elem->size = heap_size;
 	malloc_elem_free_list_insert(start_elem);
+	start_elem->size = elem_size;
 
 	heap->total_size += elem_size;
 }
-- 
1.9.1

  parent reply	other threads:[~2018-05-03 10:11 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-03 10:11 [dpdk-dev] [PATCH 0/2] eal/malloc: fix wrong heap initialization over multiple memsegs Gowrishankar
2018-05-03 10:11 ` [dpdk-dev] [PATCH 1/2] eal/malloc: merge malloc_elems in heap if they are contiguous Gowrishankar
2018-05-04  9:29   ` Burakov, Anatoly
2018-05-04 10:41     ` gowrishankar muthukrishnan
2018-05-04 11:02       ` Burakov, Anatoly
2018-05-03 10:11 ` Gowrishankar [this message]
2018-05-18 13:10 ` [dpdk-dev] [PATCH 0/2] eal/malloc: fix wrong heap initialization over multiple memsegs Thomas Monjalon
2018-05-19  1:35   ` gowrishankar muthukrishnan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d6a102edb265b3c93494aef1f7cf095449e8abe1.1525341819.git.gowrishankar.m@linux.vnet.ibm.com \
    --to=gowrishankar.m@linux.vnet.ibm.com \
    --cc=anatoly.burakov@intel.com \
    --cc=dev@dpdk.org \
    --cc=sergio.gonzalez.monroy@intel.com \
    --cc=stable@dpdk.org \
    --cc=thomas@monjalon.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).