1 /*
   2  * Copyright (c) 2017, 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.io.IOException;
  26 import java.io.PrintStream;
  27 import java.nio.file.Files;
  28 import java.nio.file.Paths;
  29 import java.util.Collections;
  30 import java.util.List;
  31 
  32 import org.graalvm.compiler.options.OptionValues;
  33 import org.graalvm.util.EconomicMap;
  34 import org.graalvm.util.MapCursor;
  35 import org.graalvm.util.Pair;
  36 
  37 /**
  38  * Metric values that can be {@linkplain #add(DebugContext) updated} by multiple threads.
  39  */
  40 public class GlobalMetrics {
  41     long[] values;
  42 
  43     /**
  44      * Adds the values in {@code debug} to the values in this object.
  45      */
  46     public synchronized void add(DebugContext debug) {
  47         values = debug.addValuesTo(values);
  48     }
  49 
  50     /**
  51      * Clears all values in this object.
  52      */
  53     public void clear() {
  54         values = null;
  55     }
  56 
  57     /**
  58      * Creates and returns a sorted map from metric names to their values in this object.
  59      */
  60     public EconomicMap<MetricKey, Long> asKeyValueMap() {
  61         List<MetricKey> keys = KeyRegistry.getKeys();
  62         Collections.sort(keys, MetricKey.NAME_COMPARATOR);
  63         EconomicMap<MetricKey, Long> res = EconomicMap.create(keys.size());
  64         long[] vals = values;
  65         for (MetricKey key : keys) {
  66             int index = ((AbstractKey) key).getIndex();
  67             if (vals == null || index >= vals.length) {
  68                 res.put(key, 0L);
  69             } else {
  70                 res.put(key, vals[index]);
  71             }
  72         }
  73         return res;
  74     }
  75 
  76     /**
  77      * Prints the values in the object to the file specified by
  78      * {@link DebugOptions#AggregatedMetricsFile} if present otherwise to
  79      * {@link DebugContext#DEFAULT_LOG_STREAM}.
  80      */
  81     public void print(OptionValues options) {
  82         long[] vals = values;
  83         if (vals != null) {
  84             EconomicMap<MetricKey, Long> map = asKeyValueMap();
  85             String metricsFile = DebugOptions.AggregatedMetricsFile.getValue(options);
  86             boolean csv = metricsFile != null && (metricsFile.endsWith(".csv") || metricsFile.endsWith(".CSV"));
  87             try (PrintStream p = metricsFile == null ? DebugContext.DEFAULT_LOG_STREAM : new PrintStream(Files.newOutputStream(Paths.get(metricsFile)))) {
  88                 if (!csv) {
  89                     if (!map.isEmpty()) {
  90                         p.println("++ Aggregated Metrics ++");
  91                     }
  92                 }
  93                 String csvFormat = CSVUtil.buildFormatString("%s", "%s", "%s");
  94                 MapCursor<MetricKey, Long> e = map.getEntries();
  95                 while (e.advance()) {
  96                     MetricKey key = e.getKey();
  97                     if (csv) {
  98                         Pair<String, String> valueAndUnit = key.toCSVFormat(e.getValue());
  99                         CSVUtil.Escape.println(p, csvFormat, key.getName(), valueAndUnit.getLeft(), valueAndUnit.getRight());
 100                     } else {
 101                         p.println(key.getName() + "=" + key.toHumanReadableFormat(e.getValue()));
 102                     }
 103                 }
 104                 if (!csv) {
 105                     if (!map.isEmpty()) {
 106                         p.println("-- Aggregated Metrics --");
 107                     }
 108                 }
 109             } catch (IOException e) {
 110                 e.printStackTrace();
 111             }
 112         }
 113 
 114         if (DebugOptions.ListMetrics.getValue(options)) {
 115             PrintStream p = System.out;
 116             p.println("++ Metric Keys ++");
 117             List<MetricKey> keys = KeyRegistry.getKeys();
 118             Collections.sort(keys, MetricKey.NAME_COMPARATOR);
 119             for (MetricKey key : keys) {
 120                 String name = key.getDocName();
 121                 if (name != null) {
 122                     String doc = key.getDoc();
 123                     if (doc != null) {
 124                         p.println(name + ": " + doc);
 125                     } else {
 126                         p.println(name);
 127                     }
 128                 }
 129             }
 130             p.println("-- Metric Keys --");
 131         }
 132     }
 133 }