try fix memory usage calc

This commit is contained in:
criyle 2019-07-12 20:14:22 -07:00
parent 148937fdc2
commit bcb6df3c53
4 changed files with 34 additions and 30 deletions

View File

@ -100,9 +100,9 @@ func main() {
Stack: stackLimit, Stack: stackLimit,
}, },
TraceLimit: runprogram.TraceLimit{ TraceLimit: runprogram.TraceLimit{
TimeLimit: timeLimit * 1e3, TimeLimit: uint64(timeLimit * 1e3),
RealTimeLimit: realTimeLimit * 1e3, RealTimeLimit: uint64(realTimeLimit * 1e3),
MemoryLimit: memoryLimit << 10, MemoryLimit: uint64(memoryLimit << 10),
}, },
Files: fds, Files: fds,
SyscallAllowed: h.SyscallAllow, SyscallAllowed: h.SyscallAllow,

View File

@ -150,7 +150,7 @@ func (r *Runner) Start() (int, error) {
// mkdirs(target) // mkdirs(target)
for _, p := range dirsToMake[i] { for _, p := range dirsToMake[i] {
_, _, err1 = syscall.RawSyscall(syscall.SYS_MKDIR, uintptr(unsafe.Pointer(p)), 0755, 0) _, _, err1 = syscall.RawSyscall(syscall.SYS_MKDIR, uintptr(unsafe.Pointer(p)), 0755, 0)
if err1 != 0 { if err1 != 0 && err1 != syscall.EEXIST {
goto childerror goto childerror
} }
} }

View File

@ -50,8 +50,8 @@ func (t TraceCode) Error() string {
// TraceResult is the result returned by strat trace // TraceResult is the result returned by strat trace
type TraceResult struct { type TraceResult struct {
UserTime uint // used user CPU time (in ms) UserTime uint64 // used user CPU time (in ms)
UserMem uint // used user memory (in kb) UserMem uint64 // used user memory (in kb)
ExitCode int // exit code ExitCode int // exit code
TraceStatus TraceCode // the final status for the process TraceStatus TraceCode // the final status for the process
} }
@ -64,9 +64,9 @@ type Runner interface {
// ResLimit represents the resource limit for traced process // ResLimit represents the resource limit for traced process
type ResLimit struct { type ResLimit struct {
TimeLimit uint // user CPU time limit (in ms) TimeLimit uint64 // user CPU time limit (in ms)
RealTimeLimit uint // sig_kill will force the process to exit after this limit (in ms) RealTimeLimit uint64 // sig_kill will force the process to exit after this limit (in ms)
MemoryLimit uint // user memory limit (in kB) MemoryLimit uint64 // user memory limit (in kB)
} }
// Handler defines customized handler for traced syscall // Handler defines customized handler for traced syscall

View File

@ -25,6 +25,7 @@ func Trace(handler Handler, runner Runner, limits ResLimit) (result TraceResult,
traced = make(map[int]bool) // store all process that have set ptrace options traced = make(map[int]bool) // store all process that have set ptrace options
execved = false // store whether the runner process have successfully execvd execved = false // store whether the runner process have successfully execvd
pid int // store pid of wait4 result pid int // store pid of wait4 result
initMem uint64 // initial memory usage (likely due to original process)
) )
// ptrace is thread based (kernel proc) // ptrace is thread based (kernel proc)
@ -64,9 +65,6 @@ func Trace(handler Handler, runner Runner, limits ResLimit) (result TraceResult,
// trace unixs // trace unixs
for { for {
if err = clearRefs(pgid); err != nil {
return result, err
}
if execved { if execved {
// Wait for all child in the process group // Wait for all child in the process group
pid, err = unix.Wait4(-pgid, &wstatus, unix.WALL, &rusage) pid, err = unix.Wait4(-pgid, &wstatus, unix.WALL, &rusage)
@ -80,25 +78,31 @@ func Trace(handler Handler, runner Runner, limits ResLimit) (result TraceResult,
} }
handler.Debug("------ ", pid, " ------") handler.Debug("------ ", pid, " ------")
// update resource usage and check against limits status := TraceCodeNormal
userTime := uint(rusage.Utime.Sec*1e3 + rusage.Utime.Usec/1e3) // ms if pid == pgid {
userMem := uint(rusage.Maxrss) // kb if initMem == 0 {
status := TraceCodeNormal // check limit initMem = uint64(rusage.Maxrss)
handler.Debug("initial memory:", initMem)
}
// update resource usage and check against limits
userTime := uint64(rusage.Utime.Sec*1e3 + rusage.Utime.Usec/1e3) // ms
userMem := uint64(rusage.Maxrss) - initMem // kb
// check tle / mle // check tle / mle
if userTime > limits.TimeLimit { if userTime > limits.TimeLimit {
status = TraceCodeTLE status = TraceCodeTLE
} }
if userMem > limits.MemoryLimit { if userMem > limits.MemoryLimit {
status = TraceCodeMLE status = TraceCodeMLE
} }
result = TraceResult{ result = TraceResult{
UserTime: userTime, UserTime: userTime,
UserMem: userMem, UserMem: userMem,
TraceStatus: status, TraceStatus: status,
} }
if status != TraceCodeNormal { if status != TraceCodeNormal {
return result, status return result, status
}
} }
// check process status // check process status