Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/judge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct Judge {
pub is_interactive: bool,
pub resource: Resource,
pub time_limit: Duration,
pub idle_time_limit: Duration,
}

#[bon]
Expand All @@ -42,6 +43,7 @@ impl Judge<Created> {
#[builder(default = false, name = "interactive")] is_interactive: bool,
#[builder(default)] resource: Resource,
#[builder(default)] time_limit: Duration,
#[builder(default = Duration::from_secs(1))] idle_time_limit: Duration,
) -> io::Result<Judge<Created>> {
let project_path = env::temp_dir().join(Uuid::new_v4().to_string());
fs::create_dir(&project_path).await?;
Expand Down Expand Up @@ -73,6 +75,7 @@ impl Judge<Created> {
is_interactive,
resource,
time_limit,
idle_time_limit,
_state: PhantomData,
})
}
Expand All @@ -98,6 +101,7 @@ impl Judge {
is_interactive: self.is_interactive,
resource: self.resource,
time_limit: self.time_limit,
idle_time_limit: self.idle_time_limit,
}))
}

Expand Down Expand Up @@ -129,7 +133,7 @@ impl Judge {
cstdin.write_all(b"\n").await?;
cstdin.flush().await?;

let sandbox = Sandbox::new(self.resource, self.time_limit)?;
let sandbox = Sandbox::new(self.resource, self.time_limit, self.idle_time_limit)?;
let mut cmd = self.language.get_run_command(MAIN);
cmd.current_dir(&self.project_path)
.stdin(Stdio::piped())
Expand Down
11 changes: 8 additions & 3 deletions src/sandbox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,25 @@ use crate::{
// TODO: need further tuning
const POLL: Duration = Duration::from_millis(10);
const MIN_CPU_USAGE_PER_POLL: Duration = Duration::from_millis(1);
const IDLE_TIME_LIMIT: Duration = Duration::from_millis(100);

pub struct Sandbox {
pub cgroup: Cgroup,
pub cpu_usage_limit: Duration,
pub wall_time_limit: Duration,
pub idle_time_limit: Duration,
}

impl Sandbox {
pub fn new(resource: Resource, time_limit: Duration) -> io::Result<Sandbox> {
pub fn new(
resource: Resource,
time_limit: Duration,
idle_time_limit: Duration,
) -> io::Result<Sandbox> {
Ok(Sandbox {
cgroup: resource.try_into()?,
cpu_usage_limit: time_limit,
wall_time_limit: Duration::max(time_limit * 2, time_limit + Duration::from_secs(2)),
idle_time_limit,
})
}

Expand Down Expand Up @@ -86,7 +91,7 @@ impl Sandbox {
if cpu_usage.abs_diff(prev_cpu_usage) <= MIN_CPU_USAGE_PER_POLL {
match idle_start {
Some(idle_start) => {
if idle_start.elapsed() >= IDLE_TIME_LIMIT {
if idle_start.elapsed() >= self.idle_time_limit {
return Ok((
Some(Verdict::IdleTimeLimitExceeded),
cpu_usage,
Expand Down
Loading