|
| 1 | +package sjsonnet |
| 2 | + |
| 3 | +/** |
| 4 | + * Mutable counters and timing data for `--debug-stats` mode. A single instance is created when the |
| 5 | + * flag is set and threaded through the Interpreter, Evaluator, and ParseCache so that each |
| 6 | + * subsystem can increment its own counters. |
| 7 | + * |
| 8 | + * All fields are plain `var`s — evaluation is single-threaded, so no synchronisation is needed. |
| 9 | + */ |
| 10 | +final class DebugStats { |
| 11 | + |
| 12 | + // -- Lazy -- |
| 13 | + var lazyCreated: Long = 0 |
| 14 | + |
| 15 | + // -- Calls -- |
| 16 | + var functionCalls: Long = 0 |
| 17 | + var builtinCalls: Long = 0 |
| 18 | + |
| 19 | + // -- Comprehensions -- |
| 20 | + var arrayCompIterations: Long = 0 |
| 21 | + var objectCompIterations: Long = 0 |
| 22 | + |
| 23 | + // -- Parse / import -- |
| 24 | + var filesParsed: Long = 0 |
| 25 | + var importCalls: Long = 0 |
| 26 | + var importCacheHits: Long = 0 |
| 27 | + |
| 28 | + // -- Timing (nanoseconds) -- |
| 29 | + var parseTimeNs: Long = 0 |
| 30 | + var evalTimeNs: Long = 0 |
| 31 | + var materializeTimeNs: Long = 0 |
| 32 | + |
| 33 | + def totalTimeNs: Long = evalTimeNs + materializeTimeNs |
| 34 | + |
| 35 | + def format(): String = { |
| 36 | + val sb = new StringBuilder |
| 37 | + sb.append("=== sjsonnet debug stats ===\n") |
| 38 | + |
| 39 | + sb.append(formatLine("lazy_created", lazyCreated)) |
| 40 | + sb.append(formatLine("function_calls", functionCalls)) |
| 41 | + sb.append(formatLine("builtin_calls", builtinCalls)) |
| 42 | + sb.append(formatLine("array_comp_iterations", arrayCompIterations)) |
| 43 | + sb.append(formatLine("object_comp_iterations", objectCompIterations)) |
| 44 | + sb.append(formatLine("files_parsed", filesParsed)) |
| 45 | + sb.append(formatLine("import_calls", importCalls)) |
| 46 | + sb.append(formatLine("import_cache_hits", importCacheHits)) |
| 47 | + sb.append(formatTimeLine("parse_time", parseTimeNs)) |
| 48 | + sb.append(formatTimeLine("eval_time", evalTimeNs)) |
| 49 | + sb.append(formatTimeLine("materialize_time", materializeTimeNs)) |
| 50 | + sb.append(formatTimeLine("total_time", totalTimeNs)) |
| 51 | + |
| 52 | + val rt = Runtime.getRuntime |
| 53 | + val usedBytes = rt.totalMemory() - rt.freeMemory() |
| 54 | + val maxBytes = rt.maxMemory() |
| 55 | + sb.append(formatBytesLine("heap_used", usedBytes)) |
| 56 | + sb.append(formatBytesLine("heap_max", maxBytes)) |
| 57 | + |
| 58 | + sb.toString |
| 59 | + } |
| 60 | + |
| 61 | + private def formatLine(label: String, value: Long): String = |
| 62 | + f"$label%-25s $value%d%n" |
| 63 | + |
| 64 | + private def formatTimeLine(label: String, ns: Long): String = |
| 65 | + f"$label%-25s ${formatNs(ns)}%s%n" |
| 66 | + |
| 67 | + private def formatBytesLine(label: String, bytes: Long): String = |
| 68 | + f"$label%-25s ${formatBytes(bytes)}%s%n" |
| 69 | + |
| 70 | + private def formatNs(ns: Long): String = { |
| 71 | + if (ns < 1000L) s"${ns}ns" |
| 72 | + else if (ns < 1000000L) f"${ns / 1000.0}%.1fμs" |
| 73 | + else if (ns < 1000000000L) f"${ns / 1000000.0}%.1fms" |
| 74 | + else f"${ns / 1000000000.0}%.3fs" |
| 75 | + } |
| 76 | + |
| 77 | + private def formatBytes(bytes: Long): String = { |
| 78 | + if (bytes < 1024L) s"${bytes}B" |
| 79 | + else if (bytes < 1024L * 1024) f"${bytes / 1024.0}%.1fKB" |
| 80 | + else if (bytes < 1024L * 1024 * 1024) f"${bytes / (1024.0 * 1024)}%.1fMB" |
| 81 | + else f"${bytes / (1024.0 * 1024 * 1024)}%.2fGB" |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * A [[ParseCache]] wrapper that counts and times actual parse invocations (cache misses). The |
| 87 | + * `defaultValue` thunk in [[ParseCache.getOrElseUpdate]] is only evaluated on a cache miss, so we |
| 88 | + * increment [[DebugStats.filesParsed]] and accumulate [[DebugStats.parseTimeNs]] inside that thunk. |
| 89 | + */ |
| 90 | +final class CountingParseCache(delegate: ParseCache, stats: DebugStats) extends ParseCache { |
| 91 | + override def getOrElseUpdate( |
| 92 | + key: (Path, String), |
| 93 | + defaultValue: => Either[Error, (Expr, FileScope)]): Either[Error, (Expr, FileScope)] = { |
| 94 | + delegate.getOrElseUpdate( |
| 95 | + key, { |
| 96 | + stats.filesParsed += 1 |
| 97 | + val t0 = System.nanoTime() |
| 98 | + val result = defaultValue |
| 99 | + stats.parseTimeNs += System.nanoTime() - t0 |
| 100 | + result |
| 101 | + } |
| 102 | + ) |
| 103 | + } |
| 104 | +} |
0 commit comments