001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.yarn.api.records;
020
021import org.apache.hadoop.classification.InterfaceAudience.Public;
022import org.apache.hadoop.classification.InterfaceStability.Unstable;
023import org.apache.hadoop.yarn.util.Records;
024
025/**
026 * <p>
027 * <code>ResourceUtilization</code> models the utilization of a set of computer
028 * resources in the cluster.
029 * </p>
030 */
031@Public
032@Unstable
033public abstract class ResourceUtilization implements
034    Comparable<ResourceUtilization> {
035
036  @Public
037  @Unstable
038  public static ResourceUtilization newInstance(int pmem, int vmem, float cpu) {
039    ResourceUtilization utilization =
040        Records.newRecord(ResourceUtilization.class);
041    utilization.setPhysicalMemory(pmem);
042    utilization.setVirtualMemory(vmem);
043    utilization.setCPU(cpu);
044    return utilization;
045  }
046
047  @Public
048  @Unstable
049  public static ResourceUtilization newInstance(
050      ResourceUtilization resourceUtil) {
051    return newInstance(resourceUtil.getPhysicalMemory(),
052        resourceUtil.getVirtualMemory(), resourceUtil.getCPU());
053  }
054
055  /**
056   * Get used <em>virtual memory</em>.
057   *
058   * @return <em>virtual memory</em> in MB
059   */
060  @Public
061  @Unstable
062  public abstract int getVirtualMemory();
063
064  /**
065   * Set used <em>virtual memory</em>.
066   *
067   * @param vmem <em>virtual memory</em> in MB
068   */
069  @Public
070  @Unstable
071  public abstract void setVirtualMemory(int vmem);
072
073  /**
074   * Get <em>physical memory</em>.
075   *
076   * @return <em>physical memory</em> in MB
077   */
078  @Public
079  @Unstable
080  public abstract int getPhysicalMemory();
081
082  /**
083   * Set <em>physical memory</em>.
084   *
085   * @param pmem <em>physical memory</em> in MB
086   */
087  @Public
088  @Unstable
089  public abstract void setPhysicalMemory(int pmem);
090
091  /**
092   * Get <em>CPU</em> utilization.
093   *
094   * @return <em>CPU utilization</em> normalized to 1 CPU
095   */
096  @Public
097  @Unstable
098  public abstract float getCPU();
099
100  /**
101   * Set <em>CPU</em> utilization.
102   *
103   * @param cpu <em>CPU utilization</em> normalized to 1 CPU
104   */
105  @Public
106  @Unstable
107  public abstract void setCPU(float cpu);
108
109  @Override
110  public int hashCode() {
111    final int prime = 263167;
112    int result = 3571;
113    result = prime * result + getVirtualMemory();
114    result = prime * result + getPhysicalMemory();
115    result = 31 * result + Float.valueOf(getCPU()).hashCode();
116    return result;
117  }
118
119  @Override
120  public boolean equals(Object obj) {
121    if (this == obj) {
122      return true;
123    }
124    if (obj == null) {
125      return false;
126    }
127    if (!(obj instanceof ResourceUtilization)) {
128      return false;
129    }
130    ResourceUtilization other = (ResourceUtilization) obj;
131    if (getVirtualMemory() != other.getVirtualMemory()
132        || getPhysicalMemory() != other.getPhysicalMemory()
133        || getCPU() != other.getCPU()) {
134      return false;
135    }
136    return true;
137  }
138
139  @Override
140  public String toString() {
141    return "<pmem:" + getPhysicalMemory() + ", vmem:" + getVirtualMemory()
142        + ", vCores:" + getCPU() + ">";
143  }
144
145  /**
146   * Add utilization to the current one.
147   * @param pmem Physical memory used to add.
148   * @param vmem Virtual memory used to add.
149   * @param cpu CPU utilization to add.
150   */
151  @Public
152  @Unstable
153  public void addTo(int pmem, int vmem, float cpu) {
154    this.setPhysicalMemory(this.getPhysicalMemory() + pmem);
155    this.setVirtualMemory(this.getVirtualMemory() + vmem);
156    this.setCPU(this.getCPU() + cpu);
157  }
158
159  /**
160   * Subtract utilization from the current one.
161   * @param pmem Physical memory to be subtracted.
162   * @param vmem Virtual memory to be subtracted.
163   * @param cpu CPU utilization to be subtracted.
164   */
165  @Public
166  @Unstable
167  public void subtractFrom(int pmem, int vmem, float cpu) {
168    this.setPhysicalMemory(this.getPhysicalMemory() - pmem);
169    this.setVirtualMemory(this.getVirtualMemory() - vmem);
170    this.setCPU(this.getCPU() - cpu);
171  }
172}