Klepsydra is based in lock-free programming. This kind of programming is the high-level wrapper of the atomic operations in the processor, in particular the so-call compare-and-swap or CAS operation. It was invented back in the 70s, but it didn’t really become popular until the early 90s, when it was implemented in higher-level languages and then it really took off when Java included it in the early 2010s.
Lock-free programming consists of attempting repeatedly to write data in a small piece of memory until the data is in a consistent state. This is usually depicted as a plane trying to land in a busy airport. If the runway is busy, it flies away and then tries it once and again until success.
This technique is substantially lighter than the traditional mutex operation, because it is just specific to a one small piece of memory, as opposed to mutex, which blocks a big portion of the memory. That is why the traditional lock systems are not deterministic, while lock-free systems are more granular and deterministic. It works remarkably more efficient.
Our own ring buffer is wrapped in what we call the Klepsydra SDK. It is just another library that is installed in the operating system that receives data from multiple sources. It is essentially a memory sharing system.
https://klepsydra.com/klepsydra-ros-2-executor-a-ring-buffer-to-rule-them-all/
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
Roscon2021 Executor
1. ROSCON 2021
Executor Workshop
LOCK-FREE ROS 2 EXECUTOR: A
RING-BUFFER TO RULE THEM ALL
Dr Pablo Ghiglino
CEO and Founder
pablo.ghiglino@klepsydra.com
www.klepsydra.com
2. LOCK-FREE PROGRAMMING
• Compare-and-swap (CAS) is an instruction
used in multithreading to achieve
synchronisation. It compares the contents of
a memory location with a given value and,
only if they are the same, modi
fi
es the
contents of that memory location to a new
given value. This is done as a single
atomic operation.
• Compare-and-Swap has been an integral
part of the IBM 370 architectures since
1970.
• Maurice Herlihy (1991) proved that CAS can
implement more of these algorithms than
atomic read, write, and fetch-and-add
3. LOCK-FREE PROGRAMMING
• Threads need to acquire lock to access resource.
• Context switch:
• Suspended while resource is locked by
someone else
• Awaken when resource is available.
• Not deterministic, power consuming context switch.
• Threads access resources using ‘Atomic Operations’
• Compare and Swap (CAS):
• Try to update a memory entry
• If not possible tried again
• No locks involved, but ‘busy wait’
• No context switch required.
4. KLEPSYDRA RING-BUFFER
Klepsydra SDK
Sensors
External
Comms
Other Events
Application
Operating System
Cobham Microcontroller 716
Power consumption vs Data Processing
CPU
(%)
10
33
55
78
100
Data processing rate (Hz)
0 10 20 30 40
Traditional
edge software
Klepsydra
7. Klepsydra Lock-free executor
How does it work?
•Similar implementation to the Static Single Thread Executor
•All subscriptions and timer tasks run on the same thread
•Publishers can run on any thread
Implementation details
•Subscriptions from all message types are treated as
Eventloop’s listeners
•Timers are treated as Eventloops scheduled functions
•Similar to the Static Single Thread, there is no cloning.
8. ROS2 CHALLENGES
Open issues in ROS2:
1. High CPU use of executor. (https://github.com/ros2/rclcpp/
issues/1637)
2. Large pointcloud pubsub is unstable when there are
many subscribers. (https://github.com/ros2/
rmw_cyclonedds/issues/292)
12. CODE EXAMPLE
#include <kpsr_ros2_executor/executor_factory.hpp>
...
int main(int argc, char** argv) {
...
rclcpp::init(0, nullptr);
...
rclcpp::Executor::SharedPtr exec = kpsr::ros2::ExecutorFactory::createExecutor(kpsr::ros2::QueueSize::_256, false);
...
}
API Explained
1. Klepsydra offers a factory of executors. Mapping to nodes can be customised via con
fi
guration
fi
le.
2. Factory returns shared pointer to rclcpp::Executor
3. Size of underlying ring-buffers to be provided by constructor. It can be customised via con
fi
guration
fi
le.
4. “Test” version available (last bool arg). This test version is a synchronous, single-thread, blocking queue.
13. ROS2 Realm
Klepsydra Realm
Producer 1
Consumer 1 Consumer 2
Sensor Multiplexer
Multiplexer Executor
ROS2 Subscription ROS2 Subscription
ROS2 Publisher
How does it work?
• Each consumer on its own
thread
• Processing rate can be
anything
• Data integrity guaranteed
Best performance in the following
scenarios
• Few producers and many
consumers
• Large data sets (LiDAR,
Images, etc)
• Power reduction and
throughput needs
14. ROS2 Node
ROS2 Node
Data streaming approach to
ROS2 Executors
Camera
Video
Streaming
Lidar
Radar
IMU
Navigation
Sensors
Vision Based
Navigation
Navigation and
Control
ROS2 Node
Klepsydra Executors can be mapped to nodes (many-to-many) enabling low CPU usage, high throughput
and determinism.
15. Conclusions
Bene
fi
ts
•Low CPU usage
•High throughput with linearly growing CPU usage
•Easy integration.
Best performance in the following scenarios
•Many producers and consumers
•Low to medium data sets
•Power reduction and throughput needs
16. CONTACT INFORMATION
Dr Pablo Ghiglino
pablo.ghiglino@klepsydra.com
+41786931544
www.klepsydra.com
linkedin.com/company/klepsydra-technologies