<commit_before>package server

import (
	"github.com/cri-o/cri-o/internal/config/node"
	"github.com/cri-o/cri-o/server/cri/types"
	"github.com/gogo/protobuf/proto"
	rspec "github.com/opencontainers/runtime-spec/specs-go"
	"github.com/pkg/errors"
	"golang.org/x/net/context"
)

// UpdateContainerResources updates ContainerConfig of the container.
func (s *Server) UpdateContainerResources(ctx context.Context, req *types.UpdateContainerResourcesRequest) error {
	c, err := s.GetContainerFromShortID(req.ContainerID)
	if err != nil {
		return err
	}

	if err := c.IsAlive(); err != nil {
		return errors.Errorf("container is not created or running: %v", err)
	}

	if req.Linux != nil {
		resources := toOCIResources(req.Linux)
		if err := s.Runtime().UpdateContainer(c, resources); err != nil {
			return err
		}

		// update memory store with updated resources
		s.UpdateContainerLinuxResources(c, resources)
	}

	return nil
}

// toOCIResources converts CRI resource constraints to OCI.
func toOCIResources(r *types.LinuxContainerResources) *rspec.LinuxResources {
	var swap int64
	memory := r.MemoryLimitInBytes
	if node.CgroupHasMemorySwap() {
		swap = memory
	}
	return &rspec.LinuxResources{
		CPU: &rspec.LinuxCPU{
			Shares: proto.Uint64(uint64(r.CPUShares)),
			Quota:  proto.Int64(r.CPUQuota),
			Period: proto.Uint64(uint64(r.CPUPeriod)),
			Cpus:   r.CPUsetCPUs,
			Mems:   r.CPUsetMems,
		},
		Memory: &rspec.LinuxMemory{
			Limit: proto.Int64(memory),
			Swap:  proto.Int64(swap),
		},
		// TODO(runcom): OOMScoreAdj is missing
	}
}<commit_msg>server: leave swap mem limit unset if not supported<commit_after>package server

import (
	"github.com/cri-o/cri-o/internal/config/node"
	"github.com/cri-o/cri-o/server/cri/types"
	"github.com/gogo/protobuf/proto"
	rspec "github.com/opencontainers/runtime-spec/specs-go"
	"github.com/pkg/errors"
	"golang.org/x/net/context"
)

// UpdateContainerResources updates ContainerConfig of the container.
func (s *Server) UpdateContainerResources(ctx context.Context, req *types.UpdateContainerResourcesRequest) error {
	c, err := s.GetContainerFromShortID(req.ContainerID)
	if err != nil {
		return err
	}

	if err := c.IsAlive(); err != nil {
		return errors.Errorf("container is not created or running: %v", err)
	}

	if req.Linux != nil {
		resources := toOCIResources(req.Linux)
		if err := s.Runtime().UpdateContainer(c, resources); err != nil {
			return err
		}

		// update memory store with updated resources
		s.UpdateContainerLinuxResources(c, resources)
	}

	return nil
}

// toOCIResources converts CRI resource constraints to OCI.
func toOCIResources(r *types.LinuxContainerResources) *rspec.LinuxResources {
	var swap *int64
	memory := r.MemoryLimitInBytes
	if node.CgroupHasMemorySwap() {
		swap = proto.Int64(memory)
	}
	return &rspec.LinuxResources{
		CPU: &rspec.LinuxCPU{
			Shares: proto.Uint64(uint64(r.CPUShares)),
			Quota:  proto.Int64(r.CPUQuota),
			Period: proto.Uint64(uint64(r.CPUPeriod)),
			Cpus:   r.CPUsetCPUs,
			Mems:   r.CPUsetMems,
		},
		Memory: &rspec.LinuxMemory{
			Limit: proto.Int64(memory),
			Swap:  swap,
		},
		// TODO(runcom): OOMScoreAdj is missing
	}
}
{'fraction_non_alphanumeric': 0.1287662543609261, 'fraction_numerical': 0.01014906438312718, 'mean_word_length': 4.196046128500824, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1349617', 'n_tokens_mistral': 1210, 'n_tokens_neox': 1111, 'n_words': 234}