001 /*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License"). You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at
010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012 * See the License for the specific language governing permissions
013 * and limitations under the License.
014 *
015 * When distributing Covered Code, include this CDDL HEADER in each
016 * file and include the License file at
017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
018 * add the following below this CDDL HEADER, with the fields enclosed
019 * by brackets "[]" replaced with your own identifying information:
020 * Portions Copyright [yyyy] [name of copyright owner]
021 *
022 * CDDL HEADER END
023 *
024 *
025 * Copyright 2008 Sun Microsystems, Inc.
026 */
027
028 package org.opends.server.util;
029 import static org.opends.server.loggers.ErrorLogger.logError;
030 import static org.opends.messages.RuntimeMessages.*;
031 import static org.opends.messages.CoreMessages.*;
032 import static org.opends.server.util.DynamicConstants.*;
033 import org.opends.server.core.DirectoryServer;
034 import java.net.InetAddress;
035 import java.lang.management.RuntimeMXBean;
036 import java.lang.management.ManagementFactory;
037 import java.util.List;
038
039 import javax.management.MBeanServer;
040 import javax.management.ObjectName;
041
042 import com.sleepycat.je.JEVersion;
043
044
045 /**
046 * This class is used to gather and display information from the runtime
047 * environment.
048 */
049 public class RuntimeInformation {
050
051
052 private static boolean is64Bit=false;
053
054 static {
055 String arch = System.getProperty("sun.arch.data.model");
056 if (arch != null) {
057 try {
058 is64Bit = Integer.parseInt(arch) == 64;
059 } catch (NumberFormatException ex) {
060 //Default to 32 bit.
061 }
062 }
063 }
064
065 /**
066 * Returns whether the architecture of the JVM we are running under is 64-bit
067 * or not.
068 *
069 * @return <CODE>true</CODE> if the JVM architecture we running under is
070 * 64-bit and <CODE>false</CODE> otherwise.
071 */
072 public static boolean is64Bit() {
073 return is64Bit;
074 }
075
076 /**
077 * Returns a string representing the JVM input arguments as determined by the
078 * MX runtime bean. The individual arguments are separated by commas.
079 *
080 * @return A string representation of the JVM input arguments.
081 */
082 private static String getInputArguments() {
083 int count=0;
084 RuntimeMXBean rtBean = ManagementFactory.getRuntimeMXBean();
085 StringBuilder argList = new StringBuilder();
086 List<String> jvmArguments = rtBean.getInputArguments();
087 if ((jvmArguments != null) && (! jvmArguments.isEmpty())) {
088 for (String jvmArg : jvmArguments) {
089 if (argList.length() > 0) {
090 argList.append(" ");
091 }
092 argList.append("\"");
093 argList.append(jvmArg);
094 argList.append("\"");
095 count++;
096 if (count < jvmArguments.size()) {
097 argList.append(",");
098 }
099 }
100 }
101 return argList.toString();
102 }
103
104 /**
105 * Writes runtime information to a print stream.
106 */
107 public static void printInfo() {
108 System.out.println(NOTE_VERSION.get(DirectoryServer.getVersionString()));
109 System.out.println(NOTE_BUILD_ID.get(BUILD_ID));
110 System.out.println(
111 NOTE_JAVA_VERSION.get(System.getProperty("java.version")));
112 System.out.println(
113 NOTE_JAVA_VENDOR.get(System.getProperty("java.vendor")));
114 System.out.println(
115 NOTE_JVM_VERSION.get(System.getProperty("java.vm.version")));
116 System.out.println(
117 NOTE_JVM_VENDOR.get(System.getProperty("java.vm.vendor")));
118 System.out.println(
119 NOTE_JAVA_HOME.get(System.getProperty("java.home")));
120 System.out.println(
121 NOTE_JAVA_CLASSPATH.get(System.getProperty("java.class.path")));
122 System.out.println(
123 NOTE_JE_VERSION.get(JEVersion.CURRENT_VERSION.toString()));
124 System.out.println(
125 NOTE_CURRENT_DIRECTORY.get(System.getProperty("user.dir")));
126 System.out.println(
127 NOTE_OPERATING_SYSTEM.get(System.getProperty("os.name") + " " +
128 System.getProperty("os.version") + " " +
129 System.getProperty("os.arch")));
130 String sunOsArchDataModel = System.getProperty("sun.arch.data.model");
131 if (sunOsArchDataModel != null) {
132 if (! sunOsArchDataModel.toLowerCase().equals("unknown")) {
133 System.out.println(NOTE_JVM_ARCH.get(sunOsArchDataModel + "-bit"));
134 }
135 }
136 else{
137 System.out.println(NOTE_JVM_ARCH.get("unknown"));
138 }
139 try {
140 System.out.println(NOTE_SYSTEM_NAME.get(InetAddress.getLocalHost().
141 getCanonicalHostName()));
142 }
143 catch (Exception e) {
144 System.out.println(NOTE_SYSTEM_NAME.get("Unknown (" + e + ")"));
145 }
146 System.out.println(NOTE_AVAILABLE_PROCESSORS.get(Runtime.getRuntime().
147 availableProcessors()));
148 System.out.println(NOTE_MAX_MEMORY.get(Runtime.getRuntime().maxMemory()));
149 System.out.println(
150 NOTE_TOTAL_MEMORY.get(Runtime.getRuntime().totalMemory()));
151 System.out.println(
152 NOTE_FREE_MEMORY.get(Runtime.getRuntime().freeMemory()));
153 }
154
155 /**
156 * Returns the physical memory size, in bytes, of the hardware we are
157 * running on.
158 *
159 * @return Bytes of physical memory of the hardware we are running on.
160 */
161 private static long getPhysicalMemorySize()
162 {
163 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
164 try
165 {
166 // Assuming the RuntimeMXBean has been registered in mbs
167 ObjectName oname = new ObjectName(
168 ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME);
169 // Check if this MXBean contains Sun's extension
170 if (mbs.isInstanceOf(oname, "com.sun.management.OperatingSystemMXBean")) {
171 // Get platform-specific attribute "TotalPhysicalMemorySize"
172 Long l = (Long) mbs.getAttribute(oname, "TotalPhysicalMemorySize");
173 return l ;
174 }
175 else
176 {
177 return -1;
178 }
179 }
180 catch (Exception e)
181 {
182 return -1;
183 }
184 }
185
186 /**
187 * Returns a string representing the fully qualified domain name.
188 *
189 * @return A string representing the fully qualified domain name or the
190 * string "unknown" if an exception was thrown.
191 */
192 private static String getHostName() {
193 String host;
194 try {
195 host=InetAddress.getLocalHost().getCanonicalHostName();
196 }
197 catch (Exception e) {
198 host="Unknown (" + e + ")";
199 }
200 return host;
201 }
202
203 /**
204 * Returns string representing operating system name,
205 * version and architecture.
206 *
207 * @return String representing the operating system information the JVM is
208 * running under.
209 */
210 private static String getOSInfo() {
211 return System.getProperty("os.name") + " " +
212 System.getProperty("os.version") + " " +
213 System.getProperty("os.arch");
214 }
215
216 /**
217 * Return string representing the architecture of the JVM we are running
218 * under.
219 *
220 * @return A string representing the architecture of the JVM we are running
221 * under or "unknown" if the architecture cannot be determined.
222 */
223 private static String getArch() {
224 String sunOsArchDataModel = System.getProperty("sun.arch.data.model");
225 if (sunOsArchDataModel != null) {
226 if (! sunOsArchDataModel.toLowerCase().equals("unknown")) {
227 return (sunOsArchDataModel + "-bit");
228 }
229 }
230 return "unknown";
231 }
232
233 /**
234 * Write runtime information to error log.
235 */
236 public static void logInfo() {
237 logError(NOTE_JVM_INFO.get(System.getProperty("java.runtime.version"),
238 System.getProperty("java.vendor"),
239 getArch(),Runtime.getRuntime().maxMemory()));
240 Long physicalMemorySize = getPhysicalMemorySize();
241 if (physicalMemorySize != -1)
242 {
243 logError(NOTE_JVM_HOST.get(getHostName(), getOSInfo(),
244 physicalMemorySize, Runtime.getRuntime().availableProcessors()));
245 }
246 else
247 {
248 logError(NOTE_JVM_HOST_WITH_UNKNOWN_PHYSICAL_MEM.get(getHostName(),
249 getOSInfo(), Runtime.getRuntime().availableProcessors()));
250 }
251 logError(NOTE_JVM_ARGS.get(getInputArguments()));
252 }
253 }