Skip to content

Commit 1b7a059

Browse files
authored
Merge pull request kubernetes#128999 from macsko/improve_goroutines_metric_writes_in_parallelizer_until
Improve Goroutines metric calls in parallelizer.Until
2 parents 183ef23 + bd8dee9 commit 1b7a059

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

pkg/scheduler/framework/parallelize/parallelism.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,28 @@ func chunkSizeFor(n, parallelism int) int {
5151
return s
5252
}
5353

54+
// numWorkersForChunkSize returns number of workers (goroutines)
55+
// that will be created in workqueue.ParallelizeUntil
56+
// for given parallelism, pieces and chunkSize values.
57+
func numWorkersForChunkSize(parallelism, pieces, chunkSize int) int {
58+
chunks := (pieces + chunkSize - 1) / chunkSize
59+
if chunks < parallelism {
60+
return chunks
61+
}
62+
return parallelism
63+
}
64+
5465
// Until is a wrapper around workqueue.ParallelizeUntil to use in scheduling algorithms.
5566
// A given operation will be a label that is recorded in the goroutine metric.
5667
func (p Parallelizer) Until(ctx context.Context, pieces int, doWorkPiece workqueue.DoWorkPieceFunc, operation string) {
68+
chunkSize := chunkSizeFor(pieces, p.parallelism)
69+
workers := numWorkersForChunkSize(p.parallelism, pieces, chunkSize)
70+
5771
goroutinesMetric := metrics.Goroutines.WithLabelValues(operation)
58-
withMetrics := func(piece int) {
59-
goroutinesMetric.Inc()
60-
doWorkPiece(piece)
61-
goroutinesMetric.Dec()
62-
}
72+
// Calling single Add with workers' count is more efficient than calling Inc or Dec per each work piece.
73+
// This approach improves performance of some plugins (affinity, topology spreading) as well as preemption.
74+
goroutinesMetric.Add(float64(workers))
75+
defer goroutinesMetric.Add(float64(-workers))
6376

64-
workqueue.ParallelizeUntil(ctx, p.parallelism, pieces, withMetrics, workqueue.WithChunkSize(chunkSizeFor(pieces, p.parallelism)))
77+
workqueue.ParallelizeUntil(ctx, p.parallelism, pieces, doWorkPiece, workqueue.WithChunkSize(chunkSize))
6578
}

0 commit comments

Comments
 (0)