001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * 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.jppf.management.diagnostics;
020
021import java.io.Serializable;
022import java.util.Map;
023
024import org.jppf.management.diagnostics.provider.MonitoringConstants;
025import org.jppf.utils.TypedProperties;
026
027/**
028 * This class represents a snapshot of the JVM health.
029 * @author Laurent Cohen
030 */
031public class HealthSnapshot implements Serializable {
032  /**
033   * Explicit serialVersionUID.
034   */
035  private static final long serialVersionUID = 1L;
036  /**
037   * Contains the attributes of this snapshot with their values.
038   */
039  private final TypedProperties properties = new TypedProperties();
040
041  /**
042   * Initialize this snapshot with default values.
043   */
044  public HealthSnapshot() {
045  }
046
047  /**
048   * Get all the key / value pairs representing monitoring data in this snapshot.
049   * @return an association of propery names to their computed value.
050   */
051  public TypedProperties getProperties() {
052    return properties;
053  }
054
055  /**
056   * Add or merge the specified properties ot the existing ones.
057   * @param properties the proerties to add.
058   */
059  void putProperties(final TypedProperties properties) {
060    this.properties.putAll(properties);
061  }
062
063  /**
064   * Get the value of a property as an int.
065   * @param name the name of the property to lookup.
066   * @return the value of the property.
067   */
068  public int getInt(final String name) {
069    return properties.getInt(name);
070  }
071
072  /**
073   * Get the value of a property as a long.
074   * @param name the name of the property to lookup.
075   * @return the value of the property.
076   */
077  public long getLong(final String name) {
078    return properties.getLong(name);
079  }
080
081  /**
082   * Get the value of a property as a float.
083   * @param name the name of the property to lookup.
084   * @return the value of the property.
085   */
086  public float getFloat(final String name) {
087    return properties.getFloat(name);
088  }
089
090  /**
091   * Get the value of a property as a double.
092   * @param name the name of the property to lookup.
093   * @return the value of the property.
094   */
095  public double getDouble(final String name) {
096    return properties.getDouble(name);
097  }
098
099  /**
100   * Get the value of a property as a boolean.
101   * @param name the name of the property to lookup.
102   * @return the value of the property.
103   */
104  public boolean getBoolean(final String name) {
105    return properties.getBoolean(name);
106  }
107
108  /**
109   * Get the value of a property as a String.
110   * @param name the name of the property to lookup.
111   * @return the value of the property.
112   */
113  public String getString(final String name) {
114    return properties.getString(name);
115  }
116
117  /**
118   * Get the ratio of used / max for heap memory.
119   * @return the ratio as a double value in the range [0, 1].
120   * @deprecated use {@code getDouble(MonitoringConstants.HEAP_USAGE_RATIO)} instead.
121   */
122  public double getHeapUsedRatio() {
123    return properties.getDouble(MonitoringConstants.HEAP_USAGE_RATIO);
124  }
125
126  /**
127   * Get the ratio of used / max for non-heap memory.
128   * @return the ratio as a double value in the range [0, 1].
129   * @deprecated use {@code getDouble(MonitoringConstants.NON_HEAP_USAGE_RATIO)} instead.
130   */
131  public double getNonheapUsedRatio() {
132    return properties.getDouble(MonitoringConstants.NON_HEAP_USAGE_RATIO);
133  }
134
135  /**
136   * Determine whether a deadlock was detected.
137   * @return {@code true} if a deadlock was dertected, {@code false} otherwise.
138   * @deprecated use {@code getBoolean(MonitoringConstants.DEADLOCKED)} instead.
139   */
140  public boolean isDeadlocked() {
141    return properties.getBoolean(MonitoringConstants.DEADLOCKED);
142  }
143
144  /**
145   * Get the used heap memory in bytes.
146   * @return the heap used as a long.
147   * @deprecated use {@code getLong(MonitoringConstants.HEAP_USAGE_MB)} instead.
148   */
149  public long getHeapUsed() {
150    return properties.getLong(MonitoringConstants.HEAP_USAGE_MB);
151  }
152
153  /**
154   * Get the used non-heap memory in bytes.
155   * @return the non-heap used as a long.
156   * @deprecated use {@code getLong(MonitoringConstants.NON_HEAP_USAGE_MB)} instead.
157   */
158  public long getNonheapUsed() {
159    return properties.getLong(MonitoringConstants.NON_HEAP_USAGE_MB);
160  }
161
162  /**
163   * Get the number of live threads in the JVM.
164   * @return the number of threads as an int.
165   * @deprecated use {@code getInt(MonitoringConstants.LIVE_THREADS_COUNT)} instead.
166   */
167  public int getLiveThreads() {
168    return properties.getInt(MonitoringConstants.LIVE_THREADS_COUNT);
169  }
170
171  /**
172   * Get the cpu load of the current process.
173   * @return the cpu load as a double in the range {@code [0 ... 1]}, or {@code -1d} if it is unknown.
174   * @deprecated use {@code getDouble(MonitoringConstants.PROCESS_CPU_LOAD)} instead.
175   */
176  public double getCpuLoad() {
177    return properties.getDouble(MonitoringConstants.PROCESS_CPU_LOAD);
178  }
179
180  /**
181   * Get the cpu load of the system.
182   * @return the cpu load as a double in the range {@code [0 ... 1]}, or {@code -1d} if it is unknown.
183   * @deprecated use {@code getDouble(MonitoringConstants.SYSTEM_CPU_LOAD)} instead.
184   */
185  public double getSystemCpuLoad() {
186    return properties.getDouble(MonitoringConstants.SYSTEM_CPU_LOAD);
187  }
188
189  /**
190   * Get the ratio of used / max for physical memory.
191   * @return the percentage of used RAM in the range {@code [0 ... 1]}, or {@code -1d} if it is unknown.
192   * @deprecated use {@code getDouble(MonitoringConstants.RAM_USAGE_RATIO)} instead.
193   */
194  public double getRamUsedRatio() {
195    return properties.getDouble(MonitoringConstants.RAM_USAGE_RATIO);
196  }
197
198  /**
199   * Get the amount of used physical memory in bytes.
200   * @return the amount of used RAM in bytes, or {@code -1L} if it is unknown.
201   * @deprecated use {@code getLong(MonitoringConstants.RAM_USAGE_MB)} instead.
202   */
203  public long getRamUsed() {
204    return properties.getLong(MonitoringConstants.RAM_USAGE_MB);
205  }
206
207  @Override
208  public String toString() {
209    final StringBuilder sb = new StringBuilder(getClass().getSimpleName()).append('[');
210    int count = 0;
211    for (final Map.Entry<Object, Object> entry: properties.entrySet()) {
212      if ((entry.getKey() instanceof String) && (entry.getKey() instanceof String)) {
213        final String key = (String) entry.getKey(), value = (String) entry.getValue();
214        if (count > 0) sb.append(", ");
215        sb.append(key).append('=').append(value);
216        count++;
217      }
218    }
219    return sb.append(']').toString();
220  }
221}