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