...

Source file src/github.com/Microsoft/hcsshim/internal/uvm/update_uvm.go

Documentation: github.com/Microsoft/hcsshim/internal/uvm

     1  //go:build windows
     2  
     3  package uvm
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  
     9  	hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2"
    10  	"github.com/Microsoft/hcsshim/pkg/annotations"
    11  	"github.com/Microsoft/hcsshim/pkg/ctrdtaskapi"
    12  	"github.com/opencontainers/runtime-spec/specs-go"
    13  )
    14  
    15  func (uvm *UtilityVM) Update(ctx context.Context, data interface{}, annots map[string]string) error {
    16  	var memoryLimitInBytes *uint64
    17  	var processorLimits *hcsschema.ProcessorLimits
    18  
    19  	switch resources := data.(type) {
    20  	case *specs.WindowsResources:
    21  		if resources.Memory != nil {
    22  			memoryLimitInBytes = resources.Memory.Limit
    23  		}
    24  		if resources.CPU != nil {
    25  			processorLimits := &hcsschema.ProcessorLimits{}
    26  			if resources.CPU.Maximum != nil {
    27  				processorLimits.Limit = uint64(*resources.CPU.Maximum)
    28  			}
    29  			if resources.CPU.Shares != nil {
    30  				processorLimits.Weight = uint64(*resources.CPU.Shares)
    31  			}
    32  		}
    33  	case *specs.LinuxResources:
    34  		if resources.Memory != nil {
    35  			mem := uint64(*resources.Memory.Limit)
    36  			memoryLimitInBytes = &mem
    37  		}
    38  		if resources.CPU != nil {
    39  			processorLimits := &hcsschema.ProcessorLimits{}
    40  			if resources.CPU.Quota != nil {
    41  				processorLimits.Limit = uint64(*resources.CPU.Quota)
    42  			}
    43  			if resources.CPU.Shares != nil {
    44  				processorLimits.Weight = uint64(*resources.CPU.Shares)
    45  			}
    46  		}
    47  	case *ctrdtaskapi.PolicyFragment:
    48  		return uvm.InjectPolicyFragment(ctx, resources)
    49  	default:
    50  		return fmt.Errorf("invalid resource: %+v", resources)
    51  	}
    52  
    53  	if memoryLimitInBytes != nil {
    54  		if err := uvm.UpdateMemory(ctx, *memoryLimitInBytes); err != nil {
    55  			return err
    56  		}
    57  	}
    58  	if processorLimits != nil {
    59  		if err := uvm.UpdateCPULimits(ctx, processorLimits); err != nil {
    60  			return err
    61  		}
    62  	}
    63  
    64  	// Check if an annotation was sent to update cpugroup membership
    65  	if cpuGroupID, ok := annots[annotations.CPUGroupID]; ok {
    66  		if err := uvm.SetCPUGroup(ctx, cpuGroupID); err != nil {
    67  			return err
    68  		}
    69  	}
    70  
    71  	return nil
    72  }
    73  

View as plain text