View Javadoc

1   /**
2    * Copyright (c) 2012, University of Konstanz, Distributed Systems Group
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    * * Redistributions of source code must retain the above copyright
8    * notice, this list of conditions and the following disclaimer.
9    * * Redistributions in binary form must reproduce the above copyright
10   * notice, this list of conditions and the following disclaimer in the
11   * documentation and/or other materials provided with the distribution.
12   * * Neither the name of the University of Konstanz nor the
13   * names of its contributors may be used to endorse or promote products
14   * derived from this software without specific prior written permission.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19   * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20   * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   */
27  package org.perfidix.meter;
28  
29  import java.math.BigDecimal;
30  import java.math.MathContext;
31  
32  /**
33   * Meter to bench the amount of memory used by the current Benchmark. Please
34   * note that the results of this meter can only be seen as an approximation.
35   * Because Perfidix-processes are influencing the used memory as well, a small
36   * increasment of used memory is normal. However, because being based only on
37   * the Runtime-class of Java, no extraction of the perfidix processes themselves
38   * is possible. The MemMeter is only usable with an instance of the {@link Memory} enumeration for formatting
39   * purposes. This choose must be made
40   * by instantiation of the meter.
41   * 
42   * @see Memory
43   * @author Sebastian Graf, University of Konstanz
44   */
45  public final class MemMeter extends AbstractMeter {
46  
47      /** Name of the Meter. */
48      private static final String NAME = "MemMeter";
49  
50      /** Amount of already used memory. */
51      private transient double memAlreadyUsed;
52  
53      /** Scale of memory. */
54      private transient final Memory scale;
55  
56      /**
57       * Constructor.
58       * 
59       * @param paramScale
60       *            scale for this meter, can be any instance of Memory-enum
61       */
62      public MemMeter(final Memory paramScale) {
63          super();
64          memAlreadyUsed = 0;
65          this.scale = paramScale;
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      @Override
72      public double getValue() {
73          final Runtime runtime = Runtime.getRuntime();
74          memAlreadyUsed = memAlreadyUsed + runtime.totalMemory() - runtime.freeMemory();
75          return new BigDecimal(memAlreadyUsed, MathContext.DECIMAL128).divide(
76              new BigDecimal(scale.getNumberOfBytes()), MathContext.DECIMAL128).doubleValue();
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public String getName() {
82          return NAME;
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      public String getUnit() {
88          return scale.getUnit();
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      public String getUnitDescription() {
94          return scale.getUnitDescription();
95      }
96  
97      /** {@inheritDoc} */
98      @Override
99      public int hashCode() {
100         final int prime = 31;
101         int result = prime;
102         if (scale == null) {
103             result = prime * result;
104         } else {
105             result = prime * result + scale.hashCode();
106         }
107 
108         return result;
109     }
110 
111     /** {@inheritDoc} */
112     @Override
113     public boolean equals(final Object obj) {
114         boolean returnVal = true;
115         if (this == obj) {
116             returnVal = true;
117         }
118         if (getClass() != obj.getClass()) {
119             returnVal = false;
120         }
121         final MemMeter other = (MemMeter)obj;
122         if (scale == null) {
123             if (other.scale != null) {
124                 returnVal = false;
125             }
126         } else {
127             if (!scale.equals(other.scale)) {
128                 returnVal = false;
129             }
130         }
131         return returnVal;
132     }
133 
134 }