Skip to content
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

Load kernel BTF spec once when creating a new collection #1690

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
20 changes: 14 additions & 6 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,11 @@ func (cs *CollectionSpec) Assign(to interface{}) error {
// Returns an error if any of the fields can't be found, or
// if the same Map or Program is assigned multiple times.
func (cs *CollectionSpec) LoadAndAssign(to interface{}, opts *CollectionOptions) error {
loader, err := newCollectionLoader(cs, opts)
if opts == nil {
opts = &CollectionOptions{}
}

loader, err := newCollectionLoader(cs, *opts)
if err != nil {
return err
}
Expand Down Expand Up @@ -351,7 +355,7 @@ func NewCollection(spec *CollectionSpec) (*Collection, error) {
// Omitting Collection.Close() during application shutdown is an error.
// See the package documentation for details around Map and Program lifecycle.
func NewCollectionWithOptions(spec *CollectionSpec, opts CollectionOptions) (*Collection, error) {
loader, err := newCollectionLoader(spec, &opts)
loader, err := newCollectionLoader(spec, opts)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -405,9 +409,13 @@ type collectionLoader struct {
vars map[string]*Variable
}

func newCollectionLoader(coll *CollectionSpec, opts *CollectionOptions) (*collectionLoader, error) {
if opts == nil {
opts = &CollectionOptions{}
func newCollectionLoader(coll *CollectionSpec, opts CollectionOptions) (*collectionLoader, error) {
if opts.Programs.KernelTypes == nil {
kernelSpec, err := btf.LoadKernelSpec()
if err != nil {
return nil, fmt.Errorf("cannot load kernel spec: %w", err)
}
opts.Programs.KernelTypes = kernelSpec
}

// Check for existing MapSpecs in the CollectionSpec for all provided replacement maps.
Expand All @@ -423,7 +431,7 @@ func newCollectionLoader(coll *CollectionSpec, opts *CollectionOptions) (*collec

return &collectionLoader{
coll,
opts,
&opts,
make(map[string]*Map),
make(map[string]*Program),
make(map[string]*Variable),
Expand Down
Loading