github.com/intel/goresctrl@v0.5.0/pkg/blockio/oci.go (about)

     1  /*
     2  Copyright 2019-2021 Intel Corporation
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package blockio
    18  
    19  import (
    20  	"fmt"
    21  
    22  	oci "github.com/opencontainers/runtime-spec/specs-go"
    23  
    24  	"github.com/intel/goresctrl/pkg/cgroups"
    25  )
    26  
    27  // OciLinuxBlockIO returns OCI LinuxBlockIO structure corresponding to the class.
    28  func OciLinuxBlockIO(class string) (*oci.LinuxBlockIO, error) {
    29  	blockio, ok := classBlockIO[class]
    30  	if !ok {
    31  		return nil, fmt.Errorf("no OCI BlockIO parameters for class %#v", class)
    32  	}
    33  	ociBlockio := oci.LinuxBlockIO{}
    34  	if blockio.Weight != -1 {
    35  		w := uint16(blockio.Weight)
    36  		ociBlockio.Weight = &w
    37  	}
    38  	ociBlockio.WeightDevice = ociLinuxWeightDevices(blockio.WeightDevice)
    39  	ociBlockio.ThrottleReadBpsDevice = ociLinuxThrottleDevices(blockio.ThrottleReadBpsDevice)
    40  	ociBlockio.ThrottleWriteBpsDevice = ociLinuxThrottleDevices(blockio.ThrottleWriteBpsDevice)
    41  	ociBlockio.ThrottleReadIOPSDevice = ociLinuxThrottleDevices(blockio.ThrottleReadIOPSDevice)
    42  	ociBlockio.ThrottleWriteIOPSDevice = ociLinuxThrottleDevices(blockio.ThrottleWriteIOPSDevice)
    43  	return &ociBlockio, nil
    44  }
    45  
    46  func ociLinuxWeightDevices(dws cgroups.DeviceWeights) []oci.LinuxWeightDevice {
    47  	if len(dws) == 0 {
    48  		return nil
    49  	}
    50  	olwds := make([]oci.LinuxWeightDevice, len(dws))
    51  	for i, wd := range dws {
    52  		w := uint16(wd.Weight)
    53  		olwds[i].Major = wd.Major
    54  		olwds[i].Minor = wd.Minor
    55  		olwds[i].Weight = &w
    56  	}
    57  	return olwds
    58  }
    59  
    60  func ociLinuxThrottleDevices(drs cgroups.DeviceRates) []oci.LinuxThrottleDevice {
    61  	if len(drs) == 0 {
    62  		return nil
    63  	}
    64  	oltds := make([]oci.LinuxThrottleDevice, len(drs))
    65  	for i, dr := range drs {
    66  		oltds[i].Major = dr.Major
    67  		oltds[i].Minor = dr.Minor
    68  		oltds[i].Rate = uint64(dr.Rate)
    69  	}
    70  	return oltds
    71  }