Skip to content

Commit

Permalink
Merge pull request #144 from adrianreber/2024-10-15-fix-tree
Browse files Browse the repository at this point in the history
Fix location of Open Files in tree
  • Loading branch information
rst0git authored Oct 15, 2024
2 parents 3fcc128 + b2f5b23 commit 35c258e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 40 deletions.
102 changes: 67 additions & 35 deletions internal/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,39 @@ func RenderTreeView(tasks []Task) error {
}
}

if err = addPsTreeToTree(tree, psTree, task.OutputDir); err != nil {
return fmt.Errorf("failed to get process tree: %w", err)
}
}

if Files {
c := crit.New(nil, nil, filepath.Join(task.OutputDir, "checkpoint"), false, false)
fds, err := c.ExploreFds()
fds, err := func() ([]*crit.Fd, error) {
if !Files {
return nil, nil
}
c := crit.New(nil, nil, filepath.Join(task.OutputDir, "checkpoint"), false, false)
fds, err := c.ExploreFds()
if err != nil {
return nil, fmt.Errorf("failed to get file descriptors: %w", err)
}
return fds, nil
}()
if err != nil {
return fmt.Errorf("failed to get file descriptors: %w", err)
return err
}

addFdsToTree(tree, fds)
}

if Sockets {
c := crit.New(nil, nil, filepath.Join(task.OutputDir, "checkpoint"), false, false)
fds, err := c.ExploreSk()
sks, err := func() ([]*crit.Sk, error) {
if !Sockets {
return nil, nil
}
c := crit.New(nil, nil, filepath.Join(task.OutputDir, "checkpoint"), false, false)
sks, err := c.ExploreSk()
if err != nil {
return nil, fmt.Errorf("failed to get sockets: %w", err)
}
return sks, nil
}()
if err != nil {
return fmt.Errorf("failed to get sockets: %w", err)
return err
}

addSkToTree(tree, fds)
if err = addPsTreeToTree(tree, psTree, fds, sks, task.OutputDir); err != nil {
return fmt.Errorf("failed to get process tree: %w", err)
}
}

if Mounts {
Expand Down Expand Up @@ -138,7 +148,13 @@ func addDumpStatsToTree(tree treeprint.Tree, dumpStats *stats_pb.DumpStatsEntry)
statsTree.AddBranch(fmt.Sprintf("Pages written: %d", dumpStats.GetPagesWritten()))
}

func addPsTreeToTree(tree treeprint.Tree, psTree *crit.PsTree, checkpointOutputDir string) error {
func addPsTreeToTree(
tree treeprint.Tree,
psTree *crit.PsTree,
fds []*crit.Fd,
sks []*crit.Sk,
checkpointOutputDir string,
) error {
psRoot := psTree
if PID != 0 {
ps := psTree.FindPs(PID)
Expand Down Expand Up @@ -168,6 +184,15 @@ func addPsTreeToTree(tree treeprint.Tree, psTree *crit.PsTree, checkpointOutputD
}
}
}

if err := showFiles(fds, node, root); err != nil {
return err
}

if err := showSockets(sks, node, root); err != nil {
return err
}

for _, child := range root.Children {
if err := processNodes(node, child); err != nil {
return err
Expand All @@ -180,35 +205,41 @@ func addPsTreeToTree(tree treeprint.Tree, psTree *crit.PsTree, checkpointOutputD
return processNodes(psTreeNode, psRoot)
}

func addFdsToTree(tree treeprint.Tree, fds []*crit.Fd) {
var node treeprint.Tree
func showFiles(fds []*crit.Fd, node treeprint.Tree, root *crit.PsTree) error {
if !Files {
return nil
}
if fds == nil {
return fmt.Errorf("failed to get file descriptors")
}
for _, fd := range fds {
node = tree.FindByMeta(fd.PId)
// If FindByMeta returns nil, then the node with
// the PID has been pruned while building the tree.
// Hence, skip all associated file descriptors.
if node == nil {
var nodeSubtree treeprint.Tree
if fd.PId != root.PID {
continue
} else {
nodeSubtree = node.AddBranch("Open files")
}
nodeSubtree := node.AddBranch("Open files")
for _, file := range fd.Files {
nodeSubtree.AddMetaBranch(strings.TrimSpace(file.Type+" "+file.Fd), file.Path)
}
}
return nil
}

func addSkToTree(tree treeprint.Tree, sks []*crit.Sk) {
var node treeprint.Tree
func showSockets(sks []*crit.Sk, node treeprint.Tree, root *crit.PsTree) error {
if !Sockets {
return nil
}
if sks == nil {
return fmt.Errorf("failed to get sockets")
}
for _, sk := range sks {
node = tree.FindByMeta(sk.PId)
// If FindByMeta returns nil, then the node with
// the PID has been pruned while building the tree.
// Hence, skip all associated sockets.
if node == nil {
var nodeSubtree treeprint.Tree
if sk.PId != root.PID {
continue
} else {
nodeSubtree = node.AddBranch("Open sockets")
}

nodeSubtree := node.AddBranch("Open sockets")
var data string
var protocol string
for _, socket := range sk.Sockets {
Expand Down Expand Up @@ -243,6 +274,7 @@ func addSkToTree(tree treeprint.Tree, sks []*crit.Sk) {
nodeSubtree.AddMetaBranch(protocol, data)
}
}
return nil
}

// Recursively updates the Comm field of the psTree struct with the command line arguments
Expand Down
9 changes: 4 additions & 5 deletions test/checkpointctl.bats
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ function setup() {
}

function teardown() {
#[ "$TEST_TMP_DIR1" != "" ] && rm -rf "$TEST_TMP_DIR1"
#[ "$TEST_TMP_DIR2" != "" ] && rm -rf "$TEST_TMP_DIR2"
echo hu
[ "$TEST_TMP_DIR1" != "" ] && rm -rf "$TEST_TMP_DIR1"
[ "$TEST_TMP_DIR2" != "" ] && rm -rf "$TEST_TMP_DIR2"
}

@test "Run checkpointctl" {
Expand Down Expand Up @@ -360,7 +359,7 @@ function teardown() {
( cd "$TEST_TMP_DIR1" && tar cf "$TEST_TMP_DIR2"/test.tar . )
checkpointctl inspect "$TEST_TMP_DIR2"/test.tar --files
[ "$status" -eq 0 ]
[[ ${lines[24]} == *"[REG 0]"* ]]
[[ ${lines[11]} == *"[REG 0]"* ]]
[[ ${lines[25]} == *"[cwd]"* ]]
[[ ${lines[26]} == *"[root]"* ]]
}
Expand Down Expand Up @@ -767,4 +766,4 @@ function teardown() {
[[ "${lines[4]}" == *"| checkpoint-valid-config-modified.tar |"* ]]
[[ "${lines[6]}" == *"| default | pod-name | container-name | CRI-O |"* ]]
[[ "${lines[6]}" == *"| checkpoint-valid-config.tar |"* ]]
}
}

0 comments on commit 35c258e

Please sign in to comment.