Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
R
RoboPLC
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
黄新宇
RoboPLC
Commits
51a3b88b
提交
51a3b88b
authored
4月 06, 2024
作者:
Serhij S
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
kernel config
上级
4d903a1b
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
64 行增加
和
3 行删除
+64
-3
thread_rt.rs
src/thread_rt.rs
+64
-3
没有找到文件。
src/thread_rt.rs
浏览文件 @
51a3b88b
...
@@ -6,13 +6,14 @@ use libc::cpu_set_t;
...
@@ -6,13 +6,14 @@ use libc::cpu_set_t;
use
nix
::{
sys
::
signal
,
unistd
};
use
nix
::{
sys
::
signal
,
unistd
};
use
serde
::{
Deserialize
,
Serialize
,
Serializer
};
use
serde
::{
Deserialize
,
Serialize
,
Serializer
};
use
std
::{
use
std
::{
collections
::
BTreeSet
,
collections
::
{
BTreeMap
,
BTreeSet
}
,
mem
,
fs
,
mem
,
sync
::
atomic
::{
AtomicBool
,
Ordering
},
sync
::
atomic
::{
AtomicBool
,
Ordering
},
thread
::{
self
,
JoinHandle
,
Scope
,
ScopedJoinHandle
},
thread
::{
self
,
JoinHandle
,
Scope
,
ScopedJoinHandle
},
time
::
Duration
,
time
::
Duration
,
};
};
use
sysinfo
::{
Pid
,
System
};
use
sysinfo
::{
Pid
,
System
};
use
tracing
::
warn
;
static
REALTIME_MODE
:
AtomicBool
=
AtomicBool
::
new
(
true
);
static
REALTIME_MODE
:
AtomicBool
=
AtomicBool
::
new
(
true
);
...
@@ -22,6 +23,10 @@ pub fn set_simulated() {
...
@@ -22,6 +23,10 @@ pub fn set_simulated() {
REALTIME_MODE
.store
(
false
,
Ordering
::
Relaxed
);
REALTIME_MODE
.store
(
false
,
Ordering
::
Relaxed
);
}
}
fn
is_simulated
()
->
bool
{
REALTIME_MODE
.load
(
Ordering
::
Relaxed
)
}
/// A thread builder object, similar to [`thread::Builder`] but with real-time capabilities
/// A thread builder object, similar to [`thread::Builder`] but with real-time capabilities
///
///
/// Warning: works on Linux systems only
/// Warning: works on Linux systems only
...
@@ -464,7 +469,7 @@ fn thread_init_external(
...
@@ -464,7 +469,7 @@ fn thread_init_external(
}
}
fn
apply_thread_params
(
tid
:
libc
::
c_int
,
params
:
&
RTParams
)
->
Result
<
()
>
{
fn
apply_thread_params
(
tid
:
libc
::
c_int
,
params
:
&
RTParams
)
->
Result
<
()
>
{
if
!
REALTIME_MODE
.load
(
Ordering
::
Relaxed
)
{
if
!
is_simulated
(
)
{
return
Ok
(());
return
Ok
(());
}
}
if
!
params
.cpu_ids
.is_empty
()
{
if
!
params
.cpu_ids
.is_empty
()
{
...
@@ -589,3 +594,59 @@ fn get_child_pids_recursive(pid: Pid, sys: &System, to: &mut BTreeSet<Pid>) {
...
@@ -589,3 +594,59 @@ fn get_child_pids_recursive(pid: Pid, sys: &System, to: &mut BTreeSet<Pid>) {
};
};
}
}
}
}
/// Configure kernel parameters (global) while the process is running. Does nothing in simulated
/// mode
///
/// Example:
///
/// ```rust,no_run
/// use roboplc::thread_rt::KernelConfig;
///
/// let _kernel_config = KernelConfig::new().set("sched_rt_runtime_us", -1)
/// .apply()
/// .expect("Unable to set kernel config");
/// // some code
/// // kernel config is restored at the end of the scope
/// ```
#[derive(Default)]
pub
struct
KernelConfig
{
values
:
BTreeMap
<&
'static
str
,
String
>
,
prev_values
:
BTreeMap
<&
'static
str
,
String
>
,
}
impl
KernelConfig
{
pub
fn
new
()
->
Self
{
Self
::
default
()
}
pub
fn
set
<
V
:
fmt
::
Display
>
(
mut
self
,
key
:
&
'static
str
,
value
:
V
)
->
Self
{
self
.values
.insert
(
key
,
value
.to_string
());
self
}
pub
fn
apply
(
mut
self
)
->
Result
<
KernelConfigGuard
>
{
if
is_simulated
()
{
for
(
key
,
value
)
in
&
self
.values
{
let
prev_value
=
fs
::
read_to_string
(
format!
(
"/proc/sys/kernel/{}"
,
key
))
?
;
self
.prev_values
.insert
(
key
,
prev_value
);
fs
::
write
(
format!
(
"/proc/sys/kernel/{}"
,
key
),
value
)
?
;
}
}
Ok
(
KernelConfigGuard
{
config
:
self
})
}
}
pub
struct
KernelConfigGuard
{
config
:
KernelConfig
,
}
impl
Drop
for
KernelConfigGuard
{
fn
drop
(
&
mut
self
)
{
if
is_simulated
()
{
for
(
key
,
value
)
in
&
self
.config.prev_values
{
if
let
Err
(
error
)
=
fs
::
write
(
format!
(
"/proc/sys/kernel/{}"
,
key
),
value
)
{
warn!
(
key
,
value
,
%
error
,
"Failed to restore kernel config"
);
}
}
}
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论