mirror of
https://github.com/criyle/go-sandbox.git
synced 2025-11-04 14:49:53 +08:00
try fix memory usage calc
This commit is contained in:
parent
148937fdc2
commit
bcb6df3c53
@ -100,9 +100,9 @@ func main() {
|
||||
Stack: stackLimit,
|
||||
},
|
||||
TraceLimit: runprogram.TraceLimit{
|
||||
TimeLimit: timeLimit * 1e3,
|
||||
RealTimeLimit: realTimeLimit * 1e3,
|
||||
MemoryLimit: memoryLimit << 10,
|
||||
TimeLimit: uint64(timeLimit * 1e3),
|
||||
RealTimeLimit: uint64(realTimeLimit * 1e3),
|
||||
MemoryLimit: uint64(memoryLimit << 10),
|
||||
},
|
||||
Files: fds,
|
||||
SyscallAllowed: h.SyscallAllow,
|
||||
|
||||
@ -150,7 +150,7 @@ func (r *Runner) Start() (int, error) {
|
||||
// mkdirs(target)
|
||||
for _, p := range dirsToMake[i] {
|
||||
_, _, err1 = syscall.RawSyscall(syscall.SYS_MKDIR, uintptr(unsafe.Pointer(p)), 0755, 0)
|
||||
if err1 != 0 {
|
||||
if err1 != 0 && err1 != syscall.EEXIST {
|
||||
goto childerror
|
||||
}
|
||||
}
|
||||
|
||||
@ -50,8 +50,8 @@ func (t TraceCode) Error() string {
|
||||
|
||||
// TraceResult is the result returned by strat trace
|
||||
type TraceResult struct {
|
||||
UserTime uint // used user CPU time (in ms)
|
||||
UserMem uint // used user memory (in kb)
|
||||
UserTime uint64 // used user CPU time (in ms)
|
||||
UserMem uint64 // used user memory (in kb)
|
||||
ExitCode int // exit code
|
||||
TraceStatus TraceCode // the final status for the process
|
||||
}
|
||||
@ -64,9 +64,9 @@ type Runner interface {
|
||||
|
||||
// ResLimit represents the resource limit for traced process
|
||||
type ResLimit struct {
|
||||
TimeLimit uint // user CPU time limit (in ms)
|
||||
RealTimeLimit uint // sig_kill will force the process to exit after this limit (in ms)
|
||||
MemoryLimit uint // user memory limit (in kB)
|
||||
TimeLimit uint64 // user CPU time limit (in ms)
|
||||
RealTimeLimit uint64 // sig_kill will force the process to exit after this limit (in ms)
|
||||
MemoryLimit uint64 // user memory limit (in kB)
|
||||
}
|
||||
|
||||
// Handler defines customized handler for traced syscall
|
||||
|
||||
@ -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
|
||||
execved = false // store whether the runner process have successfully execvd
|
||||
pid int // store pid of wait4 result
|
||||
initMem uint64 // initial memory usage (likely due to original process)
|
||||
)
|
||||
|
||||
// ptrace is thread based (kernel proc)
|
||||
@ -64,9 +65,6 @@ func Trace(handler Handler, runner Runner, limits ResLimit) (result TraceResult,
|
||||
|
||||
// trace unixs
|
||||
for {
|
||||
if err = clearRefs(pgid); err != nil {
|
||||
return result, err
|
||||
}
|
||||
if execved {
|
||||
// Wait for all child in the process group
|
||||
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, " ------")
|
||||
|
||||
// update resource usage and check against limits
|
||||
userTime := uint(rusage.Utime.Sec*1e3 + rusage.Utime.Usec/1e3) // ms
|
||||
userMem := uint(rusage.Maxrss) // kb
|
||||
status := TraceCodeNormal // check limit
|
||||
status := TraceCodeNormal
|
||||
if pid == pgid {
|
||||
if initMem == 0 {
|
||||
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
|
||||
if userTime > limits.TimeLimit {
|
||||
status = TraceCodeTLE
|
||||
}
|
||||
if userMem > limits.MemoryLimit {
|
||||
status = TraceCodeMLE
|
||||
}
|
||||
result = TraceResult{
|
||||
UserTime: userTime,
|
||||
UserMem: userMem,
|
||||
TraceStatus: status,
|
||||
}
|
||||
if status != TraceCodeNormal {
|
||||
return result, status
|
||||
// check tle / mle
|
||||
if userTime > limits.TimeLimit {
|
||||
status = TraceCodeTLE
|
||||
}
|
||||
if userMem > limits.MemoryLimit {
|
||||
status = TraceCodeMLE
|
||||
}
|
||||
result = TraceResult{
|
||||
UserTime: userTime,
|
||||
UserMem: userMem,
|
||||
TraceStatus: status,
|
||||
}
|
||||
if status != TraceCodeNormal {
|
||||
return result, status
|
||||
}
|
||||
}
|
||||
|
||||
// check process status
|
||||
|
||||
Loading…
Reference in New Issue
Block a user