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