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

socket enricher: Use single instance for multiple gadgets #2324

Merged
merged 7 commits into from
Jan 23, 2024

Conversation

mauriciovasquezbernal
Copy link
Member

Check the details in #1695.

Fixes #1695

@mauriciovasquezbernal mauriciovasquezbernal force-pushed the mauricio/optmize-socket-enricher branch 2 times, most recently from f978966 to 8849525 Compare December 22, 2023 18:31
@mauriciovasquezbernal mauriciovasquezbernal changed the title socket enricher: Use single instance for multiple gadget socket enricher: Use single instance for multiple gadgets Jan 3, 2024
@mauriciovasquezbernal mauriciovasquezbernal force-pushed the mauricio/optmize-socket-enricher branch 3 times, most recently from fc775e7 to 93b7e47 Compare January 4, 2024 18:38
Copy link
Member

@alban alban left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

pkg/operators/socketenricher/socketenricher.go Outdated Show resolved Hide resolved
Comment on lines +890 to +894
func (t *Tracer) SetSocketEnricherMap(m *ebpf.Map) {
t.socketEnricherMap = m
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it mean that image-based gadgets would still inconditionally create the socket enricher, even if they don't use it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. It's still a pending issue we have with other operators as well (#2167), I gave it a try on #2168.

@mauriciovasquezbernal
Copy link
Member Author

There's something going on with the tests on this PR. Holding until I have the chance to investigate more.

@burak-ok
Copy link
Member

There's something going on with the tests on this PR. Holding until I have the chance to investigate more.

At least for TestProfileCpu the KubeInventoryCache crashed the gadget. After rebase + #2354 at least one test should pass again.
That crash seems to be a very rare race condition

@mauriciovasquezbernal
Copy link
Member Author

@burak-ok does it have any relation with #2347? Because that PR is not included in this branch yet.

@burak-ok
Copy link
Member

burak-ok commented Jan 11, 2024

@burak-ok does it have any relation with #2347? Because that PR is not included in this branch yet.

No, that rare race condition appears to have existed until that branch. I don't know if its happening more or less with #2347

@mauriciovasquezbernal mauriciovasquezbernal changed the base branch from main to burak/kubecache/close January 11, 2024 20:16
Base automatically changed from burak/kubecache/close to main January 12, 2024 12:39
@mauriciovasquezbernal mauriciovasquezbernal force-pushed the mauricio/optmize-socket-enricher branch 3 times, most recently from 7de748a to bc86a15 Compare January 17, 2024 14:16
Copy link
Member

@eiffel-fl eiffel-fl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi.

I will take a deeper look later, but I have some initial comments.

Best regards.

pkg/operators/socketenricher/socketenricher.go Outdated Show resolved Hide resolved
Comment on lines +140 to +146
socketEnricherOp := operators.GetRaw(socketenricher.OperatorName).(*socketenricher.SocketEnricher)
socketEnricherOp.Init(nil)
t.socketEnricherInst, err = socketEnricherOp.Instantiate(nil, t.tracer, nil)
if err == nil {
t.socketEnricherInst.PreGadgetRun()
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering if we cannot factorize this whole logic using an array and a loop.
For now, it is OK, but if we keep adding operators, the code will definitely grow and we should avoid this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole file is legacy and all this operators-related stuff there is a big hack, I hope we can get rid of it very soon.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do we need to get rid of it?
Cannot we just drop it now?

pkg/gadgets/trace/network/tracer/tracer.go Outdated Show resolved Hide resolved
pkg/socketenricher/tracer.go Show resolved Hide resolved
Copy link
Member

@eiffel-fl eiffel-fl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good optimization!
I tested it and we still have N to N maps but only 1 to N instance:

$ ps aux | grep ./ig               (remotes/origin/mauricio/optmize-socket-enricher) %
root       40174  0.0  0.0  16740  5632 pts/4    SN   11:38   0:00 sudo ./ig trace network
root       40178  0.0  0.0  16740  2296 pts/5    SNs+ 11:38   0:00 sudo ./ig trace network
root       40179  0.5  0.2 5635336 96668 pts/5   SNl  11:38   0:02 ./ig trace network
root       40210  0.0  0.0  16744  5632 pts/4    SN   11:38   0:00 sudo ./ig trace dns
root       40214  0.0  0.0  16744  2296 pts/6    SNs+ 11:38   0:00 sudo ./ig trace dns
root       40215  0.6  0.3 5635016 97600 pts/6   SNl  11:38   0:03 ./ig trace dns
francis    40788  0.0  0.0  11780  2560 pts/4    S+   11:47   0:00 grep --color=auto ./ig
$ sudo bpftool map list | grep 'gadget_sockets'
67: hash  name gadget_sockets  flags 0x0
90: hash  name gadget_sockets  flags 0x0


i.manager.refCount--
if i.manager.refCount == 0 {
i.manager.socketEnricher.Close()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there a way to call the above Close() here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could but I think the semantics are not the same. (s *SocketEnricher) Close() should destroy everything related to the parent, while (i *SocketEnricherInstance) PostGadgetRun() is only for this instance. They are exactly the same now, but they could be different later on.

Move it to a non-internal pkg to use it on the next commits.

Signed-off-by: Mauricio Vasquez <mauriciov@microsoft.com>
Split the logic into Init() and Run() functions. Init() now
only installs the network tracer, and Run() runs it. It's needed
by the following commit because the socket enricher operator
sets the map after Init() but before Run().

Signed-off-by: Mauricio Vasquez <mauriciov@microsoft.com>
Implement a new operator that provides the socket enricher map to gadgets
using it.

Signed-off-by: Mauricio Vasquez <mauriciov@microsoft.com>
Implement the SetSocketEnricherMap() interface in gadgets using it.

Signed-off-by: Mauricio Vasquez <mauriciov@microsoft.com>
Fixes: 35b9e87 ("socket enricher: make iterators optional")

Signed-off-by: Mauricio Vásquez <mauriciov@microsoft.com>
Signed-off-by: Mauricio Vásquez <mauriciov@microsoft.com>
See #2364

Signed-off-by: Mauricio Vásquez <mauriciov@microsoft.com>
@mauriciovasquezbernal mauriciovasquezbernal merged commit f95d4fd into main Jan 23, 2024
54 checks passed
@mauriciovasquezbernal mauriciovasquezbernal deleted the mauricio/optmize-socket-enricher branch January 23, 2024 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

socket enricher: Use single instance for multiple gadgets
4 participants