process tree improvements

- When discovering the hierarchy of a process, reuse components of
   the tree if they're already on cache, to improve speed and reduce
   mem allocs.
 - When building the tree of a proces, rebuild the tree if the first
   component doesn't have pid 1. Otherwise reuse the tree.
This commit is contained in:
Gustavo Iñiguez Goia 2024-01-18 13:35:29 +01:00
parent 379d010ab8
commit 01edd361fe
Failed to generate hash of commit

View file

@ -37,9 +37,13 @@ func (p *Process) GetParent() {
if p.PPID == 0 { if p.PPID == 0 {
return return
} }
it, found := EventsCache.IsInStoreByPID(p.PPID)
if found {
p.Parent = &it.Proc
p.Parent.GetParent()
return
}
// TODO: see how we can reuse this object and the ppid, to save some iterations.
// right now it opens the can of leaks.
p.mu.Lock() p.mu.Lock()
p.Parent = NewProcessEmpty(p.PPID, "") p.Parent = NewProcessEmpty(p.PPID, "")
p.mu.Unlock() p.mu.Unlock()
@ -51,9 +55,11 @@ func (p *Process) GetParent() {
// BuildTree returns all the parents of this process. // BuildTree returns all the parents of this process.
func (p *Process) BuildTree() { func (p *Process) BuildTree() {
if len(p.Tree) > 0 { items := len(p.Tree)
if items > 0 && p.Tree[items-1].Value == 1 {
return return
} }
// Adding this process to the tree, not to loose track of it. // Adding this process to the tree, not to loose track of it.
p.Tree = append(p.Tree, p.Tree = append(p.Tree,
&protocol.StringInt{ &protocol.StringInt{