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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
extern crate log;
extern crate env_logger;
extern crate zmq;
extern crate serde_json;
use json_protocol::*;
use self::serde_json::Error;
use std::fmt;
pub trait Scheduler {
#[warn(unused_variables)]
fn simulation_begins(&mut self,
timestamp: &f64,
nb_resources: i32,
config: serde_json::Value)
-> Option<Vec<BatsimEvent>>;
#[warn(unused_variables)]
fn on_job_submission(&mut self,
timestamp: &f64,
job: Job,
profile: Option<Profile>)
-> Option<Vec<BatsimEvent>>;
#[warn(unused_variables)]
fn on_job_completed(&mut self,
timestamp: &f64,
job_id: String,
status: String)
-> Option<Vec<BatsimEvent>>;
#[warn(unused_variables)]
fn on_job_killed(&mut self,
timestamp: &f64,
job_ids: Vec<String>)
-> Option<Vec<BatsimEvent>>;
#[warn(unused_variables)]
fn on_message_received_end(&mut self, timestamp: &mut f64) -> Option<Vec<BatsimEvent>>;
#[warn(unused_variables)]
fn on_message_received_begin(&mut self, timestamp: &f64) -> Option<Vec<BatsimEvent>>;
#[warn(unused_variables)]
fn on_simulation_ends(&mut self, timestamp: &f64);
}
pub struct Batsim<'a> {
zmq_socket: zmq::Socket,
time: f64,
nb_resources: i32,
scheduler: &'a mut Scheduler,
}
impl Job {
pub fn split_id(id: &String) -> (String, String) {
let workload;
let decimal_id;
let indx = id.find('!').unwrap();
{
let (w_id, job_id) = id.split_at(indx + 1);
decimal_id = String::from(job_id);
workload = String::from(w_id);
}
(workload, decimal_id)
}
}
impl Clone for Job {
fn clone(&self) -> Job {
Job {
id: self.id.clone(),
res: self.res,
profile: self.profile.clone(),
subtime: self.subtime,
walltime: self.walltime,
}
}
}
impl fmt::Display for Job {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f,
"(id: {}\t,res: {},\t subtime: {},\t walltime: {})",
self.id,
self.res,
self.subtime,
self.walltime)
}
}
impl<'a> Batsim<'a> {
pub fn new(scheduler: &'a mut Scheduler) -> Batsim<'a> {
let socket_url = "tcp://*:28000";
let context = zmq::Context::new();
let socket = context.socket(zmq::REP).unwrap();
assert!(socket.bind(socket_url).is_ok());
Batsim {
scheduler: scheduler,
zmq_socket: socket,
time: -1.0,
nb_resources: -1,
}
}
pub fn init(&mut self) -> Result<(), Error> {
let init_message: BatsimMessage = self.get_next_message().unwrap();
let init_event: &BatsimEvent = &init_message.events[0];
let mut res = self.get_nop().unwrap();
match *init_event {
BatsimEvent::SIMULATION_BEGINS {
ref data,
ref timestamp,
} => {
let mut temp_timestamp = *timestamp;
self.nb_resources = data.nb_resources;
match self.scheduler
.simulation_begins(&mut temp_timestamp,
data.nb_resources,
data.config.clone()) {
Some(mut events) => res.events.append(&mut events),
None => {}
}
self.time = temp_timestamp;
}
_ => panic!("We should receive a SIMULATION BEGIN at this point"),
};
res.now = self.time;
try!(self.send_message(res));
Ok(())
}
pub fn get_next_message(&self) -> Option<BatsimMessage> {
let msg = self.pull_network_message().unwrap();
Some(read_batsim_message(&msg).unwrap())
}
pub fn pull_network_message(&self) -> Result<String, Error> {
let msg = match self.zmq_socket.recv_msg(0) {
Ok(msg) => msg,
Err(why) => panic!("{:?}", why),
};
Ok(String::from(msg.as_str().unwrap()))
}
pub fn get_nop(&self) -> Result<BatsimMessage, Error> {
Ok(BatsimMessage {
now: self.time,
events: Vec::new(),
})
}
pub fn send_message(&self, message: BatsimMessage) -> Result<(), Error> {
let message_json = serde_json::to_string(&message).unwrap();
self.zmq_socket
.send_str(message_json.as_str(), 0)
.unwrap();
Ok(())
}
pub fn run_simulation(&mut self) -> Result<(), Error> {
match self.init() {
Ok(()) => {}
_ => panic!("Could not initialize the simulation, aborting."),
};
let mut next: Option<BatsimMessage> = self.get_next_message();
'main: while let Some(msg) = next {
trace!("Received msg: {:?}", msg);
self.time = msg.now;
let mut schedule_timestamp: f64 = msg.now;
let mut res = self.get_nop().unwrap();
match self.scheduler
.on_message_received_begin(&schedule_timestamp) {
Some(mut events) => res.events.append(&mut events),
None => {}
};
for event in msg.events {
match event {
BatsimEvent::SIMULATION_BEGINS { .. } => {
panic!("Received simulation_begins,
this should not happends at this point");
}
BatsimEvent::JOB_SUBMITTED {
ref data,
ref timestamp,
} => {
match self.scheduler
.on_job_submission(timestamp,
data.job.clone(),
data.profile.clone()) {
Some(mut events) => res.events.append(&mut events),
None => {}
};
}
BatsimEvent::SIMULATION_ENDS { ref timestamp } => {
self.scheduler
.on_simulation_ends(timestamp);
next = None;
try!(self.send_message(res));
continue 'main;
}
BatsimEvent::JOB_COMPLETED {
ref data,
ref timestamp,
} => {
match self.scheduler
.on_job_completed(timestamp,
data.job_id.clone(),
data.status.clone()) {
Some(mut events) => res.events.append(&mut events),
None => {}
};
}
BatsimEvent::JOB_KILLED {
ref data,
ref timestamp,
} => {
match self.scheduler
.on_job_killed(timestamp,
data.job_ids.clone()) {
Some(mut events) => res.events.append(&mut events),
None => {}
};
}
_ => panic!("Unexpected event"),
}
}
match self.scheduler
.on_message_received_end(&mut schedule_timestamp) {
Some(mut events) => res.events.append(&mut events),
None => {}
};
res.now = schedule_timestamp;
trace!("Send msg to batsim: {:?}", res);
try!(self.send_message(res));
next = self.get_next_message();
}
Ok(())
}
}
pub fn allocate_job_event(time: f64, job: &Job, allocation: String) -> BatsimEvent {
BatsimEvent::EXECUTE_JOB {
timestamp: time,
data: ExecuteJob {
job_id: job.id.clone(),
alloc: allocation,
},
}
}
pub fn reject_job_event(time: f64, job: &Job) -> BatsimEvent {
BatsimEvent::REJECT_JOB {
timestamp: time,
data: RejectJob { job_id: job.id.clone() },
}
}
pub fn notify_event(time: f64, n_type: String) -> BatsimEvent {
BatsimEvent::NOTIFY {
timestamp: time,
data: Notify { notify_type: n_type.clone() },
}
}
pub fn submit_job_event(time: f64, job: &Job, profile: Option<&Profile>) -> BatsimEvent {
let p = match profile {
Some(prof) => Some(prof.clone()),
None => None,
};
BatsimEvent::SUBMIT_JOB {
timestamp: time,
data: SubmitJob {
job_id: job.id.clone(),
job: job.clone(),
profile: p,
},
}
}
pub fn kill_jobs_event(time: f64, jobs: Vec<&Job>) -> BatsimEvent {
let mut job_ids = vec![];
for job in jobs {
job_ids.push(job.id.clone());
}
BatsimEvent::KILL_JOB {
timestamp: time,
data: KillJob { job_ids: job_ids },
}
}
pub fn read_batsim_message(msg_str: &str) -> Result<BatsimMessage, Error> {
let message: BatsimMessage = match serde_json::from_str(msg_str) {
Ok(value) => value,
Err(why) => panic!("{:?} full str: {}", why, msg_str),
};
Ok(message)
}