treewide: Refactor heap related implementation
Subsystems (6)
Kuan-Wei Chiu
maintainerv3 cover treewide: Refactor heap related implementation
This patch series focuses on several adjustments related to heap implementation. Firstly, a type-safe interface has been added to the min_heap, along with the introduction of several new functions to enhance its functionality. Additionally, the heap implementation for bcache and bcachefs has been replaced with the generic min_heap implementation from include/linux. Furthermore, several typos have been corrected.
Previous discussion with Kent Overstreet:
https://lkml.kernel.org/ioyfizrzq7w7mjrqcadtzsfgpuntowtjdw5pgn4qhvsdp4mqqg@nrlek5vmisbu
Regards,
Kuan-Wei
---
You can preview this patch series on the 'refactor-heap-v3' branch of the repository at the following link:
https://github.com/visitorckw/linux.git
Changes in v3:
- Avoid heap->heap.nr to eliminate the nested types.
- Add MIN_HEAP_PREALLOCATED macro for preallocating some elements.
- Use min_heap_sift_up() in min_heap_push().
- Fix a bug in heap_del() where we should copy the last element to 'data + idx * element_size' instead of 'data'.
- Add testcases for heap_del().
- Fix bugs in bcache/bcachefs patches where the parameter types in some compare functions should have been 'type **', but were mistakenly written as 'type *'.
Changes in v2:
- Add attribute __always_unused to the compare and swap functions that do not use the args parameter.
- Rename min_heapify() to min_heap_sift_down().
- Update lib/test_min_heap.c to use min_heap_init().
- Refine the commit message for bcache and bcachefs.
- Adjust the order of patches in the patch series.
Link to v2: https://lore.kernel.org/[email protected]
Link to v1: https://lkml.kernel.org/[email protected]
Kuan-Wei Chiu (17):
perf/core: Fix several typos
bcache: Fix typo
bcachefs: Fix typo
lib min_heap: Add type safe interface
lib min_heap: Add min_heap_init()
lib min_heap: Add min_heap_peek()
lib min_heap: Add min_heap_full()
lib min_heap: Add args for min_heap_callbacks
lib min_heap: Add min_heap_sift_up()
lib min_heap: Add min_heap_del()
lib min_heap: Update min_heap_push() and min_heap_pop() to return bool
values
lib min_heap: Rename min_heapify() to min_heap_sift_down() lib min_heap: Update min_heap_push() to use min_heap_sift_up() lib/test_min_heap: Use min_heap_init() for initializing lib/test_min_heap: Add test for heap_del()
bcache: Remove heap-related macros and switch to generic min_heap
bcachefs: Remove heap-related macros and switch to generic min_heap
drivers/md/bcache/alloc.c | 64 ++++++++--- drivers/md/bcache/bcache.h | 2 +- drivers/md/bcache/bset.c | 84 ++++++++++----- drivers/md/bcache/bset.h | 14 +-- drivers/md/bcache/btree.c | 17 ++- drivers/md/bcache/extents.c | 53 ++++++---- drivers/md/bcache/movinggc.c | 41 +++++-- drivers/md/bcache/sysfs.c | 2 + drivers/md/bcache/util.c | 2 +- drivers/md/bcache/util.h | 67 +----------- drivers/md/bcache/writeback.c | 3 + drivers/md/dm-vdo/repair.c | 25 +++-- drivers/md/dm-vdo/slab-depot.c | 20 ++-- fs/bcachefs/clock.c | 43 ++++++-- fs/bcachefs/clock_types.h | 2 +- fs/bcachefs/ec.c | 76 ++++++++----- fs/bcachefs/ec_types.h | 2 +- fs/bcachefs/util.c | 2 +- fs/bcachefs/util.h | 118 +-------------------- include/linux/min_heap.h | 188 +++++++++++++++++++++++++-------- kernel/events/core.c | 41 ++++--- lib/test_min_heap.c | 94 +++++++++++------ 22 files changed, 537 insertions(+), 423 deletions(-)
Signature · 1 line
2.34.1
Kuan-Wei Chiu
maintainerPATCH v3 1/17 perf/core: Fix several typos
Replace 'artifically' with 'artificially'. Replace 'irrespecive' with 'irrespective'. Replace 'futher' with 'further'.
Replace 'sufficent' with 'sufficient'.
--- kernel/events/core.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/events/core.c +4 -4
@@ -534,7 +534,7 @@ void perf_sample_event_took(u64 sample_len_ns)__this_cpu_write (running_sample_length, running_len);/* -* Note: this will be biased artifically low until we have +* Note: this will be biased artificially low until we have * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us * from having to maintain a count. */
@@ -596,10 +596,10 @@ static inline u64 perf_event_clock(struct perf_event *event)* * Event groups make things a little more complicated, but not terribly so. The * rules for a group are that if the group leader is OFF the entire group is -* OFF, irrespecive of what the group member states are. This results in +* OFF, irrespective of what the group member states are. This results in * __perf_effective_state(). * -* A futher ramification is that when a group leader flips between OFF and +* A further ramification is that when a group leader flips between OFF and * !OFF, we need to update all group member times. * *
@@ -891,7 +891,7 @@ static int perf_cgroup_ensure_storage(struct perf_event *event,int cpu, heap_size, ret =0 ;/* -* Allow storage to have sufficent space for an iterator for each +* Allow storage to have sufficient space for an iterator for each * possibly nested cgroup plus an iterator for events with no cgroup. */ for (heap_size =1 ; css; css = css->parent)
Signature · 1 line
2.34.1
Re: [PATCH v3 01/17] perf/core: Fix several typos
Quoted 7 lines · Kuan-Wei Chiu
Replace 'artifically' with 'artificially'. Replace 'irrespecive' with 'irrespective'. Replace 'futher' with 'further'.
Replace 'sufficent' with 'sufficient'.Signed-off-by Kuan-Wei Chiu <[email protected]>Reviewed-by Ian Rogers <[email protected]>
Thanks.
Quoted 39 lines
--- kernel/events/core.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)kernel/events/core.c +4
-4@@ -534,7 +534,7 @@ void perf_sample_event_took(u64 sample_len_ns)__this_cpu_write (running_sample_length, running_len);/* -* Note: this will be biased artifically low until we have +* Note: this will be biased artificially low until we have * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us * from having to maintain a count. */ @@ -596,10 +596,10 @@ static inline u64 perf_event_clock(struct perf_event *event)* * Event groups make things a little more complicated, but not terribly so. The * rules for a group are that if the group leader is OFF the entire group is -* OFF, irrespecive of what the group member states are. This results in +* OFF, irrespective of what the group member states are. This results in * __perf_effective_state(). * -* A futher ramification is that when a group leader flips between OFF and +* A further ramification is that when a group leader flips between OFF and * !OFF, we need to update all group member times. * * @@ -891,7 +891,7 @@ static int perf_cgroup_ensure_storage(struct perf_event *event,int cpu, heap_size, ret =0 ;/* -* Allow storage to have sufficent space for an iterator for each +* Allow storage to have sufficient space for an iterator for each * possibly nested cgroup plus an iterator for events with no cgroup. */ for (heap_size =1 ; css; css = css->parent)
Signature · 1 line
#Randy
Kuan-Wei Chiu
maintainerPATCH v3 2/17 bcache: Fix typo
In reply to Kuan-Wei ChiuReplace 'utiility' with 'utility'.
--- drivers/md/bcache/util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/bcache/util.c +1 -1
@@ -1,6 +1,6 @@// SPDX-License-Identifier: GPL-2.0 /* -* random utiility code, for bcache but in theory not specific to bcache +* random utility code, for bcache but in theory not specific to bcache * * Copyright 2010, 2011 Kent Overstreet <[email protected]> * Copyright 2012 Google, Inc.
Signature · 1 line
2.34.1
Re: [PATCH v3 02/17] bcache: Fix typo
Quoted 4 lines · Kuan-Wei Chiu
Replace 'utiility' with 'utility'.
Signed-off-by Kuan-Wei Chiu <[email protected]>Reviewed-by Ian Rogers <[email protected]>
Thanks.
Quoted 16 lines
--- drivers/md/bcache/util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/md/bcache/util.c +1
-1@@ -1,6 +1,6 @@// SPDX-License-Identifier: GPL-2.0 /* -* random utiility code, for bcache but in theory not specific to bcache +* random utility code, for bcache but in theory not specific to bcache * * Copyright 2010, 2011 Kent Overstreet <[email protected]> * Copyright 2012 Google, Inc.
Signature · 1 line
#Randy
Kuan-Wei Chiu
maintainerPATCH v3 3/17 bcachefs: Fix typo
In reply to Kuan-Wei ChiuReplace 'utiility' with 'utility'.
--- fs/bcachefs/util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/bcachefs/util.c +1 -1
@@ -1,6 +1,6 @@// SPDX-License-Identifier: GPL-2.0 /* -* random utiility code, for bcache but in theory not specific to bcache +* random utility code, for bcache but in theory not specific to bcache * * Copyright 2010, 2011 Kent Overstreet <[email protected]> * Copyright 2012 Google, Inc.
Signature · 1 line
2.34.1
Re: [PATCH v3 03/17] bcachefs: Fix typo
Quoted 4 lines · Kuan-Wei Chiu
Replace 'utiility' with 'utility'.
Signed-off-by Kuan-Wei Chiu <[email protected]>Reviewed-by Ian Rogers <[email protected]>
Thanks.
Quoted 16 lines
--- fs/bcachefs/util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/bcachefs/util.c +1
-1@@ -1,6 +1,6 @@// SPDX-License-Identifier: GPL-2.0 /* -* random utiility code, for bcache but in theory not specific to bcache +* random utility code, for bcache but in theory not specific to bcache * * Copyright 2010, 2011 Kent Overstreet <[email protected]> * Copyright 2012 Google, Inc.
Signature · 1 line
#Randy
Kuan-Wei Chiu
maintainerPATCH v3 4/17 lib min_heap: Add type safe interface
In reply to Kuan-Wei ChiuImplement a type-safe interface for min_heap using strong type pointers instead of void * in the data field. This change includes adding small macro wrappers around functions, enabling the use of __minheap_cast and __minheap_obj_size macros for type casting and obtaining element size. This implementation removes the necessity of passing element size in min_heap_callbacks. Additionally, introduce the MIN_HEAP_PREALLOCATED macro for preallocating some elements.
---
Changes in v3:
- Avoid heap->heap.nr to eliminate the nested types.
- Add MIN_HEAP_PREALLOCATED macro for preallocating some elements.
drivers/md/dm-vdo/repair.c | 15 +++---- drivers/md/dm-vdo/slab-depot.c | 11 ++--- include/linux/min_heap.h | 79 ++++++++++++++++++++++------------ kernel/events/core.c | 23 +++++----- lib/test_min_heap.c | 37 ++++++++-------- 5 files changed, 90 insertions(+), 75 deletions(-)
drivers/md/dm-vdo/repair.c +7 -8
@@ -51,6 +51,8 @@ struct recovery_point {bool increment_applied; }; +MIN_HEAP(struct numbered_block_mapping, replay_heap); +struct repair_completion {/* The completion header */ struct vdo_completion completion;
@@ -97,7 +99,7 @@ struct repair_completion {* order, then original journal order. This permits efficient iteration over the journal * entries in order. */ -struct min_heap replay_heap; +struct replay_heap replay_heap;/* Fields tracking progress through the journal entries. */ struct numbered_block_mapping *current_entry;struct numbered_block_mapping *current_unfetched_entry;
@@ -163,14 +165,13 @@ static void swap_mappings(void *item1, void *item2) }static const struct min_heap_callbacks repair_min_heap = { - .elem_size =sizeof (struct numbered_block_mapping ), .less = mapping_is_less_than, .swp = swap_mappings, };static struct numbered_block_mapping *sort_next_heap_element (struct repair_completion *repair) { -struct min_heap *heap = &repair->replay_heap; +struct replay_heap *heap = &repair->replay_heap;struct numbered_block_mapping *last;if (heap->nr ==0 )
@@ -1117,11 +1118,9 @@ static void recover_block_map(struct vdo_completion *completion)* Organize the journal entries into a binary heap so we can iterate over them in sorted * order incrementally, avoiding an expensive sort call. */ - repair->replay_heap = (struct min_heap ) { - .data = repair->entries, - .nr = repair->block_map_entry_count, - .size = repair->block_map_entry_count, - }; + repair->replay_heap.data = repair->entries; + repair->replay_heap.nr = repair->block_map_entry_count; + repair->replay_heap.size = repair->block_map_entry_count;min_heapify_all (&repair->replay_heap, &repair_min_heap); vdo_log_info("Replaying %zu recovery entries into block map",
drivers/md/dm-vdo/slab-depot.c +4 -7
@@ -3309,7 +3309,6 @@ static void swap_slab_statuses(void *item1, void *item2) }static const struct min_heap_callbacks slab_status_min_heap = { - .elem_size =sizeof (struct slab_status ), .less = slab_status_is_less_than, .swp = swap_slab_statuses, };
@@ -3509,7 +3508,7 @@ static int get_slab_statuses(struct block_allocator *allocator,static int __must_check vdo_prepare_slabs_for_allocation (struct block_allocator *allocator) {struct slab_status current_slab_status; -struct min_heap heap; + MIN_HEAP(struct slab_status, heap) heap;int result;struct slab_status *slab_statuses;struct slab_depot *depot = allocator->depot;
@@ -3521,11 +3520,9 @@ static int __must_check vdo_prepare_slabs_for_allocation(struct block_allocatorreturn result;/* Sort the slabs by cleanliness, then by emptiness hint. */ - heap = (struct min_heap ) { - .data = slab_statuses, - .nr = allocator->slab_count, - .size = allocator->slab_count, - }; + heap.data = slab_statuses; + heap.nr = allocator->slab_count; + heap.size = allocator->slab_count;min_heapify_all (&heap, &slab_status_min_heap);while (heap.nr >0 ) {
include/linux/min_heap.h +51 -28
171 lines not on this page · raw message
kernel/events/core.c +10 -13
@@ -3698,13 +3698,14 @@ static void swap_ptr(void *l, void *r)swap (*lp, *rp); } +MIN_HEAP(struct perf_event *, perf_event_min_heap); +static const struct min_heap_callbacks perf_min_heap = { - .elem_size =sizeof (struct perf_event *), .less = perf_less_group_idx, .swp = swap_ptr, }; -static void __heap_add (struct min_heap *heap,struct perf_event *event) +static void __heap_add (struct perf_event_min_heap *heap,struct perf_event *event) {struct perf_event **itrs = heap->data;
@@ -3738,7 +3739,7 @@ static noinline int visit_groups_merge(struct perf_event_context *ctx,struct perf_cpu_context *cpuctx =NULL ;/* Space for per CPU and/or any CPU event iterators. */ struct perf_event *itrs[2 ]; -struct min_heap event_heap; +struct perf_event_min_heap event_heap;struct perf_event **evt;int ret;
@@ -3747,11 +3748,9 @@ static noinline int visit_groups_merge(struct perf_event_context *ctx,if (!ctx->task) { cpuctx =this_cpu_ptr (&perf_cpu_context); - event_heap = (struct min_heap ){ - .data = cpuctx->heap, - .nr =0 , - .size = cpuctx->heap_size, - }; + event_heap.data = cpuctx->heap; + event_heap.nr =0 ; + event_heap.size = cpuctx->heap_size;lockdep_assert_held (&cpuctx->ctx.lock);
@@ -3760,11 +3759,9 @@ static noinline int visit_groups_merge(struct perf_event_context *ctx, css = &cpuctx->cgrp->css;#endif }else { - event_heap = (struct min_heap ){ - .data = itrs, - .nr =0 , - .size =ARRAY_SIZE (itrs), - }; + event_heap.data = itrs; + event_heap.nr =0 ; + event_heap.size =ARRAY_SIZE (itrs);/* Events not within a CPU context may be on any CPU. */ __heap_add (&event_heap,perf_event_groups_first (groups,-1 , pmu,NULL )); }
lib/test_min_heap.c +18 -19
@@ -11,6 +11,8 @@#include <linux/printk.h> #include <linux/random.h> +MIN_HEAP (int, min_heap_test); +static __init bool less_than (const void *lhs,const void *rhs) {return *(int *)lhs < *(int *)rhs;
@@ -30,7 +32,7 @@ static __init void swap_ints(void *lhs, void *rhs) }static __init int pop_verify_heap (bool min_heap, -struct min_heap *heap, +struct min_heap_test *heap,const struct min_heap_callbacks *funcs) {int *values = heap->data;
@@ -63,13 +65,12 @@ static __init int test_heapify_all(bool min_heap) {int values[] = {3 ,1 ,2 ,4 ,0x8000000 ,0x7FFFFFF ,0 ,-3 ,-1 ,-2 ,-4 ,0x8000000 ,0x7FFFFFF }; -struct min_heap heap = { - .data = values, - .nr =ARRAY_SIZE (values), - .size =ARRAY_SIZE (values), - }; +struct min_heap_test heap; + + heap.data = values; + heap.nr =ARRAY_SIZE (values); + heap.size =ARRAY_SIZE (values);struct min_heap_callbacks funcs = { - .elem_size =sizeof (int ), .less = min_heap ? less_than : greater_than, .swp = swap_ints, };
@@ -96,13 +97,12 @@ static __init int test_heap_push(bool min_heap)const int data[] = {3 ,1 ,2 ,4 ,0x80000000 ,0x7FFFFFFF ,0 ,-3 ,-1 ,-2 ,-4 ,0x80000000 ,0x7FFFFFFF };int values[ARRAY_SIZE (data)]; -struct min_heap heap = { - .data = values, - .nr =0 , - .size =ARRAY_SIZE (values), - }; +struct min_heap_test heap; + + heap.data = values; + heap.nr =0 ; + heap.size =ARRAY_SIZE (values);struct min_heap_callbacks funcs = { - .elem_size =sizeof (int ), .less = min_heap ? less_than : greater_than, .swp = swap_ints, };
@@ -129,13 +129,12 @@ static __init int test_heap_pop_push(bool min_heap)const int data[] = {3 ,1 ,2 ,4 ,0x80000000 ,0x7FFFFFFF ,0 ,-3 ,-1 ,-2 ,-4 ,0x80000000 ,0x7FFFFFFF };int values[ARRAY_SIZE (data)]; -struct min_heap heap = { - .data = values, - .nr =0 , - .size =ARRAY_SIZE (values), - }; +struct min_heap_test heap; + + heap.data = values; + heap.nr =0 ; + heap.size =ARRAY_SIZE (values);struct min_heap_callbacks funcs = { - .elem_size =sizeof (int ), .less = min_heap ? less_than : greater_than, .swp = swap_ints, };
Signature · 1 line
2.34.1
Peter Zijlstra
maintainerRe: [PATCH v3 04/17] lib min_heap: Add type safe interface
Quoted 43 lines · Kuan-Wei Chiu
include/linux/min_heap.h +11
-12-struct min_heap { -void *data; -int nr; -int size; -}; +#define MIN_HEAP_PREALLOCATED (_type, _name, _nr) \ +struct _name { \ + int nr; \ + int size; \ + _type *data; \ + _type preallocated[_nr]; \ +}@@ -3738,7 +3739,7 @@ static noinline int visit_groups_merge(struct perf_event_context *ctx,struct perf_cpu_context *cpuctx =NULL ;/* Space for per CPU and/or any CPU event iterators. */ struct perf_event *itrs[2 ]; -struct min_heap event_heap; +struct perf_event_min_heap event_heap;struct perf_event **evt;int ret;@@ -3747,11 +3748,9 @@ static noinline int visit_groups_merge(struct perf_event_context *ctx,if (!ctx->task) { cpuctx =this_cpu_ptr (&perf_cpu_context); - event_heap = (struct min_heap ){ - .data = cpuctx->heap, - .nr =0 , - .size = cpuctx->heap_size, - }; + event_heap.data = cpuctx->heap; + event_heap.nr =0 ; + event_heap.size = cpuctx->heap_size;lockdep_assert_held (&cpuctx->ctx.lock);@@ -3760,11 +3759,9 @@ static noinline int visit_groups_merge(struct perf_event_context *ctx, css = &cpuctx->cgrp->css;#endif }else { - event_heap = (struct min_heap){
include/linux/min_heap.h static noinline int visit_groups_merge(struct perf_event_context *ctx, · line 3767
Quoted 10 lines
include/linux/min_heap.h +3
-4- .data = itrs, - .nr = 0, - .size = ARRAY_SIZE(itrs), - }; + event_heap.data = itrs; + event_heap.nr =0 ; + event_heap.size =ARRAY_SIZE (itrs);/* Events not within a CPU context may be on any CPU. */ __heap_add (&event_heap,perf_event_groups_first (groups,-1 , pmu,NULL )); }
Not too happy about these. If you ever add more fields they will go uninitialized. Why not keep the existing form and fix the struct name?
Kuan-Wei Chiu
maintainerRe: [PATCH v3 04/17] lib min_heap: Add type safe interface
Quoted 50 lines · Peter Zijlstra
Quoted 48 lines · Kuan-Wei Chiu
include/linux/min_heap.h +12
-16-struct min_heap { -void *data; -int nr; -int size; -}; +#define MIN_HEAP_PREALLOCATED (_type, _name, _nr) \ +struct _name { \ + int nr; \ + int size; \ + _type *data; \ + _type preallocated[_nr]; \ +}@@ -3738,7 +3739,7 @@ static noinline int visit_groups_merge(struct perf_event_context *ctx,struct perf_cpu_context *cpuctx =NULL ;/* Space for per CPU and/or any CPU event iterators. */ struct perf_event *itrs[2 ]; -struct min_heap event_heap; +struct perf_event_min_heap event_heap;struct perf_event **evt;int ret;@@ -3747,11 +3748,9 @@ static noinline int visit_groups_merge(struct perf_event_context *ctx,if (!ctx->task) { cpuctx =this_cpu_ptr (&perf_cpu_context); - event_heap = (struct min_heap ){ - .data = cpuctx->heap, - .nr =0 , - .size = cpuctx->heap_size, - }; + event_heap.data = cpuctx->heap; + event_heap.nr =0 ; + event_heap.size = cpuctx->heap_size;lockdep_assert_held (&cpuctx->ctx.lock);@@ -3760,11 +3759,9 @@ static noinline int visit_groups_merge(struct perf_event_context *ctx, css = &cpuctx->cgrp->css;#endif }else { - event_heap = (struct min_heap ){ - .data = itrs, - .nr =0 , - .size =ARRAY_SIZE (itrs), - }; + event_heap.data = itrs;
Quoted 8 lines
kernel/events/core.c static noinline int visit_groups_merge(struct perf_event_context *ctx, · line 3767
Quoted 5 lines
kernel/events/core.c +2
-0+ event_heap.nr =0 ; + event_heap.size =ARRAY_SIZE (itrs);/* Events not within a CPU context may be on any CPU. */ __heap_add (&event_heap,perf_event_groups_first (groups,-1 , pmu,NULL )); }Not too happy about these. If you ever add more fields they will go uninitialized. Why not keep the existing form and fix the struct name?
Sorry for the late reply. I'm traveling.
I'll change that back in v4.
Regards,
Kuan-Wei
Kuan-Wei Chiu
maintainerPATCH v3 5/17 lib min_heap: Add min_heap_init()
In reply to Kuan-Wei ChiuAdd min_heap_init() for initializing heap with data, nr, and size.
---
Changes in v3:
- If the 'data' parameter is NULL, we now make the data pointer in the heap point to the preallocated.
include/linux/min_heap.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
include/linux/min_heap.h +15 -0
@@ -38,6 +38,21 @@ struct min_heap_callbacks {void (*swp)(void *lhs,void *rhs); }; +/* Initialize a min-heap. */ +static __always_inline +void __min_heap_init(min_heap_char *heap, void *data, int size) +{ + heap->nr =0 ; + heap->size = size; +if (data) + heap->data = data; +else + heap->data = heap->preallocated; +} + +#define min_heap_init (_heap, _data, _size) \ + __min_heap_init((min_heap_char *)_heap, _data, _size) +/* Sift the element at pos down the heap. */ static __always_inline void __min_heapify(min_heap_char *heap, int pos, size_t elem_size,
Signature · 1 line
2.34.1
Kuan-Wei Chiu
maintainerPATCH v3 6/17 lib min_heap: Add min_heap_peek()
In reply to Kuan-Wei ChiuAdd min_heap_peek() function to retrieve a pointer to the smallest element. The pointer is cast to the appropriate type of heap elements.
--- include/linux/min_heap.h | 10 ++++++++++ 1 file changed, 10 insertions(+)
include/linux/min_heap.h +10 -0
@@ -53,6 +53,16 @@ void __min_heap_init(min_heap_char *heap, void *data, int size)#define min_heap_init (_heap, _data, _size) \ __min_heap_init((min_heap_char *)_heap, _data, _size) +/* Get the minimum element from the heap. */ +static __always_inline +void *__min_heap_peek(struct min_heap_char *heap) +{ +return heap->nr ? heap->data :NULL ; +} + +#define min_heap_peek (_heap) \ + (__minheap_cast(_heap) __min_heap_peek((min_heap_char *)_heap)) +/* Sift the element at pos down the heap. */ static __always_inline void __min_heapify(min_heap_char *heap, int pos, size_t elem_size,
Signature · 1 line
2.34.1
Kuan-Wei Chiu
maintainerPATCH v3 7/17 lib min_heap: Add min_heap_full()
In reply to Kuan-Wei ChiuAdd min_heap_full() which returns a boolean value indicating whether the heap has reached its maximum capacity.
--- include/linux/min_heap.h | 10 ++++++++++ 1 file changed, 10 insertions(+)
include/linux/min_heap.h +10 -0
@@ -63,6 +63,16 @@ void *__min_heap_peek(struct min_heap_char *heap)#define min_heap_peek (_heap) \ (__minheap_cast(_heap) __min_heap_peek((min_heap_char *)_heap)) +/* Check if the heap is full. */ +static __always_inline +bool __min_heap_full(min_heap_char *heap) +{ +return heap->nr == heap->size; +} + +#define min_heap_full (_heap) \ + __min_heap_full((min_heap_char *)_heap) +/* Sift the element at pos down the heap. */ static __always_inline void __min_heapify(min_heap_char *heap, int pos, size_t elem_size,
Signature · 1 line
2.34.1
Kuan-Wei Chiu
maintainerPATCH v3 8/17 lib min_heap: Add args for min_heap_callbacks
In reply to Kuan-Wei ChiuAdd a third parameter 'args' for the 'less' and 'swp' functions in the 'struct min_heap_callbacks'. This additional parameter allows these comparison and swap functions to handle extra arguments when necessary.
--- drivers/md/dm-vdo/repair.c | 10 +++---- drivers/md/dm-vdo/slab-depot.c | 9 +++--- include/linux/min_heap.h | 51 +++++++++++++++++----------------- kernel/events/core.c | 10 +++---- lib/test_min_heap.c | 26 ++++++++--------- 5 files changed, 54 insertions(+), 52 deletions(-)
drivers/md/dm-vdo/repair.c +5 -5
@@ -137,7 +137,7 @@ struct repair_completion {* to sort by slot while still ensuring we replay all entries with the same slot in the exact order * as they appeared in the journal. */ -static bool mapping_is_less_than (const void *item1,const void *item2) +static bool mapping_is_less_than (const void *item1,const void *item2,void __always_unused *args) {const struct numbered_block_mapping *mapping1 = (const struct numbered_block_mapping *) item1;
@@ -156,7 +156,7 @@ static bool mapping_is_less_than(const void *item1, const void *item2)return 0 ; } -static void swap_mappings (void *item1,void *item2) +static void swap_mappings (void *item1,void *item2,void __always_unused *args) {struct numbered_block_mapping *mapping1 = item1;struct numbered_block_mapping *mapping2 = item2;
@@ -182,8 +182,8 @@ static struct numbered_block_mapping *sort_next_heap_element(struct repair_compl* restore the heap invariant, and return a pointer to the popped element. */ last = &repair->entries[--heap->nr]; -swap_mappings (heap->data, last); -min_heapify (heap,0 , &repair_min_heap); +swap_mappings (heap->data, last,NULL ); +min_heapify (heap,0 , &repair_min_heap,NULL );return last; }
@@ -1121,7 +1121,7 @@ static void recover_block_map(struct vdo_completion *completion) repair->replay_heap.data = repair->entries; repair->replay_heap.nr = repair->block_map_entry_count; repair->replay_heap.size = repair->block_map_entry_count; -min_heapify_all (&repair->replay_heap, &repair_min_heap); +min_heapify_all (&repair->replay_heap, &repair_min_heap,NULL );vdo_log_info ("Replaying %zu recovery entries into block map" , repair->block_map_entry_count);
drivers/md/dm-vdo/slab-depot.c +5 -4
@@ -3288,7 +3288,8 @@ int vdo_release_block_reference(struct block_allocator *allocator,* Thus, the ordering is reversed from the usual sense since min_heap returns smaller elements * before larger ones. */ -static bool slab_status_is_less_than (const void *item1,const void *item2) +static bool slab_status_is_less_than (const void *item1,const void *item2, +void __always_unused *args) {const struct slab_status *info1 = item1;const struct slab_status *info2 = item2;
@@ -3300,7 +3301,7 @@ static bool slab_status_is_less_than(const void *item1, const void *item2)return info1->slab_number < info2->slab_number; } -static void swap_slab_statuses (void *item1,void *item2) +static void swap_slab_statuses (void *item1,void *item2,void __always_unused *args) {struct slab_status *info1 = item1;struct slab_status *info2 = item2;
@@ -3523,7 +3524,7 @@ static int __must_check vdo_prepare_slabs_for_allocation(struct block_allocator heap.data = slab_statuses; heap.nr = allocator->slab_count; heap.size = allocator->slab_count; -min_heapify_all (&heap, &slab_status_min_heap); +min_heapify_all (&heap, &slab_status_min_heap,NULL );while (heap.nr >0 ) {bool high_priority;
@@ -3531,7 +3532,7 @@ static int __must_check vdo_prepare_slabs_for_allocation(struct block_allocatorstruct slab_journal *journal; current_slab_status = slab_statuses[0 ]; -min_heap_pop (&heap, &slab_status_min_heap); +min_heap_pop (&heap, &slab_status_min_heap,NULL ); slab = depot->slabs[current_slab_status.slab_number]; if ((depot->load_type == VDO_SLAB_DEPOT_REBUILD_LOAD) ||
include/linux/min_heap.h +26 -25
129 lines not on this page · raw message
kernel/events/core.c +5 -5
@@ -3683,7 +3683,7 @@ void __perf_event_task_sched_out(struct task_struct *task,perf_cgroup_switch (next); } -static bool perf_less_group_idx (const void *l,const void *r) +static bool perf_less_group_idx (const void *l,const void *r,void __always_unused *args) {const struct perf_event *le = *(const struct perf_event **)l;const struct perf_event *re = *(const struct perf_event **)r;
@@ -3691,7 +3691,7 @@ static bool perf_less_group_idx(const void *l, const void *r)return le->group_index < re->group_index; } -static void swap_ptr (void *l,void *r) +static void swap_ptr (void *l,void *r,void __always_unused *args) {void **lp = l, **rp = r;
@@ -3779,7 +3779,7 @@ static noinline int visit_groups_merge(struct perf_event_context *ctx,perf_assert_pmu_disabled ((*evt)->pmu_ctx->pmu); } -min_heapify_all (&event_heap, &perf_min_heap); +min_heapify_all (&event_heap, &perf_min_heap,NULL );while (event_heap.nr) { ret =func (*evt, data);
@@ -3788,9 +3788,9 @@ static noinline int visit_groups_merge(struct perf_event_context *ctx, *evt =perf_event_groups_next (*evt, pmu);if (*evt) -min_heapify (&event_heap,0 , &perf_min_heap); +min_heapify (&event_heap,0 , &perf_min_heap,NULL );else -min_heap_pop (&event_heap, &perf_min_heap); +min_heap_pop (&event_heap, &perf_min_heap,NULL ); }return 0 ;
lib/test_min_heap.c +13 -13
@@ -13,17 +13,17 @@MIN_HEAP (int, min_heap_test); -static __init bool less_than (const void *lhs,const void *rhs) +static __init bool less_than (const void *lhs,const void *rhs,void __always_unused *args) {return *(int *)lhs < *(int *)rhs; } -static __init bool greater_than (const void *lhs,const void *rhs) +static __init bool greater_than (const void *lhs,const void *rhs,void __always_unused *args) {return *(int *)lhs > *(int *)rhs; } -static __init void swap_ints (void *lhs,void *rhs) +static __init void swap_ints (void *lhs,void *rhs,void __always_unused *args) {int temp = *(int *)lhs;
@@ -40,7 +40,7 @@ static __init int pop_verify_heap(bool min_heap,int last; last = values[0 ]; -min_heap_pop (heap, funcs); +min_heap_pop (heap, funcs,NULL );while (heap->nr >0 ) {if (min_heap) {if (last > values[0 ]) {
@@ -56,7 +56,7 @@ static __init int pop_verify_heap(bool min_heap, } } last = values[0 ]; -min_heap_pop (heap, funcs); +min_heap_pop (heap, funcs,NULL ); }return err; }
@@ -77,7 +77,7 @@ static __init int test_heapify_all(bool min_heap)int i, err;/* Test with known set of values. */ -min_heapify_all (&heap, &funcs); +min_heapify_all (&heap, &funcs,NULL ); err =pop_verify_heap (min_heap, &heap, &funcs);
@@ -86,7 +86,7 @@ static __init int test_heapify_all(bool min_heap)for (i =0 ; i < heap.nr; i++) values[i] =get_random_u32 (); -min_heapify_all (&heap, &funcs); +min_heapify_all (&heap, &funcs,NULL ); err +=pop_verify_heap (min_heap, &heap, &funcs);return err;
@@ -110,14 +110,14 @@ static __init int test_heap_push(bool min_heap)/* Test with known set of values copied from data. */ for (i =0 ; i <ARRAY_SIZE (data); i++) -min_heap_push (&heap, &data[i], &funcs); +min_heap_push (&heap, &data[i], &funcs,NULL ); err =pop_verify_heap (min_heap, &heap, &funcs);/* Test with randomly generated values. */ while (heap.nr < heap.size) { temp =get_random_u32 (); -min_heap_push (&heap, &temp, &funcs); +min_heap_push (&heap, &temp, &funcs,NULL ); } err +=pop_verify_heap (min_heap, &heap, &funcs);
@@ -143,22 +143,22 @@ static __init int test_heap_pop_push(bool min_heap)/* Fill values with data to pop and replace. */ temp = min_heap ?0x80000000 :0x7FFFFFFF ;for (i =0 ; i <ARRAY_SIZE (data); i++) -min_heap_push (&heap, &temp, &funcs); +min_heap_push (&heap, &temp, &funcs,NULL );/* Test with known set of values copied from data. */ for (i =0 ; i <ARRAY_SIZE (data); i++) -min_heap_pop_push (&heap, &data[i], &funcs); +min_heap_pop_push (&heap, &data[i], &funcs,NULL ); err =pop_verify_heap (min_heap, &heap, &funcs); heap.nr =0 ;for (i =0 ; i <ARRAY_SIZE (data); i++) -min_heap_push (&heap, &temp, &funcs); +min_heap_push (&heap, &temp, &funcs,NULL );/* Test with randomly generated values. */ for (i =0 ; i <ARRAY_SIZE (data); i++) { temp =get_random_u32 (); -min_heap_pop_push (&heap, &temp, &funcs); +min_heap_pop_push (&heap, &temp, &funcs,NULL ); } err +=pop_verify_heap (min_heap, &heap, &funcs);
Signature · 1 line
2.34.1
Kuan-Wei Chiu
maintainerPATCH v3 9/17 lib min_heap: Add min_heap_sift_up()
In reply to Kuan-Wei ChiuAdd min_heap_sift_up() to sift up the element at index 'idx' in the heap.
--- include/linux/min_heap.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
include/linux/min_heap.h +20 -0
@@ -111,6 +111,26 @@ void __min_heapify(min_heap_char *heap, int pos, size_t elem_size,#define min_heapify (_heap, _pos, _func, _args) \ __min_heapify((min_heap_char *)_heap, _pos, __minheap_obj_size(_heap), _func, _args) +/* Sift up ith element from the heap, O(log2(nr)). */ +static __always_inline +void __min_heap_sift_up(min_heap_char *heap, size_t elem_size, size_t idx, +const struct min_heap_callbacks *func,void *args) +{ +void *data = heap->data; +size_t parent; + +while (idx) { + parent = (idx -1 ) /2 ; +if (func->less (data + parent * elem_size, data + idx * elem_size, args)) +break ; + func->swp (data + parent * elem_size, data + idx * elem_size, args); + idx = parent; + } +} + +#define min_heap_sift_up (_heap, _idx, _func, _args) \ + __min_heap_sift_up((min_heap_char *)_heap, __minheap_obj_size(_heap), _idx, _func, _args) +/* Floyd's approach to heapification that is O(nr). */ static __always_inline void __min_heapify_all(min_heap_char *heap, size_t elem_size,
Signature · 1 line
2.34.1
Re: [PATCH v3 09/17] lib min_heap: Add min_heap_sift_up()
Quoted 5 lines · Kuan-Wei Chiu
Add
min_heap_sift_up()to sift up the element at index 'idx' in the heap.Signed-off-by Kuan-Wei Chiu <[email protected]>
Thanks,
Ian
Quoted 38 lines
--- include/linux/min_heap.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)include/linux/min_heap.h +20
-1@@ -111,6 +111,26 @@ void __min_heapify(min_heap_char *heap, int pos, size_t elem_size,#define min_heapify (_heap, _pos, _func, _args) \ __min_heapify((min_heap_char *)_heap, _pos, __minheap_obj_size(_heap), _func, _args) +/* Sift up ith element from the heap, O(log2(nr)). */ +static __always_inline +void __min_heap_sift_up(min_heap_char *heap, size_t elem_size, size_t idx, +const struct min_heap_callbacks *func,void *args) +{ +void *data = heap->data; +size_t parent; + +while (idx) { + parent = (idx -1 ) /2 ; +if (func->less (data + parent * elem_size, data + idx * elem_size, args)) +break ; + func->swp (data + parent * elem_size, data + idx * elem_size, args); + idx = parent; + } +} + +#define min_heap_sift_up (_heap, _idx, _func, _args) \ + __min_heap_sift_up((min_heap_char *)_heap, __minheap_obj_size(_heap), _idx, _func, _args) +/* Floyd's approach to heapification that is O(nr). */ static __always_inline void __min_heapify_all(min_heap_char *heap, size_t elem_size, --2.34.1
Kuan-Wei Chiu
maintainerPATCH v3 10/17 lib min_heap: Add min_heap_del()
In reply to Kuan-Wei ChiuAdd min_heap_del() to delete the element at index 'idx' in the heap.
---
Changes in v3:
- Fix a bug where we should copy the last element to 'data + idx * element_size' instead of 'data'.
include/linux/min_heap.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
include/linux/min_heap.h +24 -0
@@ -212,4 +212,28 @@ void __min_heap_push(min_heap_char *heap, const void *element, size_t elem_size,#define min_heap_push (_heap, _element, _func, _args) \ __min_heap_push((min_heap_char *)_heap, _element, __minheap_obj_size(_heap), _func, _args) +/* Remove ith element from the heap, O(log2(nr)). */ +static __always_inline +bool __min_heap_del(min_heap_char *heap, size_t elem_size, size_t idx, +const struct min_heap_callbacks *func,void *args) +{ +void *data = heap->data; + +if (WARN_ONCE (heap->nr <=0 ,"Popping an empty heap" )) +return false; + +/* Place last element at the root (position 0) and then sift down. */ + heap->nr--; +if (idx == heap->nr) +return true; +memcpy (data + (idx * elem_size), data + (heap->nr * elem_size), elem_size); +__min_heap_sift_up (heap, elem_size, idx, func, args); +__min_heapify (heap, idx, elem_size, func, args); + +return true; +} + +#define min_heap_del (_heap, _idx, _func, _args) \ + __min_heap_del((min_heap_char *)_heap, __minheap_obj_size(_heap), _idx, _func, _args) +#endif /* _LINUX_MIN_HEAP_H */
Signature · 1 line
2.34.1
Kuan-Wei Chiu
maintainerPATCH v3 11/17 lib min_heap: Update min_heap_push() and min_heap_pop() to return bool values
In reply to Kuan-Wei ChiuModify the min_heap_push() and min_heap_pop() to return a boolean value. They now return false when the operation fails and true when it succeeds.
--- include/linux/min_heap.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
include/linux/min_heap.h +8 -4
@@ -147,18 +147,20 @@ void __min_heapify_all(min_heap_char *heap, size_t elem_size,/* Remove minimum element from the heap, O(log2(nr)). */ static __always_inline -void __min_heap_pop(min_heap_char *heap, size_t elem_size, +bool __min_heap_pop(min_heap_char *heap, size_t elem_size,const struct min_heap_callbacks *func,void *args) {void *data = heap->data;if (WARN_ONCE (heap->nr <=0 ,"Popping an empty heap" )) -return ; +return false;/* Place last element at the root (position 0) and then sift down. */ heap->nr--;memcpy (data, data + (heap->nr * elem_size), elem_size);__min_heapify (heap,0 , elem_size, func, args); + +return true; }#define min_heap_pop (_heap, _func, _args) \
@@ -184,7 +186,7 @@ void __min_heap_pop_push(min_heap_char *heap,/* Push an element on to the heap, O(log2(nr)). */ static __always_inline -void __min_heap_push(min_heap_char *heap, const void *element, size_t elem_size, +bool __min_heap_push(min_heap_char *heap, const void *element, size_t elem_size,const struct min_heap_callbacks *func,void *args) {void *data = heap->data;
@@ -192,7 +194,7 @@ void __min_heap_push(min_heap_char *heap, const void *element, size_t elem_size,int pos;if (WARN_ONCE (heap->nr >= heap->size,"Pushing on a full heap" )) -return ; +return false;/* Place at the end of data. */ pos = heap->nr;
@@ -207,6 +209,8 @@ void __min_heap_push(min_heap_char *heap, const void *element, size_t elem_size,break ; func->swp (parent, child, args); } + +return true; }#define min_heap_push (_heap, _element, _func, _args) \
Signature · 1 line
2.34.1
Kuan-Wei Chiu
maintainerPATCH v3 12/17 lib min_heap: Rename min_heapify() to min_heap_sift_down()
In reply to Kuan-Wei ChiuAfter adding min_heap_sift_up(), the naming convention has been adjusted to maintain consistency with the min_heap_sift_up(). Consequently, min_heapify() has been renamed to min_heap_sift_down().
--- drivers/md/dm-vdo/repair.c | 2 +- include/linux/min_heap.h | 14 +++++++------- kernel/events/core.c | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-)
drivers/md/dm-vdo/repair.c +1 -1
@@ -183,7 +183,7 @@ static struct numbered_block_mapping *sort_next_heap_element(struct repair_compl*/ last = &repair->entries[--heap->nr];swap_mappings (heap->data, last,NULL ); -min_heapify (heap,0 , &repair_min_heap,NULL ); +min_heap_sift_down (heap,0 , &repair_min_heap,NULL );return last; }
include/linux/min_heap.h +7 -7
@@ -75,7 +75,7 @@ bool __min_heap_full(min_heap_char *heap)/* Sift the element at pos down the heap. */ static __always_inline -void __min_heapify(min_heap_char *heap, int pos, size_t elem_size, +void __min_heap_sift_down(min_heap_char *heap, int pos, size_t elem_size,const struct min_heap_callbacks *func,void *args) {void *left, *right;
@@ -108,8 +108,8 @@ void __min_heapify(min_heap_char *heap, int pos, size_t elem_size, } } -#define min_heapify (_heap, _pos, _func, _args) \ - __min_heapify((min_heap_char *)_heap, _pos, __minheap_obj_size(_heap), _func, _args) +#define min_heap_sift_down (_heap, _pos, _func, _args) \ + __min_heap_sift_down((min_heap_char *)_heap, _pos, __minheap_obj_size(_heap), _func, _args)/* Sift up ith element from the heap, O(log2(nr)). */ static __always_inline
@@ -139,7 +139,7 @@ void __min_heapify_all(min_heap_char *heap, size_t elem_size,int i;for (i = heap->nr /2 -1 ; i >=0 ; i--) -__min_heapify (heap, i, elem_size, func, args); +__min_heap_sift_down (heap, i, elem_size, func, args); }#define min_heapify_all (_heap, _func, _args) \
@@ -158,7 +158,7 @@ bool __min_heap_pop(min_heap_char *heap, size_t elem_size,/* Place last element at the root (position 0) and then sift down. */ heap->nr--;memcpy (data, data + (heap->nr * elem_size), elem_size); -__min_heapify (heap,0 , elem_size, func, args); +__min_heap_sift_down (heap,0 , elem_size, func, args);return true; }
@@ -178,7 +178,7 @@ void __min_heap_pop_push(min_heap_char *heap, void *args) {memcpy (heap->data, element, elem_size); -__min_heapify (heap,0 , elem_size, func, args); +__min_heap_sift_down (heap,0 , elem_size, func, args); }#define min_heap_pop_push (_heap, _element, _func, _args) \
@@ -232,7 +232,7 @@ bool __min_heap_del(min_heap_char *heap, size_t elem_size, size_t idx,return true;memcpy (data + (idx * elem_size), data + (heap->nr * elem_size), elem_size);__min_heap_sift_up (heap, elem_size, idx, func, args); -__min_heapify (heap, idx, elem_size, func, args); +__min_heap_sift_down (heap, idx, elem_size, func, args);return true; }
kernel/events/core.c +1 -1
@@ -3788,7 +3788,7 @@ static noinline int visit_groups_merge(struct perf_event_context *ctx, *evt =perf_event_groups_next (*evt, pmu);if (*evt) -min_heapify (&event_heap,0 , &perf_min_heap,NULL ); +min_heap_sift_down (&event_heap,0 , &perf_min_heap,NULL );else min_heap_pop (&event_heap, &perf_min_heap,NULL ); }
Signature · 1 line
2.34.1
Kuan-Wei Chiu
maintainerPATCH v3 13/17 lib min_heap: Update min_heap_push() to use min_heap_sift_up()
In reply to Kuan-Wei ChiuUpdate min_heap_push() to use min_heap_sift_up() rather than its origin inline version.
--- include/linux/min_heap.h | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-)
include/linux/min_heap.h +1 -8
@@ -190,7 +190,6 @@ bool __min_heap_push(min_heap_char *heap, const void *element, size_t elem_size, const struct min_heap_callbacks *func, void *args) { void *data = heap->data; -void *child, *parent;int pos;if (WARN_ONCE (heap->nr >= heap->size,"Pushing on a full heap" ))
@@ -202,13 +201,7 @@ bool __min_heap_push(min_heap_char *heap, const void *element, size_t elem_size, heap->nr++;/* Sift child at pos up. */ -for (; pos >0 ; pos = (pos -1 ) /2 ) { - child = data + (pos * elem_size); - parent = data + ((pos -1 ) /2 ) * elem_size; -if (func->less (parent, child, args)) -break ; - func->swp (parent, child, args); - } +__min_heap_sift_up (heap, elem_size, pos, func, args);return true; }
Signature · 1 line
2.34.1
Re: [PATCH v3 13/17] lib min_heap: Update min_heap_push() to use min_heap_sift_up()
Quoted 5 lines · Kuan-Wei Chiu
Update
min_heap_push()to usemin_heap_sift_up()rather than its origin inline version.Signed-off-by Kuan-Wei Chiu <[email protected]>
Thanks,
Ian
Quoted 34 lines
--- include/linux/min_heap.h | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-)include/linux/min_heap.h +1
-9@@ -190,7 +190,6 @@ bool __min_heap_push(min_heap_char *heap, const void *element, size_t elem_size, const struct min_heap_callbacks *func, void *args) { void *data = heap->data; -void *child, *parent;int pos;if (WARN_ONCE (heap->nr >= heap->size,"Pushing on a full heap" ))@@ -202,13 +201,7 @@ bool __min_heap_push(min_heap_char *heap, const void *element, size_t elem_size, heap->nr++;/* Sift child at pos up. */ -for (; pos >0 ; pos = (pos -1 ) /2 ) { - child = data + (pos * elem_size); - parent = data + ((pos -1 ) /2 ) * elem_size; -if (func->less (parent, child, args)) -break ; - func->swp (parent, child, args); - } +__min_heap_sift_up (heap, elem_size, pos, func, args);return true; } --2.34.1
Kuan-Wei Chiu
maintainerPATCH v3 14/17 lib/test_min_heap: Use min_heap_init() for initializing
In reply to Kuan-Wei ChiuReplace direct assignment of values to heap data members with min_heap_init() for better code readability and maintainability.
--- lib/test_min_heap.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-)
lib/test_min_heap.c +3 -8
@@ -67,9 +67,8 @@ static __init int test_heapify_all(bool min_heap) -3, -1, -2, -4, 0x8000000, 0x7FFFFFF };struct min_heap_test heap; - heap.data = values; +min_heap_init (&heap, values,ARRAY_SIZE (values)); heap.nr =ARRAY_SIZE (values); - heap.size =ARRAY_SIZE (values);struct min_heap_callbacks funcs = { .less = min_heap ? less_than : greater_than, .swp = swap_ints,
@@ -99,9 +98,7 @@ static __init int test_heap_push(bool min_heap)int values[ARRAY_SIZE (data)];struct min_heap_test heap; - heap.data = values; - heap.nr =0 ; - heap.size =ARRAY_SIZE (values); +min_heap_init (&heap, values,ARRAY_SIZE (values));struct min_heap_callbacks funcs = { .less = min_heap ? less_than : greater_than, .swp = swap_ints,
@@ -131,9 +128,7 @@ static __init int test_heap_pop_push(bool min_heap)int values[ARRAY_SIZE (data)];struct min_heap_test heap; - heap.data = values; - heap.nr =0 ; - heap.size =ARRAY_SIZE (values); +min_heap_init (&heap, values,ARRAY_SIZE (values));struct min_heap_callbacks funcs = { .less = min_heap ? less_than : greater_than, .swp = swap_ints,
Signature · 1 line
2.34.1
Kuan-Wei Chiu
maintainerPATCH v3 15/17 lib/test_min_heap: Add test for heap_del()
In reply to Kuan-Wei ChiuAdd test cases for the min_heap_del() to ensure its functionality is thoroughly tested.
--- lib/test_min_heap.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)
lib/test_min_heap.c +36 -0
@@ -160,6 +160,40 @@ static __init int test_heap_pop_push(bool min_heap)return err; } +static __init int test_heap_del (bool min_heap) +{ +int values[] = {3 ,1 ,2 ,4 ,0x8000000 ,0x7FFFFFF ,0 , +-3 ,-1 ,-2 ,-4 ,0x8000000 ,0x7FFFFFF }; +struct min_heap_test heap; + +min_heap_init (&heap, values,ARRAY_SIZE (values)); + heap.nr =ARRAY_SIZE (values); +struct min_heap_callbacks funcs = { + .less = min_heap ? less_than : greater_than, + .swp = swap_ints, + }; +int i, err; + +/* Test with known set of values. */ +min_heapify_all (&heap, &funcs,NULL ); +for (i =0 ; i <ARRAY_SIZE (values) /2 ; i++) +min_heap_del (&heap,get_random_u32 () % heap.nr, &funcs,NULL ); + err =pop_verify_heap (min_heap, &heap, &funcs); + + +/* Test with randomly generated values. */ + heap.nr =ARRAY_SIZE (values); +for (i =0 ; i < heap.nr; i++) + values[i] =get_random_u32 (); +min_heapify_all (&heap, &funcs,NULL ); + +for (i =0 ; i <ARRAY_SIZE (values) /2 ; i++) +min_heap_del (&heap,get_random_u32 () % heap.nr, &funcs,NULL ); + err +=pop_verify_heap (min_heap, &heap, &funcs); + +return err; +} +static int __init test_min_heap_init (void ) {int err =0 ;
@@ -170,6 +204,8 @@ static int __init test_min_heap_init(void) err +=test_heap_push (false); err +=test_heap_pop_push (true); err +=test_heap_pop_push (false); + err +=test_heap_del (true); + err +=test_heap_del (false);if (err) {pr_err ("test failed with %d errors\n" , err);return -EINVAL ;
Signature · 1 line
2.34.1
Re: [PATCH v3 15/17] lib/test_min_heap: Add test for heap_del()
Quoted 5 lines · Kuan-Wei Chiu
Add test cases for the
min_heap_del()to ensure its functionality is thoroughly tested.Signed-off-by Kuan-Wei Chiu <[email protected]>
Thanks,
Ian
Quoted 61 lines
--- lib/test_min_heap.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)lib/test_min_heap.c +36
-1@@ -160,6 +160,40 @@ static __init int test_heap_pop_push(bool min_heap)return err; } +static __init int test_heap_del (bool min_heap) +{ +int values[] = {3 ,1 ,2 ,4 ,0x8000000 ,0x7FFFFFF ,0 , +-3 ,-1 ,-2 ,-4 ,0x8000000 ,0x7FFFFFF }; +struct min_heap_test heap; + +min_heap_init (&heap, values,ARRAY_SIZE (values)); + heap.nr =ARRAY_SIZE (values); +struct min_heap_callbacks funcs = { + .less = min_heap ? less_than : greater_than, + .swp = swap_ints, + }; +int i, err; + +/* Test with known set of values. */ +min_heapify_all (&heap, &funcs,NULL ); +for (i =0 ; i <ARRAY_SIZE (values) /2 ; i++) +min_heap_del (&heap,get_random_u32 () % heap.nr, &funcs,NULL ); + err =pop_verify_heap (min_heap, &heap, &funcs); + + +/* Test with randomly generated values. */ + heap.nr =ARRAY_SIZE (values); +for (i =0 ; i < heap.nr; i++) + values[i] =get_random_u32 (); +min_heapify_all (&heap, &funcs,NULL ); + +for (i =0 ; i <ARRAY_SIZE (values) /2 ; i++) +min_heap_del (&heap,get_random_u32 () % heap.nr, &funcs,NULL ); + err +=pop_verify_heap (min_heap, &heap, &funcs); + +return err; +} +static int __init test_min_heap_init (void ) {int err =0 ;@@ -170,6 +204,8 @@ static int __init test_min_heap_init(void) err +=test_heap_push (false); err +=test_heap_pop_push (true); err +=test_heap_pop_push (false); + err +=test_heap_del (true); + err +=test_heap_del (false);if (err) {pr_err ("test failed with %d errors\n" , err);return -EINVAL ; --2.34.1
Kuan-Wei Chiu
maintainerPATCH v3 16/17 bcache: Remove heap-related macros and switch to generic min_heap
In reply to Kuan-Wei ChiuDrop the heap-related macros from bcache and replacing them with the generic min_heap implementation from include/linux. By doing so, code readability is improved by using functions instead of macros. Moreover, the min_heap implementation in include/linux adopts a bottom-up variation compared to the textbook version currently used in bcache. This bottom-up variation allows for approximately 50% reduction in the number of comparison operations during heap siftdown, without changing the number of swaps, thus making it more efficient.
---
Changes in v3:
- Correct bugs where the parameter types in some compare functions should have been 'type **', but were mistakenly written as 'type *'.
drivers/md/bcache/alloc.c | 64 +++++++++++++++++++------- drivers/md/bcache/bcache.h | 2 +- drivers/md/bcache/bset.c | 84 ++++++++++++++++++++++++----------- drivers/md/bcache/bset.h | 14 +++--- drivers/md/bcache/btree.c | 17 ++++++- drivers/md/bcache/extents.c | 53 ++++++++++++++-------- drivers/md/bcache/movinggc.c | 41 ++++++++++++----- drivers/md/bcache/sysfs.c | 2 + drivers/md/bcache/util.h | 67 +--------------------------- drivers/md/bcache/writeback.c | 3 ++ 10 files changed, 202 insertions(+), 145 deletions(-)
drivers/md/bcache/alloc.c +47 -17
@@ -166,40 +166,68 @@ static void bch_invalidate_one_bucket(struct cache *ca, struct bucket *b)* prio is worth 1/8th of what INITIAL_PRIO is worth. */ -#define bucket_prio (b) \ -({ \ - unsigned int min_prio = (INITIAL_PRIO - ca->set->min_prio) / 8; \ - \ - (b->prio - ca->set->min_prio + min_prio) * GC_SECTORS_USED(b); \ -}) +static inline unsigned int new_bucket_prio (struct cache *ca,struct bucket *b) +{ +unsigned int min_prio = (INITIAL_PRIO - ca->set->min_prio) /8 ; + +return (b->prio - ca->set->min_prio + min_prio) *GC_SECTORS_USED (b); +} + +static inline bool new_bucket_max_cmp (const void *l,const void *r,void *args) +{ +struct bucket **lhs = (struct bucket **)l; +struct bucket **rhs = (struct bucket **)r; +struct cache *ca = args; + +return new_bucket_prio (ca, *lhs) >new_bucket_prio (ca, *rhs); +} -#define bucket_max_cmp (l, r) (bucket_prio(l) < bucket_prio(r)) -#define bucket_min_cmp (l, r) (bucket_prio(l) > bucket_prio(r)) +static inline bool new_bucket_min_cmp (const void *l,const void *r,void *args) +{ +struct bucket **lhs = (struct bucket **)l; +struct bucket **rhs = (struct bucket **)r; +struct cache *ca = args; + +return new_bucket_prio (ca, *lhs) <new_bucket_prio (ca, *rhs); +} + +static inline void new_bucket_swap (void *l,void *r,void __always_unused *args) +{ +struct bucket **lhs = l, **rhs = r; + +swap (*lhs, *rhs); +}static void invalidate_buckets_lru (struct cache *ca) {struct bucket *b; -ssize_t i; +const struct min_heap_callbacks bucket_max_cmp_callback = { + .less = new_bucket_max_cmp, + .swp = new_bucket_swap, + }; +const struct min_heap_callbacks bucket_min_cmp_callback = { + .less = new_bucket_min_cmp, + .swp = new_bucket_swap, + }; - ca->heap.used =0 ; + ca->heap.nr =0 ;for_each_bucket (b, ca) {if (!bch_can_invalidate_bucket (ca, b))continue ; -if (!heap_full (&ca->heap)) -heap_add (&ca->heap, b, bucket_max_cmp); -else if (bucket_max_cmp (b,heap_peek (&ca->heap))) { +if (!min_heap_full (&ca->heap)) +min_heap_push (&ca->heap, &b, &bucket_max_cmp_callback, ca); +else if (!new_bucket_max_cmp (&b,min_heap_peek (&ca->heap), ca)) { ca->heap.data[0 ] = b; -heap_sift (&ca->heap,0 , bucket_max_cmp); +min_heap_sift_down (&ca->heap,0 , &bucket_max_cmp_callback, ca); } } -for (i = ca->heap.used /2 -1 ; i >=0 ; --i) -heap_sift (&ca->heap, i, bucket_min_cmp); +min_heapify_all (&ca->heap, &bucket_min_cmp_callback, ca);while (!fifo_full (&ca->free_inc)) { -if (!heap_pop (&ca->heap, b, bucket_min_cmp)) { +if (!ca->heap.nr) {/* * We don't want to be calling invalidate_buckets() * multiple times when it can't do anything
@@ -208,6 +236,8 @@ static void invalidate_buckets_lru(struct cache *ca)wake_up_gc (ca->set);return ; } + b =min_heap_peek (&ca->heap)[0 ]; +min_heap_pop (&ca->heap, &bucket_min_cmp_callback, ca);bch_invalidate_one_bucket (ca, b); }
drivers/md/bcache/bcache.h +1 -1
@@ -457,7 +457,7 @@ struct cache {/* Allocation stuff: */ struct bucket *buckets; - DECLARE_HEAP(struct bucket *, heap); + MIN_HEAP(struct bucket *, cache_heap) heap;/* * If nonzero, we know we aren't going to find any buckets to invalidate
drivers/md/bcache/bset.c +59 -25
182 lines not on this page · raw message
drivers/md/bcache/bset.h +8 -6
@@ -187,8 +187,9 @@ struct bset_tree { };struct btree_keys_ops { -bool (*sort_cmp)(struct btree_iter_set l, -struct btree_iter_set r); +bool (*sort_cmp)(const void *l, +const void *r, +void *args);struct bkey *(*sort_fixup)(struct btree_iter *iter,struct bkey *tmp); bool (*insert_fixup)(struct btree_keys *b,
@@ -312,16 +313,17 @@ enum { BTREE_INSERT_STATUS_FRONT_MERGE, }; +struct btree_iter_set { +struct bkey *k, *end; +}; +/* Btree key iteration */ struct btree_iter { -size_t size, used;#ifdef CONFIG_BCACHE_DEBUG struct btree_keys *b;#endif -struct btree_iter_set { -struct bkey *k, *end; - } data[MAX_BSETS ]; + MIN_HEAP_PREALLOCATED(struct btree_iter_set, btree_iter_heap, MAX_BSETS) heap; };typedef bool (*ptr_filter_fn )(struct btree_keys *b,const struct bkey *k);
drivers/md/bcache/btree.c +15 -2
@@ -157,8 +157,8 @@ void bch_btree_node_read_done(struct btree *b)* See the comment arount cache_set->fill_iter. */ iter =mempool_alloc (&b->c->fill_iter,GFP_NOIO ); - iter->size = b->c->cache->sb.bucket_size / b->c->cache->sb.block_size; - iter->used =0 ; + iter->heap.size = b->c->cache->sb.bucket_size / b->c->cache->sb.block_size; + iter->heap.nr =0 ;#ifdef CONFIG_BCACHE_DEBUG iter->b = &b->keys;
@@ -1312,6 +1312,8 @@ static bool btree_gc_mark_node(struct btree *b, struct gc_stat *gc)struct btree_iter iter;struct bset_tree *t; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); + gc->nodes++;for_each_key_filter (&b->keys, k, &iter, bch_ptr_invalid) {
@@ -1573,6 +1575,8 @@ static unsigned int btree_gc_count_keys(struct btree *b)struct btree_iter iter;unsigned int ret =0 ; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); +for_each_key_filter (&b->keys, k, &iter, bch_ptr_bad) ret +=bkey_u64s (k);
@@ -1615,6 +1619,7 @@ static int btree_gc_recurse(struct btree *b, struct btree_op *op,struct gc_merge_info r[GC_MERGE_NODES ];struct gc_merge_info *i, *last = r +ARRAY_SIZE (r) -1 ; +min_heap_init (&iter.heap,NULL ,MAX_BSETS );bch_btree_iter_init (&b->keys, &iter, &b->c->gc_done);for (i = r; i < r +ARRAY_SIZE (r); i++)
@@ -1913,6 +1918,8 @@ static int bch_btree_check_recurse(struct btree *b, struct btree_op *op)struct bkey *k, *p =NULL ;struct btree_iter iter; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); +for_each_key_filter (&b->keys, k, &iter, bch_ptr_invalid)bch_initial_mark_key (b->c, b->level, k);
@@ -1958,6 +1965,8 @@ static int bch_btree_check_thread(void *arg) cur_idx = prev_idx =0 ; ret =0 ; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); +/* root node keys are checked before thread created */ bch_btree_iter_init (&c->root->keys, &iter,NULL ); k =bch_btree_iter_next_filter (&iter, &c->root->keys, bch_ptr_bad);
@@ -2054,6 +2063,8 @@ int bch_btree_check(struct cache_set *c)struct btree_iter iter;struct btree_check_state check_state; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); +/* check and mark root node keys */ for_each_key_filter (&c->root->keys, k, &iter, bch_ptr_invalid)bch_initial_mark_key (c, c->root->level, k);
@@ -2549,6 +2560,7 @@ static int bch_btree_map_nodes_recurse(struct btree *b, struct btree_op *op,struct bkey *k;struct btree_iter iter; +min_heap_init (&iter.heap,NULL ,MAX_BSETS );bch_btree_iter_init (&b->keys, &iter, from); while ((k = bch_btree_iter_next_filter(&iter, &b->keys,
@@ -2582,6 +2594,7 @@ int bch_btree_map_keys_recurse(struct btree *b, struct btree_op *op,struct bkey *k;struct btree_iter iter; +min_heap_init (&iter.heap,NULL ,MAX_BSETS );bch_btree_iter_init (&b->keys, &iter, from);while ((k =bch_btree_iter_next_filter (&iter, &b->keys, bch_ptr_bad))) {
drivers/md/bcache/extents.c +34 -19
106 lines not on this page · raw message
drivers/md/bcache/movinggc.c +31 -10
85 lines not on this page · raw message
drivers/md/bcache/sysfs.c +2 -0
@@ -662,6 +662,8 @@ static unsigned int bch_root_usage(struct cache_set *c)struct btree *b;struct btree_iter iter; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); + goto lock_root; do {
drivers/md/bcache/util.h +2 -65
88 lines not on this page · raw message
drivers/md/bcache/writeback.c +3 -0
@@ -915,6 +915,7 @@ static int bch_dirty_init_thread(void *arg) k = p =NULL ; prev_idx =0 ; +min_heap_init (&iter.heap,NULL ,MAX_BSETS );bch_btree_iter_init (&c->root->keys, &iter,NULL ); k =bch_btree_iter_next_filter (&iter, &c->root->keys, bch_ptr_bad);BUG_ON (!k);
@@ -984,6 +985,8 @@ void bch_sectors_dirty_init(struct bcache_device *d)struct cache_set *c = d->c;struct bch_dirty_init_state state; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); + retry_lock: b = c->root;rw_lock (0 , b, b->level);
Signature · 1 line
2.34.1
Re: [PATCH v3 16/17] bcache: Remove heap-related macros and switch to generic min_heap
Quoted 9 lines · Kuan-Wei Chiu
Drop the heap-related macros from bcache and replacing them with the generic
min_heapimplementation frominclude/linux. By doing so, code readability is improved by using functions instead of macros. Moreover, themin_heapimplementation ininclude/linuxadopts a bottom-up variation compared to the textbook version currently used in bcache. This bottom-up variation allows for approximately 50% reduction in the number of comparison operations during heap siftdown, without changing the number of swaps, thus making it more efficient.
Quoted 3 lines
Thanks.
Coly Li
Quoted 790 lines
---
Changes in v3:
- Correct bugs where the parameter types in some compare functions should have been 'type **', but were mistakenly written as 'type *'.drivers/md/bcache/alloc.c | 64 +++++++++++++++++++------- drivers/md/bcache/bcache.h | 2 +- drivers/md/bcache/bset.c | 84 ++++++++++++++++++++++++----------- drivers/md/bcache/bset.h | 14 +++--- drivers/md/bcache/btree.c | 17 ++++++- drivers/md/bcache/extents.c | 53 ++++++++++++++-------- drivers/md/bcache/movinggc.c | 41 ++++++++++++----- drivers/md/bcache/sysfs.c | 2 + drivers/md/bcache/util.h | 67 +--------------------------- drivers/md/bcache/writeback.c | 3 ++ 10 files changed, 202 insertions(+), 145 deletions(-)drivers/md/bcache/alloc.c +47
-17@@ -166,40 +166,68 @@ static void bch_invalidate_one_bucket(struct cache *ca, struct bucket *b)* prio is worth 1/8th of what INITIAL_PRIO is worth. */ -#define bucket_prio (b) \ -({ \ - unsigned int min_prio = (INITIAL_PRIO - ca->set->min_prio) / 8; \ - \ - (b->prio - ca->set->min_prio + min_prio) * GC_SECTORS_USED(b); \ -}) +static inline unsigned int new_bucket_prio (struct cache *ca,struct bucket *b) +{ +unsigned int min_prio = (INITIAL_PRIO - ca->set->min_prio) /8 ; + +return (b->prio - ca->set->min_prio + min_prio) *GC_SECTORS_USED (b); +} + +static inline bool new_bucket_max_cmp (const void *l,const void *r,void *args) +{ +struct bucket **lhs = (struct bucket **)l; +struct bucket **rhs = (struct bucket **)r; +struct cache *ca = args; + +return new_bucket_prio (ca, *lhs) >new_bucket_prio (ca, *rhs); +} -#define bucket_max_cmp (l, r) (bucket_prio(l) < bucket_prio(r)) -#define bucket_min_cmp (l, r) (bucket_prio(l) > bucket_prio(r)) +static inline bool new_bucket_min_cmp (const void *l,const void *r,void *args) +{ +struct bucket **lhs = (struct bucket **)l; +struct bucket **rhs = (struct bucket **)r; +struct cache *ca = args; + +return new_bucket_prio (ca, *lhs) <new_bucket_prio (ca, *rhs); +} + +static inline void new_bucket_swap (void *l,void *r,void __always_unused *args) +{ +struct bucket **lhs = l, **rhs = r; + +swap (*lhs, *rhs); +}static void invalidate_buckets_lru (struct cache *ca) {struct bucket *b; -ssize_t i; +const struct min_heap_callbacks bucket_max_cmp_callback = { + .less = new_bucket_max_cmp, + .swp = new_bucket_swap, + }; +const struct min_heap_callbacks bucket_min_cmp_callback = { + .less = new_bucket_min_cmp, + .swp = new_bucket_swap, + }; - ca->heap.used =0 ; + ca->heap.nr =0 ;for_each_bucket (b, ca) {if (!bch_can_invalidate_bucket (ca, b))continue ; -if (!heap_full (&ca->heap)) -heap_add (&ca->heap, b, bucket_max_cmp); -else if (bucket_max_cmp (b,heap_peek (&ca->heap))) { +if (!min_heap_full (&ca->heap)) +min_heap_push (&ca->heap, &b, &bucket_max_cmp_callback, ca); +else if (!new_bucket_max_cmp (&b,min_heap_peek (&ca->heap), ca)) { ca->heap.data[0 ] = b; -heap_sift (&ca->heap,0 , bucket_max_cmp); +min_heap_sift_down (&ca->heap,0 , &bucket_max_cmp_callback, ca); } } -for (i = ca->heap.used /2 -1 ; i >=0 ; --i) -heap_sift (&ca->heap, i, bucket_min_cmp); +min_heapify_all (&ca->heap, &bucket_min_cmp_callback, ca);while (!fifo_full (&ca->free_inc)) { -if (!heap_pop (&ca->heap, b, bucket_min_cmp)) { +if (!ca->heap.nr) {/* * We don't want to be calling invalidate_buckets() * multiple times when it can't do anything @@ -208,6 +236,8 @@ static void invalidate_buckets_lru(struct cache *ca)wake_up_gc (ca->set);return ; } + b =min_heap_peek (&ca->heap)[0 ]; +min_heap_pop (&ca->heap, &bucket_min_cmp_callback, ca);bch_invalidate_one_bucket (ca, b); }drivers/md/bcache/bcache.h +1
-1@@ -457,7 +457,7 @@ struct cache {/* Allocation stuff: */ struct bucket *buckets; - DECLARE_HEAP(struct bucket *, heap); + MIN_HEAP(struct bucket *, cache_heap) heap;/* * If nonzero, we know we aren't going to find any buckets to invalidate drivers/md/bcache/bset.c +59
-25@@ -57,6 +57,8 @@ int __bch_count_data(struct btree_keys *b)struct btree_iter iter;struct bkey *k; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); +if (b->ops->is_extents)for_each_key (b, k, &iter) ret +=KEY_SIZE (k);@@ -70,6 +72,8 @@ void __bch_check_keys(struct btree_keys *b, const char *fmt, ...)struct btree_iter iter;const char *err; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); +for_each_key (b, k, &iter) {if (b->ops->is_extents) { err ="Keys out of order" ;@@ -110,9 +114,9 @@ void __bch_check_keys(struct btree_keys *b, const char *fmt, ...)static void bch_btree_iter_next_check (struct btree_iter *iter) { -struct bkey *k = iter->data->k, *next =bkey_next (k); +struct bkey *k = iter->heap.data->k, *next =bkey_next (k); -if (next < iter->data->end && +if (next < iter->heap.data->end &&bkey_cmp (k, iter->b->ops->is_extents ? &START_KEY (next) : next) >0 ) {bch_dump_bucket (iter->b);@@ -885,6 +889,8 @@ unsigned int bch_btree_insert_key(struct btree_keys *b, struct bkey *k,BUG_ON (b->ops->is_extents && !KEY_SIZE (k)); +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); +/* * If k has preceding key, preceding_key_p will be set to address * of k's preceding key; otherwise preceding_key_p will be set @@ -1077,27 +1083,42 @@ struct bkey *__bch_bset_search(struct btree_keys *b, struct bset_tree *t,/* Btree iterator */ -typedef bool (btree_iter_cmp_fn )(struct btree_iter_set , -struct btree_iter_set ); +typedef bool (new_btree_iter_cmp_fn )(const void *,const void *,void *); + +static inline bool new_btree_iter_cmp (const void *l,const void *r,void __always_unused *args) +{ +const struct btree_iter_set *_l = l; +const struct btree_iter_set *_r = r; + +return bkey_cmp (_l->k, _r->k) <=0 ; +} -static inline bool btree_iter_cmp (struct btree_iter_set l, -struct btree_iter_set r) +static inline void new_btree_iter_swap (void *iter1,void *iter2,void __always_unused *args) { -return bkey_cmp (l.k, r.k) >0 ; +struct btree_iter_set *_iter1 = iter1; +struct btree_iter_set *_iter2 = iter2; + +swap (*_iter1, *_iter2); }static inline bool btree_iter_end (struct btree_iter *iter) { -return !iter->used; +return !iter->heap.nr; }void bch_btree_iter_push (struct btree_iter *iter,struct bkey *k,struct bkey *end) { +const struct min_heap_callbacks callbacks = { + .less = new_btree_iter_cmp, + .swp = new_btree_iter_swap, + }; +if (k != end) -BUG_ON (!heap_add (iter, - ((struct btree_iter_set ) { k, end }), - btree_iter_cmp)); +BUG_ON (!min_heap_push (&iter->heap, + &((struct btree_iter_set ) { k, end }), + &callbacks, +NULL )); } static struct bkey *__bch_btree_iter_init(struct btree_keys *b,@@ -1107,8 +1128,8 @@ static struct bkey *__bch_btree_iter_init(struct btree_keys *b, {struct bkey *ret =NULL ; - iter->size =ARRAY_SIZE (iter->data); - iter->used =0 ; + iter->heap.size =ARRAY_SIZE (iter->heap.preallocated); + iter->heap.nr =0 ;#ifdef CONFIG_BCACHE_DEBUG iter->b = b;@@ -1130,26 +1151,34 @@ struct bkey *bch_btree_iter_init(struct btree_keys *b, }static inline struct bkey *__bch_btree_iter_next (struct btree_iter *iter, -btree_iter_cmp_fn *cmp) +new_btree_iter_cmp_fn *cmp) {struct btree_iter_set b__maybe_unused ;struct bkey *ret =NULL ; +const struct min_heap_callbacks callbacks = { + .less = cmp, + .swp = new_btree_iter_swap, + };if (!btree_iter_end (iter)) {bch_btree_iter_next_check (iter); - ret = iter->data->k; - iter->data->k =bkey_next (iter->data->k); + ret = iter->heap.data->k; + iter->heap.data->k =bkey_next (iter->heap.data->k); -if (iter->data->k > iter->data->end) { +if (iter->heap.data->k > iter->heap.data->end) {WARN_ONCE (1 ,"bset was corrupt!\n" ); - iter->data->k = iter->data->end; + iter->heap.data->k = iter->heap.data->end; } -if (iter->data->k == iter->data->end) -heap_pop (iter, b, cmp); +if (iter->heap.data->k == iter->heap.data->end) { +if (iter->heap.nr) { + b =min_heap_peek (&iter->heap)[0 ]; +min_heap_pop (&iter->heap, &callbacks,NULL ); + } + }else -heap_sift (iter,0 , cmp); +min_heap_sift_down (&iter->heap,0 , &callbacks,NULL ); }return ret;@@ -1157,7 +1186,7 @@ static inline struct bkey *__bch_btree_iter_next(struct btree_iter *iter,struct bkey *bch_btree_iter_next (struct btree_iter *iter) { -return __bch_btree_iter_next (iter, btree_iter_cmp); +return __bch_btree_iter_next (iter, new_btree_iter_cmp); }@@ -1195,16 +1224,18 @@ static void btree_mergesort(struct btree_keys *b, struct bset *out,struct btree_iter *iter, bool fixup, bool remove_stale) { - int i; struct bkey *k, *last = NULL;BKEY_PADDED (k) tmp;bool (*bad)(struct btree_keys *,const struct bkey *) = remove_stale ? bch_ptr_bad : bch_ptr_invalid; +const struct min_heap_callbacks callbacks = { + .less = b->ops->sort_cmp, + .swp = new_btree_iter_swap, + };/* Heapify the iterator, using our comparison function */ -for (i = iter->used /2 -1 ; i >=0 ; --i) -heap_sift (iter, i, b->ops->sort_cmp); +min_heapify_all (&iter->heap, &callbacks,NULL );while (!btree_iter_end (iter)) {if (b->ops->sort_fixup && fixup)@@ -1296,6 +1327,7 @@ void bch_btree_sort_partial(struct btree_keys *b, unsigned int start,struct btree_iter iter;int oldsize =bch_count_data (b); +min_heap_init (&iter.heap,NULL ,MAX_BSETS );__bch_btree_iter_init (b, &iter,NULL , &b->set[start]);if (start) {@@ -1325,6 +1357,8 @@ void bch_btree_sort_into(struct btree_keys *b, struct btree_keys *new,uint64_t start_time =local_clock ();struct btree_iter iter; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); +bch_btree_iter_init (b, &iter,NULL );btree_mergesort (b, new->set->data, &iter, false, true);drivers/md/bcache/bset.h +8
-6@@ -187,8 +187,9 @@ struct bset_tree { };struct btree_keys_ops { -bool (*sort_cmp)(struct btree_iter_set l, -struct btree_iter_set r); +bool (*sort_cmp)(const void *l, +const void *r, +void *args);struct bkey *(*sort_fixup)(struct btree_iter *iter,struct bkey *tmp); bool (*insert_fixup)(struct btree_keys *b,@@ -312,16 +313,17 @@ enum { BTREE_INSERT_STATUS_FRONT_MERGE, }; +struct btree_iter_set { +struct bkey *k, *end; +}; +/* Btree key iteration */ struct btree_iter { -size_t size, used;#ifdef CONFIG_BCACHE_DEBUG struct btree_keys *b;#endif -struct btree_iter_set { -struct bkey *k, *end; - } data[MAX_BSETS ]; + MIN_HEAP_PREALLOCATED(struct btree_iter_set, btree_iter_heap, MAX_BSETS) heap; };typedef bool (*ptr_filter_fn )(struct btree_keys *b,const struct bkey *k);drivers/md/bcache/btree.c +15
-2@@ -157,8 +157,8 @@ void bch_btree_node_read_done(struct btree *b)* See the comment arount cache_set->fill_iter. */ iter =mempool_alloc (&b->c->fill_iter,GFP_NOIO ); - iter->size = b->c->cache->sb.bucket_size / b->c->cache->sb.block_size; - iter->used =0 ; + iter->heap.size = b->c->cache->sb.bucket_size / b->c->cache->sb.block_size; + iter->heap.nr =0 ;#ifdef CONFIG_BCACHE_DEBUG iter->b = &b->keys;@@ -1312,6 +1312,8 @@ static bool btree_gc_mark_node(struct btree *b, struct gc_stat *gc)struct btree_iter iter;struct bset_tree *t; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); + gc->nodes++;for_each_key_filter (&b->keys, k, &iter, bch_ptr_invalid) {@@ -1573,6 +1575,8 @@ static unsigned int btree_gc_count_keys(struct btree *b)struct btree_iter iter;unsigned int ret =0 ; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); +for_each_key_filter (&b->keys, k, &iter, bch_ptr_bad) ret +=bkey_u64s (k);@@ -1615,6 +1619,7 @@ static int btree_gc_recurse(struct btree *b, struct btree_op *op,struct gc_merge_info r[GC_MERGE_NODES ];struct gc_merge_info *i, *last = r +ARRAY_SIZE (r) -1 ; +min_heap_init (&iter.heap,NULL ,MAX_BSETS );bch_btree_iter_init (&b->keys, &iter, &b->c->gc_done);for (i = r; i < r +ARRAY_SIZE (r); i++)@@ -1913,6 +1918,8 @@ static int bch_btree_check_recurse(struct btree *b, struct btree_op *op)struct bkey *k, *p =NULL ;struct btree_iter iter; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); +for_each_key_filter (&b->keys, k, &iter, bch_ptr_invalid)bch_initial_mark_key (b->c, b->level, k);@@ -1958,6 +1965,8 @@ static int bch_btree_check_thread(void *arg) cur_idx = prev_idx =0 ; ret =0 ; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); +/* root node keys are checked before thread created */ bch_btree_iter_init (&c->root->keys, &iter,NULL ); k =bch_btree_iter_next_filter (&iter, &c->root->keys, bch_ptr_bad);@@ -2054,6 +2063,8 @@ int bch_btree_check(struct cache_set *c)struct btree_iter iter;struct btree_check_state check_state; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); +/* check and mark root node keys */ for_each_key_filter (&c->root->keys, k, &iter, bch_ptr_invalid)bch_initial_mark_key (c, c->root->level, k);@@ -2549,6 +2560,7 @@ static int bch_btree_map_nodes_recurse(struct btree *b, struct btree_op *op,struct bkey *k;struct btree_iter iter; +min_heap_init (&iter.heap,NULL ,MAX_BSETS );bch_btree_iter_init (&b->keys, &iter, from); while ((k = bch_btree_iter_next_filter(&iter, &b->keys,@@ -2582,6 +2594,7 @@ int bch_btree_map_keys_recurse(struct btree *b, struct btree_op *op,struct bkey *k;struct btree_iter iter; +min_heap_init (&iter.heap,NULL ,MAX_BSETS );bch_btree_iter_init (&b->keys, &iter, from);while ((k =bch_btree_iter_next_filter (&iter, &b->keys, bch_ptr_bad))) {drivers/md/bcache/extents.c +34
-19@@ -33,15 +33,16 @@ static void sort_key_next(struct btree_iter *iter, i->k =bkey_next (i->k);if (i->k == i->end) - *i = iter->data[--iter->used]; + *i = iter->heap.data[--iter->heap.nr]; } -static bool bch_key_sort_cmp (struct btree_iter_set l, -struct btree_iter_set r) +static bool new_bch_key_sort_cmp (const void *l,const void *r,void *args) { -int64_t c =bkey_cmp (l.k, r.k); +struct btree_iter_set *_l = (struct btree_iter_set *)l; +struct btree_iter_set *_r = (struct btree_iter_set *)r; +int64_t c =bkey_cmp (_l->k, _r->k); -return c ? c >0 : l.k < r.k; +return !(c ? c >0 : _l->k < _r->k); }static bool __ptr_invalid (struct cache_set *c,const struct bkey *k)@@ -238,7 +239,7 @@ static bool bch_btree_ptr_insert_fixup(struct btree_keys *bk, }const struct btree_keys_ops bch_btree_keys_ops = { - .sort_cmp = bch_key_sort_cmp, + .sort_cmp = new_bch_key_sort_cmp, .insert_fixup = bch_btree_ptr_insert_fixup, .key_invalid = bch_btree_ptr_invalid, .key_bad = bch_btree_ptr_bad,@@ -255,22 +256,36 @@ const struct btree_keys_ops bch_btree_keys_ops = {* Necessary for btree_sort_fixup() - if there are multiple keys that compare * equal in different sets, we have to process them newest to oldest. */ -static bool bch_extent_sort_cmp (struct btree_iter_set l, -struct btree_iter_set r) + +static bool new_bch_extent_sort_cmp (const void *l,const void *r,void __always_unused *args) +{ +struct btree_iter_set *_l = (struct btree_iter_set *)l; +struct btree_iter_set *_r = (struct btree_iter_set *)r; +int64_t c =bkey_cmp (&START_KEY (_l->k), &START_KEY (_r->k)); + +return !(c ? c >0 : _l->k < _r->k); +} + +static inline void new_btree_iter_swap (void *iter1,void *iter2,void __always_unused *args) { -int64_t c =bkey_cmp (&START_KEY (l.k), &START_KEY (r.k)); +struct btree_iter_set *_iter1 = iter1; +struct btree_iter_set *_iter2 = iter2; -return c ? c >0 : l.k < r.k; +swap (*_iter1, *_iter2); }static struct bkey *bch_extent_sort_fixup (struct btree_iter *iter,struct bkey *tmp) { -while (iter->used >1 ) { -struct btree_iter_set *top = iter->data, *i = top +1 ; - -if (iter->used >2 && -bch_extent_sort_cmp (i[0 ], i[1 ])) +const struct min_heap_callbacks callbacks = { + .less = new_bch_extent_sort_cmp, + .swp = new_btree_iter_swap, + }; +while (iter->heap.nr >1 ) { +struct btree_iter_set *top = iter->heap.data, *i = top +1 ; + +if (iter->heap.nr >2 && + !new_bch_extent_sort_cmp (&i[0 ], &i[1 ],NULL )) i++;if (bkey_cmp (top->k, &START_KEY (i->k)) <=0 )@@ -278,7 +293,7 @@ static struct bkey *bch_extent_sort_fixup(struct btree_iter *iter,if (!KEY_SIZE (i->k)) {sort_key_next (iter, i); -heap_sift (iter, i - top, bch_extent_sort_cmp); +min_heap_sift_down (&iter->heap, i - top, &callbacks,NULL );continue ; }@@ -288,7 +303,7 @@ static struct bkey *bch_extent_sort_fixup(struct btree_iter *iter,else bch_cut_front (top->k, i->k); -heap_sift (iter, i - top, bch_extent_sort_cmp); +min_heap_sift_down (&iter->heap, i - top, &callbacks,NULL ); }else {/* can't happen because of comparison func */ BUG_ON (!bkey_cmp (&START_KEY (top->k), &START_KEY (i->k)));@@ -298,7 +313,7 @@ static struct bkey *bch_extent_sort_fixup(struct btree_iter *iter,bch_cut_back (&START_KEY (i->k), tmp);bch_cut_front (i->k, top->k); -heap_sift (iter,0 , bch_extent_sort_cmp); +min_heap_sift_down (&iter->heap,0 , &callbacks,NULL );return tmp; }else {@@ -618,7 +633,7 @@ static bool bch_extent_merge(struct btree_keys *bk, }const struct btree_keys_ops bch_extent_keys_ops = { - .sort_cmp = bch_extent_sort_cmp, + .sort_cmp = new_bch_extent_sort_cmp, .sort_fixup = bch_extent_sort_fixup, .insert_fixup = bch_extent_insert_fixup, .key_invalid = bch_extent_invalid,drivers/md/bcache/movinggc.c +31
-10@@ -182,16 +182,27 @@ err: if (!IS_ERR_OR_NULL(w->private))closure_sync (&cl); } -static bool bucket_cmp (struct bucket *l,struct bucket *r) +static bool new_bucket_cmp (const void *l,const void *r,void __always_unused *args) { -return GC_SECTORS_USED (l) <GC_SECTORS_USED (r); +struct bucket **_l = (struct bucket **)l; +struct bucket **_r = (struct bucket **)r; + +return GC_SECTORS_USED (*_l) >=GC_SECTORS_USED (*_r); +} + +static void new_bucket_swap (void *l,void *r,void __always_unused *args) +{ +struct bucket **_l = l; +struct bucket **_r = r; + +swap (*_l, *_r); }static unsigned int bucket_heap_top (struct cache *ca) {struct bucket *b; -return (b =heap_peek (&ca->heap)) ?GC_SECTORS_USED (b) :0 ; +return (b =min_heap_peek (&ca->heap)[0 ]) ?GC_SECTORS_USED (b) :0 ; }void bch_moving_gc (struct cache_set *c)@@ -199,6 +210,10 @@ void bch_moving_gc(struct cache_set *c)struct cache *ca = c->cache;struct bucket *b;unsigned long sectors_to_move, reserve_sectors; +const struct min_heap_callbacks callbacks = { + .less = new_bucket_cmp, + .swp = new_bucket_swap, + };if (!c->copy_gc_enabled)return ;@@ -209,7 +224,7 @@ void bch_moving_gc(struct cache_set *c) reserve_sectors = ca->sb.bucket_size *fifo_used (&ca->free[RESERVE_MOVINGGC ]); - ca->heap.used =0 ; + ca->heap.nr =0 ;for_each_bucket (b, ca) { if (GC_MARK(b) == GC_MARK_METADATA ||@@ -218,25 +233,31 @@ void bch_moving_gc(struct cache_set *c) atomic_read(&b->pin))continue ; -if (!heap_full (&ca->heap)) { +if (!min_heap_full (&ca->heap)) { sectors_to_move +=GC_SECTORS_USED (b); -heap_add (&ca->heap, b, bucket_cmp); - }else if (bucket_cmp (b,heap_peek (&ca->heap))) { +min_heap_push (&ca->heap, &b, &callbacks,NULL ); + }else if (!new_bucket_cmp (&b,min_heap_peek (&ca->heap), ca)) { sectors_to_move -=bucket_heap_top (ca); sectors_to_move +=GC_SECTORS_USED (b); ca->heap.data[0 ] = b; -heap_sift (&ca->heap,0 , bucket_cmp); +min_heap_sift_down (&ca->heap,0 , &callbacks,NULL ); } }while (sectors_to_move > reserve_sectors) { -heap_pop (&ca->heap, b, bucket_cmp); +if (ca->heap.nr) { + b =min_heap_peek (&ca->heap)[0 ]; +min_heap_pop (&ca->heap, &callbacks,NULL ); + } sectors_to_move -=GC_SECTORS_USED (b); } -while (heap_pop (&ca->heap, b, bucket_cmp)) +while (ca->heap.nr) { + b =min_heap_peek (&ca->heap)[0 ]; +min_heap_pop (&ca->heap, &callbacks,NULL );SET_GC_MOVE (b,1 ); + }mutex_unlock (&c->bucket_lock);drivers/md/bcache/sysfs.c +2
-0@@ -662,6 +662,8 @@ static unsigned int bch_root_usage(struct cache_set *c)struct btree *b;struct btree_iter iter; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); + goto lock_root; do {drivers/md/bcache/util.h +2
-65@@ -9,6 +9,7 @@#include <linux/kernel.h> #include <linux/sched/clock.h> #include <linux/llist.h> +#include <linux/min_heap.h> #include <linux/ratelimit.h> #include <linux/vmalloc.h> #include <linux/workqueue.h> @@ -30,16 +31,10 @@ struct closure;#endif -#define DECLARE_HEAP (type, name) \ - struct { \ - size_t size, used; \ - type *data; \ - } name -#define init_heap (heap, _size, gfp) \ ({ \ size_t _bytes; \ - (heap)->used = 0; \ + (heap)->nr = 0; \ (heap)->size = (_size); \ _bytes = (heap)->size * sizeof(*(heap)->data); \ (heap)->data = kvmalloc(_bytes, (gfp) & GFP_KERNEL); \@@ -52,64 +47,6 @@ do { \ (heap)->data =NULL ; \ } while (0) -#define heap_swap (h, i, j) swap((h)->data[i], (h)->data[j]) - -#define heap_sift (h, i, cmp) \ -do { \ - size_t _r, _j = i; \ - \ - for (; _j * 2 + 1 < (h)->used; _j = _r) { \ - _r = _j * 2 + 1; \ - if (_r + 1 < (h)->used && \ - cmp((h)->data[_r], (h)->data[_r + 1])) \ - _r++; \ - \ - if (cmp((h)->data[_r], (h)->data[_j])) \ - break; \ - heap_swap(h, _r, _j); \ - } \ -} while (0) - -#define heap_sift_down (h, i, cmp) \ -do { \ - while (i) { \ - size_t p = (i - 1) / 2; \ - if (cmp((h)->data[i], (h)->data[p])) \ - break; \ - heap_swap(h, i, p); \ - i = p; \ - } \ -} while (0) - -#define heap_add (h, d, cmp) \ -({ \ - bool _r = !heap_full(h); \ - if (_r) { \ - size_t _i = (h)->used++; \ - (h)->data[_i] = d; \ - \ - heap_sift_down(h, _i, cmp); \ - heap_sift(h, _i, cmp); \ - } \ - _r; \ -}) - -#define heap_pop (h, d, cmp) \ -({ \ - bool _r = (h)->used; \ - if (_r) { \ - (d) = (h)->data[0]; \ - (h)->used--; \ - heap_swap(h, 0, (h)->used); \ - heap_sift(h, 0, cmp); \ - } \ - _r; \ -}) - -#define heap_peek (h) ((h)->used ? (h)->data[0] : NULL) - -#define heap_full (h) ((h)->used == (h)->size) -#define DECLARE_FIFO (type, name) \ struct { \ size_t front, back, size, mask; \drivers/md/bcache/writeback.c +3
-0@@ -915,6 +915,7 @@ static int bch_dirty_init_thread(void *arg) k = p =NULL ; prev_idx =0 ; +min_heap_init (&iter.heap,NULL ,MAX_BSETS );bch_btree_iter_init (&c->root->keys, &iter,NULL ); k =bch_btree_iter_next_filter (&iter, &c->root->keys, bch_ptr_bad);BUG_ON (!k);@@ -984,6 +985,8 @@ void bch_sectors_dirty_init(struct bcache_device *d)struct cache_set *c = d->c;struct bch_dirty_init_state state; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); + retry_lock: b = c->root;rw_lock (0 , b, b->level);Signature · 1 line
2.34.1
Signature · 1 line
Coly Li
Re: [PATCH v3 16/17] bcache: Remove heap-related macros and switch to generic min_heap
In reply to Kuan-Wei ChiuQuoted 9 lines · Kuan-Wei Chiu
Drop the heap-related macros from bcache and replacing them with the generic
min_heapimplementation frominclude/linux. By doing so, code readability is improved by using functions instead of macros. Moreover, themin_heapimplementation ininclude/linuxadopts a bottom-up variation compared to the textbook version currently used in bcache. This bottom-up variation allows for approximately 50% reduction in the number of comparison operations during heap siftdown, without changing the number of swaps, thus making it more efficient.
Quoted 3 lines
For the bcache replacement part,
Thanks.
Coly Li
Quoted 790 lines
---
Changes in v3:
- Correct bugs where the parameter types in some compare functions should have been 'type **', but were mistakenly written as 'type *'.drivers/md/bcache/alloc.c | 64 +++++++++++++++++++------- drivers/md/bcache/bcache.h | 2 +- drivers/md/bcache/bset.c | 84 ++++++++++++++++++++++++----------- drivers/md/bcache/bset.h | 14 +++--- drivers/md/bcache/btree.c | 17 ++++++- drivers/md/bcache/extents.c | 53 ++++++++++++++-------- drivers/md/bcache/movinggc.c | 41 ++++++++++++----- drivers/md/bcache/sysfs.c | 2 + drivers/md/bcache/util.h | 67 +--------------------------- drivers/md/bcache/writeback.c | 3 ++ 10 files changed, 202 insertions(+), 145 deletions(-)drivers/md/bcache/alloc.c +47
-17@@ -166,40 +166,68 @@ static void bch_invalidate_one_bucket(struct cache *ca, struct bucket *b)* prio is worth 1/8th of what INITIAL_PRIO is worth. */ -#define bucket_prio (b) \ -({ \ - unsigned int min_prio = (INITIAL_PRIO - ca->set->min_prio) / 8; \ - \ - (b->prio - ca->set->min_prio + min_prio) * GC_SECTORS_USED(b); \ -}) +static inline unsigned int new_bucket_prio (struct cache *ca,struct bucket *b) +{ +unsigned int min_prio = (INITIAL_PRIO - ca->set->min_prio) /8 ; + +return (b->prio - ca->set->min_prio + min_prio) *GC_SECTORS_USED (b); +} + +static inline bool new_bucket_max_cmp (const void *l,const void *r,void *args) +{ +struct bucket **lhs = (struct bucket **)l; +struct bucket **rhs = (struct bucket **)r; +struct cache *ca = args; + +return new_bucket_prio (ca, *lhs) >new_bucket_prio (ca, *rhs); +} -#define bucket_max_cmp (l, r) (bucket_prio(l) < bucket_prio(r)) -#define bucket_min_cmp (l, r) (bucket_prio(l) > bucket_prio(r)) +static inline bool new_bucket_min_cmp (const void *l,const void *r,void *args) +{ +struct bucket **lhs = (struct bucket **)l; +struct bucket **rhs = (struct bucket **)r; +struct cache *ca = args; + +return new_bucket_prio (ca, *lhs) <new_bucket_prio (ca, *rhs); +} + +static inline void new_bucket_swap (void *l,void *r,void __always_unused *args) +{ +struct bucket **lhs = l, **rhs = r; + +swap (*lhs, *rhs); +}static void invalidate_buckets_lru (struct cache *ca) {struct bucket *b; -ssize_t i; +const struct min_heap_callbacks bucket_max_cmp_callback = { + .less = new_bucket_max_cmp, + .swp = new_bucket_swap, + }; +const struct min_heap_callbacks bucket_min_cmp_callback = { + .less = new_bucket_min_cmp, + .swp = new_bucket_swap, + }; - ca->heap.used =0 ; + ca->heap.nr =0 ;for_each_bucket (b, ca) {if (!bch_can_invalidate_bucket (ca, b))continue ; -if (!heap_full (&ca->heap)) -heap_add (&ca->heap, b, bucket_max_cmp); -else if (bucket_max_cmp (b,heap_peek (&ca->heap))) { +if (!min_heap_full (&ca->heap)) +min_heap_push (&ca->heap, &b, &bucket_max_cmp_callback, ca); +else if (!new_bucket_max_cmp (&b,min_heap_peek (&ca->heap), ca)) { ca->heap.data[0 ] = b; -heap_sift (&ca->heap,0 , bucket_max_cmp); +min_heap_sift_down (&ca->heap,0 , &bucket_max_cmp_callback, ca); } } -for (i = ca->heap.used /2 -1 ; i >=0 ; --i) -heap_sift (&ca->heap, i, bucket_min_cmp); +min_heapify_all (&ca->heap, &bucket_min_cmp_callback, ca);while (!fifo_full (&ca->free_inc)) { -if (!heap_pop (&ca->heap, b, bucket_min_cmp)) { +if (!ca->heap.nr) {/* * We don't want to be calling invalidate_buckets() * multiple times when it can't do anything @@ -208,6 +236,8 @@ static void invalidate_buckets_lru(struct cache *ca)wake_up_gc (ca->set);return ; } + b =min_heap_peek (&ca->heap)[0 ]; +min_heap_pop (&ca->heap, &bucket_min_cmp_callback, ca);bch_invalidate_one_bucket (ca, b); }drivers/md/bcache/bcache.h +1
-1@@ -457,7 +457,7 @@ struct cache {/* Allocation stuff: */ struct bucket *buckets; - DECLARE_HEAP(struct bucket *, heap); + MIN_HEAP(struct bucket *, cache_heap) heap;/* * If nonzero, we know we aren't going to find any buckets to invalidate drivers/md/bcache/bset.c +59
-25@@ -57,6 +57,8 @@ int __bch_count_data(struct btree_keys *b)struct btree_iter iter;struct bkey *k; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); +if (b->ops->is_extents)for_each_key (b, k, &iter) ret +=KEY_SIZE (k);@@ -70,6 +72,8 @@ void __bch_check_keys(struct btree_keys *b, const char *fmt, ...)struct btree_iter iter;const char *err; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); +for_each_key (b, k, &iter) {if (b->ops->is_extents) { err ="Keys out of order" ;@@ -110,9 +114,9 @@ void __bch_check_keys(struct btree_keys *b, const char *fmt, ...)static void bch_btree_iter_next_check (struct btree_iter *iter) { -struct bkey *k = iter->data->k, *next =bkey_next (k); +struct bkey *k = iter->heap.data->k, *next =bkey_next (k); -if (next < iter->data->end && +if (next < iter->heap.data->end &&bkey_cmp (k, iter->b->ops->is_extents ? &START_KEY (next) : next) >0 ) {bch_dump_bucket (iter->b);@@ -885,6 +889,8 @@ unsigned int bch_btree_insert_key(struct btree_keys *b, struct bkey *k,BUG_ON (b->ops->is_extents && !KEY_SIZE (k)); +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); +/* * If k has preceding key, preceding_key_p will be set to address * of k's preceding key; otherwise preceding_key_p will be set @@ -1077,27 +1083,42 @@ struct bkey *__bch_bset_search(struct btree_keys *b, struct bset_tree *t,/* Btree iterator */ -typedef bool (btree_iter_cmp_fn )(struct btree_iter_set , -struct btree_iter_set ); +typedef bool (new_btree_iter_cmp_fn )(const void *,const void *,void *); + +static inline bool new_btree_iter_cmp (const void *l,const void *r,void __always_unused *args) +{ +const struct btree_iter_set *_l = l; +const struct btree_iter_set *_r = r; + +return bkey_cmp (_l->k, _r->k) <=0 ; +} -static inline bool btree_iter_cmp (struct btree_iter_set l, -struct btree_iter_set r) +static inline void new_btree_iter_swap (void *iter1,void *iter2,void __always_unused *args) { -return bkey_cmp (l.k, r.k) >0 ; +struct btree_iter_set *_iter1 = iter1; +struct btree_iter_set *_iter2 = iter2; + +swap (*_iter1, *_iter2); }static inline bool btree_iter_end (struct btree_iter *iter) { -return !iter->used; +return !iter->heap.nr; }void bch_btree_iter_push (struct btree_iter *iter,struct bkey *k,struct bkey *end) { +const struct min_heap_callbacks callbacks = { + .less = new_btree_iter_cmp, + .swp = new_btree_iter_swap, + }; +if (k != end) -BUG_ON (!heap_add (iter, - ((struct btree_iter_set ) { k, end }), - btree_iter_cmp)); +BUG_ON (!min_heap_push (&iter->heap, + &((struct btree_iter_set ) { k, end }), + &callbacks, +NULL )); } static struct bkey *__bch_btree_iter_init(struct btree_keys *b,@@ -1107,8 +1128,8 @@ static struct bkey *__bch_btree_iter_init(struct btree_keys *b, {struct bkey *ret =NULL ; - iter->size =ARRAY_SIZE (iter->data); - iter->used =0 ; + iter->heap.size =ARRAY_SIZE (iter->heap.preallocated); + iter->heap.nr =0 ;#ifdef CONFIG_BCACHE_DEBUG iter->b = b;@@ -1130,26 +1151,34 @@ struct bkey *bch_btree_iter_init(struct btree_keys *b, }static inline struct bkey *__bch_btree_iter_next (struct btree_iter *iter, -btree_iter_cmp_fn *cmp) +new_btree_iter_cmp_fn *cmp) {struct btree_iter_set b__maybe_unused ;struct bkey *ret =NULL ; +const struct min_heap_callbacks callbacks = { + .less = cmp, + .swp = new_btree_iter_swap, + };if (!btree_iter_end (iter)) {bch_btree_iter_next_check (iter); - ret = iter->data->k; - iter->data->k =bkey_next (iter->data->k); + ret = iter->heap.data->k; + iter->heap.data->k =bkey_next (iter->heap.data->k); -if (iter->data->k > iter->data->end) { +if (iter->heap.data->k > iter->heap.data->end) {WARN_ONCE (1 ,"bset was corrupt!\n" ); - iter->data->k = iter->data->end; + iter->heap.data->k = iter->heap.data->end; } -if (iter->data->k == iter->data->end) -heap_pop (iter, b, cmp); +if (iter->heap.data->k == iter->heap.data->end) { +if (iter->heap.nr) { + b =min_heap_peek (&iter->heap)[0 ]; +min_heap_pop (&iter->heap, &callbacks,NULL ); + } + }else -heap_sift (iter,0 , cmp); +min_heap_sift_down (&iter->heap,0 , &callbacks,NULL ); }return ret;@@ -1157,7 +1186,7 @@ static inline struct bkey *__bch_btree_iter_next(struct btree_iter *iter,struct bkey *bch_btree_iter_next (struct btree_iter *iter) { -return __bch_btree_iter_next (iter, btree_iter_cmp); +return __bch_btree_iter_next (iter, new_btree_iter_cmp); }@@ -1195,16 +1224,18 @@ static void btree_mergesort(struct btree_keys *b, struct bset *out,struct btree_iter *iter, bool fixup, bool remove_stale) { - int i; struct bkey *k, *last = NULL;BKEY_PADDED (k) tmp;bool (*bad)(struct btree_keys *,const struct bkey *) = remove_stale ? bch_ptr_bad : bch_ptr_invalid; +const struct min_heap_callbacks callbacks = { + .less = b->ops->sort_cmp, + .swp = new_btree_iter_swap, + };/* Heapify the iterator, using our comparison function */ -for (i = iter->used /2 -1 ; i >=0 ; --i) -heap_sift (iter, i, b->ops->sort_cmp); +min_heapify_all (&iter->heap, &callbacks,NULL );while (!btree_iter_end (iter)) {if (b->ops->sort_fixup && fixup)@@ -1296,6 +1327,7 @@ void bch_btree_sort_partial(struct btree_keys *b, unsigned int start,struct btree_iter iter;int oldsize =bch_count_data (b); +min_heap_init (&iter.heap,NULL ,MAX_BSETS );__bch_btree_iter_init (b, &iter,NULL , &b->set[start]);if (start) {@@ -1325,6 +1357,8 @@ void bch_btree_sort_into(struct btree_keys *b, struct btree_keys *new,uint64_t start_time =local_clock ();struct btree_iter iter; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); +bch_btree_iter_init (b, &iter,NULL );btree_mergesort (b, new->set->data, &iter, false, true);drivers/md/bcache/bset.h +8
-6@@ -187,8 +187,9 @@ struct bset_tree { };struct btree_keys_ops { -bool (*sort_cmp)(struct btree_iter_set l, -struct btree_iter_set r); +bool (*sort_cmp)(const void *l, +const void *r, +void *args);struct bkey *(*sort_fixup)(struct btree_iter *iter,struct bkey *tmp); bool (*insert_fixup)(struct btree_keys *b,@@ -312,16 +313,17 @@ enum { BTREE_INSERT_STATUS_FRONT_MERGE, }; +struct btree_iter_set { +struct bkey *k, *end; +}; +/* Btree key iteration */ struct btree_iter { -size_t size, used;#ifdef CONFIG_BCACHE_DEBUG struct btree_keys *b;#endif -struct btree_iter_set { -struct bkey *k, *end; - } data[MAX_BSETS ]; + MIN_HEAP_PREALLOCATED(struct btree_iter_set, btree_iter_heap, MAX_BSETS) heap; };typedef bool (*ptr_filter_fn )(struct btree_keys *b,const struct bkey *k);drivers/md/bcache/btree.c +15
-2@@ -157,8 +157,8 @@ void bch_btree_node_read_done(struct btree *b)* See the comment arount cache_set->fill_iter. */ iter =mempool_alloc (&b->c->fill_iter,GFP_NOIO ); - iter->size = b->c->cache->sb.bucket_size / b->c->cache->sb.block_size; - iter->used =0 ; + iter->heap.size = b->c->cache->sb.bucket_size / b->c->cache->sb.block_size; + iter->heap.nr =0 ;#ifdef CONFIG_BCACHE_DEBUG iter->b = &b->keys;@@ -1312,6 +1312,8 @@ static bool btree_gc_mark_node(struct btree *b, struct gc_stat *gc)struct btree_iter iter;struct bset_tree *t; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); + gc->nodes++;for_each_key_filter (&b->keys, k, &iter, bch_ptr_invalid) {@@ -1573,6 +1575,8 @@ static unsigned int btree_gc_count_keys(struct btree *b)struct btree_iter iter;unsigned int ret =0 ; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); +for_each_key_filter (&b->keys, k, &iter, bch_ptr_bad) ret +=bkey_u64s (k);@@ -1615,6 +1619,7 @@ static int btree_gc_recurse(struct btree *b, struct btree_op *op,struct gc_merge_info r[GC_MERGE_NODES ];struct gc_merge_info *i, *last = r +ARRAY_SIZE (r) -1 ; +min_heap_init (&iter.heap,NULL ,MAX_BSETS );bch_btree_iter_init (&b->keys, &iter, &b->c->gc_done);for (i = r; i < r +ARRAY_SIZE (r); i++)@@ -1913,6 +1918,8 @@ static int bch_btree_check_recurse(struct btree *b, struct btree_op *op)struct bkey *k, *p =NULL ;struct btree_iter iter; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); +for_each_key_filter (&b->keys, k, &iter, bch_ptr_invalid)bch_initial_mark_key (b->c, b->level, k);@@ -1958,6 +1965,8 @@ static int bch_btree_check_thread(void *arg) cur_idx = prev_idx =0 ; ret =0 ; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); +/* root node keys are checked before thread created */ bch_btree_iter_init (&c->root->keys, &iter,NULL ); k =bch_btree_iter_next_filter (&iter, &c->root->keys, bch_ptr_bad);@@ -2054,6 +2063,8 @@ int bch_btree_check(struct cache_set *c)struct btree_iter iter;struct btree_check_state check_state; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); +/* check and mark root node keys */ for_each_key_filter (&c->root->keys, k, &iter, bch_ptr_invalid)bch_initial_mark_key (c, c->root->level, k);@@ -2549,6 +2560,7 @@ static int bch_btree_map_nodes_recurse(struct btree *b, struct btree_op *op,struct bkey *k;struct btree_iter iter; +min_heap_init (&iter.heap,NULL ,MAX_BSETS );bch_btree_iter_init (&b->keys, &iter, from); while ((k = bch_btree_iter_next_filter(&iter, &b->keys,@@ -2582,6 +2594,7 @@ int bch_btree_map_keys_recurse(struct btree *b, struct btree_op *op,struct bkey *k;struct btree_iter iter; +min_heap_init (&iter.heap,NULL ,MAX_BSETS );bch_btree_iter_init (&b->keys, &iter, from);while ((k =bch_btree_iter_next_filter (&iter, &b->keys, bch_ptr_bad))) {drivers/md/bcache/extents.c +34
-19@@ -33,15 +33,16 @@ static void sort_key_next(struct btree_iter *iter, i->k =bkey_next (i->k);if (i->k == i->end) - *i = iter->data[--iter->used]; + *i = iter->heap.data[--iter->heap.nr]; } -static bool bch_key_sort_cmp (struct btree_iter_set l, -struct btree_iter_set r) +static bool new_bch_key_sort_cmp (const void *l,const void *r,void *args) { -int64_t c =bkey_cmp (l.k, r.k); +struct btree_iter_set *_l = (struct btree_iter_set *)l; +struct btree_iter_set *_r = (struct btree_iter_set *)r; +int64_t c =bkey_cmp (_l->k, _r->k); -return c ? c >0 : l.k < r.k; +return !(c ? c >0 : _l->k < _r->k); }static bool __ptr_invalid (struct cache_set *c,const struct bkey *k)@@ -238,7 +239,7 @@ static bool bch_btree_ptr_insert_fixup(struct btree_keys *bk, }const struct btree_keys_ops bch_btree_keys_ops = { - .sort_cmp = bch_key_sort_cmp, + .sort_cmp = new_bch_key_sort_cmp, .insert_fixup = bch_btree_ptr_insert_fixup, .key_invalid = bch_btree_ptr_invalid, .key_bad = bch_btree_ptr_bad,@@ -255,22 +256,36 @@ const struct btree_keys_ops bch_btree_keys_ops = {* Necessary for btree_sort_fixup() - if there are multiple keys that compare * equal in different sets, we have to process them newest to oldest. */ -static bool bch_extent_sort_cmp (struct btree_iter_set l, -struct btree_iter_set r) + +static bool new_bch_extent_sort_cmp (const void *l,const void *r,void __always_unused *args) +{ +struct btree_iter_set *_l = (struct btree_iter_set *)l; +struct btree_iter_set *_r = (struct btree_iter_set *)r; +int64_t c =bkey_cmp (&START_KEY (_l->k), &START_KEY (_r->k)); + +return !(c ? c >0 : _l->k < _r->k); +} + +static inline void new_btree_iter_swap (void *iter1,void *iter2,void __always_unused *args) { -int64_t c =bkey_cmp (&START_KEY (l.k), &START_KEY (r.k)); +struct btree_iter_set *_iter1 = iter1; +struct btree_iter_set *_iter2 = iter2; -return c ? c >0 : l.k < r.k; +swap (*_iter1, *_iter2); }static struct bkey *bch_extent_sort_fixup (struct btree_iter *iter,struct bkey *tmp) { -while (iter->used >1 ) { -struct btree_iter_set *top = iter->data, *i = top +1 ; - -if (iter->used >2 && -bch_extent_sort_cmp (i[0 ], i[1 ])) +const struct min_heap_callbacks callbacks = { + .less = new_bch_extent_sort_cmp, + .swp = new_btree_iter_swap, + }; +while (iter->heap.nr >1 ) { +struct btree_iter_set *top = iter->heap.data, *i = top +1 ; + +if (iter->heap.nr >2 && + !new_bch_extent_sort_cmp (&i[0 ], &i[1 ],NULL )) i++;if (bkey_cmp (top->k, &START_KEY (i->k)) <=0 )@@ -278,7 +293,7 @@ static struct bkey *bch_extent_sort_fixup(struct btree_iter *iter,if (!KEY_SIZE (i->k)) {sort_key_next (iter, i); -heap_sift (iter, i - top, bch_extent_sort_cmp); +min_heap_sift_down (&iter->heap, i - top, &callbacks,NULL );continue ; }@@ -288,7 +303,7 @@ static struct bkey *bch_extent_sort_fixup(struct btree_iter *iter,else bch_cut_front (top->k, i->k); -heap_sift (iter, i - top, bch_extent_sort_cmp); +min_heap_sift_down (&iter->heap, i - top, &callbacks,NULL ); }else {/* can't happen because of comparison func */ BUG_ON (!bkey_cmp (&START_KEY (top->k), &START_KEY (i->k)));@@ -298,7 +313,7 @@ static struct bkey *bch_extent_sort_fixup(struct btree_iter *iter,bch_cut_back (&START_KEY (i->k), tmp);bch_cut_front (i->k, top->k); -heap_sift (iter,0 , bch_extent_sort_cmp); +min_heap_sift_down (&iter->heap,0 , &callbacks,NULL );return tmp; }else {@@ -618,7 +633,7 @@ static bool bch_extent_merge(struct btree_keys *bk, }const struct btree_keys_ops bch_extent_keys_ops = { - .sort_cmp = bch_extent_sort_cmp, + .sort_cmp = new_bch_extent_sort_cmp, .sort_fixup = bch_extent_sort_fixup, .insert_fixup = bch_extent_insert_fixup, .key_invalid = bch_extent_invalid,drivers/md/bcache/movinggc.c +31
-10@@ -182,16 +182,27 @@ err: if (!IS_ERR_OR_NULL(w->private))closure_sync (&cl); } -static bool bucket_cmp (struct bucket *l,struct bucket *r) +static bool new_bucket_cmp (const void *l,const void *r,void __always_unused *args) { -return GC_SECTORS_USED (l) <GC_SECTORS_USED (r); +struct bucket **_l = (struct bucket **)l; +struct bucket **_r = (struct bucket **)r; + +return GC_SECTORS_USED (*_l) >=GC_SECTORS_USED (*_r); +} + +static void new_bucket_swap (void *l,void *r,void __always_unused *args) +{ +struct bucket **_l = l; +struct bucket **_r = r; + +swap (*_l, *_r); }static unsigned int bucket_heap_top (struct cache *ca) {struct bucket *b; -return (b =heap_peek (&ca->heap)) ?GC_SECTORS_USED (b) :0 ; +return (b =min_heap_peek (&ca->heap)[0 ]) ?GC_SECTORS_USED (b) :0 ; }void bch_moving_gc (struct cache_set *c)@@ -199,6 +210,10 @@ void bch_moving_gc(struct cache_set *c)struct cache *ca = c->cache;struct bucket *b;unsigned long sectors_to_move, reserve_sectors; +const struct min_heap_callbacks callbacks = { + .less = new_bucket_cmp, + .swp = new_bucket_swap, + };if (!c->copy_gc_enabled)return ;@@ -209,7 +224,7 @@ void bch_moving_gc(struct cache_set *c) reserve_sectors = ca->sb.bucket_size *fifo_used (&ca->free[RESERVE_MOVINGGC ]); - ca->heap.used =0 ; + ca->heap.nr =0 ;for_each_bucket (b, ca) { if (GC_MARK(b) == GC_MARK_METADATA ||@@ -218,25 +233,31 @@ void bch_moving_gc(struct cache_set *c) atomic_read(&b->pin))continue ; -if (!heap_full (&ca->heap)) { +if (!min_heap_full (&ca->heap)) { sectors_to_move +=GC_SECTORS_USED (b); -heap_add (&ca->heap, b, bucket_cmp); - }else if (bucket_cmp (b,heap_peek (&ca->heap))) { +min_heap_push (&ca->heap, &b, &callbacks,NULL ); + }else if (!new_bucket_cmp (&b,min_heap_peek (&ca->heap), ca)) { sectors_to_move -=bucket_heap_top (ca); sectors_to_move +=GC_SECTORS_USED (b); ca->heap.data[0 ] = b; -heap_sift (&ca->heap,0 , bucket_cmp); +min_heap_sift_down (&ca->heap,0 , &callbacks,NULL ); } }while (sectors_to_move > reserve_sectors) { -heap_pop (&ca->heap, b, bucket_cmp); +if (ca->heap.nr) { + b =min_heap_peek (&ca->heap)[0 ]; +min_heap_pop (&ca->heap, &callbacks,NULL ); + } sectors_to_move -=GC_SECTORS_USED (b); } -while (heap_pop (&ca->heap, b, bucket_cmp)) +while (ca->heap.nr) { + b =min_heap_peek (&ca->heap)[0 ]; +min_heap_pop (&ca->heap, &callbacks,NULL );SET_GC_MOVE (b,1 ); + }mutex_unlock (&c->bucket_lock);drivers/md/bcache/sysfs.c +2
-0@@ -662,6 +662,8 @@ static unsigned int bch_root_usage(struct cache_set *c)struct btree *b;struct btree_iter iter; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); + goto lock_root; do {drivers/md/bcache/util.h +2
-65@@ -9,6 +9,7 @@#include <linux/kernel.h> #include <linux/sched/clock.h> #include <linux/llist.h> +#include <linux/min_heap.h> #include <linux/ratelimit.h> #include <linux/vmalloc.h> #include <linux/workqueue.h> @@ -30,16 +31,10 @@ struct closure;#endif -#define DECLARE_HEAP (type, name) \ - struct { \ - size_t size, used; \ - type *data; \ - } name -#define init_heap (heap, _size, gfp) \ ({ \ size_t _bytes; \ - (heap)->used = 0; \ + (heap)->nr = 0; \ (heap)->size = (_size); \ _bytes = (heap)->size * sizeof(*(heap)->data); \ (heap)->data = kvmalloc(_bytes, (gfp) & GFP_KERNEL); \@@ -52,64 +47,6 @@ do { \ (heap)->data =NULL ; \ } while (0) -#define heap_swap (h, i, j) swap((h)->data[i], (h)->data[j]) - -#define heap_sift (h, i, cmp) \ -do { \ - size_t _r, _j = i; \ - \ - for (; _j * 2 + 1 < (h)->used; _j = _r) { \ - _r = _j * 2 + 1; \ - if (_r + 1 < (h)->used && \ - cmp((h)->data[_r], (h)->data[_r + 1])) \ - _r++; \ - \ - if (cmp((h)->data[_r], (h)->data[_j])) \ - break; \ - heap_swap(h, _r, _j); \ - } \ -} while (0) - -#define heap_sift_down (h, i, cmp) \ -do { \ - while (i) { \ - size_t p = (i - 1) / 2; \ - if (cmp((h)->data[i], (h)->data[p])) \ - break; \ - heap_swap(h, i, p); \ - i = p; \ - } \ -} while (0) - -#define heap_add (h, d, cmp) \ -({ \ - bool _r = !heap_full(h); \ - if (_r) { \ - size_t _i = (h)->used++; \ - (h)->data[_i] = d; \ - \ - heap_sift_down(h, _i, cmp); \ - heap_sift(h, _i, cmp); \ - } \ - _r; \ -}) - -#define heap_pop (h, d, cmp) \ -({ \ - bool _r = (h)->used; \ - if (_r) { \ - (d) = (h)->data[0]; \ - (h)->used--; \ - heap_swap(h, 0, (h)->used); \ - heap_sift(h, 0, cmp); \ - } \ - _r; \ -}) - -#define heap_peek (h) ((h)->used ? (h)->data[0] : NULL) - -#define heap_full (h) ((h)->used == (h)->size) -#define DECLARE_FIFO (type, name) \ struct { \ size_t front, back, size, mask; \drivers/md/bcache/writeback.c +3
-0@@ -915,6 +915,7 @@ static int bch_dirty_init_thread(void *arg) k = p =NULL ; prev_idx =0 ; +min_heap_init (&iter.heap,NULL ,MAX_BSETS );bch_btree_iter_init (&c->root->keys, &iter,NULL ); k =bch_btree_iter_next_filter (&iter, &c->root->keys, bch_ptr_bad);BUG_ON (!k);@@ -984,6 +985,8 @@ void bch_sectors_dirty_init(struct bcache_device *d)struct cache_set *c = d->c;struct bch_dirty_init_state state; +min_heap_init (&iter.heap,NULL ,MAX_BSETS ); + retry_lock: b = c->root;rw_lock (0 , b, b->level);Signature · 1 line
2.34.1
Signature · 4 lines
Coly Li -- Coly Li
Kuan-Wei Chiu
maintainerPATCH v3 17/17 bcachefs: Remove heap-related macros and switch to generic min_heap
In reply to Kuan-Wei ChiuDrop the heap-related macros from bcachefs and replacing them with the generic min_heap implementation from include/linux. By doing so, code readability is improved by using functions instead of macros. Moreover, the min_heap implementation in include/linux adopts a bottom-up variation compared to the textbook version currently used in bcachefs. This bottom-up variation allows for approximately 50% reduction in the number of comparison operations during heap siftdown, without changing the number of swaps, thus making it more efficient.
---
Changes in v3:
- Correct bugs where the parameter types in some compare functions should have been 'type **', but were mistakenly written as 'type *'.
fs/bcachefs/clock.c | 43 ++++++++++---- fs/bcachefs/clock_types.h | 2 +- fs/bcachefs/ec.c | 76 ++++++++++++++++-------- fs/bcachefs/ec_types.h | 2 +- fs/bcachefs/util.h | 118 +------------------------------------- 5 files changed, 87 insertions(+), 154 deletions(-)
fs/bcachefs/clock.c +32 -11
@@ -6,16 +6,29 @@#include <linux/kthread.h> #include <linux/preempt.h> -static inline long io_timer_cmp (io_timer_heap *h, -struct io_timer *l, -struct io_timer *r) +static inline bool io_timer_cmp (const void *l,const void *r,void __always_unused *args) { -return l->expire - r->expire; +struct io_timer **_l = (struct io_timer **)l; +struct io_timer **_r = (struct io_timer **)r; + +return (*_l)->expire < (*_r)->expire; +} + +static inline void io_timer_swp (void *l,void *r,void __always_unused *args) +{ +struct io_timer **_l = (struct io_timer **)l; +struct io_timer **_r = (struct io_timer **)r; + +swap (*_l, *_r); }void bch2_io_timer_add (struct io_clock *clock,struct io_timer *timer) {size_t i; +const struct min_heap_callbacks callbacks = { + .less = io_timer_cmp, + .swp = io_timer_swp, + };spin_lock (&clock->timer_lock);
@@ -26,11 +39,11 @@ void bch2_io_timer_add(struct io_clock *clock, struct io_timer *timer)return ; } -for (i =0 ; i < clock->timers.used; i++) +for (i =0 ; i < clock->timers.nr; i++)if (clock->timers.data[i] == timer) goto out; -BUG_ON (!heap_add (&clock->timers, timer, io_timer_cmp,NULL )); +BUG_ON (!min_heap_push (&clock->timers, &timer, &callbacks,NULL )); out:spin_unlock (&clock->timer_lock); }
@@ -38,12 +51,16 @@ void bch2_io_timer_add(struct io_clock *clock, struct io_timer *timer)void bch2_io_timer_del (struct io_clock *clock,struct io_timer *timer) {size_t i; +const struct min_heap_callbacks callbacks = { + .less = io_timer_cmp, + .swp = io_timer_swp, + };spin_lock (&clock->timer_lock); -for (i =0 ; i < clock->timers.used; i++) +for (i =0 ; i < clock->timers.nr; i++)if (clock->timers.data[i] == timer) { -heap_del (&clock->timers, i, io_timer_cmp,NULL ); +min_heap_del (&clock->timers, i, &callbacks,NULL );break ; }
@@ -131,12 +148,16 @@ static struct io_timer *get_expired_timer(struct io_clock *clock, unsigned long now) {struct io_timer *ret =NULL ; +const struct min_heap_callbacks callbacks = { + .less = io_timer_cmp, + .swp = io_timer_swp, + };spin_lock (&clock->timer_lock); -if (clock->timers.used && +if (clock->timers.nr &&time_after_eq (now, clock->timers.data[0 ]->expire)) -heap_pop (&clock->timers, ret, io_timer_cmp,NULL ); +min_heap_pop (&clock->timers, &callbacks,NULL );spin_unlock (&clock->timer_lock);
@@ -161,7 +182,7 @@ void bch2_io_timers_to_text(struct printbuf *out, struct io_clock *clock)spin_lock (&clock->timer_lock); now =atomic64_read (&clock->now); -for (i =0 ; i < clock->timers.used; i++) +for (i =0 ; i < clock->timers.nr; i++)prt_printf (out,"%ps:\t%li\n" , clock->timers.data[i]->fn, clock->timers.data[i]->expire - now);
fs/bcachefs/clock_types.h +1 -1
@@ -23,7 +23,7 @@ struct io_timer {/* Amount to buffer up on a percpu counter */ #define IO_CLOCK_PCPU_SECTORS 128 -typedef HEAP (struct io_timer *)io_timer_heap ; +typedef MIN_HEAP(struct io_timer *, io_timer_heap) io_timer_heap;struct io_clock {atomic64_t now;
fs/bcachefs/ec.c +51 -25
149 lines not on this page · raw message
fs/bcachefs/ec_types.h +1 -1
@@ -36,6 +36,6 @@ struct ec_stripe_heap_entry {unsigned blocks_nonempty; }; -typedef HEAP (struct ec_stripe_heap_entry )ec_stripes_heap ; +typedef MIN_HEAP(struct ec_stripe_heap_entry, ec_stripes_heap) ec_stripes_heap;#endif /* _BCACHEFS_EC_TYPES_H */
fs/bcachefs/util.h +2 -116
138 lines not on this page · raw message
Signature · 1 line
2.34.1
Kent Overstreet
maintainerQuoted 8 lines · Kuan-Wei Chiu
This patch series focuses on several adjustments related to heap implementation. Firstly, a type-safe interface has been added to the
min_heap, along with the introduction of several new functions to enhance its functionality. Additionally, the heap implementation for bcache and bcachefs has been replaced with the genericmin_heapimplementation frominclude/linux. Furthermore, several typos have been corrected.
Quoted 5 lines
Previous discussion with Kent Overstreet:
https://lkml.kernel.org/ioyfizrzq7w7mjrqcadtzsfgpuntowtjdw5pgn4qhvsdp4mqqg@nrlek5vmisbuRegards,
Kuan-Wei
pointing test automation at it now:
https://evilpiepirate.org/~testdashboard/ci?branch=refactor-heap-v3
Coli, could I get some acks from you?
Quoted 16 lines
2024年4月9日 11:40,Kent Overstreet <[email protected]> 写道:
Quoted 11 lines · Kuan-Wei Chiu
This patch series focuses on several adjustments related to heap implementation. Firstly, a type-safe interface has been added to the
min_heap, along with the introduction of several new functions to enhance its functionality. Additionally, the heap implementation for bcache and bcachefs has been replaced with the genericmin_heapimplementation frominclude/linux. Furthermore, several typos have been corrected.Previous discussion with Kent Overstreet:
https://lkml.kernel.org/ioyfizrzq7w7mjrqcadtzsfgpuntowtjdw5pgn4qhvsdp4mqqg@nrlek5vmisbu
Quoted 7 lines
Quoted 2 lines
Regards,
Kuan-Weipointing test automation at it now:
https://evilpiepirate.org/~testdashboard/ci?branch=refactor-heap-v3Coli, could I get some acks from you?
Yes, the code tested and Acked-by sent.
Thanks.
Coly Li
Kent Overstreet
maintainerQuoted 8 lines · Kuan-Wei Chiu
This patch series focuses on several adjustments related to heap implementation. Firstly, a type-safe interface has been added to the
min_heap, along with the introduction of several new functions to enhance its functionality. Additionally, the heap implementation for bcache and bcachefs has been replaced with the genericmin_heapimplementation frominclude/linux. Furthermore, several typos have been corrected.
Quoted 5 lines
Previous discussion with Kent Overstreet:
https://lkml.kernel.org/ioyfizrzq7w7mjrqcadtzsfgpuntowtjdw5pgn4qhvsdp4mqqg@nrlek5vmisbuRegards,
Kuan-Wei
We need to get this into -next, where are you at with v4?
Kuan-Wei Chiu
maintainerQuoted 9 lines · Kent Overstreet
Quoted 8 lines · Kuan-Wei Chiu
This patch series focuses on several adjustments related to heap implementation. Firstly, a type-safe interface has been added to the
min_heap, along with the introduction of several new functions to enhance its functionality. Additionally, the heap implementation for bcache and bcachefs has been replaced with the genericmin_heapimplementation frominclude/linux. Furthermore, several typos have been corrected.
Quoted 7 lines
Quoted 5 lines
Previous discussion with Kent Overstreet:
https://lkml.kernel.org/ioyfizrzq7w7mjrqcadtzsfgpuntowtjdw5pgn4qhvsdp4mqqg@nrlek5vmisbuRegards,
Kuan-WeiWe need to get this into -next, where are you at with v4?
Apologies for the delay; I've just returned from a long trip to Seattle where I attended the Open Source Summit. After reviewing the bcachefs CI testing results for v3, it appears that my patch has caused some
Kernel panic and soft lockup issues in bcachefs. However, I haven't
been able to reproduce these problems in my qemu testing environment, so I'm unsure where I went wrong to cause these errors.
Regards,
Kuan-Wei