1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.debug;
  24 
  25 import java.util.List;
  26 
  27 /**
  28  * Facility for recording value frequencies.
  29  */
  30 public interface DebugHistogram {
  31 
  32     /**
  33      * Gets the name specified when this objected was {@linkplain Debug#createHistogram(String)
  34      * created}.
  35      */
  36     String getName();
  37 
  38     /**
  39      * Increments the count for a given value.
  40      */
  41     void add(Object value);
  42 
  43     void add(Object value, long count);
  44 
  45     /**
  46      * A value and a frequency. The ordering imposed by {@link #compareTo(CountedValue)} places
  47      * values with higher frequencies first.
  48      */
  49     class CountedValue implements Comparable<CountedValue> {
  50 
  51         private long count;
  52         private final Object value;
  53 
  54         public CountedValue(long count, Object value) {
  55             this.count = count;
  56             this.value = value;
  57         }
  58 
  59         @Override
  60         public int compareTo(CountedValue o) {
  61             if (count < o.count) {
  62                 return 1;
  63             } else if (count > o.count) {
  64                 return -1;
  65             }
  66             return 0;
  67         }
  68 
  69         @Override
  70         public String toString() {
  71             return count + " -> " + value;
  72         }
  73 
  74         public void inc() {
  75             count++;
  76         }
  77 
  78         public void add(long n) {
  79             count += n;
  80         }
  81 
  82         public long getCount() {
  83             return count;
  84         }
  85 
  86         public Object getValue() {
  87             return value;
  88         }
  89     }
  90 
  91     /**
  92      * Gets a list of the counted values, sorted in descending order of frequency.
  93      */
  94     List<CountedValue> getValues();
  95 
  96     /**
  97      * Interface for a service that can render a visualization of a histogram.
  98      */
  99     public interface Printer {
 100 
 101         void print(DebugHistogram histogram);
 102     }
 103 }