1 /*
   2  * Copyright (c) 2017, 2018, 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             try (PrintStream p = metricsFile == null ? DebugContext.DEFAULT_LOG_STREAM : new PrintStream(Files.newOutputStream(Paths.get(metricsFile)))) {
  90                 if (!csv) {
  91                     if (!map.isEmpty()) {
  92                         p.println("++ Aggregated Metrics ++");
  93                     }
  94                 }
  95                 String csvFormat = CSVUtil.buildFormatString("%s", "%s", "%s");
  96                 MapCursor<MetricKey, Long> e = map.getEntries();
  97                 while (e.advance()) {
  98                     MetricKey key = e.getKey();
  99                     if (csv) {
 100                         Pair<String, String> valueAndUnit = key.toCSVFormat(e.getValue());
 101                         CSVUtil.Escape.println(p, csvFormat, key.getName(), valueAndUnit.getLeft(), valueAndUnit.getRight());
 102                     } else {
 103                         p.println(key.getName() + "=" + key.toHumanReadableFormat(e.getValue()));
 104                     }
 105                 }
 106                 if (!csv) {
 107                     if (!map.isEmpty()) {
 108                         p.println("-- Aggregated Metrics --");
 109                     }
 110                 }
 111             } catch (IOException e) {
 112                 e.printStackTrace();
 113             }
 114         }
 115 
 116         if (DebugOptions.ListMetrics.getValue(options)) {
 117             PrintStream p = System.out;
 118             p.println("++ Metric Keys ++");
 119             List<MetricKey> keys = KeyRegistry.getKeys();
 120             Collections.sort(keys, MetricKey.NAME_COMPARATOR);
 121             for (MetricKey key : keys) {
 122                 String name = key.getDocName();
 123                 if (name != null) {
 124                     String doc = key.getDoc();
 125                     if (doc != null) {
 126                         p.println(name + ": " + doc);
 127                     } else {
 128                         p.println(name);
 129                     }
 130                 }
 131             }
 132             p.println("-- Metric Keys --");
 133         }
 134     }
 135 }