Skip to content

Commit

Permalink
add allocation binpacking duration metric (#687)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjerad committed Sep 20, 2021
1 parent e783ac2 commit 4e9fb54
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pkg/controllers/allocation/binpacking/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,41 @@ import (
"context"
"math"
"sort"
"time"

"github.com/awslabs/karpenter/pkg/apis/provisioning/v1alpha4"
"github.com/awslabs/karpenter/pkg/cloudprovider"
"github.com/awslabs/karpenter/pkg/controllers/allocation/scheduling"
"github.com/awslabs/karpenter/pkg/metrics"
"github.com/awslabs/karpenter/pkg/utils/apiobject"
"github.com/awslabs/karpenter/pkg/utils/resources"
"github.com/prometheus/client_golang/prometheus"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"knative.dev/pkg/logging"
"sigs.k8s.io/controller-runtime/pkg/client"
crmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
)

var (
// MaxInstanceTypes defines the number of instance type options to return to the cloud provider
MaxInstanceTypes = 20

packTimeHistogram = prometheus.NewHistogram(
prometheus.HistogramOpts{
Namespace: metrics.KarpenterNamespace,
Subsystem: "allocation_controller",
Name: "binpacking_duration_seconds",
Help: "Duration of binpacking process in seconds.",
Buckets: metrics.DurationBuckets(),
},
)
)

func init() {
crmetrics.Registry.MustRegister(packTimeHistogram)
}

type packer struct{}

// Packer helps pack the pods and calculates efficient placement on the instances.
Expand Down Expand Up @@ -63,6 +81,11 @@ type Packing struct {
// It follows the First Fit Decreasing bin packing technique, reference-
// https://en.wikipedia.org/wiki/Bin_packing_problem#First_Fit_Decreasing_(FFD)
func (p *packer) Pack(ctx context.Context, schedule *scheduling.Schedule, instances []cloudprovider.InstanceType) []*Packing {
startTime := time.Now()
defer func() {
packTimeHistogram.Observe(time.Since(startTime).Seconds())
}()

// Sort pods in decreasing order by the amount of CPU requested, if
// CPU requested is equal compare memory requested.
sort.Sort(sort.Reverse(ByResourcesRequested{SortablePods: schedule.Pods}))
Expand Down

0 comments on commit 4e9fb54

Please sign in to comment.