Skip to content

Commit

Permalink
test for priorities
Browse files Browse the repository at this point in the history
priority is unlikely to run as typically everything is reported as 20 there. Maybe I'm running it wrong or it needs an integration test
  • Loading branch information
dsseng committed Dec 4, 2024
1 parent 783c5a3 commit 4840b76
Showing 1 changed file with 132 additions and 0 deletions.
132 changes: 132 additions & 0 deletions internal/app/machined/pkg/system/runner/process/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
"log"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"
"testing"
"time"

Expand Down Expand Up @@ -219,6 +222,135 @@ func (suite *ProcessSuite) TestStopSigKill() {
<-done
}

func (suite *ProcessSuite) TestPriority() {
pidFile := filepath.Join(suite.tmpDir, "talos-test-pid-prio")
//nolint:errcheck
_ = os.Remove(pidFile)

nice, err := syscall.Getpriority(syscall.PRIO_PROCESS, os.Getpid())
suite.Assert().NoError(err)
if nice >= 17 {
suite.T().Skipf("skipping test, we already have low priority %d", nice)
}

r := process.NewRunner(false, &runner.Args{
ID: "nokill",
ProcessArgs: []string{"/bin/sh", "-c", "echo $BASHPID >> " + pidFile + "; trap -- '' SIGTERM; while :; do :; done"},
},
runner.WithLoggingManager(suite.loggingManager),
runner.WithGracefulShutdownTimeout(10*time.Millisecond),
runner.WithPriority(17),
)
suite.Assert().NoError(r.Open())

defer func() { suite.Assert().NoError(r.Close()) }()

done := make(chan error, 1)

go func() {
done <- r.Run(MockEventSink)
}()

time.Sleep(10 * time.Millisecond)

pidString, err := os.ReadFile(pidFile)
suite.Assert().NoError(err)

pid, err := strconv.ParseUint(strings.Trim(string(pidString), "\r\n"), 10, 32)
suite.Assert().NoError(err)

nice, err = syscall.Getpriority(syscall.PRIO_PROCESS, int(pid))
suite.Assert().NoError(err)
suite.Assert().Equalf(17, nice, "process priority should be 17, got %d", nice)

time.Sleep(10 * time.Millisecond)

suite.Assert().NoError(r.Stop())
<-done
}

func (suite *ProcessSuite) TestIOPriority() {
pidFile := filepath.Join(suite.tmpDir, "talos-test-pid-ionice")
//nolint:errcheck
_ = os.Remove(pidFile)

r := process.NewRunner(false, &runner.Args{
ID: "nokill",
ProcessArgs: []string{"/bin/sh", "-c", "echo $BASHPID >> " + pidFile + "; trap -- '' SIGTERM; while :; do :; done"},
},
runner.WithLoggingManager(suite.loggingManager),
runner.WithGracefulShutdownTimeout(10*time.Millisecond),
runner.WithIOPriority(runner.IoprioClassIdle, 6),
)
suite.Assert().NoError(r.Open())

defer func() { suite.Assert().NoError(r.Close()) }()

done := make(chan error, 1)

go func() {
done <- r.Run(MockEventSink)
}()

time.Sleep(10 * time.Millisecond)

pidString, err := os.ReadFile(pidFile)
suite.Assert().NoError(err)

pid, err := strconv.ParseUint(strings.Trim(string(pidString), "\r\n"), 10, 32)
suite.Assert().NoError(err)

ioprio, _, _ := syscall.Syscall(syscall.SYS_IOPRIO_GET, uintptr(1), uintptr(pid), 0)
suite.Assert().NotEqual(-1, int(ioprio))
suite.Assert().Equal(runner.IoprioClassIdle<<13+6, int(ioprio))

time.Sleep(10 * time.Millisecond)

suite.Assert().NoError(r.Stop())
<-done
}

func (suite *ProcessSuite) TestSchedulingPolicy() {
pidFile := filepath.Join(suite.tmpDir, "talos-test-pid-sched")
//nolint:errcheck
_ = os.Remove(pidFile)

r := process.NewRunner(false, &runner.Args{
ID: "nokill",
ProcessArgs: []string{"/bin/sh", "-c", "echo $BASHPID >> " + pidFile + "; trap -- '' SIGTERM; while :; do :; done"},
},
runner.WithLoggingManager(suite.loggingManager),
runner.WithGracefulShutdownTimeout(10*time.Millisecond),
runner.WithSchedulingPolicy(runner.SchedulingPolicyIdle),
)
suite.Assert().NoError(r.Open())

defer func() { suite.Assert().NoError(r.Close()) }()

done := make(chan error, 1)

go func() {
done <- r.Run(MockEventSink)
}()

time.Sleep(10 * time.Millisecond)

pidString, err := os.ReadFile(pidFile)
suite.Assert().NoError(err)

pid, err := strconv.ParseUint(strings.Trim(string(pidString), "\r\n"), 10, 32)
suite.Assert().NoError(err)

pol, _, errno := syscall.Syscall(syscall.SYS_SCHED_GETSCHEDULER, uintptr(pid), 0, 0)
suite.Assert().Equal(0, int(errno))
suite.Assert().Equal(runner.SchedulingPolicyIdle, int(pol))

time.Sleep(10 * time.Millisecond)

suite.Assert().NoError(r.Stop())
<-done
}

func TestProcessSuite(t *testing.T) {
for _, runReaper := range []bool{true, false} {
func(runReaper bool) {
Expand Down

0 comments on commit 4840b76

Please sign in to comment.