Skip to content

Commit bfdde07

Browse files
Michael Suttonjenkins
Michael Sutton
authored and
jenkins
committed
util/util-jvm: scala 3 migration
**Problem** Build util-jvm with Scala 3 **Solution** # Change util-jvm project settings. # Compile project + test with Scala 3 migration flags # Remove flags # Fix incompatibilities # Run cross-building tests **Result** //Auto-rewrite fixes:// Scala 3 requires parentheses around the parameter of a lambda: - Jvm.scala - JvmStatsTest.scala //Other fixes:// DurationOps expects a Long for as an input for operations. The conversion to long used to happen implicitly, but now needs to be explicit in order to work. - CpuProfile.scala - CpuProfileTest.scala Implicit conversion `long2float` in object Long is deprecated, need to be explicit: - JvmStats.scala getter `Stream` is deprecated, use `compat.LazyList` instead: - CpuProfileTest.scala Widening conversion from Long to Double is deprecated: - EstimatorApp.scala Scala 3 infers that executor is of type `ScheduledExecutorService` when it should be of type `MockScheduledExecutorService`. Explicitly give type to avoid error: - JvmTest.scala JIRA Issues: CSL-11094 Differential Revision: https://phabricator.twitter.biz/D728602
1 parent 0def519 commit bfdde07

File tree

9 files changed

+58
-46
lines changed

9 files changed

+58
-46
lines changed

CHANGELOG.rst

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Unreleased
1010
New Features
1111
~~~~~~~~~~~~
1212

13+
* util-jvm: Experimentally crossbuilds with Scala 3. ``PHAB_ID=D728602``
14+
1315
* util-stats: Counter, Gauge, and Stat can be instrumented with descriptions. ``PHAB_ID = D615481``
1416

1517
* util-cache: Experimentally crossbuilds with Scala 3. ``PHAB_ID=D714304``.

build.sbt

+2-3
Original file line numberDiff line numberDiff line change
@@ -519,13 +519,12 @@ lazy val utilJvm = Project(
519519
id = "util-jvm",
520520
base = file("util-jvm")
521521
).settings(
522-
sharedSettings
522+
sharedScala3EnabledSettings
523523
).settings(
524524
name := "util-jvm",
525525
libraryDependencies ++= Seq(
526526
"org.mockito" % "mockito-core" % mockitoVersion % "test",
527-
"org.scalatestplus" %% "mockito-3-3" % "3.1.2.0" % "test"
528-
)
527+
) ++ scalatestMockitoVersionedDep(scalaVersion.value)
529528
).dependsOn(utilApp, utilCore, utilStats)
530529

