Skip to content

Commit 22a1f22

Browse files
committed
fixed colours for implicits/macros
1 parent da73760 commit 22a1f22

File tree

2 files changed

+36
-33
lines changed

2 files changed

+36
-33
lines changed

src/compiler/scala/tools/nsc/profile/Profiler.scala

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ private [profile] object RealProfiler {
117117
}
118118

119119
private [profile] class RealProfiler(reporter : ProfileReporter, val settings: Settings) extends Profiler with NotificationListener {
120-
120+
private val mainThread = Thread.currentThread()
121121
val tracePath = {
122122
if (settings.YprofileDestination.isSetByUser) new File(new java.io.File(settings.YprofileDestination.value)).changeExtension("trace").jfile.toPath
123123
else Files.createTempFile("scalac-", ".trace")
124124
}
125125
private val chromeTrace = new ChromeTrace(tracePath)
126-
chromeTrace.traceAsyncEventStart("scalac", 0, "0")
126+
chromeTrace.traceAsyncEventStart("scalac-" + mainThread.getId, mainThread.getId, "0")
127127

128128
def completeBackground(threadRange: ProfileRange): Unit = {
129129
reporter.reportBackground(this, threadRange)
@@ -139,7 +139,6 @@ private [profile] class RealProfiler(reporter : ProfileReporter, val settings: S
139139

140140
val active = RealProfiler.allPlugins map (_.generate(this, settings))
141141

142-
private val mainThread = Thread.currentThread()
143142

144143
private[profile] def snapThread( idleTimeNanos:Long): ProfileSnap = {
145144
import RealProfiler._
@@ -180,7 +179,7 @@ private [profile] class RealProfiler(reporter : ProfileReporter, val settings: S
180179
val startNanos = gcEvent.endNanos - durationNanos
181180
chromeTrace.traceDurationEvent(gcEvent.name, startNanos, durationNanos, GcThreadId)
182181
}
183-
chromeTrace.traceAsyncEventEnd("scalac", 0, "0")
182+
chromeTrace.traceAsyncEventEnd("scalac-" + mainThread.getId, 0, "0")
184183

185184
println("Trace file: " + tracePath)
186185
chromeTrace.close()
@@ -214,6 +213,21 @@ private [profile] class RealProfiler(reporter : ProfileReporter, val settings: S
214213
}
215214
}
216215

216+
override def beforePhase(phase: Phase): ProfileSnap = {
217+
assert(mainThread eq Thread.currentThread())
218+
chromeTrace.traceAsyncEventStart(phase.name, mainThread.getId, "0")
219+
if (settings.YprofileRunGcBetweenPhases.containsPhase(phase))
220+
doGC
221+
if (settings.YprofileExternalTool.containsPhase(phase)) {
222+
println("Profile hook start")
223+
ExternalToolHook.before()
224+
}
225+
active foreach {_.beforePhase(phase)}
226+
val snap = snapThread(0)
227+
if (phase.prev eq null) baseline = snap
228+
snap
229+
}
230+
217231
override def afterPhase(phase: Phase, snapBefore: ProfileSnap): Unit = {
218232
assert(mainThread eq Thread.currentThread())
219233
val initialSnap = snapThread(0)
@@ -226,21 +240,21 @@ private [profile] class RealProfiler(reporter : ProfileReporter, val settings: S
226240
doGC
227241
initialSnap.updateHeap(readHeapUsage())
228242
} else initialSnap
229-
chromeTrace.traceAsyncEventEnd(phase.name, 0, "0")
243+
chromeTrace.traceAsyncEventEnd(phase.name, mainThread.getId, "0")
230244

231245
reporter.reportForeground(this, ProfileRange(snapBefore, finalSnap, phase, "", 0, Thread.currentThread))
232246
}
233247

234248
override def beforeUnit(phase: Phase, file: AbstractFile): ProfileSnap = {
235249
assert(mainThread eq Thread.currentThread())
236-
chromeTrace.traceAsyncEventStart(file.name, 0, "0")
250+
chromeTrace.traceAsyncEventStart(file.name, mainThread.getId, "0")
237251
snapThread(0)
238252
}
239253

240254
override def afterUnit(phase: Phase, file: AbstractFile, snapBefore: ProfileSnap): Unit = {
241255
assert(mainThread eq Thread.currentThread())
242256
val initialSnap = snapThread(0)
243-
chromeTrace.traceAsyncEventEnd(file.name, 0, "0")
257+
chromeTrace.traceAsyncEventEnd(file.name, mainThread.getId, "0")
244258
chromeTrace.traceCounterEvent("allocBytes", "allocBytes", initialSnap.allocatedBytes - this.baseline.allocatedBytes)
245259
chromeTrace.traceCounterEvent("heapBytes", "heapBytes", initialSnap.heapBytes - this.baseline.heapBytes)
246260
chromeTrace.traceCounterEvent("classesLoaded", "classesLoaded", initialSnap.totalClassesLoaded - this.baseline.totalClassesLoaded)
@@ -252,27 +266,11 @@ private [profile] class RealProfiler(reporter : ProfileReporter, val settings: S
252266

253267
private var baseline: ProfileSnap = _
254268

255-
override def beforePhase(phase: Phase): ProfileSnap = {
256-
assert(mainThread eq Thread.currentThread())
257-
chromeTrace.traceAsyncEventStart(phase.name, 0, "0")
258-
if (settings.YprofileRunGcBetweenPhases.containsPhase(phase))
259-
doGC
260-
if (settings.YprofileExternalTool.containsPhase(phase)) {
261-
println("Profile hook start")
262-
ExternalToolHook.before()
263-
}
264-
active foreach {_.beforePhase(phase)}
265-
val snap = snapThread(0)
266-
if (phase.prev eq null) baseline = snap
267-
snap
268-
}
269-
270269
private val completingStack = new mutable.ArrayStack[Global#Symbol]
271270
private val stringsOf = (0 to 128).map(_.toString).toArray
272271
def intToString(i: Int) = if (i < 128) stringsOf(i) else i.toString
273272

274-
import scala.reflect.internal.util.ChromeTrace
275-
private def traceId = if (completingStack.isEmpty) 0 else completingStack.top.id
273+
private def traceId = if (completingStack.isEmpty) mainThread.getId else mainThread.getId << 32 | (completingStack.top.id.toLong & 0xffffffffL)
276274
override def beforeTypedImplDef(sym: Global#Symbol): Unit = {
277275
chromeTrace.traceAsyncEventStart(sym.rawname.toString, traceId, intToString(completingStack.length))
278276
}
@@ -281,23 +279,23 @@ private [profile] class RealProfiler(reporter : ProfileReporter, val settings: S
281279
}
282280

283281
override def beforeImplicitSearch(pt: Global#Symbol): Unit = {
284-
chromeTrace.traceAsyncEventStart("<implicit> " + pt.rawname, traceId, intToString(completingStack.length))
282+
chromeTrace.traceAsyncEventStart("<implicit> " + pt.rawname, traceId, intToString(completingStack.length), colour = "yellow")
285283
}
286284

287285
override def afterImplicitSearch(pt: Global#Symbol): Unit = {
288-
chromeTrace.traceAsyncEventEnd("<implicit> " + pt.rawname, traceId, intToString(completingStack.length))
286+
chromeTrace.traceAsyncEventEnd("<implicit> " + pt.rawname, traceId, intToString(completingStack.length), colour = "yellow")
289287
}
290288

291289
override def beforeMacroExpansion(macroSym: Global#Symbol): Unit = {
292-
chromeTrace.traceAsyncEventStart("<macro> " + macroSym.rawname, traceId, intToString(completingStack.length))
290+
chromeTrace.traceAsyncEventStart("<macro> " + macroSym.rawname, traceId, intToString(completingStack.length), colour = "olive")
293291
}
294292

295293
override def afterMacroExpansion(macroSym: Global#Symbol): Unit = {
296-
chromeTrace.traceAsyncEventEnd("<macro> " + macroSym.rawname, traceId, intToString(completingStack.length))
294+
chromeTrace.traceAsyncEventEnd("<macro> " + macroSym.rawname, traceId, intToString(completingStack.length), colour = "olive")
297295
}
298296

299297
override def beforeCompletion(root: Global#Symbol): Unit = {
300-
chromeTrace.traceAsyncEventStart("<wait>", traceId, intToString(completingStack.length))
298+
chromeTrace.traceAsyncEventStart("<wait>", traceId, intToString(completingStack.length), colour = "thread_state_sleeping")
301299
completingStack.push(root)
302300
chromeTrace.traceAsyncEventStart("<completion>", traceId, intToString(completingStack.length))
303301
chromeTrace.traceAsyncEventStart(root.rawname.toString, traceId, intToString(completingStack.length))

src/reflect/scala/reflect/internal/util/ChromeTrace.scala

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ final class ChromeTrace(f: Path) extends Closeable {
6565
traceWriter.write("""", "pid": """")
6666
traceWriter.write("0")
6767
traceWriter.write("""", "tid": """")
68-
traceWriter.write("0")
68+
traceWriter.write(Thread.currentThread().getId.toString)
6969
traceWriter.write("""", "ts": """)
7070
traceWriter.write(microTime().toString)
7171
traceWriter.write(""", "args": {"""")
@@ -76,10 +76,10 @@ final class ChromeTrace(f: Path) extends Closeable {
7676
traceWriter.write("}\n")
7777
}
7878

79-
def traceAsyncEventStart(name: String, id: Int, threadIdSuffix: String): Unit = traceAsyncEvent(EventType.AsyncStart, name, id, threadIdSuffix)
80-
def traceAsyncEventEnd(name: String, id: Int, threadIdSuffix: String): Unit = traceAsyncEvent(EventType.AsyncEnd, name, id, threadIdSuffix)
79+
def traceAsyncEventStart(name: String, id: Long, threadIdSuffix: String, colour: String = ""): Unit = traceAsyncEvent(EventType.AsyncStart, name, id, threadIdSuffix, colour)
80+
def traceAsyncEventEnd(name: String, id: Long, threadIdSuffix: String, colour: String = ""): Unit = traceAsyncEvent(EventType.AsyncEnd, name, id, threadIdSuffix, colour)
8181

82-
private def traceAsyncEvent(eventType: String, name: String, id: Int, threadIdSuffix: String): Unit = {
82+
private def traceAsyncEvent(eventType: String, name: String, id: Long, threadIdSuffix: String, colour: String): Unit = {
8383
commaIfFirst()
8484
traceWriter.write("""{"cat": "scalac", "name": """")
8585
traceWriter.write(name)
@@ -95,6 +95,11 @@ final class ChromeTrace(f: Path) extends Closeable {
9595
traceWriter.write(threadIdSuffix)
9696
traceWriter.write("""", "ts": """)
9797
traceWriter.write(microTime().toString)
98+
if (colour != "") {
99+
traceWriter.write(""", "cname": """")
100+
traceWriter.write(colour)
101+
traceWriter.write("\"")
102+
}
98103
traceWriter.write("}\n")
99104
}
100105

0 commit comments

Comments
 (0)