|
26 | 26 | package com.oracle.svm.hosted;
|
27 | 27 |
|
28 | 28 | import java.io.File;
|
29 |
| -import java.io.PrintWriter; |
| 29 | +import java.io.IOException; |
30 | 30 | import java.lang.management.ManagementFactory;
|
31 | 31 | import java.lang.management.OperatingSystemMXBean;
|
32 | 32 | import java.nio.file.Path;
|
33 | 33 | import java.util.HashMap;
|
34 |
| -import java.util.Iterator; |
35 | 34 | import java.util.Map;
|
36 |
| -import java.util.function.Consumer; |
37 | 35 |
|
38 | 36 | import com.oracle.graal.pointsto.reports.ReportUtils;
|
39 | 37 | import com.oracle.svm.core.util.VMError;
|
| 38 | +import com.oracle.svm.core.util.json.JsonWriter; |
40 | 39 |
|
41 | 40 | class ProgressReporterJsonHelper {
|
42 | 41 | protected static final long UNAVAILABLE_METRIC = -1;
|
@@ -108,51 +107,13 @@ public Path printToFile() {
|
108 | 107 | recordSystemFixedValues();
|
109 | 108 | final File file = new File(jsonOutputFile);
|
110 | 109 | String description = "image statistics in json";
|
111 |
| - return ReportUtils.report(description, file.getAbsoluteFile().toPath(), getReporter(), false); |
112 |
| - } |
113 |
| - |
114 |
| - private Consumer<PrintWriter> getReporter() { |
115 |
| - return out -> { |
116 |
| - out.println(toJson()); |
117 |
| - }; |
118 |
| - } |
119 |
| - |
120 |
| - private String toJson() { |
121 |
| - return mapToJson(statsHolder); |
122 |
| - } |
123 |
| - |
124 |
| - private String mapToJson(Map<String, Object> map) { |
125 |
| - // base case |
126 |
| - if (map.isEmpty()) { |
127 |
| - return "{}"; |
128 |
| - } |
129 |
| - StringBuilder builder = new StringBuilder(); |
130 |
| - builder.append("{"); |
131 |
| - Iterator<String> keySetIter = map.keySet().iterator(); |
132 |
| - while (keySetIter.hasNext()) { |
133 |
| - String key = keySetIter.next(); |
134 |
| - Object value = map.get(key); |
135 |
| - builder.append("\"" + key + "\":"); |
136 |
| - if (value == null) { |
137 |
| - builder.append("null"); // null string |
138 |
| - } else if (value instanceof Map) { |
139 |
| - // Always a <String, Object> map |
140 |
| - @SuppressWarnings("unchecked") |
141 |
| - Map<String, Object> subMap = (Map<String, Object>) value; |
142 |
| - builder.append(mapToJson(subMap)); |
143 |
| - } else if (value instanceof String) { |
144 |
| - builder.append("\"" + value + "\""); |
145 |
| - } else { |
146 |
| - assert value instanceof Number; |
147 |
| - // Numeric value |
148 |
| - builder.append(value); |
| 110 | + return ReportUtils.report(description, file.getAbsoluteFile().toPath(), out -> { |
| 111 | + try { |
| 112 | + new JsonWriter(out).print(statsHolder); |
| 113 | + } catch (IOException e) { |
| 114 | + throw VMError.shouldNotReachHere("Failed to create " + jsonOutputFile, e); |
149 | 115 | }
|
150 |
| - if (keySetIter.hasNext()) { |
151 |
| - builder.append(","); |
152 |
| - } |
153 |
| - } |
154 |
| - builder.append("}"); |
155 |
| - return builder.toString(); |
| 116 | + }, false); |
156 | 117 | }
|
157 | 118 |
|
158 | 119 | interface JsonMetric {
|
|
0 commit comments