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.util;
20 
21 import org.apache.hadoop.conf.Configuration;
22 import org.apache.hadoop.conf.Configured;
23 import org.apache.hadoop.util.ReflectionUtils;
24 
25 /**
26  * Plugin to calculate resource information on the system.
27  *
28  */
29 public abstract class ResourceCalculatorPlugin extends Configured {
30 
31   /**
32    * Obtain the total size of the virtual memory present in the system.
33    *
34    * @return virtual memory size in bytes.
35    */
getVirtualMemorySize()36   public abstract long getVirtualMemorySize();
37 
38   /**
39    * Obtain the total size of the physical memory present in the system.
40    *
41    * @return physical memory size bytes.
42    */
getPhysicalMemorySize()43   public abstract long getPhysicalMemorySize();
44 
45   /**
46    * Obtain the total size of the available virtual memory present
47    * in the system.
48    *
49    * @return available virtual memory size in bytes.
50    */
getAvailableVirtualMemorySize()51   public abstract long getAvailableVirtualMemorySize();
52 
53   /**
54    * Obtain the total size of the available physical memory present
55    * in the system.
56    *
57    * @return available physical memory size bytes.
58    */
getAvailablePhysicalMemorySize()59   public abstract long getAvailablePhysicalMemorySize();
60 
61   /**
62    * Obtain the total number of processors present on the system.
63    *
64    * @return number of processors
65    */
getNumProcessors()66   public abstract int getNumProcessors();
67 
68   /**
69    * Obtain the CPU frequency of on the system.
70    *
71    * @return CPU frequency in kHz
72    */
getCpuFrequency()73   public abstract long getCpuFrequency();
74 
75   /**
76    * Obtain the cumulative CPU time since the system is on.
77    *
78    * @return cumulative CPU time in milliseconds
79    */
getCumulativeCpuTime()80   public abstract long getCumulativeCpuTime();
81 
82   /**
83    * Obtain the CPU usage % of the machine. Return -1 if it is unavailable
84    *
85    * @return CPU usage in %
86    */
getCpuUsage()87   public abstract float getCpuUsage();
88 
89   /**
90    * Obtain resource status used by current process tree.
91    */
getProcResourceValues()92   public abstract ProcResourceValues getProcResourceValues();
93 
94   public static class ProcResourceValues {
95     private final long cumulativeCpuTime;
96     private final long physicalMemorySize;
97     private final long virtualMemorySize;
ProcResourceValues(long cumulativeCpuTime, long physicalMemorySize, long virtualMemorySize)98     public ProcResourceValues(long cumulativeCpuTime, long physicalMemorySize,
99                               long virtualMemorySize) {
100       this.cumulativeCpuTime = cumulativeCpuTime;
101       this.physicalMemorySize = physicalMemorySize;
102       this.virtualMemorySize = virtualMemorySize;
103     }
104     /**
105      * Obtain the physical memory size used by current process tree.
106      * @return physical memory size in bytes.
107      */
getPhysicalMemorySize()108     public long getPhysicalMemorySize() {
109       return physicalMemorySize;
110     }
111 
112     /**
113      * Obtain the virtual memory size used by a current process tree.
114      * @return virtual memory size in bytes.
115      */
getVirtualMemorySize()116     public long getVirtualMemorySize() {
117       return virtualMemorySize;
118     }
119 
120     /**
121      * Obtain the cumulative CPU time used by a current process tree.
122      * @return cumulative CPU time in milliseconds
123      */
getCumulativeCpuTime()124     public long getCumulativeCpuTime() {
125       return cumulativeCpuTime;
126     }
127   }
128 
129   /**
130    * Get the ResourceCalculatorPlugin from the class name and configure it. If
131    * class name is null, this method will try and return a memory calculator
132    * plugin available for this system.
133    *
134    * @param clazz class-name
135    * @param conf configure the plugin with this.
136    * @return ResourceCalculatorPlugin
137    */
getResourceCalculatorPlugin( Class<? extends ResourceCalculatorPlugin> clazz, Configuration conf)138   public static ResourceCalculatorPlugin getResourceCalculatorPlugin(
139       Class<? extends ResourceCalculatorPlugin> clazz, Configuration conf) {
140 
141     if (clazz != null) {
142       return ReflectionUtils.newInstance(clazz, conf);
143     }
144 
145     // No class given, try a os specific class
146     try {
147       String osName = System.getProperty("os.name");
148       if (osName.startsWith("Linux")) {
149         return new LinuxResourceCalculatorPlugin();
150       }
151     } catch (SecurityException se) {
152       // Failed to get Operating System name.
153       return null;
154     }
155 
156     // Not supported on this system.
157     return null;
158   }
159 }
160