internal: cgroups: support Tasks interface
Support obtaining pid list Signed-off-by: Tonghao Zhang <tonghao@bamaicloud.com>
This commit is contained in:
parent
1869034c55
commit
b4d7410682
|
@ -54,6 +54,8 @@ type Cgroup interface {
|
|||
UpdateRuntime(spec *specs.LinuxResources) error
|
||||
// Add pids to cgroup.procs
|
||||
AddProc(pid uint64) error
|
||||
// Pids return pids of cgroups
|
||||
Pids(path string) ([]int32, error)
|
||||
// CpuUsage return cgroups user/system and total usage.
|
||||
CpuUsage(path string) (*stats.CpuUsage, error)
|
||||
// CpuStatRaw return cpu.stat raw data
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
// Copyright 2025 The HuaTuo Authors
|
||||
//
|
||||
// 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.
|
||||
|
||||
package pids
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// 1. /sys/fs/cgroup/$GROUPPATH/cgroup.procs
|
||||
// 2. /sys/fs/cgroup/$GROUPPATH/cgroup.threads
|
||||
func Tasks(path, file string) ([]int32, error) {
|
||||
f, err := os.Open(filepath.Join(path, file))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var (
|
||||
out []int32
|
||||
s = bufio.NewScanner(f)
|
||||
)
|
||||
|
||||
for s.Scan() {
|
||||
if t := s.Text(); t != "" {
|
||||
pid, err := strconv.ParseInt(t, 10, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, int32(pid))
|
||||
}
|
||||
}
|
||||
|
||||
if err := s.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
|
@ -18,6 +18,7 @@ import (
|
|||
"math"
|
||||
|
||||
"huatuo-bamai/internal/cgroups/paths"
|
||||
"huatuo-bamai/internal/cgroups/pids"
|
||||
"huatuo-bamai/internal/cgroups/stats"
|
||||
"huatuo-bamai/internal/utils/parseutil"
|
||||
|
||||
|
@ -92,6 +93,10 @@ func (c *CgroupV1) AddProc(pid uint64) error {
|
|||
return c.cgroup.AddProc(pid)
|
||||
}
|
||||
|
||||
func (c *CgroupV1) Pids(path string) ([]int32, error) {
|
||||
return pids.Tasks(paths.Path(subsysCpu, path), "tasks")
|
||||
}
|
||||
|
||||
func (c *CgroupV1) CpuUsage(path string) (*stats.CpuUsage, error) {
|
||||
statPath := paths.Path(subsysCpu, path, "cpuacct.stat")
|
||||
raw, err := parseutil.RawKV(statPath)
|
||||
|
|
|
@ -20,6 +20,7 @@ import (
|
|||
"strconv"
|
||||
|
||||
"huatuo-bamai/internal/cgroups/paths"
|
||||
"huatuo-bamai/internal/cgroups/pids"
|
||||
"huatuo-bamai/internal/cgroups/stats"
|
||||
"huatuo-bamai/internal/utils/parseutil"
|
||||
|
||||
|
@ -83,6 +84,10 @@ func (c *CgroupV2) AddProc(pid uint64) error {
|
|||
return c.cgroup.AddProc(pid)
|
||||
}
|
||||
|
||||
func (c *CgroupV2) Pids(path string) ([]int32, error) {
|
||||
return pids.Tasks(paths.Path(path), "cgroup.threads")
|
||||
}
|
||||
|
||||
func (c *CgroupV2) CpuStatRaw(path string) (map[string]uint64, error) {
|
||||
return parseutil.RawKV(paths.Path(path, "cpu.stat"))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue