1 /* 2 * Copyright (c) 2015, 2016, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.java2d.marlin; 27 28 import java.security.AccessController; 29 import java.security.PrivilegedAction; 30 import java.util.Timer; 31 import java.util.TimerTask; 32 import java.util.concurrent.ConcurrentLinkedQueue; 33 import jdk.internal.ref.CleanerFactory; 34 import sun.java2d.marlin.ArrayCacheConst.CacheStats; 35 import static sun.java2d.marlin.MarlinUtils.logInfo; 36 import sun.java2d.marlin.stats.Histogram; 37 import sun.java2d.marlin.stats.Monitor; 38 import sun.java2d.marlin.stats.StatLong; 39 import sun.awt.util.ThreadGroupUtils; 40 41 /** 42 * This class gathers global rendering statistics for debugging purposes only 43 */ 44 public final class RendererStats implements MarlinConst { 45 46 static RendererStats createInstance(final Object parent, final String name) 47 { 48 final RendererStats stats = new RendererStats(name); 49 50 // Keep a strong reference to dump it later: 51 RendererStatsHolder.getInstance().add(parent, stats); 52 53 return stats; 54 } 55 56 public static void dumpStats() { 57 RendererStatsHolder.dumpStats(); 58 } 59 60 // context name (debugging purposes) 61 final String name; 62 // stats 63 final StatLong stat_cache_rowAA 64 = new StatLong("cache.rowAA"); 65 final StatLong stat_cache_rowAAChunk 66 = new StatLong("cache.rowAAChunk"); 67 final StatLong stat_cache_tiles 68 = new StatLong("cache.tiles"); 69 final StatLong stat_rdr_addLine 70 = new StatLong("renderer.addLine"); 71 final StatLong stat_rdr_addLine_skip 72 = new StatLong("renderer.addLine.skip"); 73 final StatLong stat_rdr_curveBreak 74 = new StatLong("renderer.curveBreakIntoLinesAndAdd"); 75 final StatLong stat_rdr_curveBreak_dec 76 = new StatLong("renderer.curveBreakIntoLinesAndAdd.dec"); 77 final StatLong stat_rdr_curveBreak_inc 78 = new StatLong("renderer.curveBreakIntoLinesAndAdd.inc"); 79 final StatLong stat_rdr_quadBreak 80 = new StatLong("renderer.quadBreakIntoLinesAndAdd"); 81 final StatLong stat_rdr_quadBreak_dec 82 = new StatLong("renderer.quadBreakIntoLinesAndAdd.dec"); 83 final StatLong stat_rdr_edges 84 = new StatLong("renderer.edges"); 85 final StatLong stat_rdr_edges_count 86 = new StatLong("renderer.edges.count"); 87 final StatLong stat_rdr_edges_resizes 88 = new StatLong("renderer.edges.resize"); 89 final StatLong stat_rdr_activeEdges 90 = new StatLong("renderer.activeEdges"); 91 final StatLong stat_rdr_activeEdges_updates 92 = new StatLong("renderer.activeEdges.updates"); 93 final StatLong stat_rdr_activeEdges_adds 94 = new StatLong("renderer.activeEdges.adds"); 95 final StatLong stat_rdr_activeEdges_adds_high 96 = new StatLong("renderer.activeEdges.adds_high"); 97 final StatLong stat_rdr_crossings_updates 98 = new StatLong("renderer.crossings.updates"); 99 final StatLong stat_rdr_crossings_sorts 100 = new StatLong("renderer.crossings.sorts"); 101 final StatLong stat_rdr_crossings_bsearch 102 = new StatLong("renderer.crossings.bsearch"); 103 final StatLong stat_rdr_crossings_msorts 104 = new StatLong("renderer.crossings.msorts"); 105 final StatLong stat_str_polystack_curves 106 = new StatLong("stroker.polystack.curves"); 107 final StatLong stat_str_polystack_types 108 = new StatLong("stroker.polystack.types"); 109 final StatLong stat_cpd_polystack_curves 110 = new StatLong("closedPathDetector.polystack.curves"); 111 final StatLong stat_cpd_polystack_types 112 = new StatLong("closedPathDetector.polystack.types"); 113 // growable arrays 114 final StatLong stat_array_dasher_dasher 115 = new StatLong("array.dasher.dasher.d_float"); 116 final StatLong stat_array_dasher_firstSegmentsBuffer 117 = new StatLong("array.dasher.firstSegmentsBuffer.d_float"); 118 final StatLong stat_array_marlincache_rowAAChunk 119 = new StatLong("array.marlincache.rowAAChunk.resize"); 120 final StatLong stat_array_marlincache_touchedTile 121 = new StatLong("array.marlincache.touchedTile.int"); 122 final StatLong stat_array_renderer_alphaline 123 = new StatLong("array.renderer.alphaline.int"); 124 final StatLong stat_array_renderer_crossings 125 = new StatLong("array.renderer.crossings.int"); 126 final StatLong stat_array_renderer_aux_crossings 127 = new StatLong("array.renderer.aux_crossings.int"); 128 final StatLong stat_array_renderer_edgeBuckets 129 = new StatLong("array.renderer.edgeBuckets.int"); 130 final StatLong stat_array_renderer_edgeBucketCounts 131 = new StatLong("array.renderer.edgeBucketCounts.int"); 132 final StatLong stat_array_renderer_edgePtrs 133 = new StatLong("array.renderer.edgePtrs.int"); 134 final StatLong stat_array_renderer_aux_edgePtrs 135 = new StatLong("array.renderer.aux_edgePtrs.int"); 136 final StatLong stat_array_str_polystack_curves 137 = new StatLong("array.stroker.polystack.curves.d_float"); 138 final StatLong stat_array_str_polystack_types 139 = new StatLong("array.stroker.polystack.curveTypes.d_byte"); 140 final StatLong stat_array_cpd_polystack_curves 141 = new StatLong("array.closedPathDetector.polystack.curves.d_float"); 142 final StatLong stat_array_cpd_polystack_types 143 = new StatLong("array.closedPathDetector.polystack.curveTypes.d_byte"); 144 // histograms 145 final Histogram hist_rdr_edges_count 146 = new Histogram("renderer.edges.count"); 147 final Histogram hist_rdr_crossings 148 = new Histogram("renderer.crossings"); 149 final Histogram hist_rdr_crossings_ratio 150 = new Histogram("renderer.crossings.ratio"); 151 final Histogram hist_rdr_crossings_adds 152 = new Histogram("renderer.crossings.adds"); 153 final Histogram hist_rdr_crossings_msorts 154 = new Histogram("renderer.crossings.msorts"); 155 final Histogram hist_rdr_crossings_msorts_adds 156 = new Histogram("renderer.crossings.msorts.adds"); 157 final Histogram hist_str_polystack_curves 158 = new Histogram("stroker.polystack.curves"); 159 final Histogram hist_tile_generator_alpha 160 = new Histogram("tile_generator.alpha"); 161 final Histogram hist_tile_generator_encoding 162 = new Histogram("tile_generator.encoding"); 163 final Histogram hist_tile_generator_encoding_dist 164 = new Histogram("tile_generator.encoding.dist"); 165 final Histogram hist_tile_generator_encoding_ratio 166 = new Histogram("tile_generator.encoding.ratio"); 167 final Histogram hist_tile_generator_encoding_runLen 168 = new Histogram("tile_generator.encoding.runLen"); 169 final Histogram hist_cpd_polystack_curves 170 = new Histogram("closedPathDetector.polystack.curves"); 171 // all stats 172 final StatLong[] statistics = new StatLong[]{ 173 stat_cache_rowAA, 174 stat_cache_rowAAChunk, 175 stat_cache_tiles, 176 stat_rdr_addLine, 177 stat_rdr_addLine_skip, 178 stat_rdr_curveBreak, 179 stat_rdr_curveBreak_dec, 180 stat_rdr_curveBreak_inc, 181 stat_rdr_quadBreak, 182 stat_rdr_quadBreak_dec, 183 stat_rdr_edges, 184 stat_rdr_edges_count, 185 stat_rdr_edges_resizes, 186 stat_rdr_activeEdges, 187 stat_rdr_activeEdges_updates, 188 stat_rdr_activeEdges_adds, 189 stat_rdr_activeEdges_adds_high, 190 stat_rdr_crossings_updates, 191 stat_rdr_crossings_sorts, 192 stat_rdr_crossings_bsearch, 193 stat_rdr_crossings_msorts, 194 stat_str_polystack_types, 195 stat_str_polystack_curves, 196 stat_cpd_polystack_curves, 197 stat_cpd_polystack_types, 198 hist_rdr_edges_count, 199 hist_rdr_crossings, 200 hist_rdr_crossings_ratio, 201 hist_rdr_crossings_adds, 202 hist_rdr_crossings_msorts, 203 hist_rdr_crossings_msorts_adds, 204 hist_tile_generator_alpha, 205 hist_tile_generator_encoding, 206 hist_tile_generator_encoding_dist, 207 hist_tile_generator_encoding_ratio, 208 hist_tile_generator_encoding_runLen, 209 hist_str_polystack_curves, 210 hist_cpd_polystack_curves, 211 stat_array_dasher_dasher, 212 stat_array_dasher_firstSegmentsBuffer, 213 stat_array_marlincache_rowAAChunk, 214 stat_array_marlincache_touchedTile, 215 stat_array_renderer_alphaline, 216 stat_array_renderer_crossings, 217 stat_array_renderer_aux_crossings, 218 stat_array_renderer_edgeBuckets, 219 stat_array_renderer_edgeBucketCounts, 220 stat_array_renderer_edgePtrs, 221 stat_array_renderer_aux_edgePtrs, 222 stat_array_str_polystack_curves, 223 stat_array_str_polystack_types, 224 stat_array_cpd_polystack_curves, 225 stat_array_cpd_polystack_types 226 }; 227 // monitors 228 final Monitor mon_pre_getAATileGenerator 229 = new Monitor("MarlinRenderingEngine.getAATileGenerator()"); 230 final Monitor mon_rdr_addLine 231 = new Monitor("Renderer.addLine()"); 232 final Monitor mon_rdr_endRendering 233 = new Monitor("Renderer.endRendering()"); 234 final Monitor mon_rdr_endRendering_Y 235 = new Monitor("Renderer._endRendering(Y)"); 236 final Monitor mon_rdr_copyAARow 237 = new Monitor("Renderer.copyAARow()"); 238 final Monitor mon_pipe_renderTiles 239 = new Monitor("AAShapePipe.renderTiles()"); 240 final Monitor mon_ptg_getAlpha 241 = new Monitor("MarlinTileGenerator.getAlpha()"); 242 final Monitor mon_debug 243 = new Monitor("DEBUG()"); 244 // all monitors 245 final Monitor[] monitors = new Monitor[]{ 246 mon_pre_getAATileGenerator, 247 mon_rdr_addLine, 248 mon_rdr_endRendering, 249 mon_rdr_endRendering_Y, 250 mon_rdr_copyAARow, 251 mon_pipe_renderTiles, 252 mon_ptg_getAlpha, 253 mon_debug 254 }; 255 // offheap stats 256 long totalOffHeapInitial = 0L; 257 // live accumulator 258 long totalOffHeap = 0L; 259 long totalOffHeapMax = 0L; 260 // cache stats 261 CacheStats[] cacheStats = null; 262 263 private RendererStats(final String name) { 264 this.name = name; 265 } 266 267 void dump() { 268 logInfo("RendererContext: " + name); 269 270 if (DO_MONITORS) { 271 for (Monitor monitor : monitors) { 272 if (monitor.count != 0) { 273 logInfo(monitor.toString()); 274 } 275 } 276 // As getAATileGenerator percents: 277 final long total = mon_pre_getAATileGenerator.sum; 278 if (total != 0L) { 279 for (Monitor monitor : monitors) { 280 logInfo(monitor.name + " : " 281 + ((100d * monitor.sum) / total) + " %"); 282 } 283 } 284 if (DO_FLUSH_MONITORS) { 285 for (Monitor m : monitors) { 286 m.reset(); 287 } 288 } 289 } 290 291 if (DO_STATS) { 292 for (StatLong stat : statistics) { 293 if (stat.count != 0) { 294 logInfo(stat.toString()); 295 if (DO_FLUSH_STATS) { 296 stat.reset(); 297 } 298 } 299 } 300 301 logInfo("OffHeap footprint: initial: " + totalOffHeapInitial 302 + " bytes - max: " + totalOffHeapMax + " bytes"); 303 if (DO_FLUSH_STATS) { 304 totalOffHeapMax = 0L; 305 } 306 307 logInfo("Array caches for RendererContext: " + name); 308 309 long totalInitialBytes = totalOffHeapInitial; 310 long totalCacheBytes = 0L; 311 312 if (cacheStats != null) { 313 for (CacheStats stat : cacheStats) { 314 totalCacheBytes += stat.dumpStats(); 315 totalInitialBytes += stat.getTotalInitialBytes(); 316 if (DO_FLUSH_STATS) { 317 stat.reset(); 318 } 319 } 320 } 321 logInfo("Heap footprint: initial: " + totalInitialBytes 322 + " bytes - cache: " + totalCacheBytes + " bytes"); 323 } 324 } 325 326 static final class RendererStatsHolder { 327 328 // singleton 329 private static volatile RendererStatsHolder SINGLETON = null; 330 331 static synchronized RendererStatsHolder getInstance() { 332 if (SINGLETON == null) { 333 SINGLETON = new RendererStatsHolder(); 334 } 335 return SINGLETON; 336 } 337 338 static void dumpStats() { 339 if (SINGLETON != null) { 340 SINGLETON.dump(); 341 } 342 } 343 344 /* RendererStats collection as hard references 345 (only used for debugging purposes) */ 346 private final ConcurrentLinkedQueue<RendererStats> allStats 347 = new ConcurrentLinkedQueue<RendererStats>(); 348 349 private RendererStatsHolder() { 350 AccessController.doPrivileged( 351 (PrivilegedAction<Void>) () -> { 352 final Thread hook = new Thread( 353 ThreadGroupUtils.getRootThreadGroup(), 354 new Runnable() { 355 @Override 356 public void run() { 357 dump(); 358 } 359 }, 360 "MarlinStatsHook" 361 ); 362 hook.setContextClassLoader(null); 363 Runtime.getRuntime().addShutdownHook(hook); 364 365 if (USE_DUMP_THREAD) { 366 final Timer statTimer = new Timer("RendererStats"); 367 statTimer.scheduleAtFixedRate(new TimerTask() { 368 @Override 369 public void run() { 370 dump(); 371 } 372 }, DUMP_INTERVAL, DUMP_INTERVAL); 373 } 374 return null; 375 } 376 ); 377 } 378 379 void add(final Object parent, final RendererStats stats) { 380 allStats.add(stats); 381 382 // Register a cleaning function to ensure removing dead entries: 383 CleanerFactory.cleaner().register(parent, () -> remove(stats)); 384 } 385 386 void remove(final RendererStats stats) { 387 stats.dump(); // dump anyway 388 allStats.remove(stats); 389 } 390 391 void dump() { 392 for (RendererStats stats : allStats) { 393 stats.dump(); 394 } 395 } 396 } 397 } --- EOF ---