-
Notifications
You must be signed in to change notification settings - Fork 418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support dedicated executor for component nodes #1781
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
rclcpp_components/include/rclcpp_components/component_manager_isolated.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2021 Open Source Robotics Foundation, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
|
||
#ifndef RCLCPP_COMPONENTS__COMPONENT_MANAGER_ISOLATED_HPP__ | ||
#define RCLCPP_COMPONENTS__COMPONENT_MANAGER_ISOLATED_HPP__ | ||
|
||
#include <map> | ||
#include <memory> | ||
#include <string> | ||
#include <utility> | ||
#include <vector> | ||
#include <unordered_map> | ||
|
||
#include "rclcpp_components/component_manager.hpp" | ||
|
||
|
||
namespace rclcpp_components | ||
{ | ||
/// ComponentManagerIsolated uses dedicated single-threaded executors for each components. | ||
template<typename ExecutorT = rclcpp::executors::SingleThreadedExecutor> | ||
class ComponentManagerIsolated : public rclcpp_components::ComponentManager | ||
{ | ||
using rclcpp_components::ComponentManager::ComponentManager; | ||
|
||
struct DedicatedExecutorWrapper | ||
{ | ||
std::shared_ptr<rclcpp::Executor> executor; | ||
std::thread thread; | ||
std::promise<void> promise; | ||
}; | ||
|
||
public: | ||
~ComponentManagerIsolated() | ||
{ | ||
if (node_wrappers_.size()) { | ||
for (auto & executor_wrapper : dedicated_executor_wrappers_) { | ||
executor_wrapper.second.promise.set_value(); | ||
executor_wrapper.second.executor->cancel(); | ||
executor_wrapper.second.thread.join(); | ||
} | ||
node_wrappers_.clear(); | ||
} | ||
} | ||
|
||
protected: | ||
/// Add component node to executor model, it's invoked in on_load_node() | ||
/** | ||
* \param node_id node_id of loaded component node in node_wrappers_ | ||
*/ | ||
RCLCPP_COMPONENTS_PUBLIC | ||
void | ||
add_node_to_executor(uint64_t node_id) override | ||
{ | ||
DedicatedExecutorWrapper executor_wrapper; | ||
auto exec = std::make_shared<ExecutorT>(); | ||
exec->add_node(node_wrappers_[node_id].get_node_base_interface()); | ||
executor_wrapper.executor = exec; | ||
executor_wrapper.thread = std::thread( | ||
[exec, cancel_token = executor_wrapper.promise.get_future()]() { | ||
exec->spin_until_future_complete(cancel_token); | ||
}); | ||
dedicated_executor_wrappers_[node_id] = std::move(executor_wrapper); | ||
} | ||
/// Remove component node from executor model, it's invoked in on_unload_node() | ||
/** | ||
* \param node_id node_id of loaded component node in node_wrappers_ | ||
*/ | ||
RCLCPP_COMPONENTS_PUBLIC | ||
void | ||
remove_node_from_executor(uint64_t node_id) override | ||
{ | ||
auto executor_wrapper = dedicated_executor_wrappers_.find(node_id); | ||
if (executor_wrapper != dedicated_executor_wrappers_.end()) { | ||
executor_wrapper->second.promise.set_value(); | ||
executor_wrapper->second.executor->cancel(); | ||
executor_wrapper->second.thread.join(); | ||
dedicated_executor_wrappers_.erase(executor_wrapper); | ||
} | ||
} | ||
|
||
private: | ||
std::unordered_map<uint64_t, DedicatedExecutorWrapper> dedicated_executor_wrappers_; | ||
}; | ||
|
||
} // namespace rclcpp_components | ||
|
||
#endif // RCLCPP_COMPONENTS__COMPONENT_MANAGER_ISOLATED_HPP__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2021 Open Source Robotics Foundation, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <memory> | ||
#include <vector> | ||
#include <string> | ||
|
||
#include "rclcpp/rclcpp.hpp" | ||
#include "rclcpp/utilities.hpp" | ||
#include "rclcpp_components/component_manager_isolated.hpp" | ||
|
||
int main(int argc, char * argv[]) | ||
{ | ||
/// Component container with dedicated single-threaded executors for each components. | ||
rclcpp::init(argc, argv); | ||
// parse arguments | ||
bool use_multi_threaded_executor{false}; | ||
std::vector<std::string> args = rclcpp::remove_ros_arguments(argc, argv); | ||
for (auto & arg : args) { | ||
if (arg == std::string("--use_multi_threaded_executor")) { | ||
use_multi_threaded_executor = true; | ||
} | ||
} | ||
// create executor and component manager | ||
auto exec = std::make_shared<rclcpp::executors::SingleThreadedExecutor>(); | ||
rclcpp::Node::SharedPtr node; | ||
if (use_multi_threaded_executor) { | ||
using ComponentManagerIsolated = | ||
rclcpp_components::ComponentManagerIsolated<rclcpp::executors::MultiThreadedExecutor>; | ||
node = std::make_shared<ComponentManagerIsolated>(exec); | ||
} else { | ||
using ComponentManagerIsolated = | ||
rclcpp_components::ComponentManagerIsolated<rclcpp::executors::SingleThreadedExecutor>; | ||
node = std::make_shared<ComponentManagerIsolated>(exec); | ||
} | ||
exec->add_node(node); | ||
exec->spin(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its up to core maintainers (@ivanpauno ) but I would rather the other containers follow suit with the
ExecutorT
so that you can remove the constructor field. The template for type + executor constructor argument are redundant. You can have the same effect by removing all constructor arguments from the existingcomponent_manager.hpp
and making that also templated, which constructs its internal executor by instantiatingExecutorT()
.I'm not strongly opinioned one way or another - my thoughts are only for consistency which we may not need to strive for here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, needs the function documentation inline (see component_manager.hpp, you added there, just add here too)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we're not taking any advantage of the template parameter here.
We can delete it or be consistent in the base class, I would just remove it as it doesn't seem to be needed.
(with the template parameter we could've not added the new virtual methods, but both approaches are fine)