ms-runner/runner/runner.go

180 lines
5.7 KiB
Go
Raw Normal View History

2024-06-20 16:41:25 +00:00
package runner
import (
2024-08-25 18:09:57 +00:00
"errors"
"io"
2024-06-20 16:41:25 +00:00
"fmt"
"log"
"os"
"strings"
exec "os/exec"
cgroups "github.com/containerd/cgroups"
rand "math/rand"
"time"
2024-08-25 18:09:57 +00:00
"git.sch9.ru/new_gate/ms-tester/pkg/models"
2024-06-20 16:41:25 +00:00
)
const runIdLength = 20
2024-06-20 17:06:53 +00:00
type Limits struct {
Processes int32
Core int32
Memory int32
Time int32
2024-06-20 16:41:25 +00:00
}
const Mib = 1024*1024;
const BuildMemory = 1024*Mib;
const BuildTime = 5000;
type RunnerService struct {
CoresIsolated []int32
CoreFreed chan int32
}
func genRunId() (runId string) {
for i := 0; i < runIdLength; i++ {
runId += string('a' + byte(rand.Int31n(26)))
}
return
}
func int32ToString(n int32) string {
return fmt.Sprintf("%d", n)
}
func copyFile(src string,dst string) error {
srcFile,err := os.Open(src)
if(err!=nil) {return err}
defer srcFile.Close()
dstFile,err := os.Create(dst)
if(err!=nil) {return err}
defer dstFile.Close()
_,err = io.Copy(dstFile,srcFile)
if(err!=nil) {return err}
err = dstFile.Sync()
if(err!=nil) {return err}
return nil
}
2024-08-25 18:09:57 +00:00
func newRunFolder() (string,error) {
runId := genRunId()
err := os.Mkdir("runs/" + runId, 0777)
if(err!=nil) { return "",err }
err = os.Chmod("runs/" + runId, 0777)
if(err!=nil) { return "",err }
return runId,nil
}
func extractNumbers(s string) (result []int32) {
2024-06-22 16:29:19 +00:00
lastNumber, isNumber := false,false
var curNumber int32 = 0
2024-06-22 16:29:19 +00:00
for _, char := range s {
isNumber = (char >= '0' && char <= '9')
2024-06-20 16:41:25 +00:00
if(isNumber) {
2024-06-22 16:29:19 +00:00
curNumber *= 10
curNumber += int32(char - '0')
2024-06-20 16:41:25 +00:00
}
if(!isNumber && lastNumber) {
2024-06-22 16:29:19 +00:00
result = append(result, curNumber)
curNumber = 0
2024-06-20 16:41:25 +00:00
}
2024-06-22 16:29:19 +00:00
lastNumber = isNumber
2024-06-20 16:41:25 +00:00
}
if(lastNumber) {
2024-06-22 16:29:19 +00:00
result = append(result, curNumber)
2024-06-20 16:41:25 +00:00
}
return
}
func NewRunnerService() (*RunnerService, error) {
runnerService := RunnerService{make([]int32,0),make(chan int32)}
2024-06-20 16:41:25 +00:00
rand.Seed(time.Now().UnixNano())
//croup initialisation:
if cgroups.Mode() == cgroups.Unified {
log.Println("cgroups v2 usage approved")
} else {
return nil,fmt.Errorf("cgroups v2 are not enabled")//TODO: trouble description
2024-06-20 16:41:25 +00:00
}
//isolated cores initialisation:
cmdlineBytes := make([]byte, 400)
2024-06-22 16:29:19 +00:00
cmdlineFile, _ := os.Open("/proc/cmdline");
countCmdlineBytes, _ := cmdlineFile.Read(cmdlineBytes);
cmdline := string(cmdlineBytes[:countCmdlineBytes])
kernelParams := strings.Split(cmdline," ")
for _, param := range kernelParams{
if(len(param) >= 9 && param[:9] == "isolcpus=") {
runnerService.CoresIsolated = append(runnerService.CoresIsolated, extractNumbers(param[9:])...)
2024-06-20 16:41:25 +00:00
}
}
if(len(runnerService.CoresIsolated) == 0) {
return nil,fmt.Errorf("no free cores available")//TODO: trouble description
2024-06-20 16:41:25 +00:00
}
go func() {
for _,core := range runnerService.CoresIsolated {
runnerService.CoreFreed <- core
}
}()
log.Println("running on cores:", runnerService.CoresIsolated)
//runs directory
os.Mkdir("runs",0777)
os.Chmod("runs",0777)
//complete
2024-06-20 16:41:25 +00:00
log.Println("Runner initialisation successful!")
return &runnerService,nil
}
func (runnerService RunnerService) Build(languageId int32, filename string) {
core := <-runnerService.CoreFreed
2024-08-25 18:09:57 +00:00
runId,err := newRunFolder()
if(err!=nil) {log.Fatal
copyFile(filename,"runs/"+runId+"/src")
2024-08-25 18:09:57 +00:00
runnerService.IsolatedRun(models.Languages[languageId].CompileCmd,"../runs/"+runId,Limits{Processes: 10,Core:core,Memory:BuildMemory,Time:BuildTime});
copyFile("runs/"+runId+"/executable","executable")
os.RemoveAll("runs/" + runId)
go func() {
runnerService.CoreFreed <- core
}()
2024-06-20 16:41:25 +00:00
}
2024-08-25 18:09:57 +00:00
func (runnerService RunnerService) RunTest(solutionLanguageId int32, solutionFilename string, inputFilename string, checkerLanguageId int32, checkerFilename string, limits Limits) (models.Result) {
core := <-runnerService.CoreFreed
testRunId,err := newRunFolder()
if(err!=nil) {log.Fatal
copyFile(solutionFilename,"runs/"+testRunId+"/executable")
copyFile(inputFilename,"runs/"+testRunId+"/in")
code,err = runnerService.IsolatedRun(models.Languages[languageId].RunCmd,"../runs/"+testRunId,limits);
defer os.RemoveAll("runs/" + testRunId)
if(err!=nil) {log.Fatal(err);}
if(code!=0) {return models.RuntimeError}
checkRunId,err := newRunFolder()
copyFile("runs/"+testRunId+"/out","runs/"+checkRunId+"/out")
err = runnerService.IsolatedRun(models.Languages[languageId].RunCmd,"../runs/"+checkRunId,Limits{Processes: 10,Core:core,Memory:BuildMemory,Time:BuildTime}); //FIXME add input/output/answer files options
defer os.RemoveAll("runs/" + checkRunId)
if(err!=nil) {log.Fatal(err);}
switch code {
case 0: return models.Accepted //FIXME add model constants
case 1: return models.WrongAnswer
case 2: return models.PresentationError
case 3: return models.SystemFailDuringTesting
}
return models.SystemFailDuringTesting //bad checker result is a system fail
go func() {
runnerService.CoreFreed <- core
}()
}
func (runnerService RunnerService) IsolatedRun(command []string, sharedFolder string, limits Limits) (int32,error) {
args := []string{int32ToString(limits.Processes),int32ToString(limits.Core), int32ToString(limits.Memory), int32ToString(limits.Time), sharedFolder}
2024-06-22 16:29:19 +00:00
args = append(args, command...)
log.Println("running isolated process with command:",args)
2024-06-22 16:29:19 +00:00
cmd := exec.Command("starter/starter", args...)
err := cmd.Run();
2024-08-25 18:09:57 +00:00
if errors.Is(err,exec.ExitError) {
return err.ProcessState.ExitCode,nil
} else if(err!=nil) {
return 0,err
}
2024-08-25 18:09:57 +00:00
return 0,nil
2024-06-20 16:41:25 +00:00
}