-
Notifications
You must be signed in to change notification settings - Fork 980
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add allocation binpacking duration metric #687
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,23 +18,41 @@ import ( | |
"context" | ||
"math" | ||
"sort" | ||
"time" | ||
|
||
"github.com/awslabs/karpenter/pkg/apis/provisioning/v1alpha3" | ||
"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. | ||
|
@@ -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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool pattern! |
||
}() | ||
|
||
// 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})) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a reason to wrap this in a func rather than just doing:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to Defer, Panic, and Recover from the Go blog:
So the proposed code will not record the elapsed time because only the
.Observe()
call is defered;time.Since(startTime).Seconds()
will be evaluated immediately.