Skip to content

Commit 32f9342

Browse files
committed
More idiomatic trace-profiler integraiton. Prevent malformed outputs by appending GC events after profiling is done.
1 parent d690573 commit 32f9342

File tree

12 files changed

+173
-185
lines changed

12 files changed

+173
-185
lines changed

compiler/src/dotty/tools/dotc/Run.scala

+3-4
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,9 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
311311
if phaseWillRun then
312312
Stats.trackTime(s"phase time ms/$phase") {
313313
val start = System.currentTimeMillis
314-
val profileBefore = profiler.beforePhase(phase)
315-
try units = phase.runOn(units)
316-
catch case _: InterruptedException => cancelInterrupted()
317-
profiler.afterPhase(phase, profileBefore)
314+
profiler.onPhase(phase):
315+
try units = phase.runOn(units)
316+
catch case _: InterruptedException => cancelInterrupted()
318317
if (ctx.settings.Xprint.value.containsPhase(phase))
319318
for (unit <- units)
320319
def printCtx(unit: CompilationUnit) = phase.printingContext(

compiler/src/dotty/tools/dotc/core/Phases.scala

+2-5
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,8 @@ object Phases {
360360
// Test that we are in a state where we need to check if the phase should be skipped for a java file,
361361
// this prevents checking the expensive `unit.typedAsJava` unnecessarily.
362362
val doCheckJava = skipIfJava && !isAfterLastJavaPhase
363-
for unit <- units do
363+
for unit <- units do ctx.profiler.onUnit(this, unit):
364364
given unitCtx: Context = runCtx.fresh.setPhase(this.start).setCompilationUnit(unit).withRootImports
365-
ctx.profiler.beforeUnit(this, unit)
366365
if ctx.run.enterUnit(unit) then
367366
try
368367
if doCheckJava && unit.typedAsJava then
@@ -372,9 +371,7 @@ object Phases {
372371
catch case ex: Throwable if !ctx.run.enrichedErrorMessage =>
373372
println(ctx.run.enrichErrorMessage(s"unhandled exception while running $phaseName on $unit"))
374373
throw ex
375-
finally
376-
ctx.profiler.afterUnit(this, unit)
377-
ctx.run.advanceUnit()
374+
finally ctx.run.advanceUnit()
378375
buf += unitCtx.compilationUnit
379376
end if
380377
end for

compiler/src/dotty/tools/dotc/core/SymbolLoaders.scala

+8-10
Original file line numberDiff line numberDiff line change
@@ -331,18 +331,15 @@ abstract class SymbolLoader extends LazyType { self =>
331331
def description(using Context): String = s"proxy to ${self.description}"
332332
}
333333

334-
private inline def tryProfileCompletion[T](root: SymDenotation)(inline body: T)(using Context): T = {
335-
if ctx.profiler eq null
336-
then body
337-
else
338-
val sym = root.symbol
339-
val associatedFile = root.symbol.associatedFile match
340-
case file: AbstractFile => file
341-
case _ => NoAbstractFile
342-
ctx.profiler.onCompletion(sym, associatedFile)(body)
334+
private inline def profileCompletion[T](root: SymDenotation)(inline body: T)(using Context): T = {
335+
val sym = root.symbol
336+
def associatedFile = root.symbol.associatedFile match
337+
case file: AbstractFile => file
338+
case _ => NoAbstractFile
339+
ctx.profiler.onCompletion(sym, associatedFile)(body)
343340
}
344341

345-
override def complete(root: SymDenotation)(using Context): Unit = tryProfileCompletion(root) {
342+
override def complete(root: SymDenotation)(using Context): Unit = profileCompletion(root) {
346343
def signalError(ex: Exception): Unit = {
347344
if (ctx.debug) ex.printStackTrace()
348345
val msg = ex.getMessage()
@@ -417,6 +414,7 @@ class ClassfileLoader(val classfile: AbstractFile) extends SymbolLoader {
417414

418415
def compilationUnitInfo: CompilationUnitInfo | Null = CompilationUnitInfo(classfile)
419416

417+
420418
def description(using Context): String = "class file " + classfile.toString
421419

422420
override def doComplete(root: SymDenotation)(using Context): Unit =

compiler/src/dotty/tools/dotc/profile/ChromeTrace.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ final class ChromeTrace(f: Path) extends Closeable {
6363
traceWriter.close()
6464
}
6565

66-
def traceDurationEvent(name: String, startNanos: Long, durationNanos: Long, tid: String = this.tid(), pidSuffix: String = ""): Unit = synchronized {
66+
def traceDurationEvent(name: String, startNanos: Long, durationNanos: Long, tid: String = this.tid(), pidSuffix: String = ""): Unit = {
6767
val durationMicros = nanosToMicros(durationNanos)
6868
val startMicros = nanosToMicros(startNanos)
6969
objStart()
@@ -85,7 +85,7 @@ final class ChromeTrace(f: Path) extends Closeable {
8585
str2("pid", pid, "-", pidSuffix)
8686
}
8787

88-
def traceCounterEvent(name: String, counterName: String, count: Long, processWide: Boolean): Unit = synchronized {
88+
def traceCounterEvent(name: String, counterName: String, count: Long, processWide: Boolean): Unit = {
8989
objStart()
9090
str("cat", "scalac")
9191
str("name", name)
@@ -104,7 +104,7 @@ final class ChromeTrace(f: Path) extends Closeable {
104104
def traceDurationEventStart(cat: String, name: String, colour: String = "", pidSuffix: String = tid()): Unit = traceDurationEventStartEnd(EventType.Start, cat, name, colour, pidSuffix)
105105
def traceDurationEventEnd(cat: String, name: String, colour: String = "", pidSuffix: String = tid()): Unit = traceDurationEventStartEnd(EventType.End, cat, name, colour, pidSuffix)
106106

107-
private def traceDurationEventStartEnd(eventType: String, cat: String, name: String, colour: String, pidSuffix: String = ""): Unit = synchronized {
107+
private def traceDurationEventStartEnd(eventType: String, cat: String, name: String, colour: String, pidSuffix: String = ""): Unit = {
108108
objStart()
109109
str("cat", cat)
110110
str("name", name)

0 commit comments

Comments
 (0)