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,
|
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,
|
||||||
|
|||||||
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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,10 +78,15 @@ func Trace(handler Handler, runner Runner, limits ResLimit) (result TraceResult,
|
|||||||
}
|
}
|
||||||
handler.Debug("------ ", pid, " ------")
|
handler.Debug("------ ", pid, " ------")
|
||||||
|
|
||||||
|
status := TraceCodeNormal
|
||||||
|
if pid == pgid {
|
||||||
|
if initMem == 0 {
|
||||||
|
initMem = uint64(rusage.Maxrss)
|
||||||
|
handler.Debug("initial memory:", initMem)
|
||||||
|
}
|
||||||
// update resource usage and check against limits
|
// update resource usage and check against limits
|
||||||
userTime := uint(rusage.Utime.Sec*1e3 + rusage.Utime.Usec/1e3) // ms
|
userTime := uint64(rusage.Utime.Sec*1e3 + rusage.Utime.Usec/1e3) // ms
|
||||||
userMem := uint(rusage.Maxrss) // kb
|
userMem := uint64(rusage.Maxrss) - initMem // kb
|
||||||
status := TraceCodeNormal // check limit
|
|
||||||
|
|
||||||
// check tle / mle
|
// check tle / mle
|
||||||
if userTime > limits.TimeLimit {
|
if userTime > limits.TimeLimit {
|
||||||
@ -100,6 +103,7 @@ func Trace(handler Handler, runner Runner, limits ResLimit) (result TraceResult,
|
|||||||
if status != TraceCodeNormal {
|
if status != TraceCodeNormal {
|
||||||
return result, status
|
return result, status
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// check process status
|
// check process status
|
||||||
switch {
|
switch {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user