Skip to content

Commit e9c1f8c

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

File tree

12 files changed

+173
-185
lines changed

12 files changed

+173
-185
lines changed

Diff for: compiler/src/dotty/tools/dotc/Run.scala

+3-4
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,9 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
339339
if phaseWillRun then
340340
Stats.trackTime(s"phase time ms/$phase") {
341341
val start = System.currentTimeMillis
342-
val profileBefore = profiler.beforePhase(phase)
343-
try units = phase.runOn(units)
344-
catch case _: InterruptedException => cancelInterrupted()
345-
profiler.afterPhase(phase, profileBefore)
342+
profiler.onPhase(phase):
343+
try units = phase.runOn(units)
344+
catch case _: InterruptedException => cancelInterrupted()
346345
if (ctx.settings.Xprint.value.containsPhase(phase))
347346
for (unit <- units)
348347
def printCtx(unit: CompilationUnit) = phase.printingContext(

Diff for: compiler/src/dotty/tools/dotc/core/Phases.scala

+2-5
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,8 @@ object Phases {
370370
// Test that we are in a state where we need to check if the phase should be skipped for a java file,
371371
// this prevents checking the expensive `unit.typedAsJava` unnecessarily.
372372
val doCheckJava = skipIfJava && !isAfterLastJavaPhase
373-
for unit <- units do
373+
for unit <- units do ctx.profiler.onUnit(this, unit):
374374
given unitCtx: Context = runCtx.fresh.setPhase(this.start).setCompilationUnit(unit).withRootImports
375-
ctx.profiler.beforeUnit(this, unit)
376375
if ctx.run.enterUnit(unit) then
377376
try
378377
if doCheckJava && unit.typedAsJava then
@@ -382,9 +381,7 @@ object Phases {
382381
catch case ex: Throwable if !ctx.run.enrichedErrorMessage =>
383382
println(ctx.run.enrichErrorMessage(s"unhandled exception while running $phaseName on $unit"))
384383
throw ex
385-
finally
386-
ctx.profiler.afterUnit(this, unit)
387-
ctx.run.advanceUnit()
384+
finally ctx.run.advanceUnit()
388385
buf += unitCtx.compilationUnit
389386
end if
390387
end for

Diff for: compiler/src/dotty/tools/dotc/core/SymbolLoaders.scala

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

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

347-
override def complete(root: SymDenotation)(using Context): Unit = tryProfileCompletion(root) {
344+
override def complete(root: SymDenotation)(using Context): Unit = profileCompletion(root) {
348345
def signalError(ex: Exception): Unit = {
349346
if (ctx.debug) ex.printStackTrace()
350347
val msg = ex.getMessage()
@@ -419,6 +416,7 @@ class ClassfileLoader(val classfile: AbstractFile) extends SymbolLoader {
419416

420417
def compilationUnitInfo: CompilationUnitInfo | Null = CompilationUnitInfo(classfile)
421418

419+
422420
def description(using Context): String = "class file " + classfile.toString
423421

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

Diff for: 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)