1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package org.apache.hadoop.mapreduce.v2.app.rm; 20 21 import org.apache.hadoop.yarn.api.records.Resource; 22 import org.apache.hadoop.yarn.proto.YarnServiceProtos.SchedulerResourceTypes; 23 import org.apache.hadoop.yarn.util.Records; 24 25 import java.util.EnumSet; 26 27 public class ResourceCalculatorUtils { divideAndCeil(int a, int b)28 public static int divideAndCeil(int a, int b) { 29 if (b == 0) { 30 return 0; 31 } 32 return (a + (b - 1)) / b; 33 } 34 computeAvailableContainers(Resource available, Resource required, EnumSet<SchedulerResourceTypes> resourceTypes)35 public static int computeAvailableContainers(Resource available, 36 Resource required, EnumSet<SchedulerResourceTypes> resourceTypes) { 37 if (resourceTypes.contains(SchedulerResourceTypes.CPU)) { 38 return Math.min(available.getMemory() / required.getMemory(), 39 available.getVirtualCores() / required.getVirtualCores()); 40 } 41 return available.getMemory() / required.getMemory(); 42 } 43 divideAndCeilContainers(Resource required, Resource factor, EnumSet<SchedulerResourceTypes> resourceTypes)44 public static int divideAndCeilContainers(Resource required, Resource factor, 45 EnumSet<SchedulerResourceTypes> resourceTypes) { 46 if (resourceTypes.contains(SchedulerResourceTypes.CPU)) { 47 return Math.max(divideAndCeil(required.getMemory(), factor.getMemory()), 48 divideAndCeil(required.getVirtualCores(), factor.getVirtualCores())); 49 } 50 return divideAndCeil(required.getMemory(), factor.getMemory()); 51 } 52 } 53