531530
lazy val utilLint = Project(

util-jvm/src/main/scala/com/twitter/jvm/CpuProfile.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ object CpuProfile {
116116
val bean = ManagementFactory.getThreadMXBean()
117117
val elapsed = Stopwatch.start()
118118
val end = howlong.fromNow
119-
val period = (1000000 / frequency).microseconds
119+
val period = ((1000000L / frequency)).microseconds
120120
val myId = Thread.currentThread().getId()
121121
var next = Time.now
122122

util-jvm/src/main/scala/com/twitter/jvm/Jvm.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ trait Jvm {
131131
buffer = (gc :: buffer).takeWhile(_.timestamp > floor)
132132
}
133133

134-
since: Time => buffer.takeWhile(_.timestamp > since)
134+
(since: Time) => buffer.takeWhile(_.timestamp > since)
135135
}
136136

137137
def forceGc(): Unit

util-jvm/src/main/scala/com/twitter/jvm/JvmStats.scala

+46-36
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,33 @@ object JvmStats {
2222

2323
def heap = mem.getHeapMemoryUsage()
2424
val heapStats = stats.scope("heap")
25-
gauges.add(heapStats.addGauge("committed") { heap.getCommitted() })
26-
gauges.add(heapStats.addGauge("max") { heap.getMax() })
27-
gauges.add(heapStats.addGauge("used") { heap.getUsed() })
25+
gauges.add(heapStats.addGauge("committed") { heap.getCommitted().toFloat })
26+
gauges.add(heapStats.addGauge("max") { heap.getMax().toFloat })
27+
gauges.add(heapStats.addGauge("used") { heap.getUsed().toFloat })
2828

2929
def nonHeap = mem.getNonHeapMemoryUsage()
3030
val nonHeapStats = stats.scope("nonheap")
31-
gauges.add(nonHeapStats.addGauge("committed") { nonHeap.getCommitted() })
32-
gauges.add(nonHeapStats.addGauge("max") { nonHeap.getMax() })
33-
gauges.add(nonHeapStats.addGauge("used") { nonHeap.getUsed() })
31+
gauges.add(nonHeapStats.addGauge("committed") { nonHeap.getCommitted().toFloat })
32+
gauges.add(nonHeapStats.addGauge("max") { nonHeap.getMax().toFloat })
33+
gauges.add(nonHeapStats.addGauge("used") { nonHeap.getUsed().toFloat })
3434

3535
val threads = ManagementFactory.getThreadMXBean()
3636
val threadStats = stats.scope("thread")
37-
gauges.add(threadStats.addGauge("daemon_count") { threads.getDaemonThreadCount().toLong })
38-
gauges.add(threadStats.addGauge("count") { threads.getThreadCount().toLong })
39-
gauges.add(threadStats.addGauge("peak_count") { threads.getPeakThreadCount().toLong })
37+
gauges.add(threadStats.addGauge("daemon_count") { threads.getDaemonThreadCount().toFloat })
38+
gauges.add(threadStats.addGauge("count") { threads.getThreadCount().toFloat })
39+
gauges.add(threadStats.addGauge("peak_count") { threads.getPeakThreadCount().toFloat })
4040

4141
val runtime = ManagementFactory.getRuntimeMXBean()
42-
val uptime = stats.addGauge("uptime") { runtime.getUptime() }
42+
val uptime = stats.addGauge("uptime") { runtime.getUptime().toFloat }
4343
gauges.add(uptime)
44-
gauges.add(stats.addGauge("start_time") { runtime.getStartTime() })
44+
gauges.add(stats.addGauge("start_time") { runtime.getStartTime().toFloat })
4545

4646
val os = ManagementFactory.getOperatingSystemMXBean()
47-
gauges.add(stats.addGauge("num_cpus") { os.getAvailableProcessors().toLong })
47+
gauges.add(stats.addGauge("num_cpus") { os.getAvailableProcessors().toFloat })
4848
os match {
4949
case unix: com.sun.management.UnixOperatingSystemMXBean =>
50-
gauges.add(stats.addGauge("fd_count") { unix.getOpenFileDescriptorCount })
51-
gauges.add(stats.addGauge("fd_limit") { unix.getMaxFileDescriptorCount })
50+
gauges.add(stats.addGauge("fd_count") { unix.getOpenFileDescriptorCount.toFloat })
51+
gauges.add(stats.addGauge("fd_limit") { unix.getMaxFileDescriptorCount.toFloat })
5252
case _ =>
5353
}
5454

@@ -64,15 +64,21 @@ object JvmStats {
6464
case null =>
6565
case compilation =>
6666
val compilationStats = stats.scope("compilation")
67-
gauges.add(compilationStats.addGauge("time_msec") { compilation.getTotalCompilationTime() })
67+
gauges.add(compilationStats.addGauge("time_msec") {
68+
compilation.getTotalCompilationTime().toFloat
69+
})
6870
}
6971

7072
val classes = ManagementFactory.getClassLoadingMXBean()
7173
val classLoadingStats = stats.scope("classes")
72-
gauges.add(classLoadingStats.addGauge("total_loaded") { classes.getTotalLoadedClassCount() })
73-
gauges.add(classLoadingStats.addGauge("total_unloaded") { classes.getUnloadedClassCount() })
74+
gauges.add(classLoadingStats.addGauge("total_loaded") {
75+
classes.getTotalLoadedClassCount().toFloat
76+
})
77+
gauges.add(classLoadingStats.addGauge("total_unloaded") {
78+
classes.getUnloadedClassCount().toFloat
79+
})
7480
gauges.add(
75-
classLoadingStats.addGauge("current_loaded") { classes.getLoadedClassCount().toLong }
81+
classLoadingStats.addGauge("current_loaded") { classes.getLoadedClassCount().toFloat }
7682
)
7783

7884
val memPool = ManagementFactory.getMemoryPoolMXBeans.asScala
@@ -83,44 +89,44 @@ object JvmStats {
8389
val name = pool.getName.regexSub("""[^\w]""".r) { m => "_" }
8490
if (pool.getCollectionUsage != null) {
8591
def usage = pool.getCollectionUsage // this is a snapshot, we can't reuse the value
86-
gauges.add(postGCStats.addGauge(name, "used") { usage.getUsed })
92+
gauges.add(postGCStats.addGauge(name, "used") { usage.getUsed.toFloat })
8793
}
8894
if (pool.getUsage != null) {
8995
def usage = pool.getUsage // this is a snapshot, we can't reuse the value
90-
gauges.add(currentMem.addGauge(name, "used") { usage.getUsed })
91-
gauges.add(currentMem.addGauge(name, "max") { usage.getMax })
96+
gauges.add(currentMem.addGauge(name, "used") { usage.getUsed.toFloat })
97+
gauges.add(currentMem.addGauge(name, "max") { usage.getMax.toFloat })
9298
}
9399
}
94100
gauges.add(postGCStats.addGauge("used") {
95-
memPool.flatMap(p => Option(p.getCollectionUsage)).map(_.getUsed).sum
101+
memPool.flatMap(p => Option(p.getCollectionUsage)).map(_.getUsed).sum.toFloat
96102
})
97103
gauges.add(currentMem.addGauge("used") {
98-
memPool.flatMap(p => Option(p.getUsage)).map(_.getUsed).sum
104+
memPool.flatMap(p => Option(p.getUsage)).map(_.getUsed).sum.toFloat
99105
})
100106

101107
// the Hotspot JVM exposes the full size that the metaspace can grow to
102108
// which differs from the value exposed by `MemoryUsage.getMax` from above
103109
val jvm = Jvm()
104110
jvm.metaspaceUsage.foreach { usage =>
105111
gauges.add(memStats.scope("metaspace").addGauge("max_capacity") {
106-
usage.maxCapacity.inBytes
112+
usage.maxCapacity.inBytes.toFloat
107113
})
108114
}
109115

110116
val spStats = stats.scope("safepoint")
111-
gauges.add(spStats.addGauge("sync_time_millis") { jvm.safepoint.syncTimeMillis })
112-
gauges.add(spStats.addGauge("total_time_millis") { jvm.safepoint.totalTimeMillis })
113-
gauges.add(spStats.addGauge("count") { jvm.safepoint.count })
117+
gauges.add(spStats.addGauge("sync_time_millis") { jvm.safepoint.syncTimeMillis.toFloat })
118+
gauges.add(spStats.addGauge("total_time_millis") { jvm.safepoint.totalTimeMillis.toFloat })
119+
gauges.add(spStats.addGauge("count") { jvm.safepoint.count.toFloat })
114120

115121
ManagementFactory.getPlatformMXBeans(classOf[BufferPoolMXBean]) match {
116122
case null =>
117123
case jBufferPool =>
118124
val bufferPoolStats = memStats.scope("buffer")
119125
jBufferPool.asScala.foreach { bp =>
120126
val name = bp.getName
121-
gauges.add(bufferPoolStats.addGauge(name, "count") { bp.getCount })
122-
gauges.add(bufferPoolStats.addGauge(name, "used") { bp.getMemoryUsed })
123-
gauges.add(bufferPoolStats.addGauge(name, "max") { bp.getTotalCapacity })
127+
gauges.add(bufferPoolStats.addGauge(name, "count") { bp.getCount.toFloat })
128+
gauges.add(bufferPoolStats.addGauge(name, "used") { bp.getMemoryUsed.toFloat })
129+
gauges.add(bufferPoolStats.addGauge(name, "max") { bp.getTotalCapacity.toFloat })
124130
}
125131
}
126132

@@ -130,10 +136,10 @@ object JvmStats {
130136
val name = gc.getName.regexSub("""[^\w]""".r) { m => "_" }
131137
val poolCycles =
132138
gcStats.metricBuilder(GaugeType).withCounterishGauge.gauge(name, "cycles") {
133-
gc.getCollectionCount
139+
gc.getCollectionCount.toFloat
134140
}
135141
val poolMsec = gcStats.metricBuilder(GaugeType).withCounterishGauge.gauge(name, "msec") {
136-
gc.getCollectionTime
142+
gc.getCollectionTime.toFloat
137143
}
138144

139145
ExpressionSchema(s"gc_cycles", Expression(poolCycles.metadata))
@@ -154,8 +160,12 @@ object JvmStats {
154160
}
155161

156162
// note, these could be -1 if the collector doesn't have support for it.
157-
val cycles = gcStats.addGauge("cycles") { gcPool.map(_.getCollectionCount).filter(_ > 0).sum }
158-
val msec = gcStats.addGauge("msec") { gcPool.map(_.getCollectionTime).filter(_ > 0).sum }
163+
val cycles = gcStats.addGauge("cycles") {
164+
gcPool.map(_.getCollectionCount).filter(_ > 0).sum.toFloat
165+
}
166+
val msec = gcStats.addGauge("msec") {
167+
gcPool.map(_.getCollectionTime).filter(_ > 0).sum.toFloat
168+
}
159169

160170
ExpressionSchema("jvm_uptime", Expression(uptime.metadata))
161171
.withLabel(ExpressionSchema.Role, "jvm")
@@ -179,12 +189,12 @@ object JvmStats {
179189
if (allocations.trackingEden) {
180190
val allocationStats = memStats.scope("allocations")
181191
val eden = allocationStats.scope("eden")
182-
gauges.add(eden.addGauge("bytes") { allocations.eden })
192+
gauges.add(eden.addGauge("bytes") { allocations.eden.toFloat })
183193
}
184194

185195
// return ms from ns while retaining precision
186196
gauges.add(stats.addGauge("application_time_millis") { jvm.applicationTime.toFloat / 1000000 })
187-
gauges.add(stats.addGauge("tenuring_threshold") { jvm.tenuringThreshold })
197+
gauges.add(stats.addGauge("tenuring_threshold") { jvm.tenuringThreshold.toFloat })
188198
}
189199

190200
}

util-jvm/src/test/scala/com/twitter/jvm/CpuProfileTest.scala

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import com.twitter.conversions.DurationOps._
44
import com.twitter.util.Time
55
import java.io.ByteArrayOutputStream
66
import org.scalatest.funsuite.AnyFunSuite
7+
import scala.collection.compat.immutable.LazyList
78

89
class CpuProfileTest extends AnyFunSuite {
910
test("record") {
1011

1112
// record() calls Time.now 3 times initially, and then 3 times on every loop iteration.
12-
val times: Stream[Int] = (0 #:: Stream.from(0)).flatMap(x => List(x, x, x))
13+
val times: LazyList[Int] = (0 #:: LazyList.from(0)).flatMap(x => List(x, x, x))
1314
val iter = times.iterator
1415
val start = Time.now
15-
def nextTime: Time = start + iter.next().milliseconds * 10
16+
def nextTime: Time = start + iter.next().toLong.milliseconds * 10
1617

1718
val t = new Thread("CpuProfileTest") {
1819
override def run(): Unit = {

util-jvm/src/test/scala/com/twitter/jvm/EstimatorApp.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ object EstimatorApp extends App {
4242
var elapsed = 1
4343
for (List(begin, end) <- states.toList.sliding(2)) {
4444
val allocated = (end - begin).used
45-
estimator.measure(allocated.inBytes)
45+
estimator.measure(allocated.inBytes.toDouble)
4646
val r = end.capacity - end.used
4747
val i = (r.inBytes / estimator.estimate.toLong) + elapsed
4848
val j = states.indexWhere(_.numCollections > end.numCollections)

util-jvm/src/test/scala/com/twitter/jvm/JvmStatsTest.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class JvmStatsTest extends AnyFunSuite {
1818

1919
assert(expressions.size == 7)
2020

21-
expressions.foreach { x: (ExpressionSchemaKey, ExpressionSchema) =>
21+
expressions.foreach { (x: (ExpressionSchemaKey, ExpressionSchema)) =>
2222
assertMetric(x._2)
2323
}
2424
}

util-jvm/src/test/scala/com/twitter/jvm/JvmTest.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class JvmTest extends AnyWordSpec with MockitoSugar {
2828
currentSnap = snap
2929
}
3030

31-
override val executor = new MockScheduledExecutorService
31+
override val executor: MockScheduledExecutorService = new MockScheduledExecutorService
3232

3333
def snap = currentSnap
3434

0 commit comments

Comments
 (0)