-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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 #2669 from kolyshkin/systemd-unified
Initial v2 resources.unified systemd support
- Loading branch information
Showing
16 changed files
with
522 additions
and
270 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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package systemd | ||
|
||
import ( | ||
"encoding/binary" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/willf/bitset" | ||
) | ||
|
||
// rangeToBits converts a text representation of a CPU mask (as written to | ||
// or read from cgroups' cpuset.* files, e.g. "1,3-5") to a slice of bytes | ||
// with the corresponding bits set (as consumed by systemd over dbus as | ||
// AllowedCPUs/AllowedMemoryNodes unit property value). | ||
func rangeToBits(str string) ([]byte, error) { | ||
bits := &bitset.BitSet{} | ||
|
||
for _, r := range strings.Split(str, ",") { | ||
// allow extra spaces around | ||
r = strings.TrimSpace(r) | ||
// allow empty elements (extra commas) | ||
if r == "" { | ||
continue | ||
} | ||
ranges := strings.SplitN(r, "-", 2) | ||
if len(ranges) > 1 { | ||
start, err := strconv.ParseUint(ranges[0], 10, 32) | ||
if err != nil { | ||
return nil, err | ||
} | ||
end, err := strconv.ParseUint(ranges[1], 10, 32) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if start > end { | ||
return nil, errors.New("invalid range: " + r) | ||
} | ||
for i := uint(start); i <= uint(end); i++ { | ||
bits.Set(i) | ||
} | ||
} else { | ||
val, err := strconv.ParseUint(ranges[0], 10, 32) | ||
if err != nil { | ||
return nil, err | ||
} | ||
bits.Set(uint(val)) | ||
} | ||
} | ||
|
||
val := bits.Bytes() | ||
if len(val) == 0 { | ||
// do not allow empty values | ||
return nil, errors.New("empty value") | ||
} | ||
ret := make([]byte, len(val)*8) | ||
for i := range val { | ||
// bitset uses BigEndian internally | ||
binary.BigEndian.PutUint64(ret[i*8:], val[len(val)-1-i]) | ||
} | ||
// remove upper all-zero bytes | ||
for ret[0] == 0 { | ||
ret = ret[1:] | ||
} | ||
|
||
return ret, nil | ||
} |
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,58 @@ | ||
package systemd | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestRangeToBits(t *testing.T) { | ||
testCases := []struct { | ||
in string | ||
out []byte | ||
isErr bool | ||
}{ | ||
{in: "", isErr: true}, | ||
{in: "0", out: []byte{1}}, | ||
{in: "1", out: []byte{2}}, | ||
{in: "0-1", out: []byte{3}}, | ||
{in: "0,1", out: []byte{3}}, | ||
{in: ",0,1,", out: []byte{3}}, | ||
{in: "0-3", out: []byte{0x0f}}, | ||
{in: "0,1,2-3", out: []byte{0x0f}}, | ||
{in: "4-7", out: []byte{0xf0}}, | ||
{in: "0-7", out: []byte{0xff}}, | ||
{in: "0-15", out: []byte{0xff, 0xff}}, | ||
{in: "16", out: []byte{1, 0, 0}}, | ||
{in: "0-3,32-33", out: []byte{3, 0, 0, 0, 0x0f}}, | ||
// extra spaces and tabs are ok | ||
{in: "1, 2, 1-2", out: []byte{6}}, | ||
{in: " , 1 , 3 , 5-7, ", out: []byte{0xea}}, | ||
// somewhat large values | ||
{in: "128-130,1", out: []byte{7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}}, | ||
|
||
{in: "-", isErr: true}, | ||
{in: "1-", isErr: true}, | ||
{in: "-3", isErr: true}, | ||
// bad range (start > end) | ||
{in: "54-53", isErr: true}, | ||
// kernel does not allow extra spaces inside a range | ||
{in: "1 - 2", isErr: true}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Logf("case: %q", tc.in) | ||
out, err := rangeToBits(tc.in) | ||
if err != nil { | ||
t.Logf(" got error: %s", err) | ||
if !tc.isErr { | ||
t.Error(" ^^^ unexpected error") | ||
} | ||
|
||
continue | ||
} | ||
t.Logf(" expected %v, got %v", tc.out, out) | ||
if !bytes.Equal(out, tc.out) { | ||
t.Error(" ^^^ unexpected result") | ||
} | ||
} | ||
} |
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
Oops, something went wrong.