-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from thediveo/develop
Develop
- Loading branch information
Showing
6 changed files
with
223 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/thediveo/lxkns | ||
|
||
go 1.21 | ||
go 1.22 | ||
|
||
require ( | ||
github.com/PaesslerAG/jsonpath v0.1.1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2024 Harald Albrecht. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
// use this file except in compliance with the License. You may obtain a copy | ||
// of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
// License for the specific language governing permissions and limitations | ||
// under the License. | ||
|
||
//go:build go1.23 | ||
|
||
package model | ||
|
||
import "iter" | ||
|
||
// Tasks returns an iterator over all tasks of the process identified by PID. In | ||
// case of non-existing/invalid PIDs, Tasks returns an empty iterator. If the | ||
// process table does not contain information about the tasks of the process, | ||
// the iterator won't emit any tasks, not even the task group leader | ||
// representing the process. | ||
func (t ProcessTable) Tasks(pid PIDType) iter.Seq[*Task] { | ||
return Tasks(t, pid) | ||
} | ||
|
||
// Tasks returns an iterator over all tasks of the process identified by PID. In | ||
// case of non-existing/invalid PIDs, Tasks returns an empty iterator. If the | ||
// process table does not contain information about the tasks of the process, | ||
// the iterator won't emit any tasks, not even the task group leader | ||
// representing the process. | ||
func Tasks(t ProcessTable, pid PIDType) iter.Seq[*Task] { | ||
proc := t[pid] | ||
if proc == nil { | ||
return func(yield func(*Task) bool) {} | ||
} | ||
return func(yield func(*Task) bool) { | ||
tasksOfProcess(proc, yield) | ||
} | ||
} | ||
|
||
// TasksRecursive returns an iterator over all tasks of the process identified | ||
// by PID, as well as all tasks of children, grandchildren, et cetera. If the | ||
// process table does not contain information about the tasks of the process, | ||
// the iterator won't emit any tasks, not even the task group leader | ||
// representing the process. | ||
func (t ProcessTable) TasksRecursive(pid PIDType) iter.Seq[*Task] { | ||
return TasksRecursive(t, pid) | ||
} | ||
|
||
// TasksRecursive returns an iterator over all tasks of the process identified | ||
// by PID, as well as all tasks of children, grandchildren, et cetera. If the | ||
// process table does not contain information about the tasks of the process, | ||
// the iterator won't emit any tasks, not even the task group leader | ||
// representing the process. | ||
func TasksRecursive(t ProcessTable, pid PIDType) iter.Seq[*Task] { | ||
proc := t[pid] | ||
if proc == nil { | ||
return func(yield func(*Task) bool) {} | ||
} | ||
return func(yield func(*Task) bool) { | ||
tasksRecursive(proc, yield) | ||
} | ||
} | ||
|
||
func tasksRecursive(proc *Process, yield func(*Task) bool) bool { | ||
if !tasksOfProcess(proc, yield) { | ||
return false | ||
} | ||
for _, child := range proc.Children { | ||
if !tasksRecursive(child, yield) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func tasksOfProcess(proc *Process, yield func(*Task) bool) bool { | ||
for _, task := range proc.Tasks { | ||
if !yield(task) { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright 2024 Harald Albrecht. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
// use this file except in compliance with the License. You may obtain a copy | ||
// of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
// License for the specific language governing permissions and limitations | ||
// under the License. | ||
|
||
//go:build go1.23 | ||
|
||
package model | ||
|
||
import ( | ||
"iter" | ||
|
||
. "github.com/onsi/ginkgo/v2/dsl/core" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func taskNames(it iter.Seq[*Task]) []string { | ||
names := []string{} | ||
for task := range it { | ||
names = append(names, task.Name) | ||
} | ||
return names | ||
} | ||
|
||
// Returns only the first task name and then tells the iterator to shut up. This | ||
// exercises also the unhappy little paths... | ||
func firstTaskName(it iter.Seq[*Task]) []string { | ||
names := []string{} | ||
for task := range it { | ||
names = append(names, task.Name) | ||
break | ||
} | ||
return names | ||
} | ||
|
||
var _ = Describe("process and task iterators", func() { | ||
|
||
pt := ProcessTable{ | ||
42: { | ||
ProTaskCommon: ProTaskCommon{Name: "pfoo"}, | ||
Tasks: []*Task{ | ||
{ | ||
ProTaskCommon: ProTaskCommon{Name: "rfoo"}, | ||
}, | ||
{ | ||
ProTaskCommon: ProTaskCommon{Name: "rbar"}, | ||
}, | ||
{ | ||
ProTaskCommon: ProTaskCommon{Name: "rbaz"}, | ||
}, | ||
}, | ||
Children: []*Process{ | ||
{ | ||
ProTaskCommon: ProTaskCommon{Name: "pfrobz"}, | ||
Tasks: []*Task{ | ||
{ | ||
ProTaskCommon: ProTaskCommon{Name: "ffrobz"}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
ProTaskCommon: ProTaskCommon{Name: "pgnampf"}, | ||
Tasks: []*Task{ | ||
{ | ||
ProTaskCommon: ProTaskCommon{Name: "fgnampf"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
It("iterates over tasks of a single process", func() { | ||
Expect(taskNames(Tasks(pt, 0))).To(BeEmpty()) | ||
Expect(firstTaskName(Tasks(pt, 42))).To(ConsistOf("rfoo")) | ||
Expect(taskNames(Tasks(pt, 42))).To(ConsistOf( | ||
"rfoo", "rbar", "rbaz")) | ||
}) | ||
|
||
It("iterates over tasks of processes recursively", func() { | ||
Expect(taskNames(TasksRecursive(pt, 0))).To(BeEmpty()) | ||
Expect(firstTaskName(TasksRecursive(pt, 42))).To(ConsistOf("rfoo")) | ||
Expect(taskNames(TasksRecursive(pt, 42))).To(ConsistOf( | ||
"rfoo", "rbar", "rbaz", "ffrobz", "fgnampf")) | ||
}) | ||
|
||
}) |