2024-06-20 16:41:25 +00:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
2024-06-22 15:59:29 +00:00
|
|
|
"bytes"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
const runIdLength = 20
|
|
|
|
var coresIsolated []int
|
|
|
|
|
2024-06-20 17:06:53 +00:00
|
|
|
type Limits struct {
|
2024-06-22 15:59:29 +00:00
|
|
|
Core int
|
|
|
|
Memory int
|
|
|
|
Time int
|
2024-06-20 16:41:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func extractNumbers(s string) (result []int) {
|
2024-06-22 16:29:19 +00:00
|
|
|
lastNumber, isNumber := false,false
|
|
|
|
var curNumber = 0
|
|
|
|
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 += int(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 Init() error {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
//croup initialisation:
|
|
|
|
if cgroups.Mode() == cgroups.Unified {
|
|
|
|
log.Println("cgroups v2 usage approved")
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("cgroups v2 are not enabled")//TODO: trouble description
|
|
|
|
}
|
|
|
|
//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=") {
|
|
|
|
coresIsolated = append(coresIsolated, extractNumbers(param[9:])...)
|
2024-06-20 16:41:25 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-22 16:29:19 +00:00
|
|
|
if(len(coresIsolated) == 0) {
|
2024-06-20 16:41:25 +00:00
|
|
|
return fmt.Errorf("no free cores available")//TODO: trouble description
|
|
|
|
}
|
2024-06-22 16:29:19 +00:00
|
|
|
log.Println("running on cores:", coresIsolated)
|
2024-06-20 16:41:25 +00:00
|
|
|
log.Println("Runner initialisation successful!")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-22 16:29:19 +00:00
|
|
|
func IsolatedRun(command []string, limits Limits) {
|
2024-06-20 16:41:25 +00:00
|
|
|
var runId string
|
2024-06-22 16:29:19 +00:00
|
|
|
for i := 0; i < runIdLength; i++ {runId += string('a' + byte(rand.Int31n(26)))}
|
|
|
|
os.Mkdir("starter/" + runId, 0777)
|
|
|
|
args := []string{string(limits.Core), string(limits.Memory), string(limits.Time), runId}
|
|
|
|
args = append(args, command...)
|
|
|
|
cmd := exec.Command("starter/starter", args...)
|
2024-06-22 15:59:29 +00:00
|
|
|
var stdBuffer bytes.Buffer
|
|
|
|
mw := io.MultiWriter(os.Stdout, &stdBuffer)
|
|
|
|
cmd.Stdout = mw
|
|
|
|
err := cmd.Run();
|
|
|
|
if err != nil { log.Fatal(err) }
|
2024-06-22 16:29:19 +00:00
|
|
|
os.RemoveAll("starter/" + runId)
|
2024-06-22 15:59:29 +00:00
|
|
|
cmd.Wait()
|
2024-06-20 16:41:25 +00:00
|
|
|
}
|