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.util;
020
021import org.apache.hadoop.classification.InterfaceAudience;
022import org.apache.hadoop.classification.InterfaceStability;
023
024/**
025 * Plugin to calculate resource information on the system.
026 */
027@InterfaceAudience.Public
028@InterfaceStability.Evolving
029public abstract class SysInfo {
030
031  /**
032   * Return default OS instance.
033   * @throws UnsupportedOperationException If cannot determine OS.
034   * @return Default instance for the detected OS.
035   */
036  public static SysInfo newInstance() {
037    if (Shell.LINUX) {
038      return new SysInfoLinux();
039    }
040    if (Shell.WINDOWS) {
041      return new SysInfoWindows();
042    }
043    throw new UnsupportedOperationException("Could not determine OS");
044  }
045
046  /**
047   * Obtain the total size of the virtual memory present in the system.
048   *
049   * @return virtual memory size in bytes.
050   */
051  public abstract long getVirtualMemorySize();
052
053  /**
054   * Obtain the total size of the physical memory present in the system.
055   *
056   * @return physical memory size bytes.
057   */
058  public abstract long getPhysicalMemorySize();
059
060  /**
061   * Obtain the total size of the available virtual memory present
062   * in the system.
063   *
064   * @return available virtual memory size in bytes.
065   */
066  public abstract long getAvailableVirtualMemorySize();
067
068  /**
069   * Obtain the total size of the available physical memory present
070   * in the system.
071   *
072   * @return available physical memory size bytes.
073   */
074  public abstract long getAvailablePhysicalMemorySize();
075
076  /**
077   * Obtain the total number of logical processors present on the system.
078   *
079   * @return number of logical processors
080   */
081  public abstract int getNumProcessors();
082
083  /**
084   * Obtain total number of physical cores present on the system.
085   *
086   * @return number of physical cores
087   */
088  public abstract int getNumCores();
089
090  /**
091   * Obtain the CPU frequency of on the system.
092   *
093   * @return CPU frequency in kHz
094   */
095  public abstract long getCpuFrequency();
096
097  /**
098   * Obtain the cumulative CPU time since the system is on.
099   *
100   * @return cumulative CPU time in milliseconds
101   */
102  public abstract long getCumulativeCpuTime();
103
104  /**
105   * Obtain the CPU usage % of the machine. Return -1 if it is unavailable
106   *
107   * @return CPU usage as a percentage (from 0 to 100) of available cycles.
108   */
109  public abstract float getCpuUsagePercentage();
110
111  /**
112   * Obtain the number of VCores used. Return -1 if it is unavailable
113   *
114   * @return Number of VCores used a percentage (from 0 to #VCores).
115   */
116  public abstract float getNumVCoresUsed();
117
118  /**
119   * Obtain the aggregated number of bytes read over the network.
120   * @return total number of bytes read.
121   */
122  public abstract long getNetworkBytesRead();
123
124  /**
125   * Obtain the aggregated number of bytes written to the network.
126   * @return total number of bytes written.
127   */
128  public abstract long getNetworkBytesWritten();
129
130  /**
131   * Obtain the aggregated number of bytes read from disks.
132   *
133   * @return total number of bytes read.
134   */
135  public abstract long getStorageBytesRead();
136
137  /**
138   * Obtain the aggregated number of bytes written to disks.
139   *
140   * @return total number of bytes written.
141   */
142  public abstract long getStorageBytesWritten();
143
144}