Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ type Config struct {
// Workers is the number of I/O worker goroutines (the loops of the native
// engines). Default: GOMAXPROCS, less a reserve on Linux: with Workers unset
// the native engines (epoll, io_uring, adaptive) on a host whose allowed
// CPUs are alike start GOMAXPROCS - max(1, GOMAXPROCS/8) loops (no reserve
// for a GOMAXPROCS of 2 or less): 28 on 32, 14 on 16, 7 on 8, 3 on 4, 2 on
// CPUs are alike start GOMAXPROCS - max(1, GOMAXPROCS/4) loops (no reserve
// for a GOMAXPROCS of 2 or less): 24 on 32, 12 on 16, 6 on 8, 3 on 4, 2 on
// 3. A loop locks its thread and can block in the kernel while it holds its
// P, so the Ps that run no loop are left to the goroutines that are not
// loops (async and WebSocket handlers, the HTTP/2 stream pool, the garbage
Expand Down
4 changes: 2 additions & 2 deletions internal/adaptive/started_loops_756_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// celeris#756, #757: startedLoops, which the adaptive engine uses to size the
// io_uring memlock check, counts the loops the engines start: the default
// count on a host of alike CPUs is the request less the reserve of
// max(1, n/8) (none at 2 or less), a user-set count is the request. The real
// max(1, n/4) (none at 2 or less), a user-set count is the request. The real
// planner, no stub; the figures are written out.
func TestStartedLoopsLeaveAReserve756(t *testing.T) {
if platform.PlanWorkerCPUs(12, true).Heterogeneous {
Expand All @@ -24,7 +24,7 @@ func TestStartedLoopsLeaveAReserve756(t *testing.T) {
explicit bool
want int
}{
{16, false, 14}, {16, true, 16}, {8, false, 7}, {32, false, 28},
{16, false, 12}, {16, true, 16}, {8, false, 6}, {32, false, 24},
{4, false, 3}, {3, false, 2}, {2, false, 2}, {1, false, 1}, {5, true, 5},
} {
if got := startedLoops(tc.requested, tc.explicit); got != tc.want {
Expand Down
6 changes: 3 additions & 3 deletions internal/engine/epoll/default_loops_756_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// celeris#756, #757: an epoll engine whose worker count is not set by the user
// starts GOMAXPROCS - max(1, GOMAXPROCS/8) loops on a host of alike CPUs (none
// starts GOMAXPROCS - max(1, GOMAXPROCS/4) loops on a host of alike CPUs (none
// for a GOMAXPROCS of 2 or less; Resolve raises a count to two), and a count
// the user sets is started whole. This is the real planner and real engine, no
// stub: Metrics().Workers, NumWorkers(), the loops and the "listening" line
Expand All @@ -37,11 +37,11 @@ func TestDefaultLoopsLeaveAReserve756(t *testing.T) {
workers int // Resources.Workers; 0 = the default
want int // loops started
}{
{16, 0, 14}, // the default: 16 Ps, 14 loops (rc1: 16)
{16, 0, 12}, // the default: 16 Ps, 12 loops (rc1: 16, first reserve: 14)
{16, 16, 16}, // Workers = GOMAXPROCS: one loop per P, as before
{16, 5, 5}, // a user-set count is kept whole
{16, 20, 20}, // also above GOMAXPROCS
{8, 0, 7},
{8, 0, 6},
{4, 0, 3},
{3, 0, 2},
{2, 0, 2}, // no reserve at two
Expand Down
4 changes: 2 additions & 2 deletions internal/platform/cpuplan_1036_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ func TestPlanHeterogeneousNeverUnpinnedNeverLittle1036(t *testing.T) {
}
if !het {
// Alike CPUs: one entry per worker for a user-set count; a
// default count leaves the reserve of max(1, n/8) without
// default count leaves the reserve of max(1, n/4) without
// a loop, none for n <= 2 (celeris#756, #757).
wantLoops := workers
if !explicit && workers > 2 {
wantLoops -= max(1, workers/8)
wantLoops -= max(1, workers/4)
}
if len(p.CPUs) != wantLoops {
t.Fatalf("%s: %d entries, want %d (a host of alike CPUs keeps every worker it is asked for, less the default reserve)", tag, len(p.CPUs), wantLoops)
Expand Down
17 changes: 11 additions & 6 deletions internal/platform/cpuplan_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,21 @@ func schedAllowedCPUs() ([]int, error) {
// loop (celeris#756, #757; a mechanism inferred from traces, not established).
// The reserve leaves that many Ps to them.
//
// max(1, n/8), none for n <= 2 (a count that small would drop to one loop or
// none): 14 of 16, chosen by the maintainer after a cluster comparison of 16,
// 14 and 12 loops on one amd64 host. The runs on the final commit that would
// confirm the reserve are still owed, so the value may change; changing it is
// this function's one return statement (celeris#756, #757).
// max(1, n/4), none for n <= 2 (a count that small would drop to one loop or
// none): 12 of 16. It was max(1, n/8), 14 of 16, until a cluster comparison on
// one amd64 host (celeris#756: 16 CPUs, WebSocket large-echo p99 against the
// std engine, 5 repetitions per arm, the two rated levels 0.5 and 0.75 of
// saturation) found that the Ps left free decide the tail: 14 loops on 16 Ps
// 3.26 and 8.75 times, 12 loops 1.45 and 3.37 times at the same levels (a
// measurement of one host, lane-finding, not a closing run). The runs on the
// final commit that would confirm the reserve are still owed, so the value may
// change; changing it is this function's one return statement (celeris#756,
// #757).
func defaultLoopReserve(n int) int {
if n <= 2 {
return 0
}
return max(1, n/8)
return max(1, n/4)
}

func planWorkerCPUs(numWorkers int, explicit bool, src planSource) CPUPlan {
Expand Down
2 changes: 1 addition & 1 deletion internal/platform/cpuplan_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ func TestPlanUnrestrictedHomogeneousEqualsTheOldPlan(t *testing.T) {
for _, explicit := range []bool{false, true} {
wantN := workers
if !explicit && workers > 2 {
wantN -= max(1, workers/8)
wantN -= max(1, workers/4)
}
wantPlan := want[:wantN]
got := planWorkerCPUs(workers, explicit, h.src()).CPUs
Expand Down
37 changes: 19 additions & 18 deletions internal/platform/cpuplan_reserve_756_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// celeris#756, #757: a DEFAULT loop count (Config.Workers not set) on a host of
// alike CPUs leaves a reserve of Ps to the goroutines that are not loops: the
// plan holds n - max(1, n/8) loops, none for n <= 2. A user-set count is
// plan holds n - max(1, n/4) loops, none for n <= 2. A user-set count is
// honoured, and so is the big.LITTLE plan. These tests pin the POLICY (the
// loop counts), not any latency: the figures are written out and the tests do
// not call defaultLoopReserve, so they compile and fail by assertion on a tree
Expand All @@ -21,12 +21,12 @@ func TestPlanDefaultLoopReserve756(t *testing.T) {
for _, tc := range []struct{ n, want int }{
{0, 0}, {1, 1}, {2, 2}, // no reserve: a count that small would drop to one loop or none
{3, 2}, {4, 3}, {7, 6},
{8, 7},
{9, 8}, {15, 14},
{16, 14},
{17, 15}, {24, 21}, {31, 28},
{32, 28},
{48, 42}, {64, 56},
{8, 6},
{9, 7}, {15, 12},
{16, 12},
{17, 13}, {24, 18}, {31, 24},
{32, 24},
{48, 36}, {64, 48},
} {
t.Run(fmt.Sprintf("n=%d", tc.n), func(t *testing.T) {
h := newFakeHost(t, rng(0, max(tc.n, 1)-1))
Expand All @@ -53,12 +53,13 @@ func TestPlanDefaultLoopReserve756(t *testing.T) {
}
}

// The headline case, alone: 16 identical CPUs, default request 16 -> 14 loops
// (rc1 gave 16). The same request with the count set by the user -> 16.
// The headline case, alone: 16 identical CPUs, default request 16 -> 12 loops
// (rc1 gave 16, the first reserve 14). The same request with the count set by
// the user -> 16.
func TestPlanSixteenAlikeCPUs756(t *testing.T) {
h := newFakeHost(t, rng(0, 15))
got := planWorkerCPUs(16, false, h.src()).CPUs
eq(t, "default 16 -> 14 loops", got, rng(0, 13))
eq(t, "default 16 -> 12 loops", got, rng(0, 11))
got = planWorkerCPUs(16, true, h.src()).CPUs
eq(t, "explicit 16 -> 16 loops", got, rng(0, 15))
}
Expand All @@ -82,13 +83,13 @@ func TestPlanExplicitCountIsHonoured756(t *testing.T) {
}

// 1..64 default workers on a host of that many CPUs, against the rule written
// out inline: loops = n - max(1, n/8) for n > 2, n otherwise.
// out inline: loops = n - max(1, n/4) for n > 2, n otherwise.
func TestPlanDefaultReserveTable756(t *testing.T) {
for n := 1; n <= 64; n++ {
h := newFakeHost(t, rng(0, n-1))
want := n
if n > 2 {
want = n - max(1, n/8)
want = n - max(1, n/4)
}
if got := len(planWorkerCPUs(n, false, h.src()).CPUs); got != want {
t.Errorf("default %d workers: %d loops, want %d", n, got, want)
Expand All @@ -97,14 +98,14 @@ func TestPlanDefaultReserveTable756(t *testing.T) {
}

// The reserve counts the workers asked for, not the CPUs allowed: a default
// request of 8 (GOMAXPROCS lowered under a CPU limit) on a 16-CPU host runs 7
// loops on the first 7 CPUs; a request above the CPUs (GOMAXPROCS set above
// request of 8 (GOMAXPROCS lowered under a CPU limit) on a 16-CPU host runs 6
// loops on the first 6 CPUs; a request above the CPUs (GOMAXPROCS set above
// them) wraps.
func TestPlanDefaultReserveCountsWorkersNotCPUs756(t *testing.T) {
h := newFakeHost(t, rng(0, 15))
eq(t, "default 8 of 16 CPUs", planWorkerCPUs(8, false, h.src()).CPUs, rng(0, 6))
eq(t, "default 8 of 16 CPUs", planWorkerCPUs(8, false, h.src()).CPUs, rng(0, 5))
h = newFakeHost(t, rng(0, 3))
eq(t, "default 8 of 4 CPUs wraps", planWorkerCPUs(8, false, h.src()).CPUs, []int{0, 1, 2, 3, 0, 1, 2})
eq(t, "default 8 of 4 CPUs wraps", planWorkerCPUs(8, false, h.src()).CPUs, []int{0, 1, 2, 3, 0, 1})
}

// A read error means "homogeneous" (no allowed mask, no capacity files): the
Expand All @@ -113,8 +114,8 @@ func TestPlanDefaultReserveWithoutAMask756(t *testing.T) {
h := newFakeHost(t, nil)
src := h.src()
src.allowed = func() ([]int, error) { return nil, fmt.Errorf("boom") }
if p := planWorkerCPUs(16, false, src); len(p.CPUs) != 14 || p.Heterogeneous {
t.Errorf("default 16 without a mask: %d loops (%+v), want 14", len(p.CPUs), p)
if p := planWorkerCPUs(16, false, src); len(p.CPUs) != 12 || p.Heterogeneous {
t.Errorf("default 16 without a mask: %d loops (%+v), want 12", len(p.CPUs), p)
}
if p := planWorkerCPUs(16, true, src); len(p.CPUs) != 16 {
t.Errorf("explicit 16 without a mask: %d loops, want 16", len(p.CPUs))
Expand Down
2 changes: 1 addition & 1 deletion internal/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Resources struct {
// count was not user-set: on a host whose CPUs differ in capacity they then
// start at most one pinned loop per non-little CPU, min(GOMAXPROCS,
// non-little CPUs) (celeris#1036), and on a host whose CPUs are alike (Linux)
// they start the resolved count less a reserve of max(1, count/8) loops
// they start the resolved count less a reserve of max(1, count/4) loops
// (none for a count of 2 or less) that leaves Ps to the goroutines that are
// not loops (celeris#756, #757): the loops started are what the engines
// report, not Resolve's figure. A count set here is honoured, wrapping over
Expand Down
Loading