-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.cc
68 lines (51 loc) · 1.79 KB
/
main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <cactus_rt/rt.h>
#include <chrono>
#include <iostream>
using cactus_rt::App;
using cactus_rt::CyclicThread;
/**
* This is a no-op thread that does nothing at 1 kHz.
*
*/
class ExampleRTThread : public CyclicThread {
int64_t loop_counter_ = 0;
static cactus_rt::CyclicThreadConfig MakeConfig() {
cactus_rt::CyclicThreadConfig thread_config;
thread_config.period_ns = 1'000'000;
thread_config.cpu_affinity = std::vector<size_t>{2};
thread_config.SetFifoScheduler(80);
return thread_config;
}
public:
ExampleRTThread() : CyclicThread("ExampleRTThread", MakeConfig()) {}
int64_t GetLoopCounter() const {
return loop_counter_;
}
protected:
LoopControl Loop(int64_t /*now*/) noexcept final {
loop_counter_++;
if (loop_counter_ % 1000 == 0) {
LOG_INFO(Logger(), "Loop {}", loop_counter_);
}
LOG_INFO_LIMIT(std::chrono::milliseconds{1500}, Logger(), "Log limit: Loop {}", loop_counter_);
return LoopControl::Continue;
}
};
int main() {
// Create a cactus_rt app configuration
cactus_rt::AppConfig app_config;
// Disable strict timestamp order - this will be faster, but logs may appear out of order
app_config.logger_config.backend_thread_strict_log_timestamp_order = false;
// Set the background logging thread CPU affinity
app_config.logger_config.backend_thread_cpu_affinity = 1; // Different CPU than the CyclicThread CPU!
App app("LoggingExampleApp", app_config);
auto thread = app.CreateThread<ExampleRTThread>();
constexpr unsigned int time = 5;
std::cout << "Testing RT loop for " << time << " seconds.\n";
app.Start();
std::this_thread::sleep_for(std::chrono::seconds(time));
app.RequestStop();
app.Join();
std::cout << "Number of loops executed: " << thread->GetLoopCounter() << "\n";
return 0;
}