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 final StatLong stat_pcf_idxstack_indices 114 = new StatLong("pathClipFilter.stack.indices"); 115 // growable arrays 116 final StatLong stat_array_dasher_dasher 117 = new StatLong("array.dasher.dasher.d_float"); 118 final StatLong stat_array_dasher_firstSegmentsBuffer 119 = new StatLong("array.dasher.firstSegmentsBuffer.d_float"); 120 final StatLong stat_array_marlincache_rowAAChunk 121 = new StatLong("array.marlincache.rowAAChunk.resize"); 122 final StatLong stat_array_marlincache_touchedTile 123 = new StatLong("array.marlincache.touchedTile.int"); 124 final StatLong stat_array_renderer_alphaline 125 = new StatLong("array.renderer.alphaline.int"); 126 final StatLong stat_array_renderer_crossings 127 = new StatLong("array.renderer.crossings.int"); 128 final StatLong stat_array_renderer_aux_crossings 129 = new StatLong("array.renderer.aux_crossings.int"); 130 final StatLong stat_array_renderer_edgeBuckets 131 = new StatLong("array.renderer.edgeBuckets.int"); 132 final StatLong stat_array_renderer_edgeBucketCounts 133 = new StatLong("array.renderer.edgeBucketCounts.int"); 134 final StatLong stat_array_renderer_edgePtrs 135 = new StatLong("array.renderer.edgePtrs.int"); 136 final StatLong stat_array_renderer_aux_edgePtrs 137 = new StatLong("array.renderer.aux_edgePtrs.int"); 138 final StatLong stat_array_str_polystack_curves 139 = new StatLong("array.stroker.polystack.curves.d_float"); 140 final StatLong stat_array_str_polystack_types 141 = new StatLong("array.stroker.polystack.curveTypes.d_byte"); 142 final StatLong stat_array_cpd_polystack_curves 143 = new StatLong("array.closedPathDetector.polystack.curves.d_float"); 144 final StatLong stat_array_cpd_polystack_types 145 = new StatLong("array.closedPathDetector.polystack.curveTypes.d_byte"); 146 final StatLong stat_array_pcf_idxstack_indices 147 = new StatLong("array.pathClipFilter.stack.indices.d_int"); 148 // histograms 149 final Histogram hist_rdr_edges_count 150 = new Histogram("renderer.edges.count"); 151 final Histogram hist_rdr_crossings 152 = new Histogram("renderer.crossings"); 153 final Histogram hist_rdr_crossings_ratio 154 = new Histogram("renderer.crossings.ratio"); 155 final Histogram hist_rdr_crossings_adds 156 = new Histogram("renderer.crossings.adds"); 157 final Histogram hist_rdr_crossings_msorts 158 = new Histogram("renderer.crossings.msorts"); 159 final Histogram hist_rdr_crossings_msorts_adds 160 = new Histogram("renderer.crossings.msorts.adds"); 161 final Histogram hist_str_polystack_curves 162 = new Histogram("stroker.polystack.curves"); 163 final Histogram hist_tile_generator_alpha 164 = new Histogram("tile_generator.alpha"); 165 final Histogram hist_tile_generator_encoding 166 = new Histogram("tile_generator.encoding"); 167 final Histogram hist_tile_generator_encoding_dist 168 = new Histogram("tile_generator.encoding.dist"); 169 final Histogram hist_tile_generator_encoding_ratio 170 = new Histogram("tile_generator.encoding.ratio"); 171 final Histogram hist_tile_generator_encoding_runLen 172 = new Histogram("tile_generator.encoding.runLen"); 173 final Histogram hist_cpd_polystack_curves 174 = new Histogram("closedPathDetector.polystack.curves"); 175 final Histogram hist_pcf_idxstack_indices 176 = new Histogram("pathClipFilter.stack.indices"); 177 // all stats 178 final StatLong[] statistics = new StatLong[]{ 179 stat_cache_rowAA, 180 stat_cache_rowAAChunk, 181 stat_cache_tiles, 182 stat_rdr_addLine, 183 stat_rdr_addLine_skip, 184 stat_rdr_curveBreak, 185 stat_rdr_curveBreak_dec, 186 stat_rdr_curveBreak_inc, 187 stat_rdr_quadBreak, 188 stat_rdr_quadBreak_dec, 189 stat_rdr_edges, 190 stat_rdr_edges_count, 191 stat_rdr_edges_resizes, 192 stat_rdr_activeEdges, 193 stat_rdr_activeEdges_updates, 194 stat_rdr_activeEdges_adds, 195 stat_rdr_activeEdges_adds_high, 196 stat_rdr_crossings_updates, 197 stat_rdr_crossings_sorts, 198 stat_rdr_crossings_bsearch, 199 stat_rdr_crossings_msorts, 200 stat_str_polystack_types, 201 stat_str_polystack_curves, 202 stat_cpd_polystack_curves, 203 stat_cpd_polystack_types, 204 stat_pcf_idxstack_indices, 205 hist_rdr_edges_count, 206 hist_rdr_crossings, 207 hist_rdr_crossings_ratio, 208 hist_rdr_crossings_adds, 209 hist_rdr_crossings_msorts, 210 hist_rdr_crossings_msorts_adds, 211 hist_tile_generator_alpha, 212 hist_tile_generator_encoding, 213 hist_tile_generator_encoding_dist, 214 hist_tile_generator_encoding_ratio, 215 hist_tile_generator_encoding_runLen, 216 hist_str_polystack_curves, 217 hist_cpd_polystack_curves, 218 hist_pcf_idxstack_indices, 219 stat_array_dasher_dasher, 220 stat_array_dasher_firstSegmentsBuffer, 221 stat_array_marlincache_rowAAChunk, 222 stat_array_marlincache_touchedTile, 223 stat_array_renderer_alphaline, 224 stat_array_renderer_crossings, 225 stat_array_renderer_aux_crossings, 226 stat_array_renderer_edgeBuckets, 227 stat_array_renderer_edgeBucketCounts, 228 stat_array_renderer_edgePtrs, 229 stat_array_renderer_aux_edgePtrs, 230 stat_array_str_polystack_curves, 231 stat_array_str_polystack_types, 232 stat_array_cpd_polystack_curves, 233 stat_array_cpd_polystack_types, 234 stat_array_pcf_idxstack_indices 235 }; 236 // monitors 237 final Monitor mon_pre_getAATileGenerator 238 = new Monitor("MarlinRenderingEngine.getAATileGenerator()"); 239 final Monitor mon_rdr_addLine 240 = new Monitor("Renderer.addLine()"); 241 final Monitor mon_rdr_endRendering 242 = new Monitor("Renderer.endRendering()"); 243 final Monitor mon_rdr_endRendering_Y 244 = new Monitor("Renderer._endRendering(Y)"); 245 final Monitor mon_rdr_copyAARow 246 = new Monitor("Renderer.copyAARow()"); 247 final Monitor mon_pipe_renderTiles 248 = new Monitor("AAShapePipe.renderTiles()"); 249 final Monitor mon_ptg_getAlpha 250 = new Monitor("MarlinTileGenerator.getAlpha()"); 251 final Monitor mon_debug 252 = new Monitor("DEBUG()"); 253 // all monitors 254 final Monitor[] monitors = new Monitor[]{ 255 mon_pre_getAATileGenerator, 256 mon_rdr_addLine, 257 mon_rdr_endRendering, 258 mon_rdr_endRendering_Y, 259 mon_rdr_copyAARow, 260 mon_pipe_renderTiles, 261 mon_ptg_getAlpha, 262 mon_debug 263 }; 264 // offheap stats 265 long totalOffHeapInitial = 0L; 266 // live accumulator 267 long totalOffHeap = 0L; 268 long totalOffHeapMax = 0L; 269 // cache stats 270 CacheStats[] cacheStats = null; 271 272 private RendererStats(final String name) { 273 this.name = name; 274 } 275 276 void dump() { 277 logInfo("RendererContext: " + name); 278 279 if (DO_MONITORS) { 280 for (Monitor monitor : monitors) { 281 if (monitor.count != 0) { 282 logInfo(monitor.toString()); 283 } 284 } 285 // As getAATileGenerator percents: 286 final long total = mon_pre_getAATileGenerator.sum; 287 if (total != 0L) { 288 for (Monitor monitor : monitors) { 289 logInfo(monitor.name + " : " 290 + ((100d * monitor.sum) / total) + " %"); 291 } 292 } 293 if (DO_FLUSH_MONITORS) { 294 for (Monitor m : monitors) { 295 m.reset(); 296 } 297 } 298 } 299 300 if (DO_STATS) { 301 for (StatLong stat : statistics) { 302 if (stat.count != 0) { 303 logInfo(stat.toString()); 304 if (DO_FLUSH_STATS) { 305 stat.reset(); 306 } 307 } 308 } 309 310 logInfo("OffHeap footprint: initial: " + totalOffHeapInitial 311 + " bytes - max: " + totalOffHeapMax + " bytes"); 312 if (DO_FLUSH_STATS) { 313 totalOffHeapMax = 0L; 314 } 315 316 logInfo("Array caches for RendererContext: " + name); 317 318 long totalInitialBytes = totalOffHeapInitial; 319 long totalCacheBytes = 0L; 320 321 if (cacheStats != null) { 322 for (CacheStats stat : cacheStats) { 323 totalCacheBytes += stat.dumpStats(); 324 totalInitialBytes += stat.getTotalInitialBytes(); 325 if (DO_FLUSH_STATS) { 326 stat.reset(); 327 } 328 } 329 } 330 logInfo("Heap footprint: initial: " + totalInitialBytes 331 + " bytes - cache: " + totalCacheBytes + " bytes"); 332 } 333 } 334 335 static final class RendererStatsHolder { 336 337 // singleton 338 private static volatile RendererStatsHolder SINGLETON = null; 339 340 static synchronized RendererStatsHolder getInstance() { 341 if (SINGLETON == null) { 342 SINGLETON = new RendererStatsHolder(); 343 } 344 return SINGLETON; 345 } 346 347 static void dumpStats() { 348 if (SINGLETON != null) { 349 SINGLETON.dump(); 350 } 351 } 352 353 /* RendererStats collection as hard references 354 (only used for debugging purposes) */ 355 private final ConcurrentLinkedQueue<RendererStats> allStats 356 = new ConcurrentLinkedQueue<RendererStats>(); 357 358 private RendererStatsHolder() { 359 AccessController.doPrivileged( 360 (PrivilegedAction<Void>) () -> { 361 final Thread hook = new Thread( 362 ThreadGroupUtils.getRootThreadGroup(), 363 new Runnable() { 364 @Override 365 public void run() { 366 dump(); 367 } 368 }, 369 "MarlinStatsHook" 370 ); 371 hook.setContextClassLoader(null); 372 Runtime.getRuntime().addShutdownHook(hook); 373 374 if (USE_DUMP_THREAD) { 375 final Timer statTimer = new Timer("RendererStats"); 376 statTimer.scheduleAtFixedRate(new TimerTask() { 377 @Override 378 public void run() { 379 dump(); 380 } 381 }, DUMP_INTERVAL, DUMP_INTERVAL); 382 } 383 return null; 384 } 385 ); 386 } 387 388 void add(final Object parent, final RendererStats stats) { 389 allStats.add(stats); 390 391 // Register a cleaning function to ensure removing dead entries: 392 CleanerFactory.cleaner().register(parent, () -> remove(stats)); 393 } 394 395 void remove(final RendererStats stats) { 396 stats.dump(); // dump anyway 397 allStats.remove(stats); 398 } 399 400 void dump() { 401 for (RendererStats stats : allStats) { 402 stats.dump(); 403 } 404 } 405 } 406 }