mem: reduce memory fragmentation
cover mem: reduce memory fragmentation
This patchset is mostly dealing with changes fbarray, but it is actually about reducing fragmentation in Linuxapp memalloc.
We allocate hugepages from lower VA to higher VA. However, our malloc heap allocates addresses from higher VA to lower VA. This results in a situation where, whenever new page is allocated, malloc starts to allocate memory from the top, leaving fragmented space between new allocation's leftover and previous leftover.
Over time, this leads to lots of free elements sitting at page boundaries, small enough to be useful but large enough to have an impact on memory fragmentation in certain circumstances.
To fix this, we need to allocate memory from higher VA first. However, in order to do that, we need the ability to search fbarray in reverse, which is currently not supported. Adding this support is what most of this patchset is about.
First 4 patches fix some issues in existing fbarray implementation and remove some code duplication, preparing for adding of new functionality.
Next 3 patches add new functionality - reverse search of used/free slots, mirroring already existing functionality in semantics and capable of returning identical results but in reverse order.
Patch 8 adds unit tests for fbarray, testing both existing and new functionality.
Finally, patch 9 changes memalloc to look up free slots in memseg list in reverse order. No other changes is necessary, as all other code can handle segments, wherever they are allocated.
Anatoly Burakov (9):
fbarray: fix errno values returned from functions
fbarray: reduce duplication in find_contig code
fbarray: reduce duplication in find_next_n code
fbarray: reduce duplication in find_next code
fbarray: add reverse find_free/used
fbarray: add reverse find n used/free
fbarray: add reverse find contig used/free
test: add fbarray autotests
memalloc: allocate memory in reverse
lib/librte_eal/common/eal_common_fbarray.c | 493 ++++++++++++++--- lib/librte_eal/common/include/rte_fbarray.h | 114 ++++ lib/librte_eal/linuxapp/eal/eal_memalloc.c | 3 +- lib/librte_eal/rte_eal_version.map | 6 + test/test/Makefile | 1 + test/test/test_fbarray.c | 576 ++++++++++++++++++++ 6 files changed, 1119 insertions(+), 74 deletions(-) create mode 100644 test/test/test_fbarray.c
Signature · 1 line
2.17.1
PATCH 1/9 fbarray: fix errno values returned from functions
Errno values are supposed to be positive, yet they were negative.
This changes API, so not backporting.
--- lib/librte_eal/common/eal_common_fbarray.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/librte_eal/common/eal_common_fbarray.c +2 -2
@@ -231,7 +231,7 @@ find_next_n(const struct rte_fbarray *arr, unsigned int start, unsigned int n,return MASK_GET_IDX (msk_idx, run_start); }/* we didn't find anything */ - rte_errno = used ? -ENOENT : -ENOSPC ; + rte_errno = used ?ENOENT :ENOSPC ;return -1 ; }
@@ -287,7 +287,7 @@ find_next(const struct rte_fbarray *arr, unsigned int start, bool used)return MASK_GET_IDX (idx, found); }/* we didn't find anything */ - rte_errno = used ? -ENOENT : -ENOSPC ; + rte_errno = used ?ENOENT :ENOSPC ;return -1 ; }
Signature · 1 line
2.17.1
PATCH 2/9 fbarray: reduce duplication in find_contig code
In reply to Anatoly BurakovMostly a code move, to have all code related to find_contig in one place. This slightly changes the API in that previously, calling find_contig_free() on a full fbarray would've been an error, but equivalent call to find_contig_used() on an empty array does not return an error, leading to an inconsistency in the API.
The decision was made to not treat this condition as an error, because it is equivalent to calling find_contig() on an index that just happens to be used/free, which is not an error and will return 0.
--- lib/librte_eal/common/eal_common_fbarray.c | 52 ++++++++++++---------- 1 file changed, 28 insertions(+), 24 deletions(-)
lib/librte_eal/common/eal_common_fbarray.c +28 -24
@@ -773,8 +773,8 @@ rte_fbarray_find_next_n_used(struct rte_fbarray *arr, unsigned int start,return ret; } -int __rte_experimental -rte_fbarray_find_contig_free (struct rte_fbarray *arr,unsigned int start) +static int +fbarray_find_contig (struct rte_fbarray *arr,unsigned int start,bool used) {int ret =-1 ;
@@ -786,14 +786,25 @@ rte_fbarray_find_contig_free(struct rte_fbarray *arr, unsigned int start)/* prevent array from changing under us */ rte_rwlock_read_lock (&arr->rwlock); -if (arr->len == arr->count) { - rte_errno =ENOSPC ; - goto out; - } - -if (arr->count ==0 ) { - ret = arr->len - start; - goto out; +/* cheap checks to prevent doing useless work */ +if (used) { +if (arr->count ==0 ) { + ret =0 ; + goto out; + } +if (arr->len == arr->count) { + ret = arr->len - start; + goto out; + } + }else { +if (arr->len == arr->count) { + ret =0 ; + goto out; + } +if (arr->count ==0 ) { + ret = arr->len - start; + goto out; + } } ret =find_contig (arr, start, false);
@@ -803,22 +814,15 @@ rte_fbarray_find_contig_free(struct rte_fbarray *arr, unsigned int start) } int __rte_experimental -rte_fbarray_find_contig_used (struct rte_fbarray *arr,unsigned int start) +rte_fbarray_find_contig_free (struct rte_fbarray *arr,unsigned int start) { -int ret =-1 ; - -if (arr ==NULL || start >= arr->len) { - rte_errno =EINVAL ; -return -1 ; - } - -/* prevent array from changing under us */ -rte_rwlock_read_lock (&arr->rwlock); - - ret =find_contig (arr, start, true); +return fbarray_find_contig (arr, start, false); +} -rte_rwlock_read_unlock (&arr->rwlock); -return ret; +int __rte_experimental +rte_fbarray_find_contig_used (struct rte_fbarray *arr,unsigned int start) +{ +return fbarray_find_contig (arr, start, true); }int __rte_experimental
Signature · 1 line
2.17.1
PATCH 3/9 fbarray: reduce duplication in find_next_n code
In reply to Anatoly BurakovMostly code move, aside from more quick checks done to avoid doing computations in obviously hopeless cases.
--- lib/librte_eal/common/eal_common_fbarray.c | 63 ++++++++++++---------- 1 file changed, 36 insertions(+), 27 deletions(-)
lib/librte_eal/common/eal_common_fbarray.c +36 -27
@@ -723,54 +723,63 @@ rte_fbarray_find_next_used(struct rte_fbarray *arr, unsigned int start)return ret; } -int __rte_experimental -rte_fbarray_find_next_n_free (struct rte_fbarray *arr,unsigned int start, -unsigned int n) +static int +fbarray_find_n (struct rte_fbarray *arr,unsigned int start,unsigned int n, +bool used) {int ret =-1 ; -if (arr ==NULL || start >= arr->len || n > arr->len) { +if (arr ==NULL || start >= arr->len || n > arr->len || n ==0 ) { rte_errno =EINVAL ;return -1 ; } +if (arr->len - start < n) { + rte_errno = used ?ENOENT :ENOSPC ; +return -1 ; + }/* prevent array from changing under us */ rte_rwlock_read_lock (&arr->rwlock); -if (arr->len == arr->count || arr->len - arr->count < n) { - rte_errno =ENOSPC ; - goto out; +/* cheap checks to prevent doing useless work */ +if (!used) { +if (arr->len == arr->count || arr->len - arr->count < n) { + rte_errno =ENOSPC ; + goto out; + } +if (arr->count ==0 ) { + ret = start; + goto out; + } + }else { +if (arr->count < n) { + rte_errno =ENOENT ; + goto out; + } +if (arr->count == arr->len) { + ret = start; + goto out; + } } - ret =find_next_n (arr, start, n, false); + ret =find_next_n (arr, start, n, used); out:rte_rwlock_read_unlock (&arr->rwlock);return ret; } int __rte_experimental -rte_fbarray_find_next_n_used (struct rte_fbarray *arr,unsigned int start, +rte_fbarray_find_next_n_free (struct rte_fbarray *arr,unsigned int start,unsigned int n) { -int ret =-1 ; - -if (arr ==NULL || start >= arr->len || n > arr->len) { - rte_errno =EINVAL ; -return -1 ; - } - -/* prevent array from changing under us */ -rte_rwlock_read_lock (&arr->rwlock); - -if (arr->count < n) { - rte_errno =ENOENT ; - goto out; - } +return fbarray_find_n (arr, start, n, false); +} - ret =find_next_n (arr, start, n, true); -out: -rte_rwlock_read_unlock (&arr->rwlock); -return ret; +int __rte_experimental +rte_fbarray_find_next_n_used (struct rte_fbarray *arr,unsigned int start, +unsigned int n) +{ +return fbarray_find_n (arr, start, n, true); } static int
Signature · 1 line
2.17.1
PATCH 4/9 fbarray: reduce duplication in find_next code
In reply to Anatoly BurakovJust code move to put all checks and calls in one place.
--- lib/librte_eal/common/eal_common_fbarray.c | 54 ++++++++++++---------- 1 file changed, 29 insertions(+), 25 deletions(-)
lib/librte_eal/common/eal_common_fbarray.c +29 -25
@@ -675,8 +675,8 @@ rte_fbarray_is_used(struct rte_fbarray *arr, unsigned int idx)return ret; } -int __rte_experimental -rte_fbarray_find_next_free (struct rte_fbarray *arr,unsigned int start) +static int +fbarray_find (struct rte_fbarray *arr,unsigned int start,bool used) {int ret =-1 ;
@@ -688,39 +688,43 @@ rte_fbarray_find_next_free(struct rte_fbarray *arr, unsigned int start)/* prevent array from changing under us */ rte_rwlock_read_lock (&arr->rwlock); -if (arr->len == arr->count) { - rte_errno =ENOSPC ; - goto out; +/* cheap checks to prevent doing useless work */ +if (!used) { +if (arr->len == arr->count) { + rte_errno =ENOSPC ; + goto out; + } +if (arr->count ==0 ) { + ret = start; + goto out; + } + }else { +if (arr->count ==0 ) { + rte_errno =ENOENT ; + goto out; + } +if (arr->len == arr->count) { + ret = start; + goto out; + } } - ret =find_next (arr, start, false); + ret =find_next (arr, start, used); out:rte_rwlock_read_unlock (&arr->rwlock);return ret; } int __rte_experimental -rte_fbarray_find_next_used (struct rte_fbarray *arr,unsigned int start) +rte_fbarray_find_next_free (struct rte_fbarray *arr,unsigned int start) { -int ret =-1 ; - -if (arr ==NULL || start >= arr->len) { - rte_errno =EINVAL ; -return -1 ; - } - -/* prevent array from changing under us */ -rte_rwlock_read_lock (&arr->rwlock); - -if (arr->count ==0 ) { - rte_errno =ENOENT ; - goto out; - } +return fbarray_find (arr, start, false); +} - ret =find_next (arr, start, true); -out: -rte_rwlock_read_unlock (&arr->rwlock); -return ret; +int __rte_experimental +rte_fbarray_find_next_used (struct rte_fbarray *arr,unsigned int start) +{ +return fbarray_find (arr, start, true); } static int
Signature · 1 line
2.17.1
PATCH 5/9 fbarray: add reverse find_free/used
In reply to Anatoly BurakovAdd function to look up used/free indexes starting from specified index, but going backwards instead of forward. Semantics are kept similar to the existing function, except for the fact that, given the same input, the results returned will be in reverse order.
--- lib/librte_eal/common/eal_common_fbarray.c | 78 +++++++++++++++++++-- lib/librte_eal/common/include/rte_fbarray.h | 34 +++++++++ lib/librte_eal/rte_eal_version.map | 2 + 3 files changed, 109 insertions(+), 5 deletions(-)
lib/librte_eal/common/eal_common_fbarray.c +73 -5
@@ -352,6 +352,60 @@ find_contig(const struct rte_fbarray *arr, unsigned int start, bool used)return result; } +static int +find_prev (const struct rte_fbarray *arr,unsigned int start,bool used) +{ +const struct used_mask *msk =get_used_mask (arr->data, arr->elt_sz, + arr->len); +unsigned int idx, first, first_mod; +uint64_t ignore_msk; + +/* +* mask only has granularity of MASK_ALIGN, but start may not be aligned +* on that boundary, so construct a special mask to exclude anything we +* don't want to see to avoid confusing clz. +*/ + first =MASK_LEN_TO_IDX (start); + first_mod =MASK_LEN_TO_MOD (start); +/* we're going backwards, so mask must start from the top */ + ignore_msk = first_mod ==MASK_ALIGN -1 ? +-1ULL :/* prevent overflow */ + ~(-1ULL << (first_mod +1 )); + +/* go backwards, include zero */ + idx = first; +do { +uint64_t cur = msk->data[idx]; +int found; + +/* if we're looking for free entries, invert mask */ +if (!used) + cur = ~cur; + +/* ignore everything before start on first iteration */ +if (idx == first) + cur &= ignore_msk; + +/* check if we have any entries */ +if (cur ==0 ) +continue ; + +/* +* find last set bit - that will correspond to whatever it is +* that we're looking for. we're counting trailing zeroes, thus +* the value we get is counted from end of mask, so calculate +* position from start of mask. +*/ + found =MASK_ALIGN -__builtin_clzll (cur) -1 ; + +return MASK_GET_IDX (idx, found); + }while (idx-- !=0 );/* decrement after check to include zero*/ + +/* we didn't find anything */ + rte_errno = used ?ENOENT :ENOSPC ; +return -1 ; +} +static int set_used (struct rte_fbarray *arr,unsigned int idx,bool used) {
@@ -676,7 +730,7 @@ rte_fbarray_is_used(struct rte_fbarray *arr, unsigned int idx) }static int -fbarray_find (struct rte_fbarray *arr,unsigned int start,bool used) +fbarray_find (struct rte_fbarray *arr,unsigned int start,bool next,bool used) {int ret =-1 ;
@@ -708,8 +762,10 @@ fbarray_find(struct rte_fbarray *arr, unsigned int start, bool used) goto out; } } - - ret =find_next (arr, start, used); +if (next) + ret =find_next (arr, start, used); +else + ret =find_prev (arr, start, used); out:rte_rwlock_read_unlock (&arr->rwlock);return ret;
@@ -718,13 +774,25 @@ fbarray_find(struct rte_fbarray *arr, unsigned int start, bool used) int __rte_experimentalrte_fbarray_find_next_free (struct rte_fbarray *arr,unsigned int start) { -return fbarray_find (arr, start, false); +return fbarray_find (arr, start, true, false); } int __rte_experimentalrte_fbarray_find_next_used (struct rte_fbarray *arr,unsigned int start) { -return fbarray_find (arr, start, true); +return fbarray_find (arr, start, true, true); +} + +int __rte_experimental +rte_fbarray_find_prev_free (struct rte_fbarray *arr,unsigned int start) +{ +return fbarray_find (arr, start, false, false); +} + +int __rte_experimental +rte_fbarray_find_prev_used (struct rte_fbarray *arr,unsigned int start) +{ +return fbarray_find (arr, start, false, true); } static int
lib/librte_eal/common/include/rte_fbarray.h +34 -0
@@ -336,6 +336,40 @@ rte_fbarray_find_contig_free(struct rte_fbarray *arr, int __rte_experimentalrte_fbarray_find_contig_used (struct rte_fbarray *arr,unsigned int start); +/** +* Find index of previous free element, starting at specified index. +* +* @param arr +* Valid pointer to allocated and correctly set up ``rte_fbarray`` structure. +* +* @param start +* Element index to start search from. +* +* @return +* - non-negative integer on success. +* - -1 on failure, with ``rte_errno`` indicating reason for failure. +*/ +int __rte_experimental +rte_fbarray_find_prev_free (struct rte_fbarray *arr,unsigned int start); + + +/** +* Find index of previous used element, starting at specified index. +* +* @param arr +* Valid pointer to allocated and correctly set up ``rte_fbarray`` structure. +* +* @param start +* Element index to start search from. +* +* @return +* - non-negative integer on success. +* - -1 on failure, with ``rte_errno`` indicating reason for failure. +*/ +int __rte_experimental +rte_fbarray_find_prev_used (struct rte_fbarray *arr,unsigned int start); + +/** * Dump ``rte_fbarray`` metadata.
lib/librte_eal/rte_eal_version.map +2 -0
@@ -269,6 +269,8 @@ EXPERIMENTAL { rte_fbarray_find_next_used; rte_fbarray_find_next_n_free; rte_fbarray_find_next_n_used; + rte_fbarray_find_prev_free; + rte_fbarray_find_prev_used; rte_fbarray_find_contig_free; rte_fbarray_find_contig_used; rte_fbarray_get;
Signature · 1 line
2.17.1
PATCH 6/9 fbarray: add reverse find n used/free
In reply to Anatoly BurakovAdd a function to look for N used/free slots, but going backwards instead of forwards. All semantics are kept similar to the existing function, with the difference being that given the same input, the same results will be returned in reverse order.
--- lib/librte_eal/common/eal_common_fbarray.c | 198 +++++++++++++++++++- lib/librte_eal/common/include/rte_fbarray.h | 44 +++++ lib/librte_eal/rte_eal_version.map | 2 + 3 files changed, 237 insertions(+), 7 deletions(-)
lib/librte_eal/common/eal_common_fbarray.c +191 -7
@@ -352,6 +352,169 @@ find_contig(const struct rte_fbarray *arr, unsigned int start, bool used)return result; } +static int +find_prev_n (const struct rte_fbarray *arr,unsigned int start,unsigned int n, +bool used) +{ +const struct used_mask *msk =get_used_mask (arr->data, arr->elt_sz, + arr->len); +unsigned int msk_idx, lookbehind_idx, first, first_mod; +uint64_t ignore_msk; + +/* +* mask only has granularity of MASK_ALIGN, but start may not be aligned +* on that boundary, so construct a special mask to exclude anything we +* don't want to see to avoid confusing ctz. +*/ + first =MASK_LEN_TO_IDX (start); + first_mod =MASK_LEN_TO_MOD (start); +/* we're going backwards, so mask must start from the top */ + ignore_msk = first_mod ==MASK_ALIGN -1 ? +-1ULL :/* prevent overflow */ + ~(-1ULL << (first_mod +1 )); + +/* go backwards, include zero */ + msk_idx = first; +do { +uint64_t cur_msk, lookbehind_msk; +unsigned int run_start, run_end, ctz, left; +bool found = false; +/* +* The process of getting n consecutive bits from the top for +* arbitrary n is a bit involved, but here it is in a nutshell: +* +* 1. let n be the number of consecutive bits we're looking for +* 2. check if n can fit in one mask, and if so, do n-1 +* lshift-ands to see if there is an appropriate run inside +* our current mask +* 2a. if we found a run, bail out early +* 2b. if we didn't find a run, proceed +* 3. invert the mask and count trailing zeroes (that is, count +* how many consecutive set bits we had starting from the +* start of current mask) as k +* 3a. if k is 0, continue to next mask +* 3b. if k is not 0, we have a potential run +* 4. to satisfy our requirements, next mask must have n-k +* consecutive set bits at the end, so we will do (n-k-1) +* lshift-ands and check if last bit is set. +* +* Step 4 will need to be repeated if (n-k) > MASK_ALIGN until +* we either run out of masks, lose the run, or find what we +* were looking for. +*/ + cur_msk = msk->data[msk_idx]; + left = n; + +/* if we're looking for free spaces, invert the mask */ +if (!used) + cur_msk = ~cur_msk; + +/* if we have an ignore mask, ignore once */ +if (ignore_msk) { + cur_msk &= ignore_msk; + ignore_msk =0 ; + } + +/* if n can fit in within a single mask, do a search */ +if (n <=MASK_ALIGN ) { +uint64_t tmp_msk = cur_msk; +unsigned int s_idx; +for (s_idx =0 ; s_idx < n -1 ; s_idx++) + tmp_msk &= tmp_msk <<1ULL ; +/* we found what we were looking for */ +if (tmp_msk !=0 ) { +/* clz will give us offset from end of mask, and +* we only get the end of our run, not start, +* so adjust result to point to where start +* would have been. +*/ + run_start =MASK_ALIGN - +__builtin_clzll (tmp_msk) - n; +return MASK_GET_IDX (msk_idx, run_start); + } + } + +/* +* we didn't find our run within the mask, or n > MASK_ALIGN, +* so we're going for plan B. +*/ + +/* count trailing zeroes on inverted mask */ +if (~cur_msk ==0 ) + ctz =sizeof (cur_msk) *8 ; +else + ctz =__builtin_ctzll (~cur_msk); + +/* if there aren't any runs at the start either, just +* continue +*/ +if (ctz ==0 ) +continue ; + +/* we have a partial run at the start, so try looking behind */ + run_end =MASK_GET_IDX (msk_idx, ctz); + left -= ctz; + +/* go backwards, include zero */ + lookbehind_idx = msk_idx -1 ; + +/* we can't lookbehind as we've run out of masks, so stop */ +if (msk_idx ==0 ) +break ; + +do { +const uint64_t last_bit =1ULL << (MASK_ALIGN -1 ); +unsigned int s_idx, need; + + lookbehind_msk = msk->data[lookbehind_idx]; + +/* if we're looking for free space, invert the mask */ +if (!used) + lookbehind_msk = ~lookbehind_msk; + +/* figure out how many consecutive bits we need here */ + need =RTE_MIN (left,MASK_ALIGN ); + +for (s_idx =0 ; s_idx < need -1 ; s_idx++) + lookbehind_msk &= lookbehind_msk <<1ULL ; + +/* if last bit is not set, we've lost the run */ +if ((lookbehind_msk & last_bit) ==0 ) { +/* +* we've scanned this far, so we know there are +* no runs in the space we've lookbehind-scanned +* as well, so skip that on next iteration. +*/ + ignore_msk =-1ULL << need; + msk_idx = lookbehind_idx; +break ; + } + + left -= need; + +/* check if we've found what we were looking for */ +if (left ==0 ) { + found = true; +break ; + } + }while ((lookbehind_idx--) !=0 );/* decrement after check to +* include zero +*/ + +/* we didn't find anything, so continue */ +if (!found) +continue ; + +/* we've found what we were looking for, but we only know where +* the run ended, so calculate start position. +*/ +return run_end - n; + }while (msk_idx-- !=0 );/* decrement after check to include zero */ +/* we didn't find anything */ + rte_errno = used ?ENOENT :ENOSPC ; +return -1 ; +} +static int find_prev (const struct rte_fbarray *arr,unsigned int start,bool used) {
@@ -797,7 +960,7 @@ rte_fbarray_find_prev_used(struct rte_fbarray *arr, unsigned int start)static int fbarray_find_n (struct rte_fbarray *arr,unsigned int start,unsigned int n, -bool used) +bool next,bool used) {int ret =-1 ;
@@ -805,7 +968,11 @@ fbarray_find_n(struct rte_fbarray *arr, unsigned int start, unsigned int n, rte_errno =EINVAL ;return -1 ; } -if (arr->len - start < n) { +if (next && (arr->len - start) < n) { + rte_errno = used ?ENOENT :ENOSPC ; +return -1 ; + } +if (!next && start < (n -1 )) { rte_errno = used ?ENOENT :ENOSPC ;return -1 ; }
@@ -820,7 +987,7 @@ fbarray_find_n(struct rte_fbarray *arr, unsigned int start, unsigned int n, goto out; }if (arr->count ==0 ) { - ret = start; + ret = next ? start : start - n +1 ; goto out; } }else {
@@ -829,12 +996,15 @@ fbarray_find_n(struct rte_fbarray *arr, unsigned int start, unsigned int n, goto out; }if (arr->count == arr->len) { - ret = start; + ret = next ? start : start - n +1 ; goto out; } } - ret =find_next_n (arr, start, n, used); +if (next) + ret =find_next_n (arr, start, n, used); +else + ret =find_prev_n (arr, start, n, used); out:rte_rwlock_read_unlock (&arr->rwlock);return ret;
@@ -844,14 +1014,28 @@ int __rte_experimental rte_fbarray_find_next_n_free(struct rte_fbarray *arr, unsigned int start, unsigned int n) { - return fbarray_find_n(arr, start, n, false); + return fbarray_find_n(arr, start, n, true, false); } int __rte_experimentalrte_fbarray_find_next_n_used (struct rte_fbarray *arr,unsigned int start,unsigned int n) { -return fbarray_find_n (arr, start, n, true); +return fbarray_find_n (arr, start, n, true, true); +} + +int __rte_experimental +rte_fbarray_find_prev_n_free (struct rte_fbarray *arr,unsigned int start, +unsigned int n) +{ +return fbarray_find_n (arr, start, n, false, false); +} + +int __rte_experimental +rte_fbarray_find_prev_n_used (struct rte_fbarray *arr,unsigned int start, +unsigned int n) +{ +return fbarray_find_n (arr, start, n, false, true); } static int
lib/librte_eal/common/include/rte_fbarray.h +44 -0
50 lines not on this page · raw message
lib/librte_eal/rte_eal_version.map +2 -0
@@ -271,6 +271,8 @@ EXPERIMENTAL { rte_fbarray_find_next_n_used; rte_fbarray_find_prev_free; rte_fbarray_find_prev_used; + rte_fbarray_find_prev_n_free; + rte_fbarray_find_prev_n_used; rte_fbarray_find_contig_free; rte_fbarray_find_contig_used; rte_fbarray_get;
Signature · 1 line
2.17.1
PATCH 7/9 fbarray: add reverse find contig used/free
In reply to Anatoly BurakovAdd a function to return starting point of current contiguous block, going backwards. All semantics are kept the same as the existing function, with the only difference being that given the same input, results will be returned in reverse order.
--- lib/librte_eal/common/eal_common_fbarray.c | 90 +++++++++++++++++++-- lib/librte_eal/common/include/rte_fbarray.h | 36 +++++++++ lib/librte_eal/rte_eal_version.map | 2 + 3 files changed, 122 insertions(+), 6 deletions(-)
lib/librte_eal/common/eal_common_fbarray.c +84 -6
@@ -569,6 +569,60 @@ find_prev(const struct rte_fbarray *arr, unsigned int start, bool used)return -1 ; } +static int +find_rev_contig (const struct rte_fbarray *arr,unsigned int start,bool used) +{ +const struct used_mask *msk =get_used_mask (arr->data, arr->elt_sz, + arr->len); +unsigned int idx, first, first_mod; +unsigned int need_len, result =0 ; + + first =MASK_LEN_TO_IDX (start); + first_mod =MASK_LEN_TO_MOD (start); + +/* go backwards, include zero */ + idx = first; +do { +uint64_t cur = msk->data[idx]; +unsigned int run_len; + + need_len =MASK_ALIGN ; + +/* if we're looking for free entries, invert mask */ +if (!used) + cur = ~cur; + +/* ignore everything after start on first iteration */ +if (idx == first) { +unsigned int end_len =MASK_ALIGN - first_mod -1 ; + cur <<= end_len; +/* at the start, we don't need the full mask len */ + need_len -= end_len; + } + +/* we will be looking for zeroes, so invert the mask */ + cur = ~cur; + +/* if mask is zero, we have a complete run */ +if (cur ==0 ) + goto endloop; + +/* +* see where run ends, starting from the end. +*/ + run_len =__builtin_clzll (cur); + +/* add however many zeroes we've had in the last run and quit */ +if (run_len < need_len) { + result += run_len; +break ; + } +endloop: + result += need_len; + }while (idx-- !=0 );/* decrement after check to include zero */ +return result; +} +static int set_used (struct rte_fbarray *arr,unsigned int idx,bool used) {
@@ -1039,7 +1093,8 @@ rte_fbarray_find_prev_n_used(struct rte_fbarray *arr, unsigned int start, }static int -fbarray_find_contig (struct rte_fbarray *arr,unsigned int start,bool used) +fbarray_find_contig (struct rte_fbarray *arr,unsigned int start,bool next, +bool used) {int ret =-1 ;
@@ -1057,22 +1112,33 @@ fbarray_find_contig(struct rte_fbarray *arr, unsigned int start, bool used) ret =0 ; goto out; } -if (arr->len == arr->count) { +if (next && arr->count == arr->len) { ret = arr->len - start; goto out; } +if (!next && arr->count == arr->len) { + ret = start +1 ; + goto out; + } }else {if (arr->len == arr->count) { ret =0 ; goto out; } -if (arr->count ==0 ) { +if (next && arr->count ==0 ) { ret = arr->len - start; goto out; } +if (!next && arr->count ==0 ) { + ret = start +1 ; + goto out; + } } - ret =find_contig (arr, start, false); +if (next) + ret =find_contig (arr, start, used); +else + ret =find_rev_contig (arr, start, used); out:rte_rwlock_read_unlock (&arr->rwlock);return ret;
@@ -1081,13 +1147,25 @@ fbarray_find_contig(struct rte_fbarray *arr, unsigned int start, bool used) int __rte_experimentalrte_fbarray_find_contig_free (struct rte_fbarray *arr,unsigned int start) { -return fbarray_find_contig (arr, start, false); +return fbarray_find_contig (arr, start, true, false); } int __rte_experimentalrte_fbarray_find_contig_used (struct rte_fbarray *arr,unsigned int start) { -return fbarray_find_contig (arr, start, true); +return fbarray_find_contig (arr, start, true, true); +} + +int __rte_experimental +rte_fbarray_find_rev_contig_free (struct rte_fbarray *arr,unsigned int start) +{ +return fbarray_find_contig (arr, start, false, false); +} + +int __rte_experimental +rte_fbarray_find_rev_contig_used (struct rte_fbarray *arr,unsigned int start) +{ +return fbarray_find_contig (arr, start, false, true); }int __rte_experimental
lib/librte_eal/common/include/rte_fbarray.h +36 -0
@@ -414,6 +414,42 @@ rte_fbarray_find_prev_n_used(struct rte_fbarray *arr, unsigned int start, unsigned int n); +/** +* Find how many more free entries there are before specified index (like +* ``rte_fbarray_find_contig_free`` but going in reverse). +* +* @param arr +* Valid pointer to allocated and correctly set up ``rte_fbarray`` structure. +* +* @param start +* Element index to start search from. +* +* @return +* - non-negative integer on success. +* - -1 on failure, with ``rte_errno`` indicating reason for failure. +*/ +int __rte_experimental +rte_fbarray_find_rev_contig_free (struct rte_fbarray *arr, +unsigned int start); + + +/** +* Find how many more used entries there are before specified index (like +* ``rte_fbarray_find_contig_used`` but going in reverse). +* +* @param arr +* Valid pointer to allocated and correctly set up ``rte_fbarray`` structure. +* +* @param start +* Element index to start search from. +* +* @return +* - non-negative integer on success. +* - -1 on failure, with ``rte_errno`` indicating reason for failure. +*/ +int __rte_experimental +rte_fbarray_find_rev_contig_used (struct rte_fbarray *arr,unsigned int start); +/** * Dump ``rte_fbarray`` metadata.
lib/librte_eal/rte_eal_version.map +2 -0
@@ -275,6 +275,8 @@ EXPERIMENTAL { rte_fbarray_find_prev_n_used; rte_fbarray_find_contig_free; rte_fbarray_find_contig_used; + rte_fbarray_find_rev_contig_free; + rte_fbarray_find_rev_contig_used; rte_fbarray_get; rte_fbarray_init; rte_fbarray_is_used;
Signature · 1 line
2.17.1
PATCH 8/9 test: add fbarray autotests
In reply to Anatoly BurakovIntroduce a suite of autotests to cover functionality of fbarray. This will check for invalid parameters, check API return values and errno codes, and will also do some basic functionality checks on the indexing code.
--- test/test/Makefile | 1 + test/test/test_fbarray.c | 576 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 577 insertions(+) create mode 100644 test/test/test_fbarray.c
test/test/Makefile +1 -0
@@ -70,6 +70,7 @@ SRCS-y += test_memzone.c SRCS-y += test_bitmap.c SRCS-y += test_reciprocal_division.c SRCS-y += test_reciprocal_division_perf.c +SRCS-y += test_fbarray.c SRCS-y += test_ring.c SRCS-y += test_ring_perf.c
test/test/test_fbarray.c new file +576 -0
576 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH 9/9 memalloc: allocate memory in reverse
In reply to Anatoly BurakovCurrently, all hugepages are allocated from lower VA address to higher VA address, while malloc heap allocates from higher VA address to lower VA address. This results in heap fragmentation over time due to multiple reserves leaving small space below the allocated elements.
Fix this by allocating VA memory from the top, thereby reducing fragmentation and lowering overall memory usage.
--- lib/librte_eal/linuxapp/eal/eal_memalloc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/librte_eal/linuxapp/eal/eal_memalloc.c +2 -1
@@ -682,7 +682,8 @@ alloc_seg_walk(const struct rte_memseg_list *msl, void *arg) need = wa->n_segs;/* try finding space in memseg list */ - cur_idx =rte_fbarray_find_next_n_free (&cur_msl->memseg_arr,0 , need); + cur_idx =rte_fbarray_find_prev_n_free (&cur_msl->memseg_arr, + cur_msl->memseg_arr.len -1 , need);if (cur_idx <0 )return 0 ; start_idx = cur_idx;
Signature · 1 line
2.17.1
Re: [PATCH 9/9] memalloc: allocate memory in reverse
Quoted 20 lines · Anatoly Burakov
Currently, all hugepages are allocated from lower VA address to higher VA address, while malloc heap allocates from higher VA address to lower VA address. This results in heap fragmentation over time due to multiple reserves leaving small space below the allocated elements.
Fix this by allocating VA memory from the top, thereby reducing fragmentation and lowering overall memory usage.
Signed-off-by Anatoly Burakov <[email protected]>--- lib/librte_eal/linuxapp/eal/eal_memalloc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)lib/librte_eal/linuxapp/eal/eal_memalloc.c +0
-0
lib/librte_eal/linuxapp/eal/eal_memalloc.c alloc_seg_walk(const struct rte_memseg_list *msl, void *arg) · line 689
Quoted 9 lines
lib/librte_eal/linuxapp/eal/eal_memalloc.c +2
-1@@ -682,7 +682,8 @@ alloc_seg_walk(const struct rte_memseg_list *msl, void *arg) need = wa->n_segs;/* try finding space in memseg list */ - cur_idx =rte_fbarray_find_next_n_free (&cur_msl->memseg_arr,0 , need); + cur_idx =rte_fbarray_find_prev_n_free (&cur_msl->memseg_arr, + cur_msl->memseg_arr.len -1 , need);if (cur_idx <0 )return 0 ; start_idx = cur_idx;
Hi Thomas,
We have discovered a few regressions in virtio/vhost use cases due to this patch. Virtio expects to map all segments starting from 0 address, and this patch breaks that assumption.
Can we revert this for 18.08? It will be reintroduced in 18.11 along with fixes for virtio/vhost fixes to account for this change.
Signature · 2 lines
Thanks, Anatoly
Re: [PATCH 9/9] memalloc: allocate memory in reverse
24/07/2018 12:10, Burakov, Anatoly:
Quoted 33 lines · Anatoly Burakov
Quoted 20 lines
Currently, all hugepages are allocated from lower VA address to higher VA address, while malloc heap allocates from higher VA address to lower VA address. This results in heap fragmentation over time due to multiple reserves leaving small space below the allocated elements.
Fix this by allocating VA memory from the top, thereby reducing fragmentation and lowering overall memory usage.
Signed-off-by Anatoly Burakov <[email protected]>--- lib/librte_eal/linuxapp/eal/eal_memalloc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)lib/librte_eal/linuxapp/eal/eal_memalloc.c +0
-0lib/librte_eal/linuxapp/eal/eal_memalloc.c alloc_seg_walk(const struct rte_memseg_list *msl, void *arg) · line 689
Quoted 9 lines
lib/librte_eal/linuxapp/eal/eal_memalloc.c +2
-1@@ -682,7 +682,8 @@ alloc_seg_walk(const struct rte_memseg_list *msl, void *arg) need = wa->n_segs;/* try finding space in memseg list */ - cur_idx =rte_fbarray_find_next_n_free (&cur_msl->memseg_arr,0 , need); + cur_idx =rte_fbarray_find_prev_n_free (&cur_msl->memseg_arr, + cur_msl->memseg_arr.len -1 , need);if (cur_idx <0 )return 0 ; start_idx = cur_idx;Hi Thomas,
Quoted 6 lines
We have discovered a few regressions in virtio/vhost use cases due to this patch. Virtio expects to map all segments starting from 0 address, and this patch breaks that assumption.
Can we revert this for 18.08? It will be reintroduced in 18.11 along with fixes for virtio/vhost fixes to account for this change.
Yes, please send the patch with explanations.
Thomas Monjalon
Applied11/06/2018 22:55, Anatoly Burakov:
Quoted 33 lines
This patchset is mostly dealing with changes fbarray, but it is actually about reducing fragmentation in Linuxapp memalloc.
We allocate hugepages from lower VA to higher VA. However, our malloc heap allocates addresses from higher VA to lower VA. This results in a situation where, whenever new page is allocated, malloc starts to allocate memory from the top, leaving fragmented space between new allocation's leftover and previous leftover.
Over time, this leads to lots of free elements sitting at page boundaries, small enough to be useful but large enough to have an impact on memory fragmentation in certain circumstances.
To fix this, we need to allocate memory from higher VA first. However, in order to do that, we need the ability to search fbarray in reverse, which is currently not supported. Adding this support is what most of this patchset is about.
First 4 patches fix some issues in existing fbarray implementation and remove some code duplication, preparing for adding of new functionality.
Next 3 patches add new functionality - reverse search of used/free slots, mirroring already existing functionality in semantics and capable of returning identical results but in reverse order.
Patch 8 adds unit tests for fbarray, testing both existing and new functionality.
Finally, patch 9 changes memalloc to look up free slots in memseg list in reverse order. No other changes is necessary, as all other code can handle segments, wherever they are allocated.
Quoted 10 lines
Anatoly Burakov (9):
fbarray: fix errno values returned from functions
fbarray: reduce duplication infind_contigcode
fbarray: reduce duplication infind_next_ncode
fbarray: reduce duplication infind_nextcode
fbarray: add reverse find_free/used
fbarray: add reverse find n used/free
fbarray: add reverse find contig used/free
test: add fbarray autotests
memalloc: allocate memory in reverse
Applied, thanks