github.com/blixtra/rkt@v0.8.1-0.20160204105720-ab0d1add1a43/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 const ( 25 defaultMem = 128 // MB 26 systemMemOverhead = 128 // MB 27 ) 28 29 // findResources finds value of last isolator for particular type. 30 func findResources(isolators types.Isolators) (mem, cpus int64) { 31 mem = defaultMem 32 for _, i := range isolators { 33 switch v := i.Value().(type) { 34 case *types.ResourceMemory: 35 mem = v.Limit().Value() 36 // Convert bytes into megabytes 37 mem /= 1024 * 1024 38 case *types.ResourceCPU: 39 cpus = v.Limit().Value() 40 } 41 } 42 return mem, cpus 43 } 44 45 // GetAppsResources returns values specified by user in pod-manifest. 46 // Function expects a podmanifest apps. 47 // Return aggregate quantity of mem (in MB) and cpus. 48 func GetAppsResources(apps schema.AppList) (totalCpus, totalMem int64) { 49 cpusSpecified := false 50 for i := range apps { 51 ra := &apps[i] 52 app := ra.App 53 mem, cpus := findResources(app.Isolators) 54 cpusSpecified = cpusSpecified || cpus != 0 55 totalCpus += cpus 56 totalMem += mem 57 } 58 // If user doesn't specify cpus for at least one app, we set no limit for 59 // whole pod. 60 if !cpusSpecified { 61 totalCpus = int64(runtime.NumCPU()) 62 } 63 64 // Add an overhead for the VM system 65 totalMem += systemMemOverhead 66 67 return totalCpus, totalMem 68 }