RTUtility.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 
6 {
7  /**
8  * @class RTUtility
9  * @ingroup Library-ethercat
10  *
11  * @brief The RTUtility class contains functions for improving the realtime capabilities
12  * of a thread or of the whole system.
13  */
14  class RTUtility
15  {
16 
17  public:
18  /**
19  * The thread priority for rt-threads.
20  *
21  * 49 is the highest value that can be safely used, because PRREMPT_RT uses 50 for kernel
22  * tasklets and interrupt handler by default.
23  */
24  static constexpr int RT_THREAD_PRIORITY = 49;
25 
26  /**
27  * @brief Elevate the thread priority of the calling thread to the given priority.
28  *
29  * The owning user of the calling thread must be allowed to set its priority to the given
30  * value.
31  * Each user has a maximum value it can sets its thread priorities to.
32  * This maximum value can be configured by editing the file /etc/security/limits.conf
33  * according to https://linux.die.net/man/5/limits.conf and setting the entry \b rtprio for
34  * the executing users.
35  * This function will only work if the operating system is build with a PRREMPT_RT
36  * patch (https://wiki.linuxfoundation.org/realtime/documentation/technical_details/start).
37  *
38  * @param priority the new priority of the calling thread
39  * @return true if the requested priority could be set, false if not.
40  */
41  static bool elevateThreadPriority(int priority);
42 
43  /**
44  * @brief Pins the calling thread to the CPU with the given id.
45  *
46  * The thread can only be pinned to a cpu which actually exists on the system.
47  *
48  * @param cpu The id of the CPU the calling thread should be pinned to
49  * @return tue if the pinning was successful, false if not
50  */
51  static bool pinThreadToCPU(unsigned int cpu);
52 
53  /**
54  * @brief Activate low latency mode of the system.
55  *
56  * If the file /dev/cpu_dma_latency exists, open it and write a zero into it. This will
57  * tell the power management system not to transition to a high cstate
58  * (in fact, the system acts like idle=poll).
59  * When the fd to /dev/cpu_dma_latency is closed, the behavior goes back to the system
60  * default.
61  *
62  * Taken from https://wiki.linuxfoundation.org/realtime/documentation/howto/tools/rt-tests.
63  *
64  * @return true if the low latency mode could be activated, false if not.
65  */
66  static bool startLowLatencyMode();
67 
68  /**
69  * @brief Deactivate low latency mode of the system.
70  * @return true if low latency mode was active, false if not and nothng needs to be done
71  */
72  static bool stopLowLatencyMode();
73  };
74 } // namespace armarx::control::ethercat
armarx::control::ethercat::RTUtility::RT_THREAD_PRIORITY
static constexpr int RT_THREAD_PRIORITY
The thread priority for rt-threads.
Definition: RTUtility.h:24
armarx::control::ethercat::RTUtility::startLowLatencyMode
static bool startLowLatencyMode()
Activate low latency mode of the system.
Definition: RTUtility.cpp:95
armarx::control::ethercat::RTUtility::pinThreadToCPU
static bool pinThreadToCPU(unsigned int cpu)
Pins the calling thread to the CPU with the given id.
Definition: RTUtility.cpp:52
armarx::control::ethercat::RTUtility::stopLowLatencyMode
static bool stopLowLatencyMode()
Deactivate low latency mode of the system.
Definition: RTUtility.cpp:131
armarx::control::ethercat::RTUtility
The RTUtility class contains functions for improving the realtime capabilities of a thread or of the ...
Definition: RTUtility.h:14
armarx::control::ethercat
Definition: Bus.cpp:24
armarx::control::ethercat::RTUtility::elevateThreadPriority
static bool elevateThreadPriority(int priority)
Elevate the thread priority of the calling thread to the given priority.
Definition: RTUtility.cpp:17