-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.rs
291 lines (262 loc) · 9.41 KB
/
scheduler.rs
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// done
use rand::{Rng, SeedableRng};
use std::collections::HashMap;
const HELP: &str = include_str!("help.txt");
struct SchedulerOption {
seed: u64,
jobs: u32,
jlist: String,
maxlen: u32,
policy: String,
quantum: u32,
solve: bool,
}
impl SchedulerOption {
fn new() -> SchedulerOption {
SchedulerOption {
seed: 0,
jobs: 3,
jlist: String::from(""),
maxlen: 10,
policy: String::from("FIFO"),
quantum: 1,
solve: false,
}
}
}
pub fn parse_op(op_vec: Vec<&str>) {
let mut sche_op = SchedulerOption::new();
let mut i = 2;
while i < op_vec.len() {
// println!("{}", op_vec[i]);
match op_vec[i] {
"-h" | "--help" => {
print!("{}", HELP);
return;
}
"-s" => {
sche_op.seed = op_vec[i + 1].parse().unwrap();
i = i + 2;
}
"-j" => {
sche_op.jobs = op_vec[i + 1].parse().unwrap();
i = i + 2;
}
"-l" => {
sche_op.jlist = op_vec[i + 1].to_string();
i = i + 2;
}
"-m" => {
sche_op.maxlen = op_vec[i + 1].parse().unwrap();
i = i + 2;
}
"-p" => {
sche_op.policy = op_vec[i + 1].to_string();
i = i + 2;
}
"-q" => {
sche_op.quantum = op_vec[i + 1].parse().unwrap();
i = i + 2;
}
"-c" => {
sche_op.solve = true;
i = i + 1;
}
_ => {
println!("scheduler_op_parse match wrong!!");
return;
}
}
}
execute_scheduler_op(sche_op);
}
#[derive(Debug, PartialEq, PartialOrd, Clone)]
struct Job {
jobnum: u32,
runtime: f32,
}
impl Job {
fn new(jobnum: u32, runtime: f32) -> Self {
Job { jobnum, runtime }
}
}
fn execute_scheduler_op(mut options: SchedulerOption) {
let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(options.seed);
println!("ARG policy : {}", options.policy);
if options.jlist == "" {
println!("ARG jobs : {}", options.jobs);
println!("ARG maxlen : {}", options.maxlen);
println!("ARG seed : {}", options.seed);
} else {
println!("ARG jlist : {}", options.jlist)
}
println!("");
println!("Here is the job list, with the run time of each job: ");
let mut joblist: Vec<Job> = Vec::new();
if options.jlist == "" {
for jobnum in 0..options.jobs {
let rand_x: f64 = rng.gen();
let runtime = (options.maxlen as f64 * rand_x) as u32 + 1;
let v = Job::new(jobnum, runtime as f32);
// v.push(jobnum as f32);
// v.push(runtime as f32);
joblist.push(v);
println!(" Job : {} ( length = {} )", jobnum, runtime);
}
} else {
let mut jobnum = 0;
for runtime in options.jlist.split(",") {
// let mut v: Vec<f32> = Vec::new();
let v = Job::new(jobnum, runtime.to_string().parse::<f32>().unwrap());
// v.push(jobnum as f32);
// v.push(runtime.to_string().parse::<f32>().unwrap());
joblist.push(v);
jobnum += 1;
}
for job in &joblist {
println!(
" Job : {} (length = {} )",
job.jobnum,
job.runtime.to_string()
);
}
}
println!("");
if options.solve == true {
println!("** Solutions **");
if options.policy == "SJF" {
// not impl SJF
joblist.sort_by(|a, b| a.runtime.partial_cmp(&b.runtime).unwrap());
options.policy = "FIFO".to_string();
}
if options.policy == "FIFO" {
let mut thetime: f32 = 0.0;
println!("Execution trace:");
for job in &joblist {
println!(" [ time {time:>width$} ] Run job {job0} for {job1} secs ( DONE at {donetime} )" ,time = thetime,width = 3, job0 = job.jobnum, job1= format!("{:.*}",2,job.runtime),donetime = format!("{:.*}",2,(thetime + job.runtime)));
thetime += job.runtime;
}
println!("Final statistics :");
let mut t = 0.0;
let mut count = 0;
let mut turnaround_sum = 0.0;
let mut wait_sum = 0.0;
let mut response_sum = 0.0;
for tmp in &joblist {
let jobnum = tmp.jobnum;
let runtime = tmp.runtime;
let response = t;
let turnaround = t + runtime;
let wait = t;
println!(
" Job {} -- Response: {} Turnaround {} Wait {}",
jobnum, response, turnaround, wait
);
response_sum += response;
turnaround_sum += turnaround;
wait_sum += wait;
t += runtime;
count = count + 1;
}
println!();
println!(
" Average -- Response: {} Turnaround : {} Wait : {}",
response_sum / count as f32,
turnaround_sum / count as f32,
wait_sum / count as f32
);
}
if options.policy == "RR" {
println!("Execution trace:");
let mut turnaround: HashMap<usize, f32> = HashMap::new();
let mut response: HashMap<usize, f32> = HashMap::new();
let mut lastran: HashMap<usize, f32> = HashMap::new();
let mut wait: HashMap<usize, f32> = HashMap::new();
let mut jobcount = joblist.len();
for _i in 0..jobcount {
lastran.entry(_i).or_insert(0.0f32);
wait.entry(_i).or_insert(0.0f32);
turnaround.entry(_i).or_insert(0.0f32);
response.entry(_i).or_insert(-1f32);
}
let mut runlist: Vec<Job> = Vec::new();
for e in &joblist {
runlist.push(e.clone());
}
let mut thetime = 0.0f32;
while jobcount > 0 {
let job = runlist.remove(0);
let jobnum = job.jobnum;
let mut runtime = job.runtime;
let quantum = options.quantum;
let mut _ranfor: f32;
if response[&(jobnum as usize)] == -1f32 {
if let Some(x) = response.get_mut(&(jobnum as usize)) {
*x = thetime;
}
}
let currwait = thetime - lastran[&(jobnum as usize)];
if let Some(x) = wait.get_mut(&(jobnum as usize)) {
*x += currwait;
}
if runtime > quantum as f32 {
runtime -= quantum as f32;
_ranfor = quantum as f32;
println!(
" [ time {:3} ] Run job {:3} for {:.2} secs",
thetime, jobnum, _ranfor
);
runlist.push(Job::new(jobnum, runtime));
} else {
_ranfor = runtime;
println!(
" [ time {:3} ] Run job {:3} for {:.2} secs ( DONE at {:.2} )",
thetime,
jobnum,
_ranfor,
format!("{:.*}", 2, (thetime + job.runtime))
);
if let Some(x) = turnaround.get_mut(&(jobnum as usize)) {
*x = thetime + _ranfor;
}
jobcount -= 1;
}
thetime += _ranfor;
if let Some(x) = lastran.get_mut(&(jobnum as usize)) {
*x = thetime;
}
}
println!("Final statistics :");
let mut turnaround_sum = 0.0;
let mut wait_sum = 0.0;
let mut response_sum = 0.0;
for i in 0..joblist.len() {
turnaround_sum += turnaround[&i];
response_sum += response[&i];
wait_sum += wait[&i];
println!(
" Job {:3} -- Response: {:3.2} Turnaround {:3.2} Wait {:3.2}",
i, response[&i], turnaround[&i], wait[&i]
);
}
let count = joblist.len();
println!();
println!(
" Average -- Response: {} Turnaround : {} Wait : {}",
response_sum / count as f32,
turnaround_sum / count as f32,
wait_sum / count as f32
);
}
if options.policy != "FIFO" && options.policy != "SJF" && options.policy != "RR" {
println!("Error : Policy {} is not available", options.policy);
}
} else {
println!("Compute the turnaround time, response time, and wait time for each job.");
println!("When you are done, run this program again, with the same arguments,");
println!("but with -c, which will thus provide you with the answers. You can use");
println!("-s <somenumber> or your own job list (-l 10,15,20 for example)");
println!("to generate different problems for yourself.");
println!("");
}
}