WaitSets: What can I do to reduce my CPU usage?

For high-rate data, the usage of a WaitSet may lead to a very high CPU usage if the WaitSet wakes up too often.

By default, a WaitSet is created without any properties specified. In this case, the WaitSet wakes up immediately every time a trigger event occurs. However, "waking up" involves a context switch—the operating system must signal and schedule the thread that is waiting on the WaitSet. A context switch consumes significant CPU. Therefore waking up on each data update is not optimal in situations where the data is sent at a high-rate, as the CPU usage from the context switching of threads may become too high.

To reduce the number of context switches and the associated high CPU usage, the WaitSet can be configured to wake up less often. This is done by specifying non-default properties for the WaitSet at creation time. Note that there is a trade-off between the wake up latency and reduced CPU consumption.  

The WaitSet properties are: 

  • (long) max_event_count: Maximum number of trigger events to occur prior to waking up the WaitSet.
  • (DDS_Duration_t) max_event_delay: Maximum time delay from the first trigger event to waking up the WaitSet.

The properties configure the “wake up throttling” behavior of a WaitSet. Assuming there were no “active” conditions at the time the operation WaitSet::wait() was called, the WaitSet will wait until one of the three situations below occurs, whichever happens first:

  1. The occurrence of max_event_count trigger events,
  2. The passage of max_event_delay time from the occurrence of the first trigger event,
  3. The passage of the maximum wait duration specified in the call to wait().

We can use these properties to control how often the WaitSet will wake up. For example, let's say that we want our WaitSet to wake up only once for every 10 data updates, but with a maximum wait of 30 seconds after getting the first data update. To create a WaitSet with these properties: 

DDS_WaitSetProperty_t prop = DDS_WaitSetProperty_t_INITIALIZER;
prop.max_event_count = 10;
prop.max_event_delay.sec = 30;
prop.max_event_delay.nanosec = 0;
 
DDSWaitSet* waitset = new DDSWaitSet(prop);

You can find further information about WaitSet properties in sections 4.6.1 “Creating and Deleting WaitSets” and 4.6.3 “Waiting for Conditions” in the User’s Manual.

You can also find information about WaitSets for each of our supported APIs in the HTML online documentation ( C | C++ | C# | JAVA ) .