-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
84 lines (68 loc) · 1.97 KB
/
main.cpp
File metadata and controls
84 lines (68 loc) · 1.97 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <cpputils2/cpputils2.hpp>
#include <cpputils2/linux/sched/sched.hpp>
#include <atomic>
#include <chrono>
#include <csignal>
#include <iostream>
#include <spdlog/spdlog.h>
#include <linux/sched.h>
const int32_t expected = 42;
const std::string file_name("test_shm");
std::atomic_flag run_thread = ATOMIC_FLAG_INIT;
void signalHandler(int signum)
{
if (signum == SIGXCPU)
{
SPDLOG_INFO("Caught signal SIGXCPU. We are overruning the deadline");
run_thread.clear();
}
else if (signum == SIGINT)
{
run_thread.clear();
}
}
int main(int argc, char **argv)
{
SPDLOG_INFO("cpputils2 version {}", CppUtils2::VERSION);
SPDLOG_INFO("Test deadline Linux scheduler. Ctrl-C to finish test.");
CppUtils2::sched_attr attr;
attr.size = sizeof(CppUtils2::sched_attr);
attr.sched_policy = SCHED_DEADLINE;
attr.sched_priority = 0;
attr.sched_runtime = 200'000'000;
attr.sched_deadline = 500'000'000;
attr.sched_period = 500'000'000; // 1s
attr.sched_flags = SCHED_FLAG_RESET_ON_FORK | SCHED_FLAG_RECLAIM | SCHED_FLAG_DL_OVERRUN;
signal(SIGXCPU, signalHandler);
signal(SIGINT, signalHandler);
run_thread.test_and_set();
auto ret = CppUtils2::set_self_attributes(&attr);
if (!ret.has_value())
{
auto error = ret.error();
SPDLOG_ERROR("Error setting attributes. Error code: {}, errno: {}", error, errno);
return -1;
}
auto start = std::chrono::high_resolution_clock::now();
bool first = true;
uint64_t mean_time_ns = 0;
uint64_t count = 0;
while (run_thread.test())
{
if (first)
{
first = false;
}
else
{
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now() - start).count();
std::cout << "Elapsed: " << elapsed << "ns\n";
start = std::chrono::high_resolution_clock::now();
mean_time_ns += elapsed;
count++;
}
sched_yield();
}
SPDLOG_INFO("Exiting. Mean activation time {}ns", mean_time_ns / count);
return 0;
}