All Downloads are FREE. Search and download functionalities are using the official Maven repository.

template.trace.CacheAnalyzer.tt Maven / Gradle / Ivy

There is a newer version: 2.3.6
Show newest version
# Copyright (c) 2013, The JastAdd Team
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     * Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the Lund University nor the names of its
#       contributors may be used to endorse or promote products derived from
#       this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# Code for inner class CacheAnalyzer
CacheAnalyzer [[
/**
 * Cache analyzer based on the ideas presented in
 *
 *   
 *   Automated selective caching for reference attribute grammars
 *   Emma Söderberg and Görel Hedin
 *   Dept. of Computer Science, Lund University
 *
 */
public class CacheAnalyzer extends $StateClass.Trace.InputFilter {

  private final String[] ATTRIBUTES = { $AttrNameList };
  private java.util.Map> attrMap = null;

  public $StateClass.Trace.Entry createEntry($StateClass.Trace.Event evt, $ASTNode node, String attr, Object params, Object value) {
    if (evt == $StateClass.Trace.Event.CACHED || evt == $StateClass.Trace.Event.CACHE_READ) {
      if (attrMap == null) {
        createAttrMap();
      }
      java.util.Map instanceMap = attrMap.get(attr);
      if (instanceMap == null) {
        instanceMap = new java.util.HashMap();
        attrMap.put(attr, instanceMap);
      }
      String instance = String.format("%08X", node.hashCode());
      Integer calls = instanceMap.get(instance);
      instanceMap.put(instance, calls == null ? 1 : calls + 1);
    }
    return super.createEntry(evt, node, attr, params, value);
  }

  public boolean include($StateClass.Trace.Entry e) {
    return false;
  }

  public String desc() {
    return "Cache analyzer";
  }

  private java.util.Map> createAttrMap() {
    attrMap = new java.util.HashMap>();
    for (String attrName : ATTRIBUTES) {
      attrMap.put(attrName, null);
    }
    return attrMap;
  }

  /**
   * @return The raw trace result of the analyzer, mapping attribute names to call counts
   */
  public java.util.Map> getRawResult() {
    java.util.Map> map = new java.util.HashMap>();
    if (attrMap == null) {
      return map;
    }
    for (java.util.Map.Entry> e : attrMap.entrySet()) {
      if (e.getValue() != null) {
        java.util.Map innerMap = new java.util.HashMap();
        for (java.util.Map.Entry ie : e.getValue().entrySet()) {
          innerMap.put(ie.getKey(), ie.getValue());
        }
        map.put(e.getKey(), innerMap);
      } else {
        map.put(e.getKey(), null);
      }
    }
    return map;
  }

  /**
   * @return The average number of calls per instance for the given attribute
   */
  public double getAverageAttrCalls(String attr) {
    if (attrMap == null) {
      return 0.0;
    }
    java.util.Map instanceMap = attrMap.get(attr);
    if (instanceMap == null) {
      return 0.0;
    }
    double sum = 0.0;
    for (Integer calls : instanceMap.values()) {
      sum += calls;
    }
    return sum / instanceMap.size();
  }

  /**
   * @return The set of all attributes, represented with signature strings
   */
  public java.util.Set getAllSet() {
    java.util.Set set = new java.util.HashSet();
    if (attrMap == null) {
      return set;
    }
    for (String attrName : ATTRIBUTES) {
      set.add(attrName);
    }
    return set;
  }

  /**
   * @return The set of used attributes, represented with signature strings
   */
  public java.util.Set getUsedSet() {
    java.util.Set set = new java.util.HashSet();
    if (attrMap == null) {
      return set;
    }
    for (java.util.Map.Entry> e : attrMap.entrySet()) {
      if (e.getValue() != null) {
        set.add(e.getKey());
      }
    }
    return set;
  }

  /**
   * @return The set of attributes used once, represented with signature strings
   */
  public java.util.Set getOneSet() {
    java.util.Set set = new java.util.HashSet();
    if (attrMap == null) {
      return set;
    }
    for (java.util.Map.Entry> e : attrMap.entrySet()) {
      if (e.getValue() != null && getAverageAttrCalls(e.getKey()) <= 1.0) {
        set.add(e.getKey());
      }
    }
    return set;
  }

  /**
   * @return The attribute coverage in percent (i.e., Used/All)
   */
   public double getAttributeCoverage() {
     return (getUsedSet().size()*1.0 / getAllSet().size()*1.0) * 100.0;
   }

  /**
   * Prints a cache configuration corresponding to All-One
   * @param out Print writer to use
   */
   public void printAllMinusOneConfig(java.io.PrintWriter out) {
     java.util.Set configSet = getAllSet();
     configSet.removeAll(getOneSet());
     out.println("aspect CacheConfig {");
     for (String attr : configSet) {
       out.println("  cache " + attr + ";");
     }
     out.println("}");
     out.flush();
   }

  /**
   * Prints a cache configuration corresponding to Used-One
   * @param out Print writer to use
   */
   public void printUsedMinusOneConfig(java.io.PrintWriter out) {
     java.util.Set configSet = getUsedSet();
     configSet.removeAll(getOneSet());
     out.println("aspect CacheConfig {");
     for (String attr : configSet) {
       out.println("  cache " + attr + ";");
     }
     out.println("}");
     out.flush();
   }
}
]]




© 2015 - 2024 Weber Informatics LLC | Privacy Policy