github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/stage1/init/kvm/resources.go (about)

     1  // Copyright 2015 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package kvm
    16  
    17  import (
    18  	"runtime"
    19  
    20  	"github.com/appc/spec/schema"
    21  	"github.com/appc/spec/schema/types"
    22  )
    23  
    24  // The algorithm/reasoning:
    25  // When running the VM takes a small amount of memory out of the container
    26  //  (systemMemOverhead below)
    27  // The VM also has a transient startup need - some extra 'space' really only
    28  //  needed to 'get going' before we get to the workload.
    29  //
    30  // So, we have two rules:
    31  // - we always add on the system overhead
    32  // - we bump up to the minimum required for boot if we need to
    33  //
    34  // From those, and the app memory asks, calculate how much memory to hand to
    35  //  the VM.
    36  const (
    37  	minMem            = 512 // MB - minimum we need in VM
    38  	systemMemOverhead = 128 // MB - overhead we need for VM
    39  )
    40  
    41  // findResources finds value of last isolator for particular type.
    42  func findResources(isolators types.Isolators) (mem, cpus int64) {
    43  	for _, i := range isolators {
    44  		switch v := i.Value().(type) {
    45  		case *types.ResourceMemory:
    46  			mem = v.Limit().Value()
    47  			// Convert bytes into megabytes
    48  			mem /= 1024 * 1024
    49  		case *types.ResourceCPU:
    50  			cpus = v.Limit().Value()
    51  		}
    52  	}
    53  	return mem, cpus
    54  }
    55  
    56  // GetAppsResources returns values specified by user in pod-manifest.
    57  // Function expects a podmanifest apps.
    58  // Return aggregate quantity of mem (in MB) and cpus.
    59  func GetAppsResources(apps schema.AppList) (totalCpus, totalMem int64) {
    60  	cpusSpecified := false
    61  	for i := range apps {
    62  		ra := &apps[i]
    63  		app := ra.App
    64  		mem, cpus := findResources(app.Isolators)
    65  		cpusSpecified = cpusSpecified || cpus != 0
    66  		totalCpus += cpus
    67  		totalMem += mem
    68  	}
    69  	// In case when number of specified cpus is greater than
    70  	// number or when cpus aren't specified, we set number
    71  	// of logical cpus as a limit.
    72  	availableCpus := int64(runtime.NumCPU())
    73  	if !cpusSpecified || totalCpus > availableCpus {
    74  		totalCpus = availableCpus
    75  	}
    76  
    77  	// Add an overhead for the VM system
    78  	totalMem += systemMemOverhead
    79  
    80  	// Always ensure we have at least the minimum RAM needed
    81  	if totalMem < minMem {
    82  		totalMem = minMem
    83  	}
    84  
    85  	return totalCpus, totalMem
    86  }