refactor(kmod): reuse topic_local_id#1143
refactor(kmod): reuse topic_local_id#1143tomiy-0x62 wants to merge 15 commits intoautowarefoundation:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR changes how topic_local_id values are managed in the kernel module by replacing a monotonically increasing allocator with a bitmap-based allocator that supports smallest-free-ID assignment and ID recycling.
Changes:
- Replace
current_pubsub_idwith a per-topic bitmap (pubsub_id_map) to track used IDs. - Allocate the smallest available
topic_local_idviafind_first_zero_bit()for new subscribers/publishers. - Attempt to recycle IDs by clearing the corresponding bitmap bit when subscribers/publishers are removed.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| clear_bit(publisher_id, wrapper->topic.pubsub_id_map); | ||
|
|
There was a problem hiding this comment.
clear_bit(publisher_id, wrapper->topic.pubsub_id_map) happens even when pub_info (and/or message entries with en->publisher_id == publisher_id) are intentionally kept because some messages are still referenced by subscribers. This makes the ID reusable while it is still in use, allowing a new publisher to be assigned the same topic_local_id and causing collisions (e.g., find_publisher_info(wrapper, en->publisher_id) can resolve to the wrong publisher or prevent orphaned entries from being GC’d). Only recycle the ID once it is guaranteed that no remaining entries/publisher_info use it (e.g., clear the bit only when pub_info->entries_num == 0 and the publisher_info is actually removed, or track a separate lifetime/epoch for publisher instances).
There was a problem hiding this comment.
This is true. You shouldn't clear bit here: should do it as the same time when pub_info is deleted in agnocast_ioctl_remove_publisher and agnocast_ioctl_remove_subscriber.
There was a problem hiding this comment.
You should also add clear_bit for publisher_id in agnocast_ioctl_remove_subscriber.
There was a problem hiding this comment.
There was a problem hiding this comment.
publisher_id not subscriber_id.
| const topic_local_id_t new_id = | ||
| find_first_zero_bit(wrapper->topic.pubsub_id_map, MAX_TOPIC_LOCAL_ID); | ||
| if (new_id >= MAX_TOPIC_LOCAL_ID) { | ||
| dev_warn( | ||
| agnocast_device, |
There was a problem hiding this comment.
This introduces smallest-free-ID allocation / recycling for topic_local_id, but the current KUnit suite doesn’t cover reuse scenarios (remove then add should reuse the smallest freed ID; publisher IDs must not be reused while entries with the old publisher_id still exist). Please add KUnit tests covering these new invariants to prevent regressions.
There was a problem hiding this comment.
Please try adding the kunit tests as described above.
There was a problem hiding this comment.
Please add a comment to explicitly show that the test assume the specific algorithm: to allocate the smallest usable id.
We usually don't need the test that assume a specific algorithm, since we also need to change the test when we update it. However, I want the test for this "id reusing feature" case.
There was a problem hiding this comment.
test_case_add_publisher_subscriber_too_many verifies whether it is possible to register publishers and subscribers up to MAX_PUBLISHER_NUM and MAX_SUBSCRIBER_NUM.
test_case_add_remove_publisher_subscriber tests the ID reuse feature. It confirms this by registering the maximum number of publishers and subscribers, deleting them all, and then ensuring they can be registered up to the maximum limit again.
Currently, the tests do not include any logic that depends on a specific algorithm.
There was a problem hiding this comment.
Could you do the following?
- Delete the
agnocast_kmod/agnocast_kunit/agnocast_kunit_add_remove_pub_sub.cfile and implement the test in the existing test file such asagnocast_kmod/agnocast_kunit/agnocast_kunit_remove_publisher.c. - Implement the test that depend on the specific algorithm so that we can test the reusing feature. (Since we don't have other ways that does it without depending on the specific algorithm.)
There was a problem hiding this comment.
Delete the agnocast_kmod/agnocast_kunit/agnocast_kunit_add_remove_pub_sub.c and implement test in agnocast_kmod/agnocast_kunit/agnocast_kunit_remove_publisher.c.
bb651d9
|
@tomiy-0x62 |
Signed-off-by: tomiy <tomiy@tomiylab.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: tomiy <tomiy@tomiylab.com>
Signed-off-by: tomiy <tomiy@tomiylab.com>
1ba072e to
6c228a6
Compare
| goto unlock; | ||
| } | ||
|
|
||
| clear_bit(subscriber_id, wrapper->topic.pubsub_id_map); |
There was a problem hiding this comment.
Move this clear_bit and subscriber_id range check above the dev_info( agnocast_device, "Subscriber (id=%d) removed from topic %s.\n", subscriber_id, topic_name);, to make it more natural.
|
@tomiy-0x62 |
Signed-off-by: tomiy <tomiy@tomiylab.com>
Signed-off-by: tomiy <tomiy@tomiylab.com>
Signed-off-by: tomiy <tomiy@tomiylab.com>
Signed-off-by: tomiy <tomiy@tomiylab.com>
|
Signed-off-by: tomiy <tomiy@tomiylab.com>
Signed-off-by: tomiy <tomiy@tomiylab.com>
Signed-off-by: tomiy <tomiy@tomiylab.com>
Signed-off-by: tomiy <tomiy@tomiylab.com>
Signed-off-by: tomiy <tomiy@tomiylab.com>
Signed-off-by: tomiy <tomiy@tomiylab.com>
Description
Switched to bitmap-based management for topic_local_id usage.
Enabled ID recycling: topic_local_id is now reused when subscribers or publishers are removed.
Smallest-ID allocation: New subscribers/publishers are assigned the smallest available unused ID.
Related links
#1037
How was this PR tested?
bash scripts/test/e2e_test_1to1.bash(required)bash scripts/test/e2e_test_2to2.bash(required)Notes for reviewers
Version Update Label (Required)
Please add exactly one of the following labels to this PR:
need-major-update: User API breaking changesneed-minor-update: Internal API breaking changes (heaphook/kmod/agnocastlib compatibility)need-patch-update: Bug fixes and other changesImportant notes:
need-major-updateorneed-minor-update, please include this in the PR title as well.fix(foo)[needs major version update]: barorfeat(baz)[needs minor version update]: quxrun-build-testlabel. The PR can only be merged after the build tests pass.See CONTRIBUTING.md for detailed versioning rules.