Skip to content

Commit

Permalink
fix: fixed cpu restriction bug
Browse files Browse the repository at this point in the history
  • Loading branch information
seo-rii committed Dec 30, 2021
1 parent cbb291f commit 807f38b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
7 changes: 4 additions & 3 deletions res/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ <h4>Scoring</h4>
<h4>Message</h4>
<p>${res.data.message}</p>
<h4>Run Info</h4>
<p>Time: ${res.data.time}, Memory: ${res.data.memory}</p>
<p>Time: ${res.data.time}ms, Memory: ${
res.data.memory
}KB</p>
</div>`
)
if (res.type === 'JUDGE_PROGRESS')
Expand Down Expand Up @@ -256,10 +258,9 @@ <h4>State</h4>
card.className = 'mdc-card mdc-card--outlined card'
document.querySelector('.container').prepend(card)
mdc.ripple.MDCRipple.attachTo(body)
const linearProgress = new mdc.linearProgress.MDCLinearProgress(
mdcProgress[id] = new mdc.linearProgress.MDCLinearProgress(
document.getElementById(`progress-${id}`)
)
mdcProgress[id] = linearProgress
return card
}

Expand Down
2 changes: 1 addition & 1 deletion src/runner/cpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default function (
const result = await execute(
`p-${data.uid}`,
getLimitString(
{ cpuLimit: 6 },
{ cpuLimit: 50 },
`g++ ${tmpPath}/main.cpp -o ${exePath} -O2 -Wall -lm --static -pipe -std=c++17`
)
)
Expand Down
13 changes: 8 additions & 5 deletions src/runner/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ export function execute(
const child = spawn(`su`, [userName, '-c', exePath], {
stdio: ['pipe', 'pipe', 'pipe'],
})
child.stdin.on('error', (err) => {
child.stdin.on('error', () => {
resolve({
resultType: ResultType.stdioError,
code: -1,
stdout: '',
stderr: '',
})
})
child.on('error', (err) => {
child.on('error', () => {
resolve({
resultType: ResultType.stdioError,
code: -1,
Expand All @@ -40,9 +40,11 @@ export function execute(
})
})

let timeHandler: NodeJS.Timeout
let timeHandler: NodeJS.Timeout,
timeouted = false
if (timeout)
timeHandler = setTimeout(() => {
timeouted = true
child.kill()
resolve({
resultType: ResultType.timeLimitExceeded,
Expand All @@ -67,6 +69,7 @@ export function execute(
})

child.on('close', (code) => {
if (timeouted) return
if (timeHandler) clearTimeout(timeHandler)
resolve({
resultType: ResultType.normal,
Expand Down Expand Up @@ -125,7 +128,7 @@ export function getLimitString(
limit.memoryLimit * 1024
};`
: ''
}${limit.cpuLimit ? `cpulimit -l ${limit.cpuLimit} -- ` : ''}${command}`
}${limit.cpuLimit ? `cpulimit -i -l ${limit.cpuLimit} -- ` : ''}${command}`
}

export function executeJudge(
Expand All @@ -136,7 +139,7 @@ export function executeJudge(
return execute(
`p-${data.uid}`,
getLimitString(
{ memoryLimit: data.memoryLimit, cpuLimit: 6 },
{ memoryLimit: data.memoryLimit, cpuLimit: 10 },
`/usr/bin/time -f "%E|%M" ${exePath}`
),
input,
Expand Down

0 comments on commit 807f38b

Please sign in to comment.