Skip to content

Commit d3b91fd

Browse files
authored
并行执行多个DADK任务 (#15)
1 parent 70a5e6c commit d3b91fd

File tree

6 files changed

+393
-142
lines changed

6 files changed

+393
-142
lines changed

src/console/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ pub struct CommandLineArgs {
3939
/// DADK缓存根目录
4040
#[arg(long, value_parser = parse_check_dir_exists)]
4141
pub cache_dir: Option<PathBuf>,
42+
43+
/// DADK任务并行线程数量
44+
#[arg(short, long)]
45+
pub thread: Option<usize>,
4246
}
4347

4448
/// @brief 检查目录是否存在

src/executor/cache.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{path::PathBuf, rc::Rc};
1+
use std::{path::PathBuf, sync::Arc};
22

33
use log::info;
44

@@ -78,7 +78,7 @@ pub fn cache_root_init(path: Option<PathBuf>) -> Result<(), ExecutorError> {
7878
#[derive(Debug, Clone)]
7979
pub struct CacheDir {
8080
#[allow(dead_code)]
81-
entity: Rc<SchedEntity>,
81+
entity: Arc<SchedEntity>,
8282
pub path: PathBuf,
8383
pub cache_type: CacheDirType,
8484
}
@@ -92,9 +92,9 @@ pub enum CacheDirType {
9292
impl CacheDir {
9393
pub const DADK_BUILD_CACHE_DIR_ENV_KEY_PREFIX: &'static str = "DADK_BUILD_CACHE_DIR";
9494
pub const DADK_SOURCE_CACHE_DIR_ENV_KEY_PREFIX: &'static str = "DADK_SOURCE_CACHE_DIR";
95-
pub fn new(entity: Rc<SchedEntity>, cache_type: CacheDirType) -> Result<Self, ExecutorError> {
95+
pub fn new(entity: Arc<SchedEntity>, cache_type: CacheDirType) -> Result<Self, ExecutorError> {
9696
let task = entity.task();
97-
let path = Self::get_path(task, cache_type);
97+
let path = Self::get_path(&task, cache_type);
9898

9999
let result = Self {
100100
entity,
@@ -122,15 +122,15 @@ impl CacheDir {
122122
return PathBuf::from(cache_dir);
123123
}
124124

125-
pub fn build_dir(entity: Rc<SchedEntity>) -> Result<PathBuf, ExecutorError> {
126-
return Ok(Self::new(entity, CacheDirType::Build)?.path);
125+
pub fn build_dir(entity: Arc<SchedEntity>) -> Result<PathBuf, ExecutorError> {
126+
return Ok(Self::new(entity.clone(), CacheDirType::Build)?.path);
127127
}
128128

129-
pub fn source_dir(entity: Rc<SchedEntity>) -> Result<PathBuf, ExecutorError> {
130-
return Ok(Self::new(entity, CacheDirType::Source)?.path);
129+
pub fn source_dir(entity: Arc<SchedEntity>) -> Result<PathBuf, ExecutorError> {
130+
return Ok(Self::new(entity.clone(), CacheDirType::Source)?.path);
131131
}
132132

133-
pub fn build_dir_env_key(entity: &Rc<SchedEntity>) -> Result<String, ExecutorError> {
133+
pub fn build_dir_env_key(entity: &Arc<SchedEntity>) -> Result<String, ExecutorError> {
134134
let name_version_env = entity.task().name_version_env();
135135
return Ok(format!(
136136
"{}_{}",
@@ -139,7 +139,7 @@ impl CacheDir {
139139
));
140140
}
141141

142-
pub fn source_dir_env_key(entity: &Rc<SchedEntity>) -> Result<String, ExecutorError> {
142+
pub fn source_dir_env_key(entity: &Arc<SchedEntity>) -> Result<String, ExecutorError> {
143143
let name_version_env = entity.task().name_version_env();
144144
return Ok(format!(
145145
"{}_{}",
@@ -148,7 +148,7 @@ impl CacheDir {
148148
));
149149
}
150150

151-
pub fn need_source_cache(entity: &Rc<SchedEntity>) -> bool {
151+
pub fn need_source_cache(entity: &Arc<SchedEntity>) -> bool {
152152
let task_type = &entity.task().task_type;
153153

154154
if let TaskType::BuildFromSource(cs) = task_type {

src/executor/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use std::{
33
env::Vars,
44
path::PathBuf,
55
process::{Command, Stdio},
6-
rc::Rc,
7-
sync::RwLock,
6+
sync::{Arc, RwLock},
87
};
98

109
use log::{debug, error, info, warn};
@@ -30,7 +29,7 @@ lazy_static! {
3029

3130
#[derive(Debug, Clone)]
3231
pub struct Executor {
33-
entity: Rc<SchedEntity>,
32+
entity: Arc<SchedEntity>,
3433
action: Action,
3534
local_envs: EnvMap,
3635
/// 任务构建结果输出到的目录
@@ -55,7 +54,7 @@ impl Executor {
5554
/// * `Ok(Executor)` - 创建成功
5655
/// * `Err(ExecutorError)` - 创建失败
5756
pub fn new(
58-
entity: Rc<SchedEntity>,
57+
entity: Arc<SchedEntity>,
5958
action: Action,
6059
dragonos_sysroot: PathBuf,
6160
) -> Result<Self, ExecutorError> {
@@ -148,7 +147,8 @@ impl Executor {
148147

149148
/// # 执行安装操作,把构建结果安装到DragonOS
150149
fn install(&self) -> Result<(), ExecutorError> {
151-
let in_dragonos_path = self.entity.task().install.in_dragonos_path.as_ref();
150+
let binding = self.entity.task();
151+
let in_dragonos_path = binding.install.in_dragonos_path.as_ref();
152152
// 如果没有指定安装路径,则不执行安装
153153
if in_dragonos_path.is_none() {
154154
return Ok(());
@@ -336,7 +336,8 @@ impl Executor {
336336
// 设置本地环境变量
337337
self.prepare_target_env()?;
338338

339-
let task_envs: Option<&Vec<TaskEnv>> = self.entity.task().envs.as_ref();
339+
let binding = self.entity.task();
340+
let task_envs: Option<&Vec<TaskEnv>> = binding.envs.as_ref();
340341
if task_envs.is_none() {
341342
return Ok(());
342343
}
@@ -538,7 +539,7 @@ pub fn prepare_env(sched_entities: &SchedEntities) -> Result<(), ExecutorError>
538539
env_list.add_vars(envs);
539540

540541
// 为每个任务创建特定的环境变量
541-
for entity in sched_entities.iter() {
542+
for entity in sched_entities.entities().iter() {
542543
// 导出任务的构建目录环境变量
543544
let build_dir = CacheDir::build_dir(entity.clone())?;
544545

src/main.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
//! - 支持自动更新
8484
//! - 完善clean命令的逻辑
8585
86+
#![feature(extract_if)]
8687
#![feature(io_error_more)]
8788

8889
#[macro_use]
@@ -103,7 +104,7 @@ use simple_logger::SimpleLogger;
103104
use crate::{
104105
console::{interactive::InteractiveConsole, CommandLineArgs},
105106
executor::cache::cache_root_init,
106-
scheduler::Scheduler,
107+
scheduler::{task_deque::TASK_DEQUE, Scheduler},
107108
};
108109

109110
mod console;
@@ -124,6 +125,7 @@ fn main() {
124125
let dragonos_dir = args.dragonos_dir.clone();
125126
let config_dir = args.config_dir.clone();
126127
let action = args.action;
128+
let thread = args.thread;
127129
info!(
128130
"DragonOS sysroot dir: {}",
129131
dragonos_dir
@@ -137,6 +139,12 @@ fn main() {
137139
.map_or_else(|| "None".to_string(), |d| d.display().to_string())
138140
);
139141
info!("Action: {:?}", action);
142+
info!(
143+
"Thread num: {}",
144+
thread
145+
.as_ref()
146+
.map_or_else(|| "None".to_string(), |d| d.to_string())
147+
);
140148

141149
match action {
142150
console::Action::New => {
@@ -150,6 +158,10 @@ fn main() {
150158
_ => {}
151159
}
152160

161+
if let Some(thread) = thread {
162+
TASK_DEQUE.lock().unwrap().set_thread(thread);
163+
}
164+
153165
// 初始化缓存目录
154166
let r = cache_root_init(args.cache_dir);
155167
if r.is_err() {

0 commit comments

Comments
 (0)