File: oci.go

package info (click to toggle)
golang-github-intel-goresctrl 0.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,064 kB
  • sloc: makefile: 19; sh: 15
file content (71 lines) | stat: -rw-r--r-- 2,186 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
Copyright 2019-2021 Intel Corporation

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 blockio

import (
	"fmt"

	oci "github.com/opencontainers/runtime-spec/specs-go"

	"github.com/intel/goresctrl/pkg/cgroups"
)

// OciLinuxBlockIO returns OCI LinuxBlockIO structure corresponding to the class.
func OciLinuxBlockIO(class string) (*oci.LinuxBlockIO, error) {
	blockio, ok := classBlockIO[class]
	if !ok {
		return nil, fmt.Errorf("no OCI BlockIO parameters for class %#v", class)
	}
	ociBlockio := oci.LinuxBlockIO{}
	if blockio.Weight != -1 {
		w := uint16(blockio.Weight)
		ociBlockio.Weight = &w
	}
	ociBlockio.WeightDevice = ociLinuxWeightDevices(blockio.WeightDevice)
	ociBlockio.ThrottleReadBpsDevice = ociLinuxThrottleDevices(blockio.ThrottleReadBpsDevice)
	ociBlockio.ThrottleWriteBpsDevice = ociLinuxThrottleDevices(blockio.ThrottleWriteBpsDevice)
	ociBlockio.ThrottleReadIOPSDevice = ociLinuxThrottleDevices(blockio.ThrottleReadIOPSDevice)
	ociBlockio.ThrottleWriteIOPSDevice = ociLinuxThrottleDevices(blockio.ThrottleWriteIOPSDevice)
	return &ociBlockio, nil
}

func ociLinuxWeightDevices(dws cgroups.DeviceWeights) []oci.LinuxWeightDevice {
	if len(dws) == 0 {
		return nil
	}
	olwds := make([]oci.LinuxWeightDevice, len(dws))
	for i, wd := range dws {
		w := uint16(wd.Weight)
		olwds[i].Major = wd.Major
		olwds[i].Minor = wd.Minor
		olwds[i].Weight = &w
	}
	return olwds
}

func ociLinuxThrottleDevices(drs cgroups.DeviceRates) []oci.LinuxThrottleDevice {
	if len(drs) == 0 {
		return nil
	}
	oltds := make([]oci.LinuxThrottleDevice, len(drs))
	for i, dr := range drs {
		oltds[i].Major = dr.Major
		oltds[i].Minor = dr.Minor
		oltds[i].Rate = uint64(dr.Rate)
	}
	return oltds
}