Skip to content

Commit

Permalink
Make nicer logging format
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaschan committed Apr 10, 2020
1 parent c134544 commit 90476e8
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 7 deletions.
8 changes: 6 additions & 2 deletions job_completion_plugin/src/jobcomp_bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extern crate slurm_banking;
use slurm_banking::accounting;
use slurm_banking::bindings::*;
use slurm_banking::logging;
use slurm_banking::logging::display_job_record;
use slurm_banking::safe_helpers;

use chrono::prelude::*;
Expand Down Expand Up @@ -143,7 +144,7 @@ pub extern "C" fn slurm_jobcomp_log_record(job_ptr: *const job_record) -> u32 {

let job_state = unsafe { (*job_ptr).job_state };
let job_state_ptr = unsafe { job_state_string(job_state) };
let job_state_str = safe_helpers::deref_cstr(job_state_ptr).unwrap();
let job_state_str = safe_helpers::deref_cstr(job_state_ptr).unwrap_or("".to_string());

let num_req_nodes = unsafe { (*(*job_ptr).details).min_nodes };
let num_alloc_nodes = unsafe { (*job_ptr).total_nodes };
Expand All @@ -167,7 +168,10 @@ pub extern "C" fn slurm_jobcomp_log_record(job_ptr: *const job_record) -> u32 {

log_with_jobid(
&jobslurmid,
&format!("Updating job with info: {:?}", job_update_record),
&format!(
"Updating job with info: {}",
display_job_record(&job_update_record)
),
);
let base_path = slurm_banking::prices_config::get_base_path(&conf);
let auth_token = slurm_banking::prices_config::get_auth_token(&conf);
Expand Down
46 changes: 46 additions & 0 deletions slurm_banking/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::os::raw::c_char;

use super::bindings::*;

extern crate openapi;

extern "C" {
fn info(message: *const c_char);
}
Expand Down Expand Up @@ -32,3 +34,47 @@ pub fn safe_spank_error(message: &str) {
slurm_error(message_cstring.as_ptr());
}
}

fn display_option<T: std::fmt::Debug>(option: &Option<T>) -> String {
match option {
Some(x) => format!("{:?}", x),
None => "None".to_string(),
}
}

pub fn display_job_record(job_record: &openapi::models::Job) -> String {
format!(
"jobslurmid: {jobslurmid}, \
submitdate: {submitdate}, \
startdate: {startdate}, \
enddate: {enddate}, \
userid: {userid}, \
accountid: {accountid}, \
amount: {amount}, \
jobstatus: {jobstatus}, \
partition: {partition}, \
qos: {qos}, \
nodes: {nodes}, \
num_cpus: {num_cpus}, \
num_req_nodes: {num_req_nodes}, \
num_alloc_nodes: {num_alloc_nodes}, \
raw_time: {raw_time}, \
cpu_time: {cpu_time}",
jobslurmid = job_record.jobslurmid,
submitdate = display_option(&job_record.submitdate),
startdate = display_option(&job_record.startdate),
enddate = display_option(&job_record.enddate),
userid = job_record.userid,
accountid = job_record.accountid,
amount = display_option(&job_record.amount),
jobstatus = display_option(&job_record.jobstatus),
partition = display_option(&job_record.partition),
qos = display_option(&job_record.qos),
nodes = display_option(&job_record.nodes),
num_cpus = display_option(&job_record.num_cpus),
num_req_nodes = display_option(&job_record.num_req_nodes),
num_alloc_nodes = display_option(&job_record.num_alloc_nodes),
raw_time = display_option(&job_record.raw_time),
cpu_time = display_option(&job_record.cpu_time)
)
}
33 changes: 28 additions & 5 deletions spank_plugin/src/spank_plugin_bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern crate slurm_banking;
use slurm_banking::accounting;
use slurm_banking::bindings::*;
use slurm_banking::logging;
use slurm_banking::logging::display_job_record;
use slurm_banking::safe_helpers;

use chrono::prelude::*;
Expand Down Expand Up @@ -83,10 +84,28 @@ pub extern "C" fn slurm_spank_init(sp: spank_t, _ac: c_int, _argv: *const *const
// END: Check if this plugin should be enabled

let partition =
safe_helpers::deref_cstr(unsafe { (*((*job_buffer_ptr).job_array)).partition }).unwrap();
let qos = safe_helpers::deref_cstr(unsafe { (*((*job_buffer_ptr).job_array)).qos }).unwrap();
match safe_helpers::deref_cstr(unsafe { (*((*job_buffer_ptr).job_array)).partition }) {
Some(partition) => partition,
None => {
log("Failed to load partition");
return SLURM_SUCCESS;
}
};
let qos = match safe_helpers::deref_cstr(unsafe { (*((*job_buffer_ptr).job_array)).qos }) {
Some(qos) => qos,
None => {
log("Failed to load QoS");
return SLURM_SUCCESS;
}
};
let account =
safe_helpers::deref_cstr(unsafe { (*((*job_buffer_ptr).job_array)).account }).unwrap();
match safe_helpers::deref_cstr(unsafe { (*((*job_buffer_ptr).job_array)).account }) {
Some(account) => account,
None => {
log("Failed to load account name");
return SLURM_SUCCESS;
}
};
let num_cpus = unsafe { (*((*job_buffer_ptr).job_array)).num_cpus };
let max_cpus = unsafe { (*((*job_buffer_ptr).job_array)).max_cpus };
let num_nodes = unsafe { (*((*job_buffer_ptr).job_array)).num_nodes };
Expand Down Expand Up @@ -122,11 +141,12 @@ pub extern "C" fn slurm_spank_init(sp: spank_t, _ac: c_int, _argv: *const *const
.map(|name| openapi::models::Node::new(name))
.collect();
log(&format!("Nodes: {:?}", nodes));
let jobstatus = "RUNNING".to_string();

let mut job_create_record =
openapi::models::Job::new(job_id.to_string(), user_id.to_string(), account);
job_create_record.amount = Some(expected_cost.to_string());
job_create_record.jobstatus = Some("RUNNING".to_string());
job_create_record.jobstatus = Some(jobstatus);
job_create_record.partition = Some(partition);
job_create_record.qos = Some(qos);
job_create_record.startdate = Some(start_timestamp_str);
Expand All @@ -135,7 +155,10 @@ pub extern "C" fn slurm_spank_init(sp: spank_t, _ac: c_int, _argv: *const *const
job_create_record.num_cpus = Some(num_cpus as i32);
job_create_record.num_alloc_nodes = Some(num_nodes as i32);

log(&format!("Creating job wih info: {:?}", job_create_record));
log(&format!(
"Creating job wih info: {}",
display_job_record(&job_create_record)
));
let base_path = slurm_banking::prices_config::get_base_path(&conf);
let auth_token = slurm_banking::prices_config::get_auth_token(&conf);
let _ = accounting::create_job(base_path, &auth_token, job_create_record);
Expand Down

0 comments on commit 90476e8

Please sign in to comment.