Skip to content

Commit

Permalink
Add support CpusAllowedList for /proc/[pid]/status (#490)
Browse files Browse the repository at this point in the history
* add CpusAllowedList for /proc/[pid]/status

Signed-off-by: mn.albeschenko <albeschenko.work@gmail.com>

* Fix documentation comment

Co-authored-by: Ben Kochie <superq@gmail.com>
Signed-off-by: Johannes 'fish' Ziemke <github@5pi.de>

---------

Signed-off-by: mn.albeschenko <albeschenko.work@gmail.com>
Signed-off-by: Johannes 'fish' Ziemke <github@5pi.de>
Co-authored-by: Johannes 'fish' Ziemke <github@5pi.de>
Co-authored-by: Ben Kochie <superq@gmail.com>
  • Loading branch information
3 people committed May 17, 2023
1 parent f489b53 commit b5b7dd5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
32 changes: 32 additions & 0 deletions proc_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package procfs

import (
"bytes"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -76,6 +77,9 @@ type ProcStatus struct {
UIDs [4]string
// GIDs of the process (Real, effective, saved set, and filesystem GIDs)
GIDs [4]string

// CpusAllowedList: List of cpu cores processes are allowed to run on.
CpusAllowedList []uint64
}

// NewStatus returns the current status information of the process.
Expand Down Expand Up @@ -161,10 +165,38 @@ func (s *ProcStatus) fillStatus(k string, vString string, vUint uint64, vUintByt
s.VoluntaryCtxtSwitches = vUint
case "nonvoluntary_ctxt_switches":
s.NonVoluntaryCtxtSwitches = vUint
case "Cpus_allowed_list":
s.CpusAllowedList = calcCpusAllowedList(vString)
}

}

// TotalCtxtSwitches returns the total context switch.
func (s ProcStatus) TotalCtxtSwitches() uint64 {
return s.VoluntaryCtxtSwitches + s.NonVoluntaryCtxtSwitches
}

func calcCpusAllowedList(cpuString string) []uint64 {
s := strings.Split(cpuString, ",")

var g []uint64

for _, cpu := range s {
// parse cpu ranges, example: 1-3=[1,2,3]
if l := strings.Split(strings.TrimSpace(cpu), "-"); len(l) > 1 {
startCPU, _ := strconv.ParseUint(l[0], 10, 64)
endCPU, _ := strconv.ParseUint(l[1], 10, 64)

for i := startCPU; i <= endCPU; i++ {
g = append(g, i)
}
} else if len(l) == 1 {
cpu, _ := strconv.ParseUint(l[0], 10, 64)
g = append(g, cpu)
}

}

sort.Slice(g, func(i, j int) bool { return g[i] < g[j] })
return g
}
17 changes: 17 additions & 0 deletions proc_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package procfs

import (
"reflect"
"testing"
)

Expand Down Expand Up @@ -121,3 +122,19 @@ func TestProcStatusGIDs(t *testing.T) {
t.Errorf("want uids %s, have %s", want, have)
}
}

func TestCpusAllowedList(t *testing.T) {
p, err := getProcFixtures(t).Proc(26231)
if err != nil {
t.Fatal(err)
}

s, err := p.NewStatus()
if err != nil {
t.Fatal(err)
}

if want, have := []uint64{0, 1, 2, 3, 4, 5, 6, 7}, s.CpusAllowedList; !reflect.DeepEqual(want, have) {
t.Errorf("want CpusAllowedList %v, have %v", want, have)
}
}

0 comments on commit b5b7dd5

Please sign in to comment.