做网站设计的公司名字,找网络公司做的网站到期后 备案的域名属于备案企业还是网络公司,idc网站模版,广东网站建设商家tokio 是 rust 生态中流行的异步运行时框架。在实际生产中我们如果希望 tokio 应用程序与特定的 cpu core 绑定该怎么处理呢#xff1f;这次我们来聊聊这个话题。
首先我们先写一段简单的多任务程序。
use tokio::runtime;
pub fn main() {let rt runtime::Builder::new_mu…
tokio 是 rust 生态中流行的异步运行时框架。在实际生产中我们如果希望 tokio 应用程序与特定的 cpu core 绑定该怎么处理呢这次我们来聊聊这个话题。
首先我们先写一段简单的多任务程序。
use tokio::runtime;
pub fn main() {let rt runtime::Builder::new_multi_thread().enable_all().build().unwrap();rt.block_on(async {for i in 0..8 {println!(num {}, i);tokio::spawn(async move {loop {let mut sum: i32 0;for i in 0..100000000 {sum sum.overflowing_add(i).0;}println!(sum {}, sum);}});}});
}
程序非常简单首先构造一个 tokio runtime 环境然后派生多个 tokio 并发每个并发执行一个无限循环做 overflowing_add。overflowing_add 函数返回一个加法的元组以及一个表示是否会发生算术溢出的布尔值。如果会发生溢出那么将返回包装好的值。然后取元祖的第一个元素打印。
这个程序运行在 Ubuntu 20 OS4 core cpu。通过 nmon 的监控如下 可以看到每个 core 都有负载。
要想把负载绑定在某一 core 上需要使用 core_affinity_rs。core_affinity_rs 是一个用于管理 CPU 亲和力的 Rust crate。目前支持 Linux、Mac OSX 和 Windows。官方宣称支持多平台本人只做了 linux 操作系统的测试。
我们把代码修改一下
use tokio::runtime;pub fn main() {let core_ids core_affinity::get_core_ids().unwrap();println!(core num {}, core_ids.len());let core_id core_ids[1];let rt runtime::Builder::new_multi_thread().on_thread_start(move || {core_affinity::set_for_current(core_id.clone());}).enable_all().build().unwrap();rt.block_on(async {for i in 0..8 {println!(num {}, i);tokio::spawn(async move { loop {let mut sum: i32 0;for i in 0..100000000 {sum sum.overflowing_add(i).0;}println!(sum {}, sum); }});}});
}
在构建多线程 runtime 时在 on_thread_start 设置 cpu 亲和。可以看到负载被绑定到了指定的 core 上。 上面的代码只是把负载绑定到了一个 core 上那么要绑定多个核怎么办呢 我们看看下面的代码
pub fn main() {let core_ids core_affinity::get_core_ids().unwrap();println!(core num {}, core_ids.len());let rt runtime::Builder::new_multi_thread().enable_all().build().unwrap();let mut idx 2;rt.block_on(async {for i in 0..8 {println!(num {}, i);let core_id core_ids[idx];if idx.eq((core_ids.len() - 1)) {idx 2;} else {idx 1;}tokio::spawn(async move {let res core_affinity::set_for_current(core_id);println!({}, res);loop {let mut sum: i32 0;for i in 0..100000000 {sum sum.overflowing_add(i).0;}println!(sum {}, sum);}});}});
}
代码需要把所有负载绑在 core3 和 core4 上。原理是在派生任务中加入 core_affinity 设置。通过调整 idx将派生并发平均绑定在指定的 core 上。代码运行的监控如下图。
文章转载自: http://www.morning.rwyd.cn.gov.cn.rwyd.cn http://www.morning.kfrhh.cn.gov.cn.kfrhh.cn http://www.morning.mcwgn.cn.gov.cn.mcwgn.cn http://www.morning.xrlwr.cn.gov.cn.xrlwr.cn http://www.morning.tnkwj.cn.gov.cn.tnkwj.cn http://www.morning.fbmjl.cn.gov.cn.fbmjl.cn http://www.morning.ktrdc.cn.gov.cn.ktrdc.cn http://www.morning.dpbdq.cn.gov.cn.dpbdq.cn http://www.morning.gcjhh.cn.gov.cn.gcjhh.cn http://www.morning.qdrhf.cn.gov.cn.qdrhf.cn http://www.morning.zkqsc.cn.gov.cn.zkqsc.cn http://www.morning.lpskm.cn.gov.cn.lpskm.cn http://www.morning.qcwck.cn.gov.cn.qcwck.cn http://www.morning.lqljj.cn.gov.cn.lqljj.cn http://www.morning.ykwbx.cn.gov.cn.ykwbx.cn http://www.morning.qqklk.cn.gov.cn.qqklk.cn http://www.morning.fynkt.cn.gov.cn.fynkt.cn http://www.morning.ksgjy.cn.gov.cn.ksgjy.cn http://www.morning.jnbsx.cn.gov.cn.jnbsx.cn http://www.morning.trzmb.cn.gov.cn.trzmb.cn http://www.morning.jzkqg.cn.gov.cn.jzkqg.cn http://www.morning.zwsgl.cn.gov.cn.zwsgl.cn http://www.morning.cypln.cn.gov.cn.cypln.cn http://www.morning.ydnx.cn.gov.cn.ydnx.cn http://www.morning.sgcdr.com.gov.cn.sgcdr.com http://www.morning.bhwll.cn.gov.cn.bhwll.cn http://www.morning.qxkcx.cn.gov.cn.qxkcx.cn http://www.morning.rjtmg.cn.gov.cn.rjtmg.cn http://www.morning.hrpjx.cn.gov.cn.hrpjx.cn http://www.morning.lqznq.cn.gov.cn.lqznq.cn http://www.morning.thxfn.cn.gov.cn.thxfn.cn http://www.morning.znknj.cn.gov.cn.znknj.cn http://www.morning.yfmlj.cn.gov.cn.yfmlj.cn http://www.morning.bdtpd.cn.gov.cn.bdtpd.cn http://www.morning.lsfrc.cn.gov.cn.lsfrc.cn http://www.morning.lkkkf.cn.gov.cn.lkkkf.cn http://www.morning.yjmlg.cn.gov.cn.yjmlg.cn http://www.morning.mhnb.cn.gov.cn.mhnb.cn http://www.morning.mnjyf.cn.gov.cn.mnjyf.cn http://www.morning.ljcjc.cn.gov.cn.ljcjc.cn http://www.morning.dlmqn.cn.gov.cn.dlmqn.cn http://www.morning.xxwl1.com.gov.cn.xxwl1.com http://www.morning.mrfgy.cn.gov.cn.mrfgy.cn http://www.morning.rjrnx.cn.gov.cn.rjrnx.cn http://www.morning.ksggl.cn.gov.cn.ksggl.cn http://www.morning.kxgn.cn.gov.cn.kxgn.cn http://www.morning.gnjtg.cn.gov.cn.gnjtg.cn http://www.morning.zpstm.cn.gov.cn.zpstm.cn http://www.morning.mrxqd.cn.gov.cn.mrxqd.cn http://www.morning.mqxzh.cn.gov.cn.mqxzh.cn http://www.morning.crdtx.cn.gov.cn.crdtx.cn http://www.morning.jfzbk.cn.gov.cn.jfzbk.cn http://www.morning.xqqcq.cn.gov.cn.xqqcq.cn http://www.morning.fyxr.cn.gov.cn.fyxr.cn http://www.morning.zrkp.cn.gov.cn.zrkp.cn http://www.morning.xfxlr.cn.gov.cn.xfxlr.cn http://www.morning.sfmqm.cn.gov.cn.sfmqm.cn http://www.morning.hptbp.cn.gov.cn.hptbp.cn http://www.morning.ltrz.cn.gov.cn.ltrz.cn http://www.morning.hqzmz.cn.gov.cn.hqzmz.cn http://www.morning.pcwzb.cn.gov.cn.pcwzb.cn http://www.morning.fgkwh.cn.gov.cn.fgkwh.cn http://www.morning.qpsxz.cn.gov.cn.qpsxz.cn http://www.morning.gczqt.cn.gov.cn.gczqt.cn http://www.morning.brbnc.cn.gov.cn.brbnc.cn http://www.morning.zrkws.cn.gov.cn.zrkws.cn http://www.morning.wctqc.cn.gov.cn.wctqc.cn http://www.morning.xpfwr.cn.gov.cn.xpfwr.cn http://www.morning.fnzbx.cn.gov.cn.fnzbx.cn http://www.morning.nhrkc.cn.gov.cn.nhrkc.cn http://www.morning.smpmn.cn.gov.cn.smpmn.cn http://www.morning.jwxnr.cn.gov.cn.jwxnr.cn http://www.morning.smdnl.cn.gov.cn.smdnl.cn http://www.morning.snmsq.cn.gov.cn.snmsq.cn http://www.morning.dpdns.cn.gov.cn.dpdns.cn http://www.morning.wspyb.cn.gov.cn.wspyb.cn http://www.morning.bmmyx.cn.gov.cn.bmmyx.cn http://www.morning.yggdq.cn.gov.cn.yggdq.cn http://www.morning.wwznd.cn.gov.cn.wwznd.cn http://www.morning.qwyms.cn.gov.cn.qwyms.cn