Skip to content

Commit

Permalink
chore(proctree): use atomic types
Browse files Browse the repository at this point in the history
  • Loading branch information
geyslan committed Feb 5, 2025
1 parent 214d450 commit 9ca302c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
14 changes: 7 additions & 7 deletions pkg/proctree/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import (

// Process represents a process.
type Process struct {
processHash uint32 // hash of process (immutable, so no need of concurrency control)
parentHash uint32 // hash of parent
info *TaskInfo // task info (immutable pointer)
executable *FileInfo // executable info (immutable pointer)
processHash uint32 // hash of process (immutable, so no need of concurrency control)
parentHash atomic.Uint32 // hash of parent
info *TaskInfo // task info (immutable pointer)
executable *FileInfo // executable info (immutable pointer)
}

// NewProcess creates a new thread with an initialized task info.
func NewProcess(hash uint32, info *TaskInfo) *Process {
return &Process{
processHash: hash,
parentHash: 0,
parentHash: atomic.Uint32{},
info: info,
executable: NewFileInfo(),
}
Expand All @@ -35,7 +35,7 @@ func (p *Process) GetHash() uint32 {

// GetParentHash returns the hash of the parent.
func (p *Process) GetParentHash() uint32 {
return atomic.LoadUint32(&p.parentHash)
return p.parentHash.Load()
}

// GetInfo returns a instanced task info.
Expand All @@ -52,5 +52,5 @@ func (p *Process) GetExecutable() *FileInfo {

// SetParentHash sets the hash of the parent.
func (p *Process) SetParentHash(parentHash uint32) {
atomic.StoreUint32(&p.parentHash, parentHash)
p.parentHash.Store(parentHash)
}
14 changes: 14 additions & 0 deletions pkg/proctree/process_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package proctree

import (
"os"
"testing"

"github.com/aquasecurity/tracee/pkg/utils/tests"
)

// TestProcess_PrintSizes prints the sizes of the structs used in the Process type.
// Run it as DEBUG test to see the output.
func TestProcess_PrintSizes(t *testing.T) {
tests.PrintStructSizes(t, os.Stdout, Process{})
}
22 changes: 11 additions & 11 deletions pkg/proctree/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import "sync/atomic"

// Thread represents a thread.
type Thread struct {
threadHash uint32 // hash of thread (immutable, so no need of concurrency control)
parentHash uint32 // hash of parent
leaderHash uint32 // hash of thread group leader
_ [4]byte // padding
info *TaskInfo // task info (immutable pointer)
threadHash uint32 // hash of thread (immutable, so no need of concurrency control)
parentHash atomic.Uint32 // hash of parent
leaderHash atomic.Uint32 // hash of thread group leader
_ [4]byte // padding
info *TaskInfo // task info (immutable pointer)
}

// NOTE: The importance of having the thread group leader hash to each thread is the following: the
Expand All @@ -21,8 +21,8 @@ type Thread struct {
func NewThread(hash uint32, info *TaskInfo) *Thread {
return &Thread{
threadHash: hash,
parentHash: 0,
leaderHash: 0,
parentHash: atomic.Uint32{},
leaderHash: atomic.Uint32{},
info: info,
}
}
Expand All @@ -36,12 +36,12 @@ func (t *Thread) GetHash() uint32 {

// GetParentHash returns the hash of the parent.
func (t *Thread) GetParentHash() uint32 {
return atomic.LoadUint32(&t.parentHash)
return t.parentHash.Load()
}

// GEtLeaderHash returns the hash of the thread group leader.
func (t *Thread) GetLeaderHash() uint32 {
return atomic.LoadUint32(&t.leaderHash)
return t.leaderHash.Load()
}

// GetInfo returns a instanced task info.
Expand All @@ -53,10 +53,10 @@ func (t *Thread) GetInfo() *TaskInfo {

// SetParentHash sets the hash of the parent.
func (t *Thread) SetParentHash(parentHash uint32) {
atomic.StoreUint32(&t.parentHash, parentHash)
t.parentHash.Store(parentHash)
}

// SetLeaderHash sets the hash of the thread group leader.
func (t *Thread) SetLeaderHash(leaderHash uint32) {
atomic.StoreUint32(&t.leaderHash, leaderHash)
t.leaderHash.Store(leaderHash)
}
3 changes: 1 addition & 2 deletions pkg/proctree/thread_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ import (
// TestThread_PrintSizes prints the sizes of the structs used in the Thread type.
// Run it as DEBUG test to see the output.
func TestThread_PrintSizes(t *testing.T) {
thread := Thread{}
tests.PrintStructSizes(t, os.Stdout, thread)
tests.PrintStructSizes(t, os.Stdout, Thread{})
}

0 comments on commit 9ca302c

Please sign in to comment.