Skip to content

refactor(kmod): reuse topic_local_id#1143

Open
tomiy-0x62 wants to merge 15 commits intoautowarefoundation:mainfrom
tomiy-0x62:reuse_topic_local_id
Open

refactor(kmod): reuse topic_local_id#1143
tomiy-0x62 wants to merge 15 commits intoautowarefoundation:mainfrom
tomiy-0x62:reuse_topic_local_id

Conversation

@tomiy-0x62
Copy link
Copy Markdown
Contributor

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?

  • Autoware (required)
  • bash scripts/test/e2e_test_1to1.bash (required)
  • bash scripts/test/e2e_test_2to2.bash (required)
  • kunit tests (required when modifying the kernel module)
  • sample application

Notes for reviewers

Version Update Label (Required)

Please add exactly one of the following labels to this PR:

  • need-major-update: User API breaking changes
  • need-minor-update: Internal API breaking changes (heaphook/kmod/agnocastlib compatibility)
  • need-patch-update: Bug fixes and other changes

Important notes:

  • If you need need-major-update or need-minor-update, please include this in the PR title as well.
    • Example: fix(foo)[needs major version update]: bar or feat(baz)[needs minor version update]: qux
  • After receiving approval from reviewers, add the run-build-test label. The PR can only be merged after the build tests pass.

See CONTRIBUTING.md for detailed versioning rules.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_id with a per-topic bitmap (pubsub_id_map) to track used IDs.
  • Allocate the smallest available topic_local_id via find_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.

Comment thread agnocast_kmod/agnocast_main.c
Comment thread agnocast_kmod/agnocast_main.c
Comment thread agnocast_kmod/agnocast_main.c Outdated
Comment on lines +2005 to +2006
clear_bit(publisher_id, wrapper->topic.pubsub_id_map);

Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix in 5a10a58

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also add clear_bit for publisher_id in agnocast_ioctl_remove_subscriber.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

publisher_id not subscriber_id.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clear_bit for publisher_id in agnocast_ioctl_remove_subscriber
08b8b95

clear_bit for publisher_id in pre_handler_subscriber_exit
e161844

Comment on lines +291 to +295
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,
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please try adding the kunit tests as described above.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add kunit test in 97902af

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you do the following?

  1. Delete the agnocast_kmod/agnocast_kunit/agnocast_kunit_add_remove_pub_sub.c file and implement the test in the existing test file such as agnocast_kmod/agnocast_kunit/agnocast_kunit_remove_publisher.c.
  2. 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.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@Koichi98 Koichi98 added the need-patch-update Bug fixes and other changes - requires PATCH version update label Mar 3, 2026
@Koichi98
Copy link
Copy Markdown
Collaborator

Koichi98 commented Mar 3, 2026

@tomiy-0x62
Could you also fix DCO issue, following the direction explained in https://github.com/autowarefoundation/agnocast/pull/1143/checks?check_run_id=65371642276?

tomiy-0x62 and others added 3 commits March 12, 2026 15:49
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>
@tomiy-0x62 tomiy-0x62 force-pushed the reuse_topic_local_id branch from 1ba072e to 6c228a6 Compare March 12, 2026 06:50
goto unlock;
}

clear_bit(subscriber_id, wrapper->topic.pubsub_id_map);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix in 5a10a58

@Koichi98
Copy link
Copy Markdown
Collaborator

@tomiy-0x62
You should also add clear_bit in pre_handler_publisher_exit and pre_handler_subscriber_exit.

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>
@tomiy-0x62
Copy link
Copy Markdown
Contributor Author

@tomiy-0x62 You should also add clear_bit in pre_handler_publisher_exit and pre_handler_subscriber_exit.

fa85435

Signed-off-by: tomiy <tomiy@tomiylab.com>
Signed-off-by: tomiy <tomiy@tomiylab.com>
Signed-off-by: tomiy <tomiy@tomiylab.com>
@Koichi98 Koichi98 changed the title reuse topic_local_id refactor: reuse topic_local_id Apr 4, 2026
@Koichi98 Koichi98 changed the title refactor: reuse topic_local_id refactor(kmod): reuse topic_local_id Apr 4, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

need-patch-update Bug fixes and other changes - requires PATCH version update

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants