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 */
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 --");
|
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 */
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 --");
|