rust_rsm/common/
sched.rs

1#[cfg(unix)]
2use libc;
3#[cfg(unix)]
4use std::mem;
5#[cfg(unix)]
6use libc::{cpu_set_t};
7#[cfg(windows)]
8use windows_sys::Win32::System::Threading;
9use super::errcode;
10
11#[derive(Debug)]
12pub struct sys_info_t {
13    pub uptime: i64,
14    pub loads: [u64; 3],
15    pub totalram: u64,
16    pub freeram: u64,
17    pub sharedram: u64,
18    pub bufferram: u64,
19    pub totalswap: u64,
20    pub freeswap: u64,
21    pub procs: u16,
22    pub totalhigh: u64,
23    pub freehigh: u64,
24    pub mem_unit: u32,
25}
26
27#[cfg(unix)]
28impl sys_info_t {
29    pub fn from_sys_info(info:&libc::sysinfo)->Self {
30        return Self {
31            uptime: info.uptime,
32            loads: info.loads,
33            totalram: info.totalram,
34            freeram: info.freeram,
35            sharedram: info.sharedram,
36            bufferram: info.bufferram,
37            totalswap: info.totalswap,
38            freeswap: info.freeswap,
39            procs: info.procs,
40            totalhigh: info.totalhigh,
41            freehigh: info.freehigh,
42            mem_unit: info.mem_unit,
43        }
44    }
45}
46
47#[cfg(unix)]
48pub fn get_sys_info()->sys_info_t {
49    unsafe {
50        let mut info = mem::zeroed::<libc::sysinfo>();
51        libc::sysinfo(&mut info as * mut libc::sysinfo);
52        return sys_info_t::from_sys_info(&info)
53    }
54}
55
56#[cfg(unix)]pub fn get_sys_cpu_num()->u16 {
57
58        let count = unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) };
59        return count as u16;
60}
61
62
63#[cfg(windows)]
64pub fn get_sys_cpu_num()->usize {
65    0
66}
67
68
69///设置指定线程的CPU亲和性
70#[cfg(unix)]
71pub fn set_thread_cpu_affinity(threadId:u64,bitmask:u64)->i32 {
72    unsafe {
73        let cpus = build_cpu_set(bitmask);
74        return libc::pthread_setaffinity_np(threadId,std::mem::size_of::<cpu_set_t>(),&cpus as *const cpu_set_t);
75
76    }
77}
78
79#[cfg(windows)]
80pub fn get_sys_info()->sys_info_t {
81    unsafe {
82        return std::mem::zeroed::<sys_info_t>();
83    }
84}
85
86///设置指定线程的CPU亲和性
87#[cfg(windows)]
88pub fn set_thread_cpu_affinity(_threadId:u64,_bitmask:u64)->i32  {
89    0
90
91}
92
93///设置自身的CPU亲和性
94#[cfg(unix)]
95pub fn set_self_cpu_affinity(bitmask:u64) {
96    unsafe {
97        let threadId = libc::pthread_self();
98        set_thread_cpu_affinity(threadId,bitmask);
99    }
100}
101
102///设置自身的CPU亲和性
103#[cfg(windows)]
104pub fn set_self_cpu_affinity(_bitmask:u64) {
105
106}
107
108#[cfg(unix)]
109fn build_cpu_set(bitmask:u64)->cpu_set_t {
110    let mut cpu_set = unsafe { std::mem::zeroed::<cpu_set_t>()};
111
112    for i in 0..64 {
113        if bitmask & (1u64<<i) != 0{
114            unsafe { libc::CPU_SET(i,&mut cpu_set) };
115        }
116    }
117    return cpu_set;
118
119}
120
121
122#[cfg(unix)]
123pub fn get_self_threadId()->usize {
124    unsafe {
125        libc::pthread_self() as usize
126    }
127}
128
129#[cfg(windows)]
130pub fn get_self_threadId()->usize {
131    0
132}
133
134#[cfg(unix)]
135pub fn set_self_priority(policy:i32,priority:i32)->errcode::RESULT {
136
137    let mut sparam = unsafe { mem::zeroed::<libc::sched_param>() };
138    sparam.sched_priority=priority;
139
140    let ret = unsafe { libc::sched_setscheduler(0,policy,&sparam) };
141    if ret!=0 {
142        return errcode::ERROR_OS_CALL_FAILED
143    }
144    return errcode::RESULT_SUCCESS;
145
146}
147
148/*设置自身线程的优先级*/
149#[cfg(windows)]
150pub fn set_self_priority(_policy:i32,priority:i32)->errcode::RESULT {
151	let h = unsafe { Threading::GetCurrentThread() };
152	let res = unsafe { Threading::SetThreadPriority(h, priority) };
153    // SetPriorityClass(h, priority as u32) };
154	if res == 0 {
155		return errcode::ERROR_OS_CALL_FAILED
156	}
157
158	return errcode::RESULT_SUCCESS
159
160}
161
162#[cfg(windows)]
163pub type os_task_id_t = isize;
164#[cfg(unix)]
165pub type os_task_id_t = libc::pid_t;
166
167
168#[cfg(unix)]
169pub fn get_self_os_task_id()->os_task_id_t {
170    unsafe { 
171        return libc::gettid()
172    }
173}
174#[cfg(windows)]
175pub fn get_self_os_task_id()->os_task_id_t {
176    unsafe { 
177    return Threading::GetCurrentThread()
178    }
179